From b3e5f0be833d93ab4d5ee0126849f89d90751ce9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Sachse?= <joerg.sachse@slub-dresden.de>
Date: Thu, 1 Dec 2022 17:15:04 +0100
Subject: [PATCH] feat: use --workdir on network shares to make sure that
 repair workflow doesn't run out of space

---
 handlers/main.yml                             |  2 +-
 tasks/configure-nfs-mounts.yml                |  9 ++++++++
 tasks/configure-systemd-services.yml          | 22 +++++++++++++++----
 .../system}/repair_daemon_ddz.service.j2      | 12 +++++++++-
 .../system}/repair_daemon_digas.service.j2    | 11 +++++++++-
 .../system}/repair_daemon_fotothek.service.j2 | 12 +++++++++-
 vars/main.yml                                 |  2 +-
 7 files changed, 61 insertions(+), 9 deletions(-)
 rename templates/{etc/systemd/user => usr/local/lib/systemd/system}/repair_daemon_ddz.service.j2 (72%)
 rename templates/{etc/systemd/user => usr/local/lib/systemd/system}/repair_daemon_digas.service.j2 (72%)
 rename templates/{etc/systemd/user => usr/local/lib/systemd/system}/repair_daemon_fotothek.service.j2 (67%)

diff --git a/handlers/main.yml b/handlers/main.yml
index 9c66982..974b01a 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -5,7 +5,7 @@
     daemon-reload: true
 
 - name: enable systemd-units
-  ansible.builtin.command: "systemctl enable /etc/systemd/user/{{ item }}.service"
+  ansible.builtin.command: "systemctl enable /usr/local/lib/systemd/system/{{ item }}.service"
   loop: "{{ vault_service_files.keys() | list }}"
 
 # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html
diff --git a/tasks/configure-nfs-mounts.yml b/tasks/configure-nfs-mounts.yml
index c838b9f..dfb4208 100644
--- a/tasks/configure-nfs-mounts.yml
+++ b/tasks/configure-nfs-mounts.yml
@@ -32,3 +32,12 @@
     opts: "{{ item.opts | default('defaults,nodev,nosuid,rsize=65536,wsize=65536,vers=3') }}"
   loop: "{{ vault_nfs_mounts | flatten(levels=1) }}"
   tags: [molecule-notest]
+
+- name: create subdirectories in NFS mounts
+  ansible.builtin.file:
+    path: "{{ item[0] }}/{{ item[1] }}"
+    state: directory
+    mode: "0777"    # it's a mixed mode share, so actual mode is 0777 anyway
+  loop: "{{ vault_nfs_mounts | map(attribute='name') | product( ['unprocessed', 'processed', 'workdir']) }}"
+  when: '"/var/log/" not in item[0]'
+  tags: [ci]
diff --git a/tasks/configure-systemd-services.yml b/tasks/configure-systemd-services.yml
index ee8cdd4..a1fbb8b 100644
--- a/tasks/configure-systemd-services.yml
+++ b/tasks/configure-systemd-services.yml
@@ -1,14 +1,28 @@
 ---
 - name: Verzeichnis für Unitfiles erstellen
   ansible.builtin.file:
-    path: "/etc/systemd/user/"
+    path: "/usr/local/lib/systemd/system/"
     state: directory
-    mode: "755"
+    mode: "0755"
+
+- name: Systemd-Unitfiles disablen
+  ansible.builtin.systemd:
+    unit: "{{ item.key }}"
+    state: stopped
+    enabled: false
+  with_dict: "{{ vault_service_files }}"
+  notify: systemctl daemon-reload
+
+- name: Systemd-Unitfiles entfernen
+  ansible.builtin.file:
+    path: "/etc/systemd/user/{{ item.key }}.service"
+    state: absent
+  with_dict: "{{ vault_service_files }}"
 
 - name: Systemd-Unitfiles installieren
   ansible.builtin.template:
-    src: "etc/systemd/user/{{ item.key }}.service.j2"
-    dest: "/etc/systemd/user/{{ item.key }}.service"
+    src: "usr/local/lib/systemd/system/{{ item.key }}.service.j2"
+    dest: "/usr/local/lib/systemd/system/{{ item.key }}.service"
     owner: "root"
     group: "root"
     mode: "0400"
diff --git a/templates/etc/systemd/user/repair_daemon_ddz.service.j2 b/templates/usr/local/lib/systemd/system/repair_daemon_ddz.service.j2
similarity index 72%
rename from templates/etc/systemd/user/repair_daemon_ddz.service.j2
rename to templates/usr/local/lib/systemd/system/repair_daemon_ddz.service.j2
index 318a661..94eec00 100644
--- a/templates/etc/systemd/user/repair_daemon_ddz.service.j2
+++ b/templates/usr/local/lib/systemd/system/repair_daemon_ddz.service.j2
@@ -3,7 +3,17 @@ Description=Repair Tool Daemon (DDZ)
 After=network.target
 
 [Service]
-ExecStart=/usr/bin/perl -I/usr/lib/perl5 /usr/bin/repair_daemon.pl --sourcedir=/mnt/lza_repair_ddz/unprocessed/ --targetdir=/mnt/lza_repair_ddz/processed/ --logdir=/mnt/lza_repair_ddz/log/ --minwait=65 --minfileage=65 --crashmail="{{ vault_service_files.repair_daemon_ddz.crashmail }}" --daemonname="Repair-Daemon-DDZ" --nomd5file --workers 2
+ExecStart=/usr/bin/perl -I/usr/lib/perl5 /usr/bin/repair_daemon.pl \
+	--sourcedir=/mnt/lza_repair_ddz/unprocessed/ \
+	--targetdir=/mnt/lza_repair_ddz/processed/ \
+	--workdir=/mnt/lza_repair_ddz/workdir/ \
+	--logdir=/mnt/lza_repair_ddz/log/ \
+	--minwait=65 \
+	--minfileage=65 \
+	--crashmail="{{ vault_service_files.repair_daemon_ddz.crashmail }}" \
+	--daemonname="Repair-Daemon-DDZ" \
+	--nomd5file \
+	--workers 2
 ExecReload=/bin/kill -HUP $MAINPID
 KillMode=process
 Restart=on-failure
diff --git a/templates/etc/systemd/user/repair_daemon_digas.service.j2 b/templates/usr/local/lib/systemd/system/repair_daemon_digas.service.j2
similarity index 72%
rename from templates/etc/systemd/user/repair_daemon_digas.service.j2
rename to templates/usr/local/lib/systemd/system/repair_daemon_digas.service.j2
index 63c8e8a..c62a03c 100644
--- a/templates/etc/systemd/user/repair_daemon_digas.service.j2
+++ b/templates/usr/local/lib/systemd/system/repair_daemon_digas.service.j2
@@ -3,7 +3,16 @@ Description=Repair Tool Daemon (DiGAS)
 After=network.target
 
 [Service]
-ExecStart=/usr/bin/perl -I/usr/lib/perl5 /usr/bin/repair_daemon.pl --sourcedir=/mnt/lza_repair_digas/unprocessed/ --targetdir=/mnt/lza_repair_digas/processed/ --logdir=/mnt/lza_repair_digas/log/ --minwait=65 --minfileage=65 --crashmail="{{ vault_service_files.repair_daemon_digas.crashmail }}" --daemonname="Repair-Daemon-DiGAS" --workers 2
+ExecStart=/usr/bin/perl -I/usr/lib/perl5 /usr/bin/repair_daemon.pl \
+	--sourcedir=/mnt/lza_repair_digas/unprocessed/ \
+	--targetdir=/mnt/lza_repair_digas/processed/ \
+	--workdir=/mnt/lza_repair_digas/workdir/ \
+	--logdir=/mnt/lza_repair_digas/log/ \
+	--minwait=65 \
+	--minfileage=65 \
+	--crashmail="{{ vault_service_files.repair_daemon_digas.crashmail }}" \
+	--daemonname="Repair-Daemon-DiGAS" \
+	--workers 2
 ExecReload=/bin/kill -HUP $MAINPID
 KillMode=process
 Restart=on-failure
diff --git a/templates/etc/systemd/user/repair_daemon_fotothek.service.j2 b/templates/usr/local/lib/systemd/system/repair_daemon_fotothek.service.j2
similarity index 67%
rename from templates/etc/systemd/user/repair_daemon_fotothek.service.j2
rename to templates/usr/local/lib/systemd/system/repair_daemon_fotothek.service.j2
index d1f4e7b..e4b435f 100644
--- a/templates/etc/systemd/user/repair_daemon_fotothek.service.j2
+++ b/templates/usr/local/lib/systemd/system/repair_daemon_fotothek.service.j2
@@ -3,7 +3,17 @@ Description=Repair Tool Daemon (Fotothek)
 After=network.target
 
 [Service]
-ExecStart=/usr/bin/perl -I/usr/lib/perl5 /usr/bin/repair_daemon.pl --sourcedir=/mnt/lza_repair_fotothek/unprocessed/ --targetdir=/mnt/lza_repair_fotothek/processed/ --logdir=/mnt/lza_repair_fotothek/log/ --minwait=65 --minfileage=65 --crashmail="{{ vault_service_files.repair_daemon_fotothek.crashmail }}" --daemonname="Repair-Daemon-Fotothek" --workers 2 --citcfg="{{ vault_service_files.repair_daemon_fotothek.citcfg }}"
+ExecStart=/usr/bin/perl -I/usr/lib/perl5 /usr/bin/repair_daemon.pl \
+	--sourcedir=/mnt/lza_repair_fotothek/unprocessed/ \
+	--targetdir=/mnt/lza_repair_fotothek/processed/ \
+	--workdir=/mnt/lza_repair_fotothek/workdir/ \
+	--logdir=/mnt/lza_repair_fotothek/log/ \
+	--minwait=65 \
+	--minfileage=65 \
+	--crashmail="{{ vault_service_files.repair_daemon_fotothek.crashmail }}" \
+	--daemonname="Repair-Daemon-Fotothek" \
+	--workers 2 \
+	--citcfg="{{ vault_service_files.repair_daemon_fotothek.citcfg }}"
 ExecReload=/bin/kill -HUP $MAINPID
 KillMode=process
 Restart=on-failure
diff --git a/vars/main.yml b/vars/main.yml
index f74f633..146cae3 100644
--- a/vars/main.yml
+++ b/vars/main.yml
@@ -9,4 +9,4 @@ tool_versions_public:
   - package_name: "libtiff-tools"
     version: "4.2.0-1+deb11u1"
   - package_name: "libslub-lza-repair-perl"
-    version: "0.9.25-1"
+    version: "0.9.26-1"
-- 
GitLab