Improved variable expansion in service definitions.

This commit is contained in:
Luka Vandervelden 2019-11-09 14:47:29 +01:00
parent 5852a1b818
commit 4a03200130

View File

@ -190,7 +190,8 @@ class Service
end
end
private def build_environment
@commands_environment : Hash(String, String)?
private def build_commands_environment!
env = {} of String => String
env["SERVICE_NAME"] = name
@ -223,7 +224,10 @@ class Service
env[variable] = value
end
env
@commands_environment = env
end
def commands_environment : Hash(String, String)
@commands_environment || build_commands_environment!
end
# FIXME: Is working on ${} really a good idea?
@ -231,18 +235,12 @@ class Service
# environment, and we should take our variables from that
# instead of hardcoding everything.
private def evaluate(string)
env = commands_environment
string.gsub /\${[a-zA-Z_]+}/ do |match|
match = match[2..match.size-2]
if match.downcase == "environment"
@environment.name
elsif match.downcase == "service_root"
root
elsif match.downcase == "service_name"
name
else
""
end
env[match.upcase]? || ""
end
end
@ -283,7 +281,7 @@ class Service
Process.exec "sh", ["-c", creation_command],
output: Process::Redirect::Inherit,
error: Process::Redirect::Inherit,
env: build_environment
env: commands_environment
end.wait
unless child.success?
@ -315,7 +313,7 @@ class Service
Process.exec command, args,
chdir: (@reference.directory.try { |x| evaluate x } || root),
env: build_environment
env: commands_environment
end
self.save_pid context.pid_directory, process.pid
@ -489,7 +487,7 @@ class Service
Process.exec "sh", ["-c", command],
output: Process::Redirect::Inherit,
error: Process::Redirect::Inherit,
env: build_environment
env: commands_environment
else
FileUtils.rm_rf file_path
end