mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Shorted cleanup- to cln-
This commit is contained in:
24
roles/cln-disc-space/README.md
Normal file
24
roles/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/cln-disc-space/handlers/main.yml
Normal file
5
roles/cln-disc-space/handlers/main.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
- name: "reload cln-disc-space.cymais.service"
|
||||
systemd:
|
||||
name: cln-disc-space.cymais.service
|
||||
enabled: yes
|
||||
daemon_reload: yes
|
26
roles/cln-disc-space/meta/main.yml
Normal file
26
roles/cln-disc-space/meta/main.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
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: "CyMaIS NonCommercial License (CNCL)"
|
||||
license_url: "https://s.veen.world/cncl"
|
||||
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.veen.world/cymais"
|
||||
issue_tracker_url: "https://s.veen.world/cymaisissues"
|
||||
documentation: "https://s.veen.world/cymais"
|
||||
dependencies:
|
||||
- alert-compose
|
||||
- maint-lock
|
26
roles/cln-disc-space/tasks/main.yml
Normal file
26
roles/cln-disc-space/tasks/main.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
- name: "create {{cleanup_disc_space_folder}}"
|
||||
file:
|
||||
path: "{{cleanup_disc_space_folder}}"
|
||||
state: directory
|
||||
mode: 0755
|
||||
|
||||
- name: create cln-disc-space.sh
|
||||
template:
|
||||
src: cln-disc-space.sh.j2
|
||||
dest: "{{cleanup_disc_space_folder}}cln-disc-space.sh"
|
||||
|
||||
- name: create cln-disc-space.cymais.service
|
||||
template:
|
||||
src: cln-disc-space.service.j2
|
||||
dest: /etc/systemd/system/cln-disc-space.cymais.service
|
||||
notify: reload cln-disc-space.cymais.service
|
||||
|
||||
- name: set service_name to the name of the current role
|
||||
set_fact:
|
||||
service_name: "{{ role_name }}"
|
||||
|
||||
- name: "include role for generic-timer for {{service_name}}"
|
||||
include_role:
|
||||
name: generic-timer
|
||||
vars:
|
||||
on_calendar: "{{on_calendar_cleanup_disc_space}}"
|
8
roles/cln-disc-space/templates/cln-disc-space.service.j2
Normal file
8
roles/cln-disc-space/templates/cln-disc-space.service.j2
Normal file
@@ -0,0 +1,8 @@
|
||||
[Unit]
|
||||
Description=freeing disc space
|
||||
OnFailure=alert-compose.cymais@%n.service
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStartPre=/bin/sh -c '/usr/bin/python {{ path_system_lock_script }} {{ system_maintenance_services | join(' ') }} --ignore {{system_maintenance_cleanup_services| join(' ') }} --timeout "{{system_maintenance_lock_timeout_backup_services}}"'
|
||||
ExecStart=/bin/sh -c '/bin/bash {{cleanup_disc_space_folder}}cln-disc-space.sh {{size_percent_cleanup_disc_space}}'
|
52
roles/cln-disc-space/templates/cln-disc-space.sh.j2
Normal file
52
roles/cln-disc-space/templates/cln-disc-space.sh.j2
Normal file
@@ -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}}cln-backups/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="nextcloud-application"
|
||||
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/cln-disc-space/vars/main.yml
Normal file
1
roles/cln-disc-space/vars/main.yml
Normal file
@@ -0,0 +1 @@
|
||||
cleanup_disc_space_folder: "{{path_administrator_scripts}}cln-disc-space/"
|
Reference in New Issue
Block a user