Files
bash_mng_scripts/update-lxc-single-container.sh

49 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
Color_Off='\033[0m' # Text Reset
function stop_container() {
sleep 120
lxc stop ${1}
echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}"
}
if [[ $1 == '' ]]; then
echo "use $0 container-name"
exit
fi
echo $(date) "- Start updating $1 LXC container..."
status=$(lxc info $1 | 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"