Files
OpenFrontIO/update.sh
T
2025-03-11 15:01:38 -07:00

125 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
# update.sh - Script to update Docker container on Hetzner server
# Called by deploy.sh after uploading Docker image to Docker Hub
# Check if environment parameter is provided
if [ $# -lt 3 ]; then
echo "Error: Required parameters missing"
echo "Usage: $0 <environment> <docker_username> <docker_repo>"
exit 1
fi
# Set parameters
ENV=$1
DOCKER_USERNAME=$2
DOCKER_REPO=$3
# Container and image configuration
CONTAINER_NAME="openfront-${ENV}"
IMAGE_NAME="${DOCKER_USERNAME}/${DOCKER_REPO}"
FULL_IMAGE_NAME="${IMAGE_NAME}:latest"
echo "======================================================"
echo "🔄 UPDATING SERVER: ${ENV} ENVIRONMENT"
echo "======================================================"
echo "Container name: ${CONTAINER_NAME}"
echo "Docker image: ${FULL_IMAGE_NAME}"
# Load environment variables if .env exists
if [ -f /root/.env ]; then
echo "Loading environment variables from .env file..."
export $(grep -v '^#' /root/.env | xargs)
fi
# Set the Loki URL
LOKI_URL=${LOKI_URL:-"http://localhost:3100/loki/api/v1/push"}
echo "Using Loki URL: ${LOKI_URL}"
echo "Pulling latest image from Docker Hub..."
docker pull $FULL_IMAGE_NAME
echo "Checking for existing container..."
# Check for running container
RUNNING_CONTAINER=$(docker ps | grep ${CONTAINER_NAME} | awk '{print $1}')
if [ -n "$RUNNING_CONTAINER" ]; then
echo "Stopping running container $RUNNING_CONTAINER..."
docker stop $RUNNING_CONTAINER
echo "Waiting for container to fully stop and release resources..."
sleep 5 # Add a 5-second delay
docker rm $RUNNING_CONTAINER
echo "Container $RUNNING_CONTAINER stopped and removed."
fi
# Also check for stopped containers with the same name
STOPPED_CONTAINER=$(docker ps -a | grep ${CONTAINER_NAME} | awk '{print $1}')
if [ -n "$STOPPED_CONTAINER" ]; then
echo "Removing stopped container $STOPPED_CONTAINER..."
docker rm $STOPPED_CONTAINER
echo "Container $STOPPED_CONTAINER removed."
fi
# Check if port 80 is still in use
echo "Checking if port 80 is still in use..."
if command -v lsof >/dev/null 2>&1; then
PORT_CHECK=$(lsof -i :80 | grep LISTEN)
elif command -v netstat >/dev/null 2>&1; then
PORT_CHECK=$(netstat -tuln | grep ":80 ")
else
PORT_CHECK=""
echo "Warning: Cannot check if port is in use (neither lsof nor netstat found)"
fi
if [ -n "$PORT_CHECK" ]; then
echo "Warning: Port 80 is still in use by another process:"
echo "$PORT_CHECK"
echo "Attempting to proceed anyway..."
fi
# Check if the monitoring network exists and connect to it
MONITORING_NETWORK=$(docker network ls | grep monitoring | wc -l)
NETWORK_FLAGS=""
if [ "$MONITORING_NETWORK" -gt 0 ]; then
echo "Connecting to monitoring network for metrics collection..."
NETWORK_FLAGS="--network monitoring"
else
echo "Warning: Monitoring network not found. Node Exporter metrics may not be accessible."
fi
echo "Starting new container for ${ENV} environment with Loki logging..."
docker run -d -p 80:80 \
--restart=always \
$VOLUME_MOUNTS \
$NETWORK_FLAGS \
--env APP_ENV=${ENV} \
--env-file /root/.env \
--name ${CONTAINER_NAME} \
--log-driver=loki \
--log-opt loki-url="${LOKI_URL}" \
--log-opt loki-batch-size="400" \
--log-opt loki-min-backoff="100ms" \
--log-opt loki-max-backoff="10s" \
--log-opt loki-retries="5" \
--log-opt loki-timeout="10s" \
--log-opt loki-external-labels="job=openfront,env=${ENV},container=${CONTAINER_NAME}" \
$FULL_IMAGE_NAME
if [ $? -eq 0 ]; then
echo "Update complete! New ${ENV} container is running."
# Final cleanup after successful deployment
echo "Performing final cleanup of unused Docker resources..."
echo "Removing unused images (not tagged and not referenced)..."
docker image prune -f
docker container prune -f
echo "Cleanup complete."
else
echo "Failed to start container"
exit 1
fi
echo "======================================================"
echo "✅ SERVER UPDATE COMPLETED SUCCESSFULLY"
echo "Container name: ${CONTAINER_NAME}"
echo "Image: ${FULL_IMAGE_NAME}"
echo "Logs: Streaming to Loki at ${LOKI_URL}"
echo "======================================================"