server-scripts/docker-updater.sh
Jose Henrique 40eaf4702e
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 3s
fix
2024-12-28 21:13:51 -03:00

84 lines
1.9 KiB
Bash

#!/bin/bash
### AUTO-UPDATER ###
# Variables
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 "Error: $SERVER_FILE not found." >&2
SERVER_OK=0
fi
if [ $SERVER_OK -eq 1 ]; then
echo "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 "Updating $FILE_NAME..."
curl -s -o $FILE_NAME $SERVER_FILE
echo "$FILE_NAME updated."
chmod +x $FILE_NAME
echo "Permissions set up."
echo "Running updated $FILE_NAME..."
./$FILE_NAME
exit 0
else
echo "$FILE_NAME is up to date.."
fi
fi
####################
# Navigate to docker folder
DOCKER_FOLDER=/root/docker
if [ -d "$DOCKER_FOLDER" ]; then
cd $DOCKER_FOLDER
else
echo "Error: $DOCKER_FOLDER not found." >&2
exit 1
fi
# Navigate on each folder and run docker-compose stop, pull and up -d
for folder in */; do
cd $folder
DOCKER_RUNNING=$(docker-compose ps -q)
if [ -n "$DOCKER_RUNNING" ]; then
echo "[$folder] Stopping Docker containers"
docker-compose down
else
echo "[$folder] No Docker containers running"
fi
echo "[$folder] Updating images"
docker-compose pull
if [ -n "$DOCKER_RUNNING" ]; then
echo "[$folder] Starting Docker containers"
docker-compose up -d
fi
echo "[$folder] Updated!"
cd ..
done
# Run Docker image prune
echo "Running Docker image prune..."
docker image prune -af
echo "Done!"