saltstack-formulas / systemd-formula

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Formula try do start/stop multiinstance service - it won't work

unix196 opened this issue · comments

https://www.stevenrombauts.be/2019/01/run-multiple-instances-of-the-same-systemd-unit/
I needed to start multiple instances of a single unit automatically with systemd, write config like this:

systemctl cat browser.target
[Install]
WantedBy=multi-user.target

[Unit]
After=network.target
Description=Chromium target
Wants=browser-runner@1.service
Wants=browser-runner@2.service
Wants=browser-runner@3.service

systemctl cat browser-runner@.service
[Service]
ExecStart=some_command
Group=chromium
...
Type=simple

[Unit]
After=network.target
Description=Browser runner %i
PartOf=browser-runner.target

Multiinstance unit service didn't support enable/disable or start/stop (it's like a template). Start/stop or enable/disable I can only with target or explicitly given instances:

systemctl  status browser-runner@.service
systemctl  is-enabled browser-runner.target

systemctl  status browser-runner@.service
Failed to get properties: Unit name browser-runner@.service is missing the instance name.

systemctl  status browser-runner@1.service
● browser-runner@1.service - Browser runner 1
   Loaded: loaded (/etc/systemd/system/browser-runner@.service; static; vendor preset: enabled)

In this code state try start start/stop only for service unittypes, but In my example it's need doing and for target unittypes. I think can be done like this:

-      {% if unittype == 'service' %}
-        {% set activation_status = unitconfig.status if unitconfig.status is defined and unitconfig.status == 'start' else 'stop' %}
+      {% if unittype == 'service' or unittype == 'target' %}
+      {% if unitconfig.status is defined %}
        {% set activation_status = unitconfig.status if unitconfig.status == 'start' else 'stop' %}
systemd_systemd_units_activate_or_deactivate_{{ unit }}_{{ unittype }}:
  cmd.wait:
    - name: systemctl {{ activation_status }} {{ unit }}.{{ unittype }}
...
+     {% endif %}

In pillar explicitly define status variable.

Also timer units would be ideal