gen-config: random_password(id, name)

master
Luka Vandervelden 2019-10-24 14:29:35 +02:00
parent 46eb8fa9c4
commit 10dcca2392
2 changed files with 39 additions and 1 deletions

View File

@ -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

View File

@ -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.