From 134c6f7f245373a15a78c80abec964cf20be665d Mon Sep 17 00:00:00 2001 From: Logan V Date: Tue, 20 Mar 2018 23:35:34 -0500 Subject: [PATCH] Simplify unit file Combine the flow for simple and oneshot services into one set of execstart/execreload/execstop loops. Change-Id: I2c250f7d0d14747b50fd77f54d3777c28f11e957 --- defaults/main.yml | 4 +++ templates/systemd-service.j2 | 47 ++++++++++++++++++------------------ vars/main.yml | 21 ++++++++++++++++ 3 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 vars/main.yml diff --git a/defaults/main.yml b/defaults/main.yml index 87e782c..fd093cf 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -55,6 +55,10 @@ systemd_service_enabled: yes # Set global service overrides used within the service unit file. systemd_service_config_overrides: {} +# Systemd service type. Options include simple, forking, oneshot, etc. +# https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type= +systemd_default_service_type: simple + # The systemd services dictionary is a set of services that will be created. The dictionary # can contain the following options: # `service_name` -- (required) used to define the name of the service. This is typically the name of the executable. diff --git a/templates/systemd-service.j2 b/templates/systemd-service.j2 index b957f1c..3f538b6 100644 --- a/templates/systemd-service.j2 +++ b/templates/systemd-service.j2 @@ -7,35 +7,34 @@ After={{ item }} {% endfor %} [Service] -{% set service_type = item.service_type | default('simple') %} +{% set service_type = item.service_type | default(systemd_default_service_type) %} Type={{ service_type }} User={{ systemd_user_name }} Group={{ systemd_group_name }} -{% if service_type == 'oneshot' and item.program_execstarts is defined %} -{% for execstart in item.program_execstarts %} -ExecStart={{ execstart }} -{% endfor %} -{% if item.program_execstops is defined %} -{% for execstop in item.program_execstops %} -ExecStop={{ execstop }} -{% endfor %} -{% endif %} -{% else %} -{% if item.program_override is defined %} -ExecStart={{ item.program_override }} {{ item.program_config_options | default('') }} -{% else %} -ExecStart={{ systemd_bin_path }}/{{ item.service_name }} {{ item.program_config_options | default('') }} -{% endif %} -{% if item.program_reload is defined and service_type != 'oneshot' %} -ExecReload={{ item.program_reload }} -{% else %} -ExecReload=/bin/kill -HUP $MAINPID -{% endif %} -{% if item.program_stop is defined %} -ExecStop={{ item.program_stop }} -{% endif %} +{% set _execstarts = item.execstarts | default(systemd_default_execstarts) %} +{% if _execstarts is string %} +{% set _execstarts = [_execstarts] %} {% endif %} +{% for execstart in _execstarts %} +ExecStart={{ execstart }} +{% endfor %} + +{% set _execreloads = item.execreloads | default(systemd_default_execreloads) %} +{% if _execreloads is string %} +{% set _execreloads = [_execreloads] %} +{% endif %} +{% for execreload in _execreloads %} +ExecReload={{ execreload }} +{% endfor %} + +{% set _execstops = item.execstops | default(systemd_default_execstops) %} +{% if _execstops is string %} +{% set _execstops = [_execstops] %} +{% endif %} +{% for execstop in _execstops %} +ExecStop={{ execstop }} +{% endfor %} # Give a reasonable amount of time for the server to start up/shut down TimeoutSec={{ systemd_TimeoutSec }} diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..a956d1c --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2018, Logan Vig +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +systemd_default_execstarts: + - "{{ item.program_override | default(systemd_bin_path ~ '/' ~ item.service_name) }} {{ item.program_config_options | default('') }}" +systemd_default_execreloads: + - '/bin/kill -HUP $MAINPID' +systemd_default_execstops: "{{ item.program_stop | default([]) }}" +