diff --git a/roles/native-backups-cleanup/README.md b/roles/native-backups-cleanup/README.md index 973d90f3..722bcfc9 100644 --- a/roles/native-backups-cleanup/README.md +++ b/roles/native-backups-cleanup/README.md @@ -9,3 +9,6 @@ It may be neccessary to install gcc seperat to use psutil ```bash sudo pacman -S gcc ``` + +## further information +- https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python \ No newline at end of file diff --git a/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 b/roles/native-backups-cleanup/files/backups-cleanup.py similarity index 50% rename from roles/native-backups-cleanup/templates/backups-cleanup.py.j2 rename to roles/native-backups-cleanup/files/backups-cleanup.py index 7a51c32f..38ea209d 100644 --- a/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 +++ b/roles/native-backups-cleanup/files/backups-cleanup.py @@ -1,15 +1,19 @@ -# @see https://stackoverflow.com/questions/48929553/get-hard-disk-size-in-python import psutil import shutil import os -backup_disk_path = "{{backup_disk_path}}" -backups_folder_path = os.path.join(backup_disk_path, "Backups/") +import argparse + +# Validating arguments +parser.add_argument('--maximum-backup-size-percent', type=int, dest='maximum_backup_size_percent',required=True, choices=range(0,100), help="The directory from which the data should be encrypted.") +parser.add_argument('--backups-folder-path',type=str,dest='backups_folder_path',required=True, help="The folder in which the backups are stored") +args = parser.parse_args() + deleted = True -while psutil.disk_usage(backup_disk_path).percent > int({{size_percent_maximum_backup}}) and deleted: +while psutil.disk_usage(args.backups_folder_path).percent > args.maximum_backup_size_percent and deleted: deleted = False - print("%d %% of disk %s are used. Freeing space..." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path)) - for primary_directory in os.listdir(backups_folder_path): - primary_directory = os.path.join(backups_folder_path, primary_directory) + print("%d %% of disk %s are used. Freeing space..." % (psutil.disk_usage(args.backups_folder_path).percent,args.backups_folder_path)) + for primary_directory in os.listdir(args.backups_folder_path): + primary_directory = os.path.join(args.backups_folder_path, primary_directory) for application_directory in os.listdir(primary_directory): application_directory = os.path.join(primary_directory, application_directory) versions_directory = os.path.join(application_directory, "versions/") @@ -22,4 +26,4 @@ while psutil.disk_usage(backup_disk_path).percent > int({{size_percent_maximum_b deleted = True if not deleted: print("All versions had been deleted!") -print("Cleaning up finished: %d %% of disk %s are used." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path)) +print("Cleaning up finished: %d %% of disk %s are used." % (psutil.disk_usage(args.backups_folder_path).percent,args.backups_folder_path)) diff --git a/roles/native-backups-cleanup/tasks/main.yml b/roles/native-backups-cleanup/tasks/main.yml index 6a3f2f86..cf6a1706 100644 --- a/roles/native-backups-cleanup/tasks/main.yml +++ b/roles/native-backups-cleanup/tasks/main.yml @@ -9,7 +9,9 @@ mode: 0755 - name: create backups-cleanup.py - template: src=backups-cleanup.py.j2 dest={{docker_backups_cleanup}}backups-cleanup.py + copy: + src: backups-cleanup.py + dest: {{docker_backups_cleanup}}backups-cleanup.py - name: create backups-cleanup.service template: src=backups-cleanup.service.j2 dest=/etc/systemd/system/backups-cleanup.service diff --git a/roles/native-backups-cleanup/templates/backups-cleanup.service.j2 b/roles/native-backups-cleanup/templates/backups-cleanup.service.j2 index a1734e0b..d2eab83b 100644 --- a/roles/native-backups-cleanup/templates/backups-cleanup.service.j2 +++ b/roles/native-backups-cleanup/templates/backups-cleanup.service.j2 @@ -4,4 +4,4 @@ OnFailure=systemd-email@%n.service [Service] Type=oneshot -ExecStart=/usr/bin/python {{docker_backups_cleanup}}/backups-cleanup.py +ExecStart=/usr/bin/python {{docker_backups_cleanup}}/backups-cleanup.py --backups-folder-path {{backups_folder_path}} --maximum-backup-size-percent {{size_percent_maximum_backup}} diff --git a/roles/native-disc-space-check/templates/disc-space-check.sh.j2 b/roles/native-disc-space-check/files/disc-space-check.sh similarity index 59% rename from roles/native-disc-space-check/templates/disc-space-check.sh.j2 rename to roles/native-disc-space-check/files/disc-space-check.sh index 2cec0f36..b0f930c3 100644 --- a/roles/native-disc-space-check/templates/disc-space-check.sh.j2 +++ b/roles/native-disc-space-check/files/disc-space-check.sh @@ -1,12 +1,14 @@ #!/bin/sh +# @param $1 mimimum free disc space errors=0 +minimum_percent_free_disc_space="$1" echo "checking disc space use..." df 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 "{{size_percent_disc_space_warning}}" ]; then - echo "WARNING: $disc_use_percent_number exceeds the limit of {{size_percent_disc_space_warning}}%." + if [ "$disc_use_percent_number" -gt "$minimum_percent_free_disc_space" ]; then + echo "WARNING: $disc_use_percent_number exceeds the limit of $minimum_percent_free_disc_space%." errors+=1; fi done diff --git a/roles/native-disc-space-check/tasks/main.yml b/roles/native-disc-space-check/tasks/main.yml index cfb37098..c05cc7ee 100644 --- a/roles/native-disc-space-check/tasks/main.yml +++ b/roles/native-disc-space-check/tasks/main.yml @@ -4,9 +4,9 @@ state: directory mode: 0755 -- name: create disc-space-check.sh +- copy: create disc-space-check.sh template: - src: disc-space-check.sh.j2 + src: disc-space-check.sh dest: "{{disc_space_check_folder}}disc-space-check.sh" - name: create disc-space-check.service diff --git a/roles/native-disc-space-check/templates/disc-space-check.service.j2 b/roles/native-disc-space-check/templates/disc-space-check.service.j2 index ec7d8a5a..d243c9d3 100644 --- a/roles/native-disc-space-check/templates/disc-space-check.service.j2 +++ b/roles/native-disc-space-check/templates/disc-space-check.service.j2 @@ -4,4 +4,4 @@ OnFailure=systemd-email@%n.service [Service] Type=oneshot -ExecStart=/bin/bash {{disc_space_check_folder}}disc-space-check.sh +ExecStart=/bin/bash {{disc_space_check_folder}}disc-space-check.sh {{size_percent_disc_space_warning}}