diff --git a/services/gitea.spec b/services/gitea.spec index 5cb25b8..28ae5b5 100644 --- a/services/gitea.spec +++ b/services/gitea.spec @@ -3,26 +3,26 @@ command: ./gitea -C . -w . -c ./custom/conf/app.ini directory: ${SERVICE_ROOT} # FIXME: Could this be removed? consumes: postgresql -%check +%pre-start name: working directory # 'command' is run only if this directory doesn't exist - directory: ${SERVICE_ROOT}/custom/conf/ + unless-directory: ${SERVICE_ROOT}/custom/conf/ command: mkdir -p ${SERVICE_ROOT}/custom/conf/ -%check +%pre-start name: symlink # 'command' is run only if this directory doesn't exist - file: ${SERVICE_ROOT}/gitea + unless-file: ${SERVICE_ROOT}/gitea command: ln -s $(which gitea) ${SERVICE_ROOT}/gitea -%check +%pre-start name: configuration file # 'command' is run only if this file doesn't exist - file: ${SERVICE_ROOT}/custom/conf/app.ini + unless-file: ${SERVICE_ROOT}/custom/conf/app.ini command: gen-config gitea.cfg ${SERVICE_ROOT}/custom/conf/app.ini postgresql=${POSTGRESQL_PROVIDER} -%check +%pre-start name: gitea database creation # 'command' is run only if this directory doesn't exist - file: ${SERVICE_ROOT}/db_is_setup + unless-file: ${SERVICE_ROOT}/db_is_setup command: pg_create_user.sh --pghost=localhost --pgport=$(get-port $POSTGRESQL_PROVIDER) --pgdatadir=$POSTGRESQL_ROOT --pguser=postgres --dbuser=${ENVIRONMENT}_gitea --dbpassfile=${SERVICE_ROOT}/password_main --dbname=${ENVIRONMENT}_gitea_db create_user_and_db && touch ${SERVICE_ROOT}/db_is_setup diff --git a/services/postgresql.spec b/services/postgresql.spec index 0850499..cc26fbe 100644 --- a/services/postgresql.spec +++ b/services/postgresql.spec @@ -6,26 +6,26 @@ environment-variables: - PGROOT=${SERVICE_ROOT} provides: postgresql -%check +%pre-start name: database directory creation - directory: ${SERVICE_ROOT} + unless-directory: ${SERVICE_ROOT} command: mkdir -p ${SERVICE_ROOT} && chown postgres:postgres ${SERVICE_ROOT} -%check +%pre-start name: database creation - file: ${SERVICE_ROOT}/base + unless-file: ${SERVICE_ROOT}/base command: su - postgres -c "initdb --locale en_US.UTF-8 -D '${SERVICE_ROOT}'" && rm ${SERVICE_ROOT}/postgresql.conf -%check +%pre-start name: database configuration # once this file is created, there is no need to perform the command - file: ${SERVICE_ROOT}/postgresql.conf + unless-file: ${SERVICE_ROOT}/postgresql.conf # gen-config inherits its parameters from the environment command: gen-config postgresql.conf ${SERVICE_ROOT}/postgresql.conf && chown postgres:postgres ${SERVICE_ROOT}/postgresql.conf -%check +%pre-start name: sockets directory - directory: /tmp/postgresql-${ENVIRONMENT} + unless-directory: /tmp/postgresql-${ENVIRONMENT} # FIXME: impose permissions command: mkdir -p /tmp/postgresql-${ENVIRONMENT} && chown postgres:postgres /tmp/postgresql-${ENVIRONMENT} - # FIXME: add postgresql-check-db-dir around here + # FIXME: add postgresql-pre-start-db-dir around here diff --git a/src/service/environment.cr b/src/service/environment.cr index e58c282..14d5071 100644 --- a/src/service/environment.cr +++ b/src/service/environment.cr @@ -9,7 +9,7 @@ class Environment getter name : String getter type : Type = Type::Prefix getter domain_name : String? - getter checks = Array(ServiceDefinition::Checks).new + getter pre_start_hooks = Array(ServiceDefinition::Hook).new # The place we’ll put services’ data and configuration. @root : String? @@ -21,12 +21,12 @@ class Environment def initialize(@name, type = "prefix") @type = Type.parse type - @checks = Array(ServiceDefinition::Checks).new + @pre_start_hooks = Array(ServiceDefinition::Hook).new # FIXME: Should this *really* be here? - @checks << ServiceDefinition::Checks.new "Creating data directory", + @pre_start_hooks << ServiceDefinition::Hook.new "Creating data directory", "mkdir -p /srv/${ENVIRONMENT} && chmod a+rwt /srv/${ENVIRONMENT}", - directory: "/srv/${ENVIRONMENT}" + unless_directory: "/srv/${ENVIRONMENT}" end def initialize(@name, specs : SpecParser) @@ -37,7 +37,7 @@ class Environment end specs.sections.select(&.name.==("check")).each do |check| - @checks << ServiceDefinition::Checks.new check + @pre_start_hooks << ServiceDefinition::Hook.new check end end diff --git a/src/service/service.cr b/src/service/service.cr index 5178b28..3ac77af 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -179,28 +179,32 @@ class Service end end + def pre_start_hooks + @environment.pre_start_hooks + @reference.pre_start_hooks + end + def start(pid_dir : String, log_dir : String) - (@environment.checks + @reference.checks).each do |check| - run_check = false + pre_start_hooks.each do |hook| + run_hook = false - check.file.try do |file| + hook.unless_file.try do |file| file = evaluate file - run_check = true if ! File.exists? file + run_hook = true if ! File.exists? file end - check.directory.try do |directory| + hook.unless_directory.try do |directory| directory = evaluate directory - run_check = true if ! Dir.exists? directory + run_hook = true if ! Dir.exists? directory end - unless run_check + unless run_hook next end - puts " - #{check.name}" + puts " - #{hook.name}" child = Process.fork do - Process.exec "sh", ["-c", check.command], + Process.exec "sh", ["-c", hook.command], output: Process::Redirect::Inherit, error: Process::Redirect::Inherit, env: build_environment diff --git a/src/service/service_definition.cr b/src/service/service_definition.cr index ac76a85..0e3b985 100644 --- a/src/service/service_definition.cr +++ b/src/service/service_definition.cr @@ -18,19 +18,21 @@ class ServiceDefinition def initialize(@token) end end - struct Checks + struct Hook getter name : String getter command : String - getter directory : String? - getter file : String? + getter unless_directory : String? + getter unless_file : String? - def initialize(@name, @command, @file = nil, @directory = nil) + def initialize(@name, @command, @unless_file = nil, @unless_directory = nil) end def initialize(section : SpecParser::Section) @name = section.content["name"].as_s - @file = section.content["file"]?.try &.as_s - @directory = section.content["directory"]?.try &.as_s @command = section.content["command"].as_s + + @unless_directory = section.content["unless-directory"]? + .try &.as_s + @unless_file = section.content["unless-file"]?.try &.as_s end end @@ -44,7 +46,7 @@ class ServiceDefinition getter provides : String? getter consumes : Array(Consumes) getter environment_variables : Array(String) - getter checks : Array(Checks) + getter pre_start_hooks : Array(Hook) getter provides : Array(Provides) def initialize(@name, specs : SpecParser) @@ -56,8 +58,9 @@ class ServiceDefinition @user = specs["user"]?.try &.as_s @provides = specs["provides"]?.try &.as_a_or_s.map { |x| Provides.new x } || Array(Provides).new @consumes = specs["consumes"]?.try &.as_a_or_s.map { |x| Consumes.new x } || Array(Consumes).new - @checks = sections.select(&.name.== "check").map { |x| Checks.new x } || Array(Checks).new @environment_variables = specs["environment-variables"]?.try &.as_a_or_s || Array(String).new + + @pre_start_hooks = sections.select(&.name.== "pre-start").map { |x| Hook.new x } || Array(Hook).new end def self.load(path)