From d3bbc8380d42b21ae67a0d716ee4f7f4a11a5dae Mon Sep 17 00:00:00 2001 From: Alessandro Battilani Date: Mon, 5 Feb 2024 20:50:25 +0100 Subject: [PATCH] add snap app refresh --- .gitignore | 2 +- update-lxc-container.sh | 3 +++ update-lxc-single-container.sh | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100755 update-lxc-single-container.sh diff --git a/.gitignore b/.gitignore index a2bb350..333c1e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -logs/rsync-backup.log +logs/ diff --git a/update-lxc-container.sh b/update-lxc-container.sh index 204929f..fc140d1 100755 --- a/update-lxc-container.sh +++ b/update-lxc-container.sh @@ -28,6 +28,9 @@ for i in $(lxc list type=container -c ns --format csv); do sleep 2 lxc exec ${cont_name} -- sh -c "apt autoremove" sleep 2 + echo -e $(date) "- Refresh snap packages" + lxc exec ${cont_name} -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi" + if [ "${i#*,}" == "STOPPED" ]; then echo $(date) "- Stop container ${cont_name}" stop_container ${cont_name} & diff --git a/update-lxc-single-container.sh b/update-lxc-single-container.sh new file mode 100755 index 0000000..092ffcc --- /dev/null +++ b/update-lxc-single-container.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +BRed='\033[1;31m' # Red +BGreen='\033[1;32m' # Green +Color_Off='\033[0m' # Text Reset + +function stop_container() { + lxc stop ${1} + echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}" +} + +echo $(date) "- Start updating $1 LXC container..." +status=$(lxc info microk8s | grep Status | cut -d' ' -f 2) + +echo $(date) "- Updating $1 container..." +if [ $status == "STOPPED" ]; then + echo -e $(date) "- ${BGreen}Start container $1${Color_Off}" + lxc start $1 + sleep 5 +else + echo -e $(date) "- ${BGreen}Container $1 is already started${Color_Off}" +fi +lxc exec $1 -- sh -c "apt update" +sleep 2 +lxc exec $1 -- sh -c "apt upgrade -y" +sleep 2 +lxc exec $1 -- sh -c "apt autoremove" +sleep 2 +echo -e $(date) "- Refresh snap packages" +lxc exec $1 -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi" + +if [ $status == "STOPPED" ]; then + echo $(date) "- Stop container $1" + stop_container $1 & + pids[$1]=$! +fi +sleep 2 + +for pid in ${pids[@]}; do + wait $pid +done +echo $(date) "- Updating LXC container ended"