Updates for configuration generation.

master
Luka Vandervelden 2019-10-20 18:05:31 +02:00
parent 6237a9bb10
commit 2dfcfcb979
3 changed files with 53 additions and 4 deletions

View File

@ -8,9 +8,9 @@ variables+=(
VARSTATEDIR '/var' VARSTATEDIR '/var'
) )
targets=(service status) targets=(service status get-port)
for target in service status; do for target in service status get-port; do
type[$target]=crystal type[$target]=crystal
sources[$target]=src/${target}.cr sources[$target]=src/${target}.cr
depends[$target]=src/config.cr depends[$target]=src/config.cr

41
src/get-port.cr Normal file
View File

@ -0,0 +1,41 @@
require "file_utils"
require "./config.cr"
START_PORT = 49152
PORTS_CACHE_DIRECTORY = "#{CACHE_DIRECTORY}/ports/"
service = ARGV[0]
wanted_default_port = ARGV[1]?.try &.to_i
service_port_file = "#{PORTS_CACHE_DIRECTORY}/#{service}"
begin
if File.exists? service_port_file
puts File.read service_port_file
exit 0
end
FileUtils.mkdir_p PORTS_CACHE_DIRECTORY
used_ports = Dir.children(PORTS_CACHE_DIRECTORY)
.map { |x| "#{PORTS_CACHE_DIRECTORY}/#{x}" }
.map { |x| File.read(x).to_i }
.sort
port = START_PORT
if wanted_default_port && ! used_ports.any? &.==(wanted_default_port)
port = wanted_default_port
else
while used_ports.any? &.==(port)
port = port + 1
end
end
File.write service_port_file, port
puts port
rescue e
STDERR.puts "error: #{e.message}"
exit 1
end

View File

@ -113,9 +113,17 @@ class Service
private def build_environment private def build_environment
env = {} of String => String env = {} of String => String
env["SERVICE_ID"] = id
env["ENVIRONMENT"] = @environment.name env["ENVIRONMENT"] = @environment.name
env["ENVIRONMENT_TYPE"] = @environment.type.to_s env["ENVIRONMENT_TYPE"] = @environment.type.to_s
@providers.each do |token, provider|
env["#{token.upcase}_PROVIDER"] = provider
env["#{token.upcase}_ENVIRONMENT"] = provider.gsub /\/.*/, ""
end
env["SERVICE_TOKENS"] = @providers.to_a.map(&.[0]).join ':'
# FIXME: Parsing should probably be done… when parsing the file. # FIXME: Parsing should probably be done… when parsing the file.
# FIXME: Parsing is probably a bit primitive. Maybe this isnt the right way of defining this. # FIXME: Parsing is probably a bit primitive. Maybe this isnt the right way of defining this.
@reference.environment_variables.each do |string| @reference.environment_variables.each do |string|
@ -164,7 +172,7 @@ class Service
puts " - #{check.name}" puts " - #{check.name}"
child = Process.fork do child = Process.fork do
Process.exec "sh", ["-c", evaluate check.command], Process.exec "sh", ["-c", check.command],
output: Process::Redirect::Inherit, output: Process::Redirect::Inherit,
error: Process::Redirect::Inherit, error: Process::Redirect::Inherit,
env: build_environment env: build_environment
@ -196,7 +204,7 @@ class Service
end end
Process.exec command, args, Process.exec command, args,
chdir: @reference.directory, chdir: @reference.directory.try { |x| evaluate x },
env: build_environment env: build_environment
end end