From 1bc9181caa928fc59aadb6a73ea6c523fa2da3ea Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Fri, 25 Oct 2019 15:14:56 +0200 Subject: [PATCH] Internal changes in gen-config. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/gen-config.cr | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/gen-config.cr b/src/gen-config.cr index 77e75e5..4100a3e 100644 --- a/src/gen-config.cr +++ b/src/gen-config.cr @@ -8,10 +8,16 @@ def sanitize_path(path) end 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)) 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| key, value = entry @@ -41,7 +47,10 @@ class GenConfig::Context def initialize(@root) 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" sources = [ @@ -65,12 +74,37 @@ class GenConfig::Context raise Exception.new "Could not find template to generate file ('#{target}')." end - options["id"] = ENV["SERVICE_ID"]? || "" - options["environment"] = ENV["ENVIRONMENT"]? || "" + pp ENV["SERVICE_ID"]? + 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 service = (arguments.varargs[0]? || "").to_s.gsub /\//, ':' `get-port #{service}`.chomp end + options["random_password"] = Crinja.function do id = (arguments.varargs[0]? || options["id"]).to_s password_id = arguments.varargs[1]? || "main"