Skip to content

Commit

Permalink
feat(unit): allow to start/stop, enable/disable services
Browse files Browse the repository at this point in the history
* allow to use lists on keys that can be repeated
* add tests to verify unit files contents
  • Loading branch information
javierbertoli committed May 25, 2020
1 parent 246a3c7 commit 5f7d854
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 6 deletions.
4 changes: 4 additions & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ suites:
- systemd
- systemd.networkd
- systemd.networkd.profiles
- rsync_test_package
- systemd.units
pillars:
top.sls:
Expand All @@ -166,6 +167,9 @@ suites:
- systemd
pillars_from_files:
systemd.sls: pillar.example
dependencies:
- name: rsync_test_package
path: test/salt/states
verifier:
inspec_tests:
- path: test/integration/default
30 changes: 30 additions & 0 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,19 @@ systemd:
- 'alt_timesyncd.conf'

## units
# The valid units type are listed in
# https://github.com/saltstack-formulas/systemd-formula/blob/master/systemd/units/unittypes.yaml
service:
syncthing-someuser:
# these two parameters are passed to systemctl to manage the unit status
# (enabled / disabled) and (running / stopped) and are not passed to the
# service file contents.
# Default to
# enabled: true
# status: stop
# to be backward compatible
enabled: true
status: stop
Unit:
Description: Syncthing P2P sync service for someuser
After: network.target
Expand All @@ -44,8 +55,27 @@ systemd:
Install:
WantedBy: multi-user.target

rsync:
status: start
Unit:
Description: fast remote file copy program daemon
Documentation: 'man:rsync(1) man:rsyncd.conf(5)'
ConditionPathExists:
- /etc/rsyncd.conf
- /etc/passwd
After: network.target
Service:
ExecStart: /usr/bin/rsync --daemon --no-detach
Install:
WantedBy: multi-user.target

path:
trigger-service-on-changes:
# this parameter is passed to systemctl to enable/disable the unit
# Defaults to
# enabled: true
# to be backward compatible
# enabled: true
Path:
PathModified: /path/to/watch
Unit: oneshot_service_to_trigger.service
Expand Down
19 changes: 16 additions & 3 deletions systemd/units/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ include:
{% for unittype, units in pillar.get('systemd', {}).items() %}
{% 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' %}

/etc/systemd/system/{{ unit }}.{{ unittype }}:
systemd_systemd_units_file_{{ unit }}_{{ unittype }}:
file.managed:
- name: /etc/systemd/system/{{ unit }}.{{ unittype }}
- template: jinja
- source: salt://systemd/units/unit.jinja
- context:
config: {{ unitconfig|json }}
- watch_in:
- cmd: reload_systemd_configuration

enable_{{ unit }}_{{ unittype }}:
systemd_systemd_units_cmd_enable_or_disable_{{ unit }}_{{ unittype }}:
cmd.wait:
- name: systemctl enable {{ unit }}.{{ unittype }}
- name: systemctl {{ unit_status }} {{ unit }}.{{ unittype }}
- watch:
- cmd: reload_systemd_configuration

{% if unittype == 'service' %}
{% set activation_status = unitconfig.status if unitconfig.status is defined and unitconfig.status == 'start' else 'stop' %}
systemd_systemd_units_activate_or_deactivate_{{ unit }}_{{ unittype }}:
cmd.wait:
- name: systemctl {{ activation_status }} {{ unit }}.{{ unittype }}
- require:
- cmd: systemd_systemd_units_cmd_enable_or_disable_{{ unit }}_{{ unittype }}
- cmd: reload_systemd_configuration
- watch:
- cmd: reload_systemd_configuration
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Expand Down
17 changes: 14 additions & 3 deletions systemd/units/unit.jinja
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{% for section, sectioncontent in config.items() -%}
#
# This file managed by Salt, do not edit
#
{% for section, sectioncontent in config.items() %}
{%- if section not in ['enabled', 'status'] %}
[{{ section }}]
{% for key, value in sectioncontent.items() -%}
{%- for key, value in sectioncontent.items() %}
{%- if value is list %}
{%- for v in value %}
{{ key }}={{ v }}
{%- endfor %}
{%- else %}
{{ key }}={{ value }}
{% endfor %}
{%- endif %}
{%- endfor %}
{%- endif %}
{% endfor -%}
21 changes: 21 additions & 0 deletions test/integration/default/controls/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,26 @@

describe service('syncthing-someuser.service') do
it { should be_enabled }
it { should_not be_running }
end

describe file('/etc/systemd/system/rsync.service') 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 include 'Documentation=man:rsync(1) man:rsyncd.conf(5)' }
its('content') { should include 'ConditionPathExists=/etc/rsyncd.conf' }
its('content') { should include 'ConditionPathExists=/etc/passwd' }
its('content') { should include 'ExecStart=/usr/bin/rsync --daemon --no-detach' }
its('content') { should include '[Install]' }
its('content') { should include 'WantedBy=multi-user.target' }
end

describe service('rsync.service') do
it { should be_enabled }
it { should be_running }
end
end
8 changes: 8 additions & 0 deletions test/salt/states/rsync_test_package/init.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
rsync_test_package:
pkg.installed:
- name: rsync

rsync_test_file:
file.managed:
- name: /etc/rsyncd.conf
- content: Managed by salt

0 comments on commit 5f7d854

Please sign in to comment.