mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Refactor systemctl services and categories due to alarm bugs
This commit restructures systemctl service definitions and category mappings. Motivation: Alarm-related bugs revealed inconsistencies in service and role handling. Preparation step: lays the groundwork for fixing the alarm issues by aligning categories, roles, and service templates.
This commit is contained in:
24
roles/sys-ctl-cln-disc-space/README.md
Normal file
24
roles/sys-ctl-cln-disc-space/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Cleanup Disc Space
|
||||
|
||||
## Description
|
||||
|
||||
This role frees disk space by executing a script that cleans up temporary files, clears package caches, and optionally cleans up backup directories and Docker resources when disk usage exceeds a specified threshold.
|
||||
|
||||
## Overview
|
||||
|
||||
Optimized for efficient storage management, this role:
|
||||
- Creates a directory for disk cleanup scripts.
|
||||
- Deploys a Bash script that frees disk space by cleaning up /tmp, Docker resources, and pacman cache.
|
||||
- Configures a systemd service to run the disk cleanup script.
|
||||
- Optionally integrates with backup cleanup if backup variables are defined.
|
||||
|
||||
## Purpose
|
||||
|
||||
The primary purpose of this role is to ensure that disk space remains within safe limits by automating cleanup tasks, thereby improving system performance and stability.
|
||||
|
||||
## Features
|
||||
|
||||
- **Automated Cleanup:** Executes a script to remove temporary files and clear caches.
|
||||
- **Threshold-Based Execution:** Triggers cleanup when disk usage exceeds a defined percentage.
|
||||
- **Systemd Integration:** Configures a systemd service to manage the disk cleanup process.
|
||||
- **Docker and Backup Integration:** Optionally cleans Docker resources and backups if configured.
|
5
roles/sys-ctl-cln-disc-space/handlers/main.yml
Normal file
5
roles/sys-ctl-cln-disc-space/handlers/main.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
- name: "reload sys-ctl-cln-disc-space service"
|
||||
systemd:
|
||||
name: sys-ctl-cln-disc-space{{ SYS_SERVICE_SUFFIX }}
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
22
roles/sys-ctl-cln-disc-space/meta/main.yml
Normal file
22
roles/sys-ctl-cln-disc-space/meta/main.yml
Normal file
@@ -0,0 +1,22 @@
|
||||
galaxy_info:
|
||||
author: "Kevin Veen-Birkenbach"
|
||||
description: "Frees disk space on the target system by executing a cleanup script that removes temporary files, clears package caches, and optionally handles Docker and backup cleanup."
|
||||
license: "Infinito.Nexus NonCommercial License"
|
||||
license_url: "https://s.infinito.nexus/license"
|
||||
company: |
|
||||
Kevin Veen-Birkenbach
|
||||
Consulting & Coaching Solutions
|
||||
https://www.veen.world
|
||||
min_ansible_version: "2.9"
|
||||
platforms:
|
||||
- name: Linux
|
||||
versions:
|
||||
- all
|
||||
galaxy_tags:
|
||||
- disk
|
||||
- cleanup
|
||||
- storage
|
||||
- automation
|
||||
repository: "https://s.infinito.nexus/code"
|
||||
issue_tracker_url: "https://s.infinito.nexus/issues"
|
||||
documentation: "https://docs.infinito.nexus"
|
37
roles/sys-ctl-cln-disc-space/tasks/main.yml
Normal file
37
roles/sys-ctl-cln-disc-space/tasks/main.yml
Normal file
@@ -0,0 +1,37 @@
|
||||
- block:
|
||||
- name: Include dependencies
|
||||
include_role:
|
||||
name: '{{ item }}'
|
||||
loop:
|
||||
- sys-ctl-alm-compose
|
||||
- sys-lock
|
||||
- sys-rst-daemon
|
||||
- include_tasks: utils/run_once.yml
|
||||
when: run_once_sys_ctl_cln_disc_space is not defined
|
||||
|
||||
- name: "create {{cleanup_disc_space_folder}}"
|
||||
file:
|
||||
path: "{{cleanup_disc_space_folder}}"
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: create sys-ctl-cln-disc-space.sh
|
||||
template:
|
||||
src: sys-ctl-cln-disc-space.sh.j2
|
||||
dest: "{{cleanup_disc_space_folder}}sys-ctl-cln-disc-space.sh"
|
||||
|
||||
- name: create sys-ctl-cln-disc-space{{ SYS_SERVICE_SUFFIX }}
|
||||
template:
|
||||
src: sys-ctl-cln-disc-space.service.j2
|
||||
dest: /etc/systemd/system/sys-ctl-cln-disc-space{{ SYS_SERVICE_SUFFIX }}
|
||||
notify: reload sys-ctl-cln-disc-space service
|
||||
|
||||
- name: "set 'service_name' to '{{ role_name }}'"
|
||||
set_fact:
|
||||
service_name: "{{ role_name }}"
|
||||
|
||||
- name: "include role for sys-timer for {{ service_name }}"
|
||||
include_role:
|
||||
name: sys-timer
|
||||
vars:
|
||||
on_calendar: "{{SYS_SCHEDULE_CLEANUP_DISC_SPACE}}"
|
@@ -0,0 +1,8 @@
|
||||
[Unit]
|
||||
Description=freeing disc space
|
||||
OnFailure=sys-ctl-alm-compose.{{ SOFTWARE_NAME }}@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStartPre=/bin/sh -c '/usr/bin/python {{ PATH_SYSTEM_LOCK_SCRIPT }} {{ SYS_SERVICE_GROUP_MANIPULATION | join(' ') }} --ignore {{SYS_SERVICE_GROUP_CLEANUP| join(' ') }} --timeout "{{ SYS_TIMEOUT_BACKUP_SERVICES }}"'
|
||||
ExecStart=/bin/sh -c '/bin/bash {{cleanup_disc_space_folder}}sys-ctl-cln-disc-space.sh {{size_percent_cleanup_disc_space}}'
|
@@ -0,0 +1,52 @@
|
||||
#!/bin/sh
|
||||
# @param $1 mimimum free disc space
|
||||
# @param $2 --force to for execution indepentend on how much disc space is free
|
||||
|
||||
minimum_percent_cleanup_disc_space="$1"
|
||||
force_freeing=false
|
||||
echo "Checking free disc space..."
|
||||
df
|
||||
if [ $# -gt 0 ] && [ "$2" == "--force" ]; then
|
||||
echo "Forcing disc space freeing."
|
||||
force_freeing=true
|
||||
fi
|
||||
for disc_use_percent in $(df --output=pcent | sed 1d)
|
||||
do
|
||||
disc_use_percent_number=$(echo "$disc_use_percent" | sed "s/%//")
|
||||
if [ "$disc_use_percent_number" -gt "$minimum_percent_cleanup_disc_space" ]; then
|
||||
echo "WARNING: $disc_use_percent_number exceeds the limit of {{size_percent_disc_space_warning}}%."
|
||||
force_freeing=true
|
||||
fi
|
||||
done
|
||||
if [ "$force_freeing" = true ]; then
|
||||
echo "cleaning up /tmp" &&
|
||||
find /tmp -type f -atime +10 -delete || exit 1
|
||||
|
||||
{% if backups_folder_path is defined and size_percent_maximum_backup is defined %}
|
||||
echo "cleaning up backups" &&
|
||||
python {{ PATH_ADMINISTRATOR_SCRIPTS }}sys-ctl-cln-backups/sys-ctl-cln-backups.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}} || exit 2
|
||||
{% endif %}
|
||||
|
||||
if pacman -Qs $package > /dev/null ; then
|
||||
echo "cleaning up docker" &&
|
||||
docker system prune -f || exit 3
|
||||
|
||||
nextcloud_application_container="{{ applications | get_app_conf('web-app-nextcloud', 'docker.services.nextcloud.name', True) }}"
|
||||
if [ "$(docker ps -a -q -f name=$nextcloud_application_container)" ] ; then
|
||||
echo "cleaning up docker nextcloud" &&
|
||||
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ files:cleanup || exit 4
|
||||
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ trashbin:cleanup --all-users || exit 5
|
||||
docker exec -it -u www-data $nextcloud_application_container /var/www/html/occ versions:cleanup || exit 6
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
echo "cleaning pacman cache" &&
|
||||
yes | pacman -Sc || exit 7
|
||||
|
||||
echo "cleanup finished."
|
||||
else
|
||||
echo "Sufficiend disc space available."
|
||||
echo "To force the freeing of disc space pass the parameter --force."
|
||||
fi
|
||||
exit 0
|
1
roles/sys-ctl-cln-disc-space/vars/main.yml
Normal file
1
roles/sys-ctl-cln-disc-space/vars/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
cleanup_disc_space_folder: '{{ PATH_ADMINISTRATOR_SCRIPTS }}sys-ctl-cln-disc-space/'
|
Reference in New Issue
Block a user