2019-06-09 18:08:59 +02:00
|
|
|
|
require "yaml"
|
|
|
|
|
require "colorize"
|
2019-10-19 18:40:47 +02:00
|
|
|
|
require "file_utils"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-17 20:06:42 +01:00
|
|
|
|
require "passwd"
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
require "./context.cr"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
require "./service_definition.cr"
|
|
|
|
|
require "./environment.cr"
|
|
|
|
|
require "./libc.cr"
|
|
|
|
|
|
|
|
|
|
def split_command(string)
|
|
|
|
|
args = string.split /\ (?=([^\"]*\"[^\"]*\")*[^\"]*$)/
|
|
|
|
|
|
|
|
|
|
command = args[0]
|
|
|
|
|
args.delete_at 0
|
|
|
|
|
|
|
|
|
|
return command, args
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-29 14:22:13 +01:00
|
|
|
|
lib C
|
|
|
|
|
fun waitpid(pid : Int32, status_ptr : Int32*, options : Int32) : Int32
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class Process
|
|
|
|
|
def self.waitpid(pid)
|
|
|
|
|
options = 0
|
|
|
|
|
status_ptr = uninitialized Int32
|
|
|
|
|
|
|
|
|
|
C.waitpid(pid, pointerof(status_ptr), options)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
class Service
|
|
|
|
|
getter environment : Environment
|
2019-11-09 21:52:43 +01:00
|
|
|
|
getter providers : ProvidersList
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-07 18:08:57 +01:00
|
|
|
|
setter name : String?
|
2019-10-28 13:22:15 +01:00
|
|
|
|
property domain : String?
|
2019-10-29 12:32:49 +01:00
|
|
|
|
getter ports = Hash(String, Int32).new
|
2019-10-26 14:00:11 +02:00
|
|
|
|
|
2019-11-10 19:25:45 +01:00
|
|
|
|
@reference : ServiceDefinition
|
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
# The place we’ll store configuration and data.
|
|
|
|
|
@root : String?
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context : Context
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
class Exception < ::Exception
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
class ProvidersList < Hash(String, String)
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context : Service::Context
|
|
|
|
|
def initialize(@context)
|
|
|
|
|
initialize
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
def []?(name)
|
2019-11-09 21:52:43 +01:00
|
|
|
|
super(name).try { |x| @context.get_service_by_id x}
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
|
struct Consumer
|
|
|
|
|
getter token : String
|
|
|
|
|
getter from : String
|
|
|
|
|
def initialize(@token, @from)
|
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def initialize(@context : Context, name, environment_name : String?)
|
|
|
|
|
@providers = ProvidersList.new @context
|
2019-11-10 19:25:45 +01:00
|
|
|
|
@reference = context.get_service_definition_by_name name
|
2019-06-09 18:08:59 +02:00
|
|
|
|
@environment = if environment_name.nil? || environment_name == ""
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.root_environment
|
2019-06-09 18:08:59 +02:00
|
|
|
|
else
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.get_environment_by_name environment_name
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def initialize(@context : Context, specs : SpecParser)
|
|
|
|
|
@providers = ProvidersList.new @context
|
2019-08-10 17:16:24 +02:00
|
|
|
|
assignments = specs.assignments
|
|
|
|
|
|
2019-11-07 18:08:57 +01:00
|
|
|
|
# Complicated because of compat with when services had no dedicated
|
|
|
|
|
# name and their types were their name.
|
|
|
|
|
type = assignments["type"]?.try &.as_s
|
|
|
|
|
@name = (assignments["name"]?.try(&.as_s) || type).not_nil!
|
2019-11-10 19:25:45 +01:00
|
|
|
|
@reference = context.get_service_definition_by_name (type || @name).not_nil!
|
2019-08-10 17:16:24 +02:00
|
|
|
|
|
2019-10-27 23:02:45 +01:00
|
|
|
|
@domain = assignments["domain"]?.try &.as_s
|
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
|
env = assignments["environment"]?.try &.as_s
|
|
|
|
|
@environment = if env.nil? || env == ""
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.root_environment
|
2019-08-10 17:16:24 +02:00
|
|
|
|
else
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.get_environment_by_name env
|
2019-08-10 17:16:24 +02:00
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
@root = assignments["root"]?.try &.as_s
|
|
|
|
|
|
2019-10-29 12:32:49 +01:00
|
|
|
|
assignments["ports"]?.try &.as_a_or_s.each do |port_string|
|
|
|
|
|
match = port_string.match /(.*)=(.*)/
|
|
|
|
|
|
|
|
|
|
if match.nil?
|
|
|
|
|
STDERR.puts "warning: '#{id}' has invalid port strings"
|
|
|
|
|
next
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
_, name, number = match
|
|
|
|
|
|
|
|
|
|
number = number.to_i
|
|
|
|
|
|
|
|
|
|
@ports[name] = number
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
|
specs.sections.select(&.name.==("consumes")).each do |section|
|
2019-10-24 15:41:53 +02:00
|
|
|
|
env, provider = Service.parse_id section.content["from"].as_s
|
|
|
|
|
@providers[section.options[0]] = "#{env}/#{provider}"
|
2019-08-10 17:16:24 +02:00
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
2019-08-10 17:16:24 +02:00
|
|
|
|
|
|
|
|
|
def to_spec
|
|
|
|
|
file = [
|
2019-11-07 18:08:57 +01:00
|
|
|
|
"type: #{@reference.name}",
|
2019-08-10 17:16:24 +02:00
|
|
|
|
"environment: #{@environment.name}"
|
|
|
|
|
]
|
|
|
|
|
|
2019-11-07 18:08:57 +01:00
|
|
|
|
unless @name.nil?
|
|
|
|
|
file << "name: #{@name}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-29 12:32:49 +01:00
|
|
|
|
if @ports.size > 0
|
|
|
|
|
ports_list = @ports.map do |name, number|
|
|
|
|
|
"#{name}=#{number}"
|
|
|
|
|
end.join ", "
|
|
|
|
|
|
|
|
|
|
file << "ports: #{ports_list}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
if @root
|
|
|
|
|
file << "root: #{@root}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-28 13:22:15 +01:00
|
|
|
|
if @domain
|
|
|
|
|
file << "domain: #{@domain}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-10 22:02:11 +02:00
|
|
|
|
@providers.each do |token, provider|
|
|
|
|
|
file << "%consumes #{token}"
|
|
|
|
|
file << " from: #{provider}"
|
2019-08-10 17:16:24 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
file.join("\n") + "\n"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-24 15:29:22 +02:00
|
|
|
|
def full_id
|
|
|
|
|
"#{@environment.name}/#{name}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
def id
|
|
|
|
|
if @environment.name == "root"
|
|
|
|
|
name
|
|
|
|
|
else
|
2019-10-24 15:29:22 +02:00
|
|
|
|
full_id
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# FIXME: At this point, macros would be both more readable and shorter.
|
|
|
|
|
def name
|
2019-11-07 18:08:57 +01:00
|
|
|
|
@name || @reference.name
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
def type
|
2019-11-07 18:08:57 +01:00
|
|
|
|
@reference.name
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
def command
|
|
|
|
|
@reference.command
|
|
|
|
|
end
|
|
|
|
|
def stop_command
|
|
|
|
|
@reference.stop_command
|
|
|
|
|
end
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def reload_command
|
|
|
|
|
@reference.reload_command
|
|
|
|
|
end
|
2019-11-11 16:52:40 +01:00
|
|
|
|
def readiness_check_command
|
|
|
|
|
@reference.readiness_check_command
|
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
def provides
|
|
|
|
|
@reference.provides
|
|
|
|
|
end
|
2019-08-10 22:02:11 +02:00
|
|
|
|
def consumes
|
|
|
|
|
@reference.consumes
|
|
|
|
|
end
|
2019-10-28 13:22:15 +01:00
|
|
|
|
def requires_domain
|
|
|
|
|
@reference.requires_domain
|
|
|
|
|
end
|
2019-10-29 12:32:49 +01:00
|
|
|
|
def port_definitions
|
|
|
|
|
@reference.port_definitions
|
|
|
|
|
end
|
2019-11-08 01:08:57 +01:00
|
|
|
|
def non_runnable
|
|
|
|
|
@reference.non_runnable
|
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
def root
|
|
|
|
|
@root || "#{@environment.root}/#{name}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-10 22:29:11 +02:00
|
|
|
|
def provides?(token)
|
|
|
|
|
provides.any? do |provider|
|
|
|
|
|
provider.token == token
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 14:47:29 +01:00
|
|
|
|
@commands_environment : Hash(String, String)?
|
|
|
|
|
private def build_commands_environment!
|
2019-08-10 17:16:24 +02:00
|
|
|
|
env = {} of String => String
|
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
env["SERVICE_NAME"] = name
|
2019-10-26 14:00:11 +02:00
|
|
|
|
env["SERVICE_ROOT"] = root
|
2019-10-24 15:29:22 +02:00
|
|
|
|
env["SERVICE_ID"] = full_id
|
2019-11-17 20:06:42 +01:00
|
|
|
|
env["SERVICE_USER"] = user_name
|
2019-11-09 21:52:43 +01:00
|
|
|
|
if _pid = pid @context.pid_directory
|
|
|
|
|
env["SERVICE_PID"] = _pid.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
|
env["ENVIRONMENT"] = @environment.name
|
|
|
|
|
env["ENVIRONMENT_TYPE"] = @environment.type.to_s
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-10-20 18:05:31 +02:00
|
|
|
|
@providers.each do |token, provider|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
service_provider = @context.get_service_by_id provider
|
2019-10-26 14:00:11 +02:00
|
|
|
|
|
2019-11-17 20:06:42 +01:00
|
|
|
|
# FIXME: Warning if mandatory?
|
2019-10-26 14:00:11 +02:00
|
|
|
|
next if service_provider.nil?
|
|
|
|
|
|
2019-10-20 18:05:31 +02:00
|
|
|
|
env["#{token.upcase}_PROVIDER"] = provider
|
2019-10-26 14:00:11 +02:00
|
|
|
|
env["#{token.upcase}_ENVIRONMENT"] = service_provider.environment.name
|
|
|
|
|
env["#{token.upcase}_ROOT"] = service_provider.root
|
2019-10-20 18:05:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
env["SERVICE_TOKENS"] = @providers.to_a.map(&.[0]).join ':'
|
|
|
|
|
|
2019-11-11 16:52:40 +01:00
|
|
|
|
@ports.each do |name, number|
|
|
|
|
|
name = name.upcase
|
|
|
|
|
|
|
|
|
|
env["PORT_#{name}"] = number.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
# FIXME: Parsing should probably be done… when parsing the file.
|
|
|
|
|
# FIXME: Parsing is probably a bit primitive. Maybe this isn’t the right way of defining this.
|
|
|
|
|
@reference.environment_variables.each do |string|
|
|
|
|
|
# FIXME: Should probably deserve a warning.
|
|
|
|
|
variable = string.match(/^[^=]*=/).not_nil![0]
|
|
|
|
|
value = string[variable.size..string.size]
|
|
|
|
|
variable = variable[0..variable.size-2]
|
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
|
env[variable] = value
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
2019-08-10 17:16:24 +02:00
|
|
|
|
|
2019-11-09 14:47:29 +01:00
|
|
|
|
@commands_environment = env
|
|
|
|
|
end
|
|
|
|
|
def commands_environment : Hash(String, String)
|
|
|
|
|
@commands_environment || build_commands_environment!
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
2019-08-10 17:16:24 +02:00
|
|
|
|
|
|
|
|
|
# FIXME: Is working on ${} really a good idea?
|
2019-11-09 13:35:25 +01:00
|
|
|
|
# FIXME: There should be a unified way of obtaining a service’s
|
|
|
|
|
# environment, and we should take our variables from that
|
|
|
|
|
# instead of hardcoding everything.
|
2019-06-09 18:08:59 +02:00
|
|
|
|
private def evaluate(string)
|
2019-11-09 14:47:29 +01:00
|
|
|
|
env = commands_environment
|
|
|
|
|
|
2019-10-26 14:00:11 +02:00
|
|
|
|
string.gsub /\${[a-zA-Z_]+}/ do |match|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
match = match[2..match.size-2]
|
|
|
|
|
|
2019-11-09 14:47:29 +01:00
|
|
|
|
env[match.upcase]? || ""
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
def files
|
|
|
|
|
@environment.files + @reference.files
|
2019-10-26 14:50:03 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def build_files!(context : Context)
|
2019-11-08 14:36:59 +01:00
|
|
|
|
files.each do |file|
|
2019-10-26 14:50:03 +02:00
|
|
|
|
run_hook = false
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
next unless creation_command = file.creation_command
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
path = evaluate file.file_path
|
|
|
|
|
if path[0] != '/'
|
|
|
|
|
path = "#{root}/#{path}"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
run_hook = (!File.exists? path) || file.is_configuration?
|
|
|
|
|
|
|
|
|
|
next unless run_hook
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
context.info "Creating #{file.name}"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-17 20:06:42 +01:00
|
|
|
|
uid, gid = get_uid_gid
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
child = Process.fork do
|
2019-11-08 14:36:59 +01:00
|
|
|
|
Dir.cd root
|
|
|
|
|
|
2020-01-04 13:15:19 +01:00
|
|
|
|
System.become_user user_name
|
2019-11-17 20:06:42 +01:00
|
|
|
|
|
2019-11-08 14:36:59 +01:00
|
|
|
|
Process.exec "sh", ["-c", creation_command],
|
2019-08-10 17:16:24 +02:00
|
|
|
|
output: Process::Redirect::Inherit,
|
|
|
|
|
error: Process::Redirect::Inherit,
|
2019-11-09 14:47:29 +01:00
|
|
|
|
env: commands_environment
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end.wait
|
|
|
|
|
|
2019-11-08 15:54:44 +01:00
|
|
|
|
unless child.success?
|
|
|
|
|
raise Service::Exception.new "Child process exited with status “#{(child.exit_status/256).to_i}”."
|
2019-06-09 18:08:59 +02:00
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-11-09 21:52:43 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def start(context : Context)
|
|
|
|
|
return if running? context.pid_directory
|
|
|
|
|
|
|
|
|
|
if non_runnable
|
|
|
|
|
context.title "Setting up #{to_s}"
|
|
|
|
|
else
|
|
|
|
|
context.title "Starting #{to_s}"
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-17 20:06:42 +01:00
|
|
|
|
create_user_and_group!
|
|
|
|
|
|
|
|
|
|
uid, gid = get_uid_gid
|
2019-11-09 21:52:43 +01:00
|
|
|
|
FileUtils.mkdir_p root
|
2019-11-17 20:06:42 +01:00
|
|
|
|
File.chown root, uid, gid
|
2019-11-09 21:52:43 +01:00
|
|
|
|
|
|
|
|
|
build_files! context
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
return if non_runnable
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
# FIXME: Should evaluate be used in split_command? What namespace should split_command use?
|
|
|
|
|
command, args = split_command command
|
|
|
|
|
args.map! do |arg| evaluate arg end
|
|
|
|
|
|
|
|
|
|
process = Process.fork do
|
2019-11-09 13:35:25 +01:00
|
|
|
|
base_log_name = "#{context.log_directory}/#{name}.#{@environment.name}"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
stdout_file = File.open "#{base_log_name}.out", "w"
|
|
|
|
|
stderr_file = File.open "#{base_log_name}.err", "w"
|
|
|
|
|
|
|
|
|
|
LibC.dup2 stdout_file.fd, 1
|
|
|
|
|
LibC.dup2 stderr_file.fd, 2
|
|
|
|
|
|
2020-01-04 16:26:05 +01:00
|
|
|
|
unless @reference.start_as_root
|
|
|
|
|
System.become_user user_name
|
|
|
|
|
end
|
2019-06-10 00:39:15 +02:00
|
|
|
|
|
2019-08-10 17:16:24 +02:00
|
|
|
|
Process.exec command, args,
|
2019-10-26 15:24:22 +02:00
|
|
|
|
chdir: (@reference.directory.try { |x| evaluate x } || root),
|
2019-11-09 14:47:29 +01:00
|
|
|
|
env: commands_environment
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
self.save_pid context.pid_directory, process.pid
|
2019-11-11 16:52:40 +01:00
|
|
|
|
|
|
|
|
|
# FIXME: At this point, let’s wait a few seconds for the service to be ready.
|
|
|
|
|
if command = readiness_check_command
|
|
|
|
|
context.info "Waiting for service to become ready…"
|
|
|
|
|
|
|
|
|
|
start_time = Time.local
|
|
|
|
|
now = Time.local
|
|
|
|
|
|
|
|
|
|
while (now - start_time).seconds < 5
|
2019-11-11 19:02:04 +01:00
|
|
|
|
r = Process.run "sh", ["-c", command],
|
|
|
|
|
output: Process::Redirect::Inherit,
|
|
|
|
|
error: Process::Redirect::Inherit,
|
|
|
|
|
env: commands_environment
|
2019-11-11 16:52:40 +01:00
|
|
|
|
|
|
|
|
|
if r.success?
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
sleep 0.05
|
|
|
|
|
|
|
|
|
|
now = Time.local
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-11-11 20:52:15 +01:00
|
|
|
|
|
|
|
|
|
provides.each do |token_definition|
|
|
|
|
|
reverse_dependencies = get_consumers(token_definition.token)
|
|
|
|
|
.map(&.id)
|
|
|
|
|
.compact_map do |id|
|
|
|
|
|
context.get_service_by_id id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
reverse_dependencies.each do |service|
|
|
|
|
|
next unless service.non_runnable
|
|
|
|
|
|
|
|
|
|
should_start = service
|
|
|
|
|
.dependency_tree.flatten
|
|
|
|
|
.select(&.!=(self))
|
|
|
|
|
.map(&.status(context))
|
|
|
|
|
.map do |status|
|
|
|
|
|
status.running? || status.non_runnable?
|
|
|
|
|
end
|
|
|
|
|
.reduce do |a, b| a && b end
|
|
|
|
|
|
|
|
|
|
if should_start
|
|
|
|
|
service.start context
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# TODO:
|
|
|
|
|
# - Custom shutdown commands.
|
|
|
|
|
# - Should we wait for the process to die?
|
|
|
|
|
# - Shouldn’t we remove the pid file?
|
2019-11-09 13:35:25 +01:00
|
|
|
|
def stop(context : Context)
|
|
|
|
|
return if non_runnable
|
|
|
|
|
|
|
|
|
|
context.title "Stopping #{to_s}"
|
|
|
|
|
|
|
|
|
|
_pid = pid context.pid_directory
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
|
|
|
|
if _pid
|
|
|
|
|
command = stop_command
|
|
|
|
|
if command
|
|
|
|
|
command, args = split_command command
|
|
|
|
|
|
|
|
|
|
Process.run(command, args)
|
|
|
|
|
else
|
|
|
|
|
Process.kill Signal::TERM, _pid
|
|
|
|
|
end
|
2019-10-29 14:22:13 +01:00
|
|
|
|
|
|
|
|
|
Process.waitpid _pid
|
2019-11-09 13:35:25 +01:00
|
|
|
|
File.delete(get_pid_file context.pid_directory)
|
2019-06-09 18:08:59 +02:00
|
|
|
|
else
|
|
|
|
|
# Already stopped or dead, nothing to be done here.
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
def reloadable?
|
|
|
|
|
!@reload_command.nil?
|
|
|
|
|
end
|
|
|
|
|
def reload(context : Context)
|
|
|
|
|
build_files! context
|
|
|
|
|
|
|
|
|
|
command = reload_command
|
|
|
|
|
|
|
|
|
|
unless command
|
|
|
|
|
raise Exception.new "This service cannot be reloaded!"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Process.fork do
|
|
|
|
|
Dir.cd root
|
|
|
|
|
|
|
|
|
|
Process.exec "sh", ["-c", command],
|
|
|
|
|
output: Process::Redirect::Inherit,
|
|
|
|
|
error: Process::Redirect::Inherit,
|
|
|
|
|
env: commands_environment
|
|
|
|
|
end.wait
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
def get_pid_file(pid_dir)
|
2019-10-24 18:29:22 +02:00
|
|
|
|
"#{pid_dir}/#{name}.#{environment.name}.pid"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def pid(pid_dir)
|
|
|
|
|
File.read(get_pid_file pid_dir).to_i
|
|
|
|
|
rescue e # pid file missing, corrupted or otherwise not readable
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def save_pid(pid_dir, new_pid)
|
2019-10-19 18:43:40 +02:00
|
|
|
|
FileUtils.mkdir_p pid_dir
|
2019-06-09 18:08:59 +02:00
|
|
|
|
File.write get_pid_file(pid_dir), new_pid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
enum Status
|
|
|
|
|
Running
|
|
|
|
|
Dead
|
|
|
|
|
Stopped
|
2019-11-08 01:08:57 +01:00
|
|
|
|
NonRunnable
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def status(pid_dir)
|
2019-11-08 01:08:57 +01:00
|
|
|
|
return Status::NonRunnable if non_runnable
|
2019-06-09 18:08:59 +02:00
|
|
|
|
_pid = pid pid_dir
|
|
|
|
|
|
|
|
|
|
if _pid
|
|
|
|
|
if Process.exists? _pid
|
|
|
|
|
Status::Running
|
|
|
|
|
else
|
|
|
|
|
Status::Dead
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
Status::Stopped
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
def running?(pid_dir)
|
|
|
|
|
status(pid_dir) == Service::Status::Running
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
|
"#{name} (in #{@environment.name})"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def summary
|
2019-10-29 12:48:36 +01:00
|
|
|
|
"%-16s #{name.colorize(:white).to_s}\n" % "Name:" +
|
2019-11-07 18:08:57 +01:00
|
|
|
|
"%-16s #{type.colorize(:white).to_s}\n" % "Type:" +
|
2019-10-29 12:48:36 +01:00
|
|
|
|
"%-16s #{environment.name.colorize(:white).to_s} (#{environment.type.to_s.downcase})\n" % "Environment:" +
|
2019-06-09 18:08:59 +02:00
|
|
|
|
(
|
|
|
|
|
if provides.size > 0
|
|
|
|
|
"Provides:\n" +
|
2019-10-29 12:48:36 +01:00
|
|
|
|
(provides.map { |x|
|
|
|
|
|
consumers = get_consumers(x.token)
|
|
|
|
|
|
|
|
|
|
consumers_string = if consumers.size > 0
|
|
|
|
|
consumers.map(&.full_id.colorize(:green).to_s).join ", "
|
|
|
|
|
else
|
|
|
|
|
"n/a".colorize(:yellow).to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
" - %-12s #{consumers_string}\n" % "#{x.token}:"
|
|
|
|
|
}).join
|
2019-06-09 18:08:59 +02:00
|
|
|
|
else
|
|
|
|
|
""
|
|
|
|
|
end
|
|
|
|
|
) +
|
|
|
|
|
(
|
|
|
|
|
if @reference.consumes.size > 0
|
|
|
|
|
"Consumes:\n" +
|
|
|
|
|
(@reference.consumes.map { |x|
|
|
|
|
|
provider = @providers[x.token]?
|
2019-10-29 12:48:36 +01:00
|
|
|
|
|
|
|
|
|
provider_string = if provider
|
|
|
|
|
provider.full_id.colorize(:green).to_s
|
|
|
|
|
elsif x.optional
|
|
|
|
|
"n/a".colorize(:yellow).to_s
|
2019-06-09 18:08:59 +02:00
|
|
|
|
else
|
2019-10-29 12:48:36 +01:00
|
|
|
|
"MISSING".colorize(:red).to_s
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
2019-10-29 12:48:36 +01:00
|
|
|
|
|
|
|
|
|
" - %-12s #{provider_string}\n" % x.token
|
|
|
|
|
}).join
|
|
|
|
|
else
|
|
|
|
|
""
|
|
|
|
|
end
|
|
|
|
|
) +
|
|
|
|
|
(
|
|
|
|
|
if @ports.size > 0
|
|
|
|
|
"Ports:\n" +
|
|
|
|
|
(@ports.map { |name, number|
|
|
|
|
|
" - %-12s #{number.colorize(:magenta).to_s}\n" % "#{name}:"
|
2019-06-09 18:08:59 +02:00
|
|
|
|
}).join
|
|
|
|
|
else
|
|
|
|
|
""
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def write(path)
|
2019-10-19 18:40:47 +02:00
|
|
|
|
FileUtils.mkdir_p "#{path}"
|
2019-08-10 17:16:24 +02:00
|
|
|
|
File.write "#{path}/#{name}.#{@environment.name}.spec", to_spec
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
def remove(context)
|
|
|
|
|
context.title "Removing #{to_s}"
|
|
|
|
|
|
2019-11-08 15:54:44 +01:00
|
|
|
|
files.reverse.each do |file|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
file_path = evaluate file.file_path
|
|
|
|
|
|
2019-11-09 14:49:54 +01:00
|
|
|
|
context.info "Removing #{file.name}"
|
2019-11-08 15:54:44 +01:00
|
|
|
|
command = file.deletion_command
|
|
|
|
|
|
|
|
|
|
child = Process.fork do
|
|
|
|
|
Dir.cd root
|
|
|
|
|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
exit 0 unless File.exists? file_path
|
2019-11-08 15:54:44 +01:00
|
|
|
|
|
|
|
|
|
if command
|
|
|
|
|
Process.exec "sh", ["-c", command],
|
|
|
|
|
output: Process::Redirect::Inherit,
|
|
|
|
|
error: Process::Redirect::Inherit,
|
2019-11-09 14:47:29 +01:00
|
|
|
|
env: commands_environment
|
2019-11-08 15:54:44 +01:00
|
|
|
|
else
|
2019-11-09 13:35:25 +01:00
|
|
|
|
FileUtils.rm_rf file_path
|
2019-11-08 15:54:44 +01:00
|
|
|
|
end
|
|
|
|
|
end.wait
|
|
|
|
|
|
|
|
|
|
unless child.success?
|
|
|
|
|
raise Service::Exception.new "Child process exited with status “#{(child.exit_status/256).to_i}”."
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-17 20:06:42 +01:00
|
|
|
|
remove_user_and_group!
|
|
|
|
|
|
2019-11-09 13:35:25 +01:00
|
|
|
|
File.delete "#{context.services_directory}/#{name}.#{@environment.name}.spec"
|
2019-11-17 20:06:42 +01:00
|
|
|
|
|
|
|
|
|
FileUtils.rm_rf root
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def is_id?(id)
|
|
|
|
|
id == self.id || (@environment.name == "root" && id == "root/#{name}")
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-24 14:29:35 +02:00
|
|
|
|
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
|
|
|
|
|
|
2019-06-09 18:08:59 +02:00
|
|
|
|
alias ServiceTree = Array(ServiceTree) | Service
|
|
|
|
|
|
|
|
|
|
# Returns a dependency tree.
|
|
|
|
|
# You’ll probably want to flatten and reverse it afterwards.
|
|
|
|
|
def dependency_tree
|
|
|
|
|
tree = [self] of ServiceTree
|
|
|
|
|
|
2019-08-10 22:02:11 +02:00
|
|
|
|
@providers.each do |token, provider_id|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
service = @context.get_service_by_id provider_id
|
2019-06-09 18:08:59 +02:00
|
|
|
|
|
|
|
|
|
unless service
|
|
|
|
|
# FIXME: Does it make the dep tree invalid?
|
|
|
|
|
# FIXME: Only optional deps should be fine.
|
|
|
|
|
next
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
tree << service.dependency_tree
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
tree
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-25 18:40:27 +02:00
|
|
|
|
def reverse_dependencies
|
|
|
|
|
rdeps = [self]
|
|
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
while i < rdeps.size
|
|
|
|
|
item = rdeps[i]
|
|
|
|
|
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.services.each do |service|
|
2019-10-25 18:40:27 +02:00
|
|
|
|
service.providers.any? do |token, id|
|
|
|
|
|
if item.is_id?(id) && ! rdeps.any? service
|
|
|
|
|
rdeps << service
|
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-10-25 18:40:27 +02:00
|
|
|
|
|
|
|
|
|
i += 1
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-25 18:40:27 +02:00
|
|
|
|
rdeps
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
2019-08-10 22:29:11 +02:00
|
|
|
|
|
|
|
|
|
def get_default_provider(token) : String?
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@environment.get_provider(token) || @context.root_environment.get_provider(token)
|
2019-08-10 22:29:11 +02:00
|
|
|
|
end
|
2019-10-28 06:06:18 +01:00
|
|
|
|
|
|
|
|
|
def consumes?(token, origin)
|
|
|
|
|
providers.select do |_token, provider|
|
|
|
|
|
token == _token && origin.is_id?(provider)
|
|
|
|
|
end.size > 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_consumers(token)
|
2019-11-09 21:52:43 +01:00
|
|
|
|
@context.services.select(&.consumes?(token, self))
|
2019-10-28 06:06:18 +01:00
|
|
|
|
end
|
2019-11-17 20:06:42 +01:00
|
|
|
|
|
2020-01-04 16:15:44 +01:00
|
|
|
|
def default_user_name
|
2019-11-17 20:06:42 +01:00
|
|
|
|
full_id.sub('/', '.')
|
|
|
|
|
end
|
2020-01-04 16:15:44 +01:00
|
|
|
|
def user_name
|
|
|
|
|
@reference.user || default_user_name
|
|
|
|
|
end
|
|
|
|
|
def group_name
|
|
|
|
|
@reference.group || default_user_name
|
|
|
|
|
end
|
2019-11-17 20:06:42 +01:00
|
|
|
|
|
|
|
|
|
def get_uid_gid
|
|
|
|
|
passwd = Passwd.new("/etc/passwd", "/etc/group")
|
|
|
|
|
|
|
|
|
|
user = passwd.get_user(user_name).not_nil!
|
|
|
|
|
|
|
|
|
|
{user.uid, user.gid}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create_user_and_group!
|
|
|
|
|
Passwd.new("/etc/passwd", "/etc/group").tap do |passwd|
|
2020-01-04 09:08:55 +01:00
|
|
|
|
if user = passwd.get_user user_name
|
|
|
|
|
group = passwd.get_group user_name
|
2019-11-17 20:06:42 +01:00
|
|
|
|
|
2020-01-04 09:08:55 +01:00
|
|
|
|
return if group.nil?
|
|
|
|
|
|
|
|
|
|
group.users = reverse_dependencies.map &.user_name
|
|
|
|
|
|
|
|
|
|
passwd.mod_group group
|
|
|
|
|
else
|
|
|
|
|
passwd.add_user user_name,
|
|
|
|
|
full_name: "Service[#{id}]"
|
|
|
|
|
end
|
2019-11-17 20:06:42 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def remove_user_and_group!
|
|
|
|
|
Passwd.new("/etc/passwd", "/etc/group").tap do |passwd|
|
|
|
|
|
passwd.remove_user user_name
|
|
|
|
|
passwd.remove_group user_name
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-06-09 18:08:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|