diff --git a/project.zsh b/project.zsh index 49f5b51..6f4ffec 100644 --- a/project.zsh +++ b/project.zsh @@ -6,6 +6,7 @@ variables+=( LIBEXECDIR '$(PREFIX)/libexec' SYSCONFDIR '/etc' VARSTATEDIR '/var' + DATADIR '/srv' ) targets=(service status get-port gen-config) diff --git a/services/gitea.spec b/services/gitea.spec index 55a45dc..5cb25b8 100644 --- a/services/gitea.spec +++ b/services/gitea.spec @@ -1,28 +1,28 @@ command: ./gitea -C . -w . -c ./custom/conf/app.ini # deamon's working directory -directory: /srv/${ENVIRONMENT}/gitea +directory: ${SERVICE_ROOT} # FIXME: Could this be removed? consumes: postgresql %check name: working directory # 'command' is run only if this directory doesn't exist - directory: /srv/${ENVIRONMENT}/gitea/custom/conf/ - command: mkdir -p /srv/${ENVIRONMENT}/gitea/custom/conf/ + directory: ${SERVICE_ROOT}/custom/conf/ + command: mkdir -p ${SERVICE_ROOT}/custom/conf/ %check name: symlink # 'command' is run only if this directory doesn't exist - file: /srv/${ENVIRONMENT}/gitea/gitea - command: ln -s $(which gitea) /srv/${ENVIRONMENT}/gitea/ + file: ${SERVICE_ROOT}/gitea + command: ln -s $(which gitea) ${SERVICE_ROOT}/gitea %check name: configuration file # 'command' is run only if this file doesn't exist - file: /srv/${ENVIRONMENT}/gitea/custom/conf/app.ini - command: gen-config gitea.cfg /srv/${ENVIRONMENT}/gitea/custom/conf/app.ini postgresql=${POSTGRESQL_PROVIDER} + file: ${SERVICE_ROOT}/custom/conf/app.ini + command: gen-config gitea.cfg ${SERVICE_ROOT}/custom/conf/app.ini postgresql=${POSTGRESQL_PROVIDER} %check name: gitea database creation # 'command' is run only if this directory doesn't exist - file: /srv/${ENVIRONMENT}/gitea/db_is_setup - command: pg_create_user.sh --pghost=localhost --pgport=$(get-port $POSTGRESQL_PROVIDER) --pgdatadir=/srv/$POSTGRESQL_ENVIRONMENT/postgresql --pguser=postgres --dbuser=${ENVIRONMENT}_gitea --dbpassfile=/srv/$ENVIRONMENT/gitea/password_main --dbname=${ENVIRONMENT}_gitea_db create_user_and_db && touch /srv/$ENVIRONMENT/gitea/db_is_setup + 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 b9bfaf0..0850499 100644 --- a/services/postgresql.spec +++ b/services/postgresql.spec @@ -1,27 +1,27 @@ name: postgresql user: postgres -command: postgres -D /srv/${ENVIRONMENT}/postgresql -k /tmp/postgresql-${ENVIRONMENT} +command: postgres -D ${SERVICE_ROOT} -k /tmp/postgresql-${ENVIRONMENT} #stop-command: kill -HUP ${PID} environment-variables: - - PGROOT=/srv/${ENVIRONMENT}/postgresql + - PGROOT=${SERVICE_ROOT} provides: postgresql %check name: database directory creation - directory: /srv/${ENVIRONMENT}/postgresql - command: mkdir -p /srv/${ENVIRONMENT}/postgresql && chown postgres:postgres /srv/${ENVIRONMENT}/postgresql + directory: ${SERVICE_ROOT} + command: mkdir -p ${SERVICE_ROOT} && chown postgres:postgres ${SERVICE_ROOT} %check name: database creation - file: /srv/${ENVIRONMENT}/postgresql/base - command: su - postgres -c "initdb --locale en_US.UTF-8 -D '/srv/${ENVIRONMENT}/postgresql'" && rm /srv/${ENVIRONMENT}/postgresql/postgresql.conf + file: ${SERVICE_ROOT}/base + command: su - postgres -c "initdb --locale en_US.UTF-8 -D '${SERVICE_ROOT}'" && rm ${SERVICE_ROOT}/postgresql.conf %check name: database configuration # once this file is created, there is no need to perform the command - file: /srv/${ENVIRONMENT}/postgresql/postgresql.conf + file: ${SERVICE_ROOT}/postgresql.conf # gen-config inherits its parameters from the environment - command: gen-config postgresql.conf /srv/${ENVIRONMENT}/postgresql/postgresql.conf && chown postgres:postgres /srv/${ENVIRONMENT}/postgresql/postgresql.conf + command: gen-config postgresql.conf ${SERVICE_ROOT}/postgresql.conf && chown postgres:postgres ${SERVICE_ROOT}/postgresql.conf %check name: sockets directory diff --git a/src/config.cr.in b/src/config.cr.in index d375c60..d30a9fa 100644 --- a/src/config.cr.in +++ b/src/config.cr.in @@ -9,4 +9,5 @@ SYSTEM_CONFIGURATION_DIRECTORY = "@SYSCONFDIR@" SHARED_DATA_DIRECTORY = "@SHAREDIR@" OWN_LIBEXEC_DIR = "@LIBEXECDIR@/service" CACHE_DIRECTORY = "@VARSTATEDIR@/cache" +SERVED_DATA_DIRECTORY = "@DATADIR@" diff --git a/src/gen-config.cr b/src/gen-config.cr index 4100a3e..5ea81c1 100644 --- a/src/gen-config.cr +++ b/src/gen-config.cr @@ -81,8 +81,10 @@ class GenConfig::Context options["id"] = service.full_id options["name"] = service.name + options["service_root"] = service.root options["environment"] = environment.name + options["environment_root"] = environment.root providers = Hash(String, Variables).new service.providers.each do |token, provider| @@ -105,14 +107,15 @@ class GenConfig::Context `get-port #{service}`.chomp end + # FIXME: Move this to a separate binary? 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 + _service = Service.get_by_id(id).not_nil! # FIXME: hardcoded path - password_file = "/srv/#{env}/#{service}/password_#{password_id}" + password_file = "#{_service.root}/password_#{password_id}" if File.exists? password_file File.read password_file diff --git a/src/service/environment.cr b/src/service/environment.cr index 2c22403..e58c282 100644 --- a/src/service/environment.cr +++ b/src/service/environment.cr @@ -11,6 +11,9 @@ class Environment getter domain_name : String? getter checks = Array(ServiceDefinition::Checks).new + # The place we’ll put services’ data and configuration. + @root : String? + def initialize() initialize "root" end @@ -38,6 +41,10 @@ class Environment end end + def root + @root || "#{SERVED_DATA_DIRECTORY}/#{@name}" + end + class_getter root = Environment.new class_getter all = [@@root] of Environment diff --git a/src/service/service.cr b/src/service/service.cr index 4087afc..5178b28 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -19,6 +19,11 @@ class Service getter environment : Environment getter providers = ProvidersList.new + getter domain : String? + + # The place we’ll store configuration and data. + @root : String? + class Exception < ::Exception end @@ -56,6 +61,8 @@ class Service Environment.get env end + @root = assignments["root"]?.try &.as_s + specs.sections.select(&.name.==("consumes")).each do |section| env, provider = Service.parse_id section.content["from"].as_s @providers[section.options[0]] = "#{env}/#{provider}" @@ -66,9 +73,12 @@ class Service file = [ "name: #{@reference.name}", "environment: #{@environment.name}" - # FIXME: consumed tokens are missing. ] + if @root + file << "root: #{@root}" + end + @providers.each do |token, provider| file << "%consumes #{token}" file << " from: #{provider}" @@ -109,6 +119,10 @@ class Service @reference.consumes end + def root + @root || "#{@environment.root}/#{name}" + end + def provides?(token) provides.any? do |provider| provider.token == token @@ -118,13 +132,20 @@ class Service private def build_environment env = {} of String => String + env["SERVICE_ROOT"] = root env["SERVICE_ID"] = full_id env["ENVIRONMENT"] = @environment.name env["ENVIRONMENT_TYPE"] = @environment.type.to_s @providers.each do |token, provider| + service_provider = Service.get_by_id provider + + # FIXME: Warning? + next if service_provider.nil? + env["#{token.upcase}_PROVIDER"] = provider - env["#{token.upcase}_ENVIRONMENT"] = provider.gsub /\/.*/, "" + env["#{token.upcase}_ENVIRONMENT"] = service_provider.environment.name + env["#{token.upcase}_ROOT"] = service_provider.root end env["SERVICE_TOKENS"] = @providers.to_a.map(&.[0]).join ':' @@ -145,11 +166,13 @@ class Service # FIXME: Is working on ${} really a good idea? private def evaluate(string) - string.gsub /\${[a-zA-Z]+}/ do |match| + string.gsub /\${[a-zA-Z_]+}/ do |match| match = match[2..match.size-2] if match.downcase == "environment" @environment.name + elsif match.downcase == "service_root" + root else "" end @@ -162,12 +185,12 @@ class Service check.file.try do |file| file = evaluate file - run_check = true if ! File.exists? evaluate file + run_check = true if ! File.exists? file end check.directory.try do |directory| directory = evaluate directory - run_check = true if ! Dir.exists? evaluate directory + run_check = true if ! Dir.exists? directory end unless run_check