From 3398184d236dab3eb9ee1f6a49728334d8d0a972 Mon Sep 17 00:00:00 2001 From: Samuel Date: Wed, 6 Jul 2022 19:41:21 +0200 Subject: [PATCH] Added cleanupDockerImages script. --- ct/cleanupDockerImages.sh | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 ct/cleanupDockerImages.sh diff --git a/ct/cleanupDockerImages.sh b/ct/cleanupDockerImages.sh new file mode 100644 index 00000000..00dd1024 --- /dev/null +++ b/ct/cleanupDockerImages.sh @@ -0,0 +1,92 @@ +#!/bin/bash +set -e +BL=$(echo "\033[36m") +GN=$(echo "\033[1;92m") +CL=$(echo "\033[m") +while true; do + read -p "This Will Update All LXC Containers. Proceed(y/n)?" yn + case $yn in + [Yy]* ) break;; + [Nn]* ) exit;; + * ) echo "Please answer yes or no.";; + esac +done +clear +function header_info { +echo -e "${BL} + + ______ ______ _ _ _ _ _____ _____ ____ _____ _ ________ _____ _____ __ __ _____ ______ _____ + / ____| | | ____| /\ | \ | | | | | __ \ | __ \ / __ \ / ____| |/ / ____| __ \ |_ _| \/ | /\ / ____| ____|/ ____| + | | | | | |__ / \ | \| | | | | |__) | | | | | | | | | | /| |__ | |__) | | | | \ / | / \ | | __| |__ | (___ + | | | | | __| / /\ \ | | | | | ___/ | | | | | | | | | < | __| | _ / | | | |\/| | / /\ \| | |_ | __| \___ \ + | |____| |____| |____ / ____ \| |\ | |__| | | | |__| | |__| | |____| \| |____| | \ \ _| |_| | | |/ ____ \ |__| | |____ ____) | + \_____|______|______/_/ \_\_| \_|\____/|_| |_____/ \____/ \_____|_|\_\______|_| \_\ |_____|_| |_/_/ \_\_____|______|_____/ + + +${CL}" +} +header_info + +containers=$(pct list | tail -n +2 | cut -f1 -d' ') + +function cleanup_container() { + container=$1 + clear + header_info + echo -e "${BL}[Info]${GN} Cleaning up ${BL} $container ${CL} \n" + pct config "$container" > temp + os=$(awk '/^ostype/' temp | cut -d' ' -f2) + echo "Checking for docker" + if [ "$os" == "alpine" ] + then + + if test "$(pct exec "$container" -- ash -c "which docker 2>/dev/null")" + then + pct exec "$container" -- ash -c "docker image prune -af" + else + echo "Docker not installed. Skipping..." + fi + else + if test "$(pct exec "$container" -- bash -c "which docker 2>/dev/null")" + then + pct exec "$container" -- bash -c "docker image prune -af" + else + echo "Docker not installed. Skipping..." + fi + fi +} + +read -p "Skip stopped containers? " -n 1 -r +echo +if [[ ! $REPLY =~ ^[Yy]$ ]] +then + skip=no +else + skip=yes +fi + +for container in $containers +do + status=$(pct status "$container") + if [ "$skip" == "no" ]; then + if [ "$status" == "status: stopped" ]; then + echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n" + pct start "$container" + echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n" + sleep 5 + cleanup_container "$container" + echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n" + pct shutdown "$container" & + elif [ "$status" == "status: running" ]; then + cleanup_container "$container" + fi + fi + if [ "$skip" == "yes" ]; then + if [ "$status" == "status: running" ]; then + cleanup_container "$container" + fi + fi +done; wait + +rm temp +echo -e "${GN} Finished, Cleaned up all containers. ${CL} \n" \ No newline at end of file