Files
bash_mng_scripts/update-incus-container.sh

49 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
declare -A pids
BRed=$'\033[1;31m' # Red
BGreen=$'\033[1;32m' # Green
Color_Off=$'\033[0m' # Text Reset
TMSTMP=$(date +'%Y%m%d%H%M%S')
function stop_container() {
echo $(date) "- ${BGreen}${1} - Stop container.${Color_Off}"
incus stop ${1} | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
wait
echo -e $(date) "- ${BGreen}${1} - Container stopped.${Color_Off}"
}
function update_container() {
incus exec ${1} -- sh -c "apt update 2>&1" | sed '/^$/d' | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
sleep 2
incus exec ${1} -- sh -c "apt upgrade -y 2>&1" | sed '/^$/d' | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
sleep 2
incus exec ${1} -- sh -c "apt autoremove -y 2>&1" | sed '/^$/d' | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
sleep 2
incus exec ${1} -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then echo \"Refresh snap packages\" && snap refresh 2>&1 ; else echo \"snap not installed.\"; fi" | sed "s/^/$(date) - ${BGreen}${1}${Color_Off} /"
if [ "${2}" == "STOPPED" ]; then
stop_container ${1}
fi
}
echo $(date) "- ${BGreen}Start updating incus container...${Color_Off}"
for cont_name in $(incus list type=container -c n --format csv); do
echo $(date) "- ${BGreen}${cont_name} - Make container snapshot...${Color_Off}"
incus snapshot create ${cont_name} | sed "s/^/$(date) - ${BGreen}${cont_name}${Color_Off} /"
echo $(date) "- ${BGreen}${cont_name} - Updating container...${Color_Off}"
Status=$(incus list type=container -c ns --format csv | grep ${cont_name} | cut -d',' -f 2)
if [ "${Status}" == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}${cont_name} - Start container.${Color_Off}"
incus start ${cont_name} | sed 's/^/'${date}' - '${cont_name}' /'
sleep 5
else
echo -e $(date) "- ${BGreen}${cont_name} - Container is already started.${Color_Off}"
fi
update_container ${cont_name} ${Status} &
done
wait
echo $(date) "- ${BGreen}Updating incus container ended.${Color_Off}"