From 39da308ff74c49b636c522cfd729a0b64a0b0301 Mon Sep 17 00:00:00 2001 From: Raphael Dannecker Date: Wed, 22 Jan 2025 09:40:27 +0100 Subject: [PATCH] Rename instead of delete outdated exam-user directories on localhome-clients Exam mode don't collect home-directories on localhome clients. Deleting home of exam-users will result in potential data loss. But keeping the home under the same name will prevent new exam at the next day. Solution: Rename home (and /lmn/media/) of user after 12h and delete after 10d. --- roles/lmn_localhome/files/rmexam | 16 ++++++++++++++++ roles/lmn_localhome/files/rmexam.service | 4 ++-- roles/lmn_localhome/tasks/main.yml | 11 +++++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100755 roles/lmn_localhome/files/rmexam diff --git a/roles/lmn_localhome/files/rmexam b/roles/lmn_localhome/files/rmexam new file mode 100755 index 0000000..ad71551 --- /dev/null +++ b/roles/lmn_localhome/files/rmexam @@ -0,0 +1,16 @@ +#!/usr/bin/bash +# +# rename -exam directories in /home and /lmn/media older than 12h +# remove -exam.* directories in /home and /lmn/media older than 10d +# + +set -eu + +for dir in /home/ /lmn/media ; do + if [[ -d "${dir}" ]]; then + find "${dir}" -maxdepth 1 -mindepth 1 -name '*-exam' -type d -cmin +720 \ + -exec bash -c 'mv "$0" "$0".$( date +%Y%m%d-%H%M --reference="$0" )' {} \; + find "${dir}" -maxdepth 1 -mindepth 1 -name '*-exam.*' -type d -cmin +14400 \ + -exec rm -rf {} \; + fi +done diff --git a/roles/lmn_localhome/files/rmexam.service b/roles/lmn_localhome/files/rmexam.service index 81509a6..f442780 100644 --- a/roles/lmn_localhome/files/rmexam.service +++ b/roles/lmn_localhome/files/rmexam.service @@ -1,6 +1,6 @@ [Unit] -Description=Remove -exam homes older than 12h +Description=Rename/Remove -exam directories older than 12h/10d [Service] Type=simple -ExecStart=find /home/ -maxdepth 1 -mindepth 1 -name '*-exam' -type d -cmin +720 -exec rm -rf {} \; +ExecStart=/usr/local/sbin/rmexam diff --git a/roles/lmn_localhome/tasks/main.yml b/roles/lmn_localhome/tasks/main.yml index e6407a6..30da94d 100644 --- a/roles/lmn_localhome/tasks/main.yml +++ b/roles/lmn_localhome/tasks/main.yml @@ -34,8 +34,15 @@ content: | [[ "${UID}" -gt 10000 ]] && ! findmnt "/lmn/media/${USER}/home" > /dev/null && exit 0 +- name: Provide rmexam script + ansible.builtin.copy: + src: rmexam + dest: /usr/local/sbin/ + mode: 0755 + when: "'teacherlaptop' not in group_names" + - name: Provide rmexam services and timers for some scripts - copy: + ansible.builtin.copy: src: "{{ item }}" dest: "/etc/systemd/system/{{ item }}" mode: 0644 @@ -45,7 +52,7 @@ when: "'teacherlaptop' not in group_names" - name: Enable rmexam.timer - systemd: + ansible.builtin.systemd: name: rmexam.timer enabled: true when: "'teacherlaptop' not in group_names"