Internal changes in gen-config.

Most data are now read from service files instead of the process’
environment. Some environment variables are still being used to identify
the target service, however.
master
Luka Vandervelden 2019-10-25 15:14:56 +02:00
parent 8d0708dee8
commit 1bc9181caa
1 changed files with 38 additions and 4 deletions

View File

@ -8,10 +8,16 @@ def sanitize_path(path)
end end
module GenConfig module GenConfig
alias Variables =
String |
Array(String) | Array(Variables) |
Hash(String, String) | Hash(String, Variables) |
Crinja::Callable::Instance
def self.parse_options(unparsed : Array(String)) def self.parse_options(unparsed : Array(String))
options = ARGV.map(&.split '=') options = ARGV.map(&.split '=')
hash = Hash(String, String | Array(String) | Crinja::Callable::Instance | Hash(String, String)).new hash = Hash(String, Variables).new
options.each do |entry| options.each do |entry|
key, value = entry key, value = entry
@ -41,7 +47,10 @@ class GenConfig::Context
def initialize(@root) def initialize(@root)
end end
def generate(template, target : String, options : Hash(String, String | Array(String) | Crinja::Callable::Instance | Hash(String, String))) def generate(template, target : String, options : Hash(String, Variables))
ServiceDefinition.load SERVICES_DIRECTORY
Service.load RC_DIRECTORY
target_file = File.open target, "w" target_file = File.open target, "w"
sources = [ sources = [
@ -65,12 +74,37 @@ class GenConfig::Context
raise Exception.new "Could not find template to generate file ('#{target}')." raise Exception.new "Could not find template to generate file ('#{target}')."
end end
options["id"] = ENV["SERVICE_ID"]? || "" pp ENV["SERVICE_ID"]?
options["environment"] = ENV["ENVIRONMENT"]? || "" if service_id = ENV["SERVICE_ID"]?
if service = Service.get_by_id service_id
environment = service.environment
options["id"] = service.full_id
options["name"] = service.name
options["environment"] = environment.name
providers = Hash(String, Variables).new
service.providers.each do |token, provider|
provider = Service.get_by_id provider
next unless provider
providers[token] = Hash(String, String).new.tap do |entry|
entry["name"] = provider.name
entry["id"] = provider.full_id
entry["environment"] = provider.environment.name
end
end
options["providers"] = providers
end
end
options["port"] = Crinja.function do options["port"] = Crinja.function do
service = (arguments.varargs[0]? || "").to_s.gsub /\//, ':' service = (arguments.varargs[0]? || "").to_s.gsub /\//, ':'
`get-port #{service}`.chomp `get-port #{service}`.chomp
end end
options["random_password"] = Crinja.function do options["random_password"] = Crinja.function do
id = (arguments.varargs[0]? || options["id"]).to_s id = (arguments.varargs[0]? || options["id"]).to_s
password_id = arguments.varargs[1]? || "main" password_id = arguments.varargs[1]? || "main"