From 3d8abeeda011ffcde92b946e5599ea5201395ead Mon Sep 17 00:00:00 2001 From: Tobias Jungel Date: Thu, 1 Apr 2021 21:53:40 +0200 Subject: [PATCH] feat(unit): add dropin capability for unit files This allows to create drop-ins for unit files as described in https://www.freedesktop.org/software/systemd/man/systemd.unit.html. Either create a full unit or create a dropin for an existing unit to alter some details. Caveat: no top-level drop-ins are supported --- pillar.example | 10 ++++++++++ systemd/units/init.sls | 6 ++++++ systemd/units/unit.jinja | 2 +- test/integration/default/controls/unit_spec.rb | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pillar.example b/pillar.example index 2f3b4ee..c83f311 100644 --- a/pillar.example +++ b/pillar.example @@ -69,6 +69,16 @@ systemd: Install: WantedBy: multi-user.target + systemd-journald: + enabled: true + status: start + dropin: true + Unit: + Description: Fancy new description + Service: + WatchdocSec: 2min + RestartSec: 2 + path: trigger-service-on-changes: # this parameter is passed to systemctl to enable/disable the unit diff --git a/systemd/units/init.sls b/systemd/units/init.sls index 16e628a..8c63a2b 100644 --- a/systemd/units/init.sls +++ b/systemd/units/init.sls @@ -7,10 +7,16 @@ include: {% if unittype in unittypes.get('Valid') %} {% for unit, unitconfig in units.items() %} {% set unit_status = 'disable' if unitconfig.enabled is defined and unitconfig.enabled == false else 'enable' %} + {% set dropin = unitconfig.dropin | default(false) %} systemd_systemd_units_file_{{ unit }}_{{ unittype }}: file.managed: + {%- if dropin %} + - name: /etc/systemd/system/{{ unit }}.{{ unittype }}.d/salt-override.conf + - makedirs: True + {%- else %} - name: /etc/systemd/system/{{ unit }}.{{ unittype }} + {%- endif %} - template: jinja - source: salt://systemd/units/unit.jinja - context: diff --git a/systemd/units/unit.jinja b/systemd/units/unit.jinja index de2e034..8bf2ffa 100644 --- a/systemd/units/unit.jinja +++ b/systemd/units/unit.jinja @@ -2,7 +2,7 @@ # This file managed by Salt, do not edit # {% for section, sectioncontent in config.items() %} - {%- if section not in ['enabled', 'status'] %} + {%- if section not in ['enabled', 'status', 'dropin'] %} [{{ section }}] {%- for key, value in sectioncontent.items() %} {%- if value is list %} diff --git a/test/integration/default/controls/unit_spec.rb b/test/integration/default/controls/unit_spec.rb index d7ffc5f..413bfe4 100644 --- a/test/integration/default/controls/unit_spec.rb +++ b/test/integration/default/controls/unit_spec.rb @@ -22,6 +22,7 @@ its('group') { should eq 'root' } its('content') { should_not include '[status]' } its('content') { should_not include '[enabled]' } + its('content') { should_not include '[dropin]' } its('content') { should include 'Documentation=man:rsync(1) man:rsyncd.conf(5)' } its('content') { should include 'ConditionPathExists=/etc/rsyncd.conf' } its('content') { should include 'ConditionPathExists=/etc/passwd' } @@ -34,4 +35,20 @@ it { should be_enabled } it { should be_running } end + + describe file('/etc/systemd/system/systemd-journald.service.d/salt-override.conf') do + its('type') { should eq :file } + its('mode') { should cmp '0644' } + its('owner') { should eq 'root' } + its('group') { should eq 'root' } + its('content') { should_not include '[status]' } + its('content') { should_not include '[enabled]' } + its('content') { should_not include '[dropin]' } + its('content') { should include '[Unit]' } + its('content') { should include 'Description=Fancy new description' } + its('content') { should include '[Service]' } + its('content') { should include 'WatchdocSec=2min' } + its('content') { should include 'RestartSec=2' } + its('content') { should_not include '[Install]' } + end end