From f53db2c6cc2101d4f857ed60de1e784787112e02 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Sat, 26 Oct 2019 15:24:22 +0200 Subject: [PATCH] Service definitions get some sugar. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - %configuration template target - %directory target - Services are started in their “root directory” if no “directory” option is provided. --- services/gitea.spec | 23 ++++---------------- src/service/service.cr | 2 +- src/service/service_definition.cr | 36 ++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/services/gitea.spec b/services/gitea.spec index 28ae5b5..80e24a2 100644 --- a/services/gitea.spec +++ b/services/gitea.spec @@ -1,25 +1,10 @@ -command: ./gitea -C . -w . -c ./custom/conf/app.ini -# deamon's working directory -directory: ${SERVICE_ROOT} # FIXME: Could this be removed? +command: gitea -C . -w . -c ./custom/conf/app.ini consumes: postgresql -%pre-start - name: working directory - # 'command' is run only if this directory doesn't exist - unless-directory: ${SERVICE_ROOT}/custom/conf/ - command: mkdir -p ${SERVICE_ROOT}/custom/conf/ +%directory ${SERVICE_ROOT}/custom/conf + name: working directory -%pre-start - name: symlink - # 'command' is run only if this directory doesn't exist - unless-file: ${SERVICE_ROOT}/gitea - command: ln -s $(which gitea) ${SERVICE_ROOT}/gitea - -%pre-start - name: configuration file - # 'command' is run only if this file doesn't exist - unless-file: ${SERVICE_ROOT}/custom/conf/app.ini - command: gen-config gitea.cfg ${SERVICE_ROOT}/custom/conf/app.ini postgresql=${POSTGRESQL_PROVIDER} +%configuration gitea.cfg ${SERVICE_ROOT}/custom/conf/app.ini %pre-start name: gitea database creation diff --git a/src/service/service.cr b/src/service/service.cr index 3ac77af..ee605c0 100644 --- a/src/service/service.cr +++ b/src/service/service.cr @@ -236,7 +236,7 @@ class Service end Process.exec command, args, - chdir: @reference.directory.try { |x| evaluate x }, + chdir: (@reference.directory.try { |x| evaluate x } || root), env: build_environment end diff --git a/src/service/service_definition.cr b/src/service/service_definition.cr index 0e3b985..2e53fb8 100644 --- a/src/service/service_definition.cr +++ b/src/service/service_definition.cr @@ -60,7 +60,41 @@ class ServiceDefinition @consumes = specs["consumes"]?.try &.as_a_or_s.map { |x| Consumes.new x } || Array(Consumes).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 + @pre_start_hooks = Array(Hook).new + + sections.each do |section| + case section.name + when "pre-start" + @pre_start_hooks << Hook.new section + when "directory" + directory = section.options[0]? + name = section.content["name"]?.try &.as_s + + if directory.nil? + STDERR.puts "warning: (#{@name}) %directory was not provided a path" + next + end + + pre_start_hooks << Hook.new (name || "directory: #{directory}"), + "mkdir -p \"#{directory}\"", + unless_directory: directory + when "configuration" + options = section.options[0].split /[ \t]/ + + template = options[0]? + target = options[1]? + name = section.content["name"]?.try &.as_s + + if template.nil? || target.nil? + STDERR.puts "warning: (#{@name}) %configuration received less than 2 options" + next + end + + pre_start_hooks << Hook.new (name || "configuration file: #{template}"), + "gen-config \"#{template}\" \"#{target}\"", + unless_file: target + end + end end def self.load(path)