diff --git a/src/gen-config.cr b/src/gen-config.cr index 11c2c19..c1bd621 100644 --- a/src/gen-config.cr +++ b/src/gen-config.cr @@ -1,5 +1,6 @@ require "crinja" +require "./service/service.cr" require "./config.cr" def sanitize_path(path) @@ -70,9 +71,29 @@ class Configure::Context service = (arguments.varargs[0]? || "").to_s.gsub /\//, ':' `get-port #{service}` end + options["random_password"] = Crinja.function do + id = (arguments.varargs[0]? || options["id"]).to_s + password_id = arguments.varargs[1]? || "main" + + env, service = Service.parse_id id + + # FIXME: hardcoded path + password_file = "/srv/#{env}/#{service}/password_#{password_id}" + + if File.exists? password_file + File.read password_file + else + password = `dd if=/dev/urandom bs=64 count=1 | base64 -` + password = password.gsub('\n', "") + + File.write password_file, password + + password + end + end providers = {} of String => String - ENV["SERVICE_TOKENS"].try &.split(':').each do |token| + ENV["SERVICE_TOKENS"]?.try &.split(':').each do |token| providers[token] = ENV["#{token.upcase}_PROVIDER"]? || "" end options["providers"] = providers @@ -102,5 +123,8 @@ begin rescue e : Configure::Exception STDERR.puts "Fatal error: #{e.message}" exit 1 +rescue e : Crinja::TypeError + STDERR.puts "Error reading template: #{e.message}" + exit 1 end diff --git a/src/service/service.cr b/src/service/service.cr index 83555fa..0a83d36 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -353,6 +353,20 @@ class Service id == self.id || (@environment.name == "root" && id == "root/#{name}") end + def self.parse_id(id) : Tuple(String, String) + s = id.split '/' + + environment = s[0] + service = s[1]? + + if service.nil? + service = environment + environment = "root" + end + + {environment, service} + end + alias ServiceTree = Array(ServiceTree) | Service # Returns a dependency tree.