From 165415b7250aa2fafdbe72009cf7d9c04e771092 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Mon, 24 Apr 2023 10:47:36 +0200 Subject: [PATCH] Less dangerous pruning script --- manifests/multinode.pp | 44 +++++++++++++++---- .../multinode/get_non_paying_customers.erb.sh | 4 ++ .../multinode/get_paying_customers.erb.sh | 4 ++ templates/multinode/restart_and_prune.erb.sh | 21 +++++++++ 4 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 templates/multinode/get_non_paying_customers.erb.sh create mode 100644 templates/multinode/get_paying_customers.erb.sh create mode 100644 templates/multinode/restart_and_prune.erb.sh diff --git a/manifests/multinode.pp b/manifests/multinode.pp index 3a41454..3948e74 100644 --- a/manifests/multinode.pp +++ b/manifests/multinode.pp @@ -53,11 +53,22 @@ class sunetdrive::multinode ( group => 'root', } file { '/usr/local/bin/upgrade23-25.sh': + ensure => absent, + } + file { '/usr/local/bin/get_paying_customers': ensure => present, force => true, owner => 'root', group => 'root', - content => template('sunetdrive/multinode/upgrade23-25.erb.sh'), + content => template('sunetdrive/multinode/get_paying_customers.erb.sh'), + mode => '0744', + } + file { '/usr/local/bin/get_non_paying_customers': + ensure => present, + force => true, + owner => 'root', + group => 'root', + content => template('sunetdrive/multinode/get_non_paying_customers.erb.sh'), mode => '0744', } file { '/usr/local/bin/get_containers': @@ -68,6 +79,14 @@ class sunetdrive::multinode ( content => template('sunetdrive/multinode/get_containers'), mode => '0744', } + file { '/usr/local/bin/restart_and_prune': + ensure => present, + force => true, + owner => 'root', + group => 'root', + content => template('sunetdrive/multinode/restart_and_prune.erb.sh'), + mode => '0744', + } file { '/usr/local/bin/add_admin_user': ensure => present, force => true, @@ -92,14 +111,21 @@ class sunetdrive::multinode ( content => template('sunetdrive/multinode/proxysql.cnf.erb'), mode => '0644', } - if $environment == 'test' { - cron { 'multinode_prune': - command => '/opt/nextcloud/prune.sh', - require => File['/opt/nextcloud/prune.sh'], - user => 'root', - minute => '25', - hour => '4', - } + sunet::scriptherder::cronjob { 'prune_non_paying': + cmd => '/usr/local/bin/restart_and_prune', + day => '1-6' + hour => '2', + minute => '45', + ok_criteria => ['exit_status=0','max_age=3d'], + warn_criteria => ['exit_status=1','max_age=5d'], + } + sunet::scriptherder::cronjob { 'prune_all_paying': + cmd => '/usr/local/bin/restart_and_prune include_paying', + day => '0' + hour => '2', + minute => '45', + ok_criteria => ['exit_status=0','max_age=7d'], + warn_criteria => ['exit_status=1','max_age=9d'], } file { '/opt/nextcloud/apache.php.ini': ensure => file, diff --git a/templates/multinode/get_non_paying_customers.erb.sh b/templates/multinode/get_non_paying_customers.erb.sh new file mode 100644 index 0000000..fe10747 --- /dev/null +++ b/templates/multinode/get_non_paying_customers.erb.sh @@ -0,0 +1,4 @@ +#!/bin/bash +me=$(hostname -s) +cat /etc/hiera/data/common.yaml | yq -r '.multinode_mapping| to_entries |map({name: .key} + .value)| map(select(.server == "'"${me}"'")) |.[] |.name' | \ + grep -Ev "$(cat /etc/hiera/data/common.yaml | yq -r '.singlenodes[]' | sed -e 's/^- //' -e 's/$/|/' | tr -d '\n' | sed 's/|$//')" diff --git a/templates/multinode/get_paying_customers.erb.sh b/templates/multinode/get_paying_customers.erb.sh new file mode 100644 index 0000000..2ec2ace --- /dev/null +++ b/templates/multinode/get_paying_customers.erb.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +cat /etc/hiera/data/common.yaml | yq -r '.multinode_mapping| to_entries |map({name: .key} + .value)| map(select(.server == "'"${me}"'")) |.[] |.name' | + grep -E "$(cat /etc/hiera/data/common.yaml | yq -r '.singlenodes[]' | sed -e 's/^- //' -e 's/$/|/' | tr -d '\n' | sed 's/|$//')" diff --git a/templates/multinode/restart_and_prune.erb.sh b/templates/multinode/restart_and_prune.erb.sh new file mode 100644 index 0000000..36e3739 --- /dev/null +++ b/templates/multinode/restart_and_prune.erb.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +include_paying="${1}" +customers="$(/usr/local/bin/get_non_paying_customers)" +echo "Starting cleanup: $(date)" +if [[ -n ${include_paying} ]]; then + echo "Including paying customers: $(date)" + customers="${customers} + $(/usr/local/bin/get_paying_customers)" +fi +touch /etc/no-automatic-cosmos +for customer in ${customers}; do + echo "Stopping ${customer}: $(date)" + systemctl stop sunet-{redis,nextcloud}-"${customer}" + echo "Pruning docker: $(date)" + docker system prune -af --volumes + echo "Starting ${customer}: $(date)" + systemctl start sunet-{redis,nextcloud}-"${customer}" +done +rm /etc/no-automatic-cosmos +echo "Cleanup done: $(date)"