server-scripts/docker-updater.sh
Jose Henrique 80a1ad59cf
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 3s
fixing docker update issues haha
2024-12-28 22:13:11 -03:00

94 lines
2.4 KiB
Bash

#!/bin/bash
NC='\033[0m'
LIGHT_GREEN='\033[1;32m'
LIGHT_BLUE='\033[1;34m'
LIGHT_GREEN='\033[1;32m'
LIGHT_GREY='\033[0;37m'
### AUTO-UPDATER ###
FILE_NAME="docker-updater.sh"
SERVER_FILE="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main/$FILE_NAME"
SERVER_OK=1
# Check if the server file exists
curl -s --head $SERVER_FILE | head -n 1 | grep -E "HTTP/[12] [23].." > /dev/null
if [ $? -ne 0 ]; then
echo -e "${RED}[x] Error: $SERVER_FILE not found.${NC}" >&2
SERVER_OK=0
fi
if [ $SERVER_OK -eq 1 ]; then
echo -e "${LIGHT_BLUE}[i] Running auto-update"
# Compare the local and server files sha256sum to check if an update is needed
LOCAL_SHA256=$(sha256sum $FILE_NAME | awk '{print $1}')
SERVER_SHA256=$(curl -s $SERVER_FILE | sha256sum | awk '{print $1}')
if [ "$LOCAL_SHA256" != "$SERVER_SHA256" ]; then
echo -e "${LIGHT_GREY}[i] Updating $FILE_NAME${NC}"
curl -s -o $FILE_NAME $SERVER_FILE
chmod +x $FILE_NAME
echo -e "${LIGHT_GREEN}[i] $FILE_NAME updated.${NC}"
echo -e "${LIGHT_BLUE}[i] Running updated $FILE_NAME...${NC}"
./$FILE_NAME
exit 0
else
echo -e "${LIGHT_GREEN}[i] $FILE_NAME is already up to date.${NC}"
fi
fi
####################
# Navigate to docker folder
DOCKER_FOLDER=/root/docker
if [ -d "$DOCKER_FOLDER" ]; then
cd $DOCKER_FOLDER
else
echo -e "${LIGHT_GREY}[i] Docker folder not found.${NC}"
exit 1
fi
# Updating Docker containers
for folder in */; do
cd $folder
# if .ignore file exists, skip the folder
if [ -f ".ignore" ]; then
echo -e "${LIGHT_BLUE}[$folder] Skipping docker container update"
cd ..
continue
fi
DOCKER_RUNNING=$(docker compose ps -q)
if [ -n "$DOCKER_RUNNING" ]; then
echo -e "${LIGHT_BLUE}[$folder] Stopping Docker containers"
docker compose down > /dev/null
else
echo -e "${LIGHT_BLUE}[$folder] No Docker containers running, will skip update"
continue
fi
echo -e "${LIGHT_BLUE}[$folder] Updating images"
docker compose pull -q > /dev/null
echo -e "${LIGHT_BLUE}[$folder] Starting Docker containers"
docker compose up -d > /dev/null
echo -e "${LIGHT_GREEN}[$folder] Updated!"
cd ..
done
# Run Docker image prune
echo -e "${LIGHT_BLUE}Running Docker image prune..."
docker image prune -af
echo -e "${LIGHT_GREEN} All done!"