Improved variable expansion in service definitions.

master
Luka Vandervelden 2019-11-09 14:47:29 +01:00
parent 5852a1b818
commit 4a03200130
1 changed files with 12 additions and 14 deletions

View File

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