mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-25 17:34:36 +00:00
updateto hetzner
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
#!/bin/bash
|
||||
# deploy.sh - Complete deployment script for Hetzner with Docker Hub and R2
|
||||
# This script:
|
||||
# 1. Builds and uploads the Docker image to Docker Hub with appropriate tag
|
||||
# 2. Copies the update script to Hetzner server
|
||||
# 3. Executes the update script on the Hetzner server
|
||||
|
||||
set -e # Exit immediately if a command exits with a non-zero status
|
||||
|
||||
# Function to print section headers
|
||||
print_header() {
|
||||
echo "======================================================"
|
||||
echo "🚀 $1"
|
||||
echo "======================================================"
|
||||
}
|
||||
|
||||
# Load environment variables
|
||||
if [ -f .env ]; then
|
||||
echo "Loading configuration from .env file..."
|
||||
export $(grep -v '^#' .env | xargs)
|
||||
fi
|
||||
|
||||
# Check command line argument
|
||||
if [ $# -ne 1 ] || ([ "$1" != "staging" ] && [ "$1" != "prod" ]); then
|
||||
echo "Error: Please specify environment (staging or prod)"
|
||||
echo "Usage: $0 [staging|prod]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ENV=$1
|
||||
VERSION_TAG="latest"
|
||||
DOCKER_REPO=""
|
||||
|
||||
# Set environment-specific variables
|
||||
if [ "$ENV" == "staging" ]; then
|
||||
print_header "DEPLOYING TO STAGING ENVIRONMENT"
|
||||
SERVER_HOST=$SERVER_HOST_STAGING
|
||||
DOCKER_REPO=$DOCKER_REPO_STAGING
|
||||
else
|
||||
print_header "DEPLOYING TO PRODUCTION ENVIRONMENT"
|
||||
SERVER_HOST=$SERVER_HOST_PROD
|
||||
DOCKER_REPO=$DOCKER_REPO_PROD
|
||||
fi
|
||||
|
||||
# Check required environment variables
|
||||
if [ -z "$SERVER_HOST" ]; then
|
||||
echo "Error: SERVER_HOST_${ENV^^} not defined in .env file or environment"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Configuration
|
||||
SSH_KEY=${SSH_KEY:-"~/.ssh/id_rsa"} # Use default or override from .env
|
||||
DOCKER_USERNAME=${DOCKER_USERNAME} # Docker Hub username
|
||||
UPDATE_SCRIPT="./update.sh" # Path to your update script
|
||||
REMOTE_UPDATE_SCRIPT="/root/update-openfront.sh" # Where to place the script on server
|
||||
|
||||
# Check if update script exists
|
||||
if [ ! -f "$UPDATE_SCRIPT" ]; then
|
||||
echo "Error: Update script $UPDATE_SCRIPT not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Step 1: Build and upload Docker image to Docker Hub
|
||||
print_header "STEP 1: Building and uploading Docker image to Docker Hub"
|
||||
echo "Environment: ${ENV}"
|
||||
echo "Using version tag: $VERSION_TAG"
|
||||
echo "Docker repository: $DOCKER_REPO"
|
||||
|
||||
# Get Git commit for build info
|
||||
GIT_COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "unknown")
|
||||
echo "Git commit: $GIT_COMMIT"
|
||||
|
||||
docker buildx build \
|
||||
--platform linux/amd64 \
|
||||
--build-arg GIT_COMMIT=$GIT_COMMIT \
|
||||
-t $DOCKER_USERNAME/$DOCKER_REPO:$VERSION_TAG \
|
||||
--push \
|
||||
.
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Docker build failed. Stopping deployment."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Failed to push image to Docker Hub. Stopping deployment."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✅ Docker image built and pushed successfully."
|
||||
|
||||
# Step 2: Copy update script to Hetzner server
|
||||
print_header "STEP 2: Copying update script to server"
|
||||
echo "Target: $SERVER_HOST"
|
||||
|
||||
# Make sure the update script is executable
|
||||
chmod +x $UPDATE_SCRIPT
|
||||
|
||||
# Copy the update script to the server
|
||||
scp -i $SSH_KEY $UPDATE_SCRIPT $SERVER_HOST:$REMOTE_UPDATE_SCRIPT
|
||||
|
||||
# Copy environment variables if needed
|
||||
if [ -f .env ]; then
|
||||
scp -i $SSH_KEY .env $SERVER_HOST:/root/.env
|
||||
# Secure the .env file
|
||||
ssh -i $SSH_KEY $SERVER_HOST "chmod 600 /root/.env"
|
||||
fi
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Failed to copy update script to server. Stopping deployment."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "✅ Update script successfully copied to server."
|
||||
|
||||
# Step 3: Execute the update script on the server
|
||||
print_header "STEP 3: Executing update script on server"
|
||||
|
||||
# Make the script executable on the remote server and execute it with the environment parameter
|
||||
ssh -i $SSH_KEY $SERVER_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && $REMOTE_UPDATE_SCRIPT $ENV $DOCKER_USERNAME $DOCKER_REPO"
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Failed to execute update script on server."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_header "DEPLOYMENT COMPLETED SUCCESSFULLY"
|
||||
echo "✅ New version deployed to ${ENV} environment!"
|
||||
echo "🌐 Check your ${ENV} server to verify the deployment."
|
||||
echo "======================================================="
|
||||
@@ -1,31 +0,0 @@
|
||||
#!/bin/bash
|
||||
# Executed on ec2 startup
|
||||
yum update -y
|
||||
amazon-linux-extras install docker -y
|
||||
service docker start
|
||||
systemctl enable docker
|
||||
usermod -a -G docker ec2-user
|
||||
|
||||
# Install AWS CLI v2
|
||||
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
|
||||
unzip awscliv2.zip
|
||||
./aws/install
|
||||
|
||||
# Install CloudWatch agent (simplified)
|
||||
yum install -y amazon-cloudwatch-agent
|
||||
|
||||
# Start CloudWatch agent with default config (collects basic system metrics)
|
||||
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
|
||||
|
||||
# Authenticate to ECR and run container
|
||||
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
|
||||
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com
|
||||
|
||||
# Pull and run container with simple CloudWatch logging
|
||||
docker pull ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest
|
||||
docker run -d -p 80:80 \
|
||||
--log-driver=awslogs \
|
||||
--log-opt awslogs-region=eu-west-1 \
|
||||
--log-opt awslogs-group=/aws/ec2/docker-containers \
|
||||
--log-opt awslogs-create-group=true \
|
||||
${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest
|
||||
@@ -1,59 +1,214 @@
|
||||
#!/bin/bash
|
||||
# Comprehensive setup script for Hetzner server with Docker and Cloudflare R2 configuration
|
||||
|
||||
# Comprehensive idempotent setup script for Hetzner server with Docker, Docker Compose, and Cloudflare R2 configuration
|
||||
# Exit on error
|
||||
set -e
|
||||
|
||||
echo 'export EDITOR=vim' >> ~/.bashrc
|
||||
source ~/.bashrc
|
||||
|
||||
echo "🔄 Updating system..."
|
||||
apt update && apt upgrade -y
|
||||
|
||||
echo "🐳 Installing Docker..."
|
||||
# Install Docker using official script
|
||||
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||
sh get-docker.sh
|
||||
systemctl enable --now docker
|
||||
# Docker installation - check if already installed
|
||||
if command -v docker &> /dev/null; then
|
||||
echo "✅ Docker is already installed"
|
||||
else
|
||||
echo "🐳 Installing Docker..."
|
||||
# Install Docker using official script
|
||||
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||
sh get-docker.sh
|
||||
rm get-docker.sh
|
||||
# Make sure Docker is enabled to start at boot
|
||||
systemctl enable docker
|
||||
echo "✅ Docker installed successfully"
|
||||
fi
|
||||
|
||||
# Set up Docker Hub credentials
|
||||
echo "🔐 Setting up Docker Hub login..."
|
||||
echo "Enter your Docker Hub username:"
|
||||
read DOCKER_USERNAME
|
||||
echo "Enter your Docker Hub password/token:"
|
||||
read -s DOCKER_PASSWORD
|
||||
echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin
|
||||
echo "✅ Docker Hub login configured"
|
||||
# Check if Docker is running
|
||||
if systemctl is-active --quiet docker; then
|
||||
echo "✅ Docker service is already running"
|
||||
else
|
||||
echo "🚀 Starting Docker service..."
|
||||
systemctl start docker
|
||||
echo "✅ Docker service started"
|
||||
fi
|
||||
|
||||
echo "☁️ Installing AWS CLI for Cloudflare R2..."
|
||||
# Install AWS CLI
|
||||
apt install -y unzip curl
|
||||
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
|
||||
unzip awscliv2.zip
|
||||
./aws/install
|
||||
rm -rf aws awscliv2.zip
|
||||
# Docker Compose v1 installation - check if already installed
|
||||
if command -v docker-compose &> /dev/null; then
|
||||
echo "✅ Docker Compose v1 is already installed"
|
||||
else
|
||||
echo "🔧 Installing Docker Compose v1..."
|
||||
# Get latest docker compose version
|
||||
COMPOSE_VERSION=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)
|
||||
# Install Docker Compose v1
|
||||
curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||
chmod +x /usr/local/bin/docker-compose
|
||||
echo "✅ Docker Compose v1 installed successfully"
|
||||
fi
|
||||
|
||||
# Configure AWS CLI for R2
|
||||
echo "🔧 Configuring AWS CLI for Cloudflare R2..."
|
||||
echo "Enter your Cloudflare R2 Access Key ID:"
|
||||
read R2_ACCESS_KEY
|
||||
echo "Enter your Cloudflare R2 Secret Access Key:"
|
||||
read -s R2_SECRET_KEY
|
||||
echo "Enter your Cloudflare Account ID:"
|
||||
read CLOUDFLARE_ACCOUNT_ID
|
||||
# Docker Compose v2 installation - check if already installed
|
||||
if command -v docker compose &> /dev/null; then
|
||||
echo "✅ Docker Compose plugin (v2) is already installed"
|
||||
else
|
||||
echo "🔧 Installing Docker Compose plugin (v2)..."
|
||||
# Install Docker Compose v2
|
||||
apt install -y docker-compose-plugin
|
||||
echo "✅ Docker Compose plugin (v2) installed successfully"
|
||||
fi
|
||||
|
||||
# Create R2 profile configuration
|
||||
mkdir -p ~/.aws
|
||||
cat > ~/.aws/credentials << EOL
|
||||
# Verify Docker Compose installations
|
||||
echo "Verifying Docker Compose installations..."
|
||||
echo "Docker Compose v1:"
|
||||
docker-compose --version
|
||||
echo "Docker Compose v2:"
|
||||
docker compose version
|
||||
|
||||
# Docker Hub login - only prompt if not already logged in
|
||||
if [ ! -f ~/.docker/config.json ] || ! grep -q "auth" ~/.docker/config.json; then
|
||||
echo "🔐 Setting up Docker Hub login..."
|
||||
docker login
|
||||
echo "✅ Docker Hub login configured"
|
||||
else
|
||||
echo "✅ Docker Hub login already configured"
|
||||
fi
|
||||
|
||||
# AWS CLI installation - check if already installed
|
||||
if command -v aws &> /dev/null; then
|
||||
echo "✅ AWS CLI is already installed"
|
||||
else
|
||||
echo "☁️ Installing AWS CLI for Cloudflare R2..."
|
||||
# Install AWS CLI
|
||||
apt install -y unzip curl
|
||||
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
|
||||
unzip awscliv2.zip
|
||||
./aws/install
|
||||
rm -rf aws awscliv2.zip
|
||||
echo "✅ AWS CLI installed successfully"
|
||||
fi
|
||||
|
||||
# R2 configuration - check if already configured
|
||||
if [ -f ~/.aws/credentials ] && grep -q "\[r2\]" ~/.aws/credentials; then
|
||||
echo "✅ R2 configuration already exists"
|
||||
echo "Do you want to update the R2 configuration? (y/n)"
|
||||
read update_r2
|
||||
if [ "$update_r2" = "y" ]; then
|
||||
configure_r2=true
|
||||
else
|
||||
configure_r2=false
|
||||
fi
|
||||
else
|
||||
configure_r2=true
|
||||
fi
|
||||
|
||||
if [ "$configure_r2" = true ]; then
|
||||
# Configure AWS CLI for R2
|
||||
echo "🔧 Configuring AWS CLI for Cloudflare R2..."
|
||||
echo "Enter your Cloudflare R2 Access Key ID:"
|
||||
read R2_ACCESS_KEY
|
||||
echo "Enter your Cloudflare R2 Secret Access Key:"
|
||||
read -s R2_SECRET_KEY
|
||||
echo "Enter your Cloudflare Account ID:"
|
||||
read CLOUDFLARE_ACCOUNT_ID
|
||||
|
||||
# Create R2 profile configuration
|
||||
mkdir -p ~/.aws
|
||||
|
||||
# Update or create credentials file
|
||||
if [ -f ~/.aws/credentials ]; then
|
||||
# Remove existing r2 section if it exists
|
||||
sed -i '/\[r2\]/,/^$/d' ~/.aws/credentials
|
||||
fi
|
||||
|
||||
# Append r2 credentials
|
||||
cat >> ~/.aws/credentials << EOL
|
||||
[r2]
|
||||
aws_access_key_id = $R2_ACCESS_KEY
|
||||
aws_secret_access_key = $R2_SECRET_KEY
|
||||
EOL
|
||||
|
||||
cat > ~/.aws/config << EOL
|
||||
# Update or create config file
|
||||
if [ -f ~/.aws/config ]; then
|
||||
# Remove existing r2 profile if it exists
|
||||
sed -i '/\[profile r2\]/,/^$/d' ~/.aws/config
|
||||
fi
|
||||
|
||||
# Append r2 config
|
||||
cat >> ~/.aws/config << EOL
|
||||
[profile r2]
|
||||
region = auto
|
||||
endpoint_url = https://$CLOUDFLARE_ACCOUNT_ID.r2.cloudflarestorage.com
|
||||
EOL
|
||||
echo "✅ R2 configuration complete"
|
||||
fi
|
||||
|
||||
echo "✅ R2 configuration complete"
|
||||
# Setting up Node Exporter for system metrics
|
||||
echo "📊 Setting up Node Exporter..."
|
||||
|
||||
echo "🎉 Setup complete! You can find helpful Docker and R2 commands in ~/docker-commands.sh"
|
||||
echo "Test your R2 connection: aws s3 ls --profile r2"
|
||||
# Create a monitoring network if it doesn't exist
|
||||
if ! docker network inspect monitoring &>/dev/null; then
|
||||
echo "Creating monitoring network..."
|
||||
docker network create monitoring
|
||||
else
|
||||
echo "✅ Monitoring network already exists"
|
||||
fi
|
||||
|
||||
# Check if Node Exporter is already running correctly
|
||||
if docker ps | grep -q "node_exporter"; then
|
||||
echo "✅ Node Exporter is already running"
|
||||
else
|
||||
# Remove existing container if it exists but not running
|
||||
if docker ps -a | grep -q node_exporter; then
|
||||
echo "Removing existing stopped Node Exporter container..."
|
||||
docker rm -f node_exporter
|
||||
fi
|
||||
|
||||
# Run Node Exporter container
|
||||
echo "Starting Node Exporter..."
|
||||
docker run -d \
|
||||
--name node_exporter \
|
||||
--restart unless-stopped \
|
||||
--network monitoring \
|
||||
-p 9100:9100 \
|
||||
-v "/proc:/host/proc:ro" \
|
||||
-v "/sys:/host/sys:ro" \
|
||||
-v "/:/rootfs:ro" \
|
||||
prom/node-exporter:latest \
|
||||
--path.procfs=/host/proc \
|
||||
--path.sysfs=/host/sys \
|
||||
--path.rootfs=/rootfs \
|
||||
--collector.filesystem.mount-points-exclude="^/(sys|proc|dev|host|etc)($$|/)"
|
||||
echo "✅ Node Exporter is now running and exposing metrics on port 9100"
|
||||
fi
|
||||
|
||||
# Setting up Loki Docker driver for log collection
|
||||
echo "📜 Setting up Loki Docker driver..."
|
||||
|
||||
# Check if plugin is already installed and up to date
|
||||
if docker plugin ls | grep -q "loki.*latest.*true"; then
|
||||
echo "✅ Loki Docker driver is already installed and enabled"
|
||||
else
|
||||
# Remove plugin if it exists but not up to date or not enabled
|
||||
if docker plugin ls | grep -q "loki"; then
|
||||
echo "Updating Loki Docker driver..."
|
||||
docker plugin disable -f loki
|
||||
docker plugin rm -f loki
|
||||
fi
|
||||
|
||||
# Install Loki Docker driver
|
||||
echo "Installing Loki Docker driver..."
|
||||
docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions
|
||||
echo "✅ Loki Docker driver installed successfully!"
|
||||
fi
|
||||
|
||||
echo "Note: Configure your containers with the Loki logging driver by adding this to your docker-compose.yml:"
|
||||
echo "
|
||||
logging:
|
||||
driver: loki
|
||||
options:
|
||||
loki-url: \"http://your-loki-server:3100/loki/api/v1/push\"
|
||||
loki-batch-size: \"400\"
|
||||
loki-external-labels: \"job=your_app,environment=production\"
|
||||
"
|
||||
|
||||
echo "🎉 Setup complete!"
|
||||
echo "Test your R2 connection: aws s3 ls --profile r2"
|
||||
echo "Metrics available at: http://$(hostname -I | awk '{print $1}'):9100/metrics"
|
||||
@@ -1,110 +0,0 @@
|
||||
#!/bin/bash
|
||||
# deploy.sh - Complete deployment script for staging and production environments
|
||||
# This script:
|
||||
# 1. Builds and uploads the Docker image to ECR with appropriate tag
|
||||
# 2. Copies the update script to EC2 instance (staging or prod)
|
||||
# 3. Executes the update script on the EC2 instance
|
||||
set -e # Exit immediately if a command exits with a non-zero status
|
||||
|
||||
# Function to print section headers
|
||||
print_header() {
|
||||
echo "======================================================"
|
||||
echo "🚀 $1"
|
||||
echo "======================================================"
|
||||
}
|
||||
|
||||
# Load environment variables
|
||||
if [ -f .env ]; then
|
||||
echo "Loading configuration from .env file..."
|
||||
export $(grep -v '^#' .env | xargs)
|
||||
fi
|
||||
|
||||
# Check command line argument
|
||||
if [ $# -ne 1 ] || ([ "$1" != "staging" ] && [ "$1" != "prod" ]); then
|
||||
echo "Error: Please specify environment (staging or prod)"
|
||||
echo "Usage: $0 [staging|prod]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ENV=$1
|
||||
VERSION_TAG=""
|
||||
|
||||
# Set environment-specific variables
|
||||
if [ "$ENV" == "staging" ]; then
|
||||
print_header "DEPLOYING TO STAGING ENVIRONMENT"
|
||||
EC2_HOST=$EC2_HOST_STAGING
|
||||
VERSION_TAG="staging"
|
||||
else
|
||||
print_header "DEPLOYING TO PRODUCTION ENVIRONMENT"
|
||||
EC2_HOST=$EC2_HOST_PROD
|
||||
VERSION_TAG="latest"
|
||||
fi
|
||||
|
||||
# Check required environment variables
|
||||
if [ -z "$EC2_HOST" ]; then
|
||||
echo "Error: EC2_HOST_${ENV^^} not defined in .env file or environment"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Configuration
|
||||
EC2_KEY=${EC2_KEY:-"~/.ssh/id_rsa"} # Use default or override from .env
|
||||
BUILD_SCRIPT="./upload.sh" # Path to your build script
|
||||
UPDATE_SCRIPT="./update.sh" # Path to your update script
|
||||
REMOTE_UPDATE_SCRIPT="/home/ec2-user/update-openfront.sh" # Where to place the script on EC2
|
||||
|
||||
# Check if required scripts exist
|
||||
if [ ! -f "$BUILD_SCRIPT" ]; then
|
||||
echo "Error: Build script $BUILD_SCRIPT not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f "$UPDATE_SCRIPT" ]; then
|
||||
echo "Error: Update script $UPDATE_SCRIPT not found!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Step 1: Build and upload Docker image to ECR
|
||||
print_header "STEP 1: Building and uploading Docker image to ECR"
|
||||
echo "Environment: ${ENV}"
|
||||
echo "Using version tag: $VERSION_TAG"
|
||||
|
||||
# Execute the build script with the version tag
|
||||
$BUILD_SCRIPT $VERSION_TAG
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Build and upload failed. Stopping deployment."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Step 2: Copy update script to EC2 instance
|
||||
print_header "STEP 2: Copying update script to EC2 instance"
|
||||
echo "Target: $EC2_HOST"
|
||||
|
||||
# Make sure the update script is executable
|
||||
chmod +x $UPDATE_SCRIPT
|
||||
|
||||
# Copy the update script to the EC2 instance
|
||||
scp -i $EC2_KEY $UPDATE_SCRIPT $EC2_HOST:$REMOTE_UPDATE_SCRIPT
|
||||
scp -i $EC2_KEY .env $EC2_HOST:/home/ec2-user/.env
|
||||
# After copying the .env file, secure it
|
||||
ssh -i $EC2_KEY $EC2_HOST "chmod 600 /home/ec2-user/.env"
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Failed to copy update script to EC2 instance. Stopping deployment."
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ Update script successfully copied to EC2 instance."
|
||||
|
||||
# Step 3: Execute the update script on the EC2 instance
|
||||
print_header "STEP 3: Executing update script on EC2 instance"
|
||||
|
||||
# Make the script executable on the remote server and execute it with the environment parameter
|
||||
ssh -i $EC2_KEY $EC2_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && $REMOTE_UPDATE_SCRIPT $ENV"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "❌ Failed to execute update script on EC2 instance."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_header "DEPLOYMENT COMPLETED SUCCESSFULLY"
|
||||
echo "✅ New version deployed to ${ENV} environment!"
|
||||
echo "🌐 Check your ${ENV} server to verify the deployment."
|
||||
echo "======================================================"
|
||||
@@ -1,28 +1,42 @@
|
||||
#!/bin/bash
|
||||
# Script to update Docker container
|
||||
# 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 [ -z "$1" ]; then
|
||||
echo "Error: Environment parameter is required (prod or staging)"
|
||||
echo "Usage: $0 <environment>"
|
||||
if [ $# -lt 3 ]; then
|
||||
echo "Error: Required parameters missing"
|
||||
echo "Usage: $0 <environment> <docker_username> <docker_repo>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set environment from parameter
|
||||
# Set parameters
|
||||
ENV=$1
|
||||
DOCKER_USERNAME=$2
|
||||
DOCKER_REPO=$3
|
||||
|
||||
# Container and image configuration
|
||||
CONTAINER_NAME="openfront-${ENV}"
|
||||
LOG_GROUP="/aws/ec2/docker-containers/${ENV}"
|
||||
IMAGE_NAME="${DOCKER_USERNAME}/${DOCKER_REPO}"
|
||||
FULL_IMAGE_NAME="${IMAGE_NAME}:latest"
|
||||
|
||||
# Get AWS account ID
|
||||
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
|
||||
ECR_REPO="${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com/openfront:latest"
|
||||
echo "======================================================"
|
||||
echo "🔄 UPDATING SERVER: ${ENV} ENVIRONMENT"
|
||||
echo "======================================================"
|
||||
echo "Container name: ${CONTAINER_NAME}"
|
||||
echo "Docker image: ${FULL_IMAGE_NAME}"
|
||||
|
||||
echo "Deploying to ${ENV} environment..."
|
||||
echo "Logging in to ECR..."
|
||||
aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.eu-west-1.amazonaws.com
|
||||
# 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
|
||||
|
||||
echo "Pulling latest image..."
|
||||
docker pull $ECR_REPO
|
||||
# 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
|
||||
@@ -61,21 +75,38 @@ if [ -n "$PORT_CHECK" ]; then
|
||||
echo "Attempting to proceed anyway..."
|
||||
fi
|
||||
|
||||
echo "Starting new container for ${ENV} environment..."
|
||||
# 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 \
|
||||
--log-driver=awslogs \
|
||||
--log-opt awslogs-region=eu-west-1 \
|
||||
--log-opt awslogs-group=${LOG_GROUP} \
|
||||
--log-opt awslogs-create-group=true \
|
||||
--env GAME_ENV=${ENV} \
|
||||
--env-file /home/ec2-user/.env \
|
||||
$VOLUME_MOUNTS \
|
||||
$NETWORK_FLAGS \
|
||||
--env APP_ENV=${ENV} \
|
||||
--env-file /root/.env \
|
||||
--name ${CONTAINER_NAME} \
|
||||
$ECR_REPO
|
||||
--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
|
||||
|
||||
# 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
|
||||
@@ -83,4 +114,12 @@ if [ $? -eq 0 ]; then
|
||||
echo "Cleanup complete."
|
||||
else
|
||||
echo "Failed to start container"
|
||||
fi
|
||||
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 "======================================================"
|
||||
@@ -1,107 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Script to build and upload OpenFront Docker image to ECR
|
||||
# Usage: ./upload-openfront.sh [version_tag]
|
||||
|
||||
# Load environment variables from .env file if it exists
|
||||
if [ -f .env ]; then
|
||||
echo "Loading configuration from .env file..."
|
||||
export $(grep -v '^#' .env | xargs)
|
||||
fi
|
||||
|
||||
# Configuration with fallbacks
|
||||
AWS_REGION=${AWS_REGION:-"eu-west-1"}
|
||||
ECR_REPO_NAME=${ECR_REPO_NAME:-"openfront"}
|
||||
AWS_ACCOUNT_ID=${AWS_ACCOUNT_ID:-$(aws sts get-caller-identity --query Account --output text)}
|
||||
ECR_REPO_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME"
|
||||
|
||||
# Default version tag is 'latest' if not provided
|
||||
VERSION_TAG=${1:-"latest"}
|
||||
|
||||
echo "===== OpenFront Docker Image Upload Script ====="
|
||||
echo "Repository: $ECR_REPO_URI"
|
||||
echo "Version tag: $VERSION_TAG"
|
||||
echo "================================================"
|
||||
|
||||
# Check if Docker is installed
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo "Error: Docker is not installed. Please install Docker first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if AWS CLI is installed
|
||||
if ! command -v aws &> /dev/null; then
|
||||
echo "Error: AWS CLI is not installed. Please install AWS CLI first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if we're in the correct directory
|
||||
if [ ! -f "Dockerfile" ]; then
|
||||
echo "Error: Dockerfile not found in current directory."
|
||||
echo "Please run this script from the directory containing your Dockerfile."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure the ECR repository exists
|
||||
echo "Ensuring ECR repository exists..."
|
||||
aws ecr describe-repositories --repository-names $ECR_REPO_NAME --region $AWS_REGION &> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Creating ECR repository $ECR_REPO_NAME..."
|
||||
aws ecr create-repository --repository-name $ECR_REPO_NAME --region $AWS_REGION
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Failed to create ECR repository."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Build the Docker image
|
||||
echo "Building Docker image..."
|
||||
docker buildx build --platform linux/amd64 -t $ECR_REPO_NAME:$VERSION_TAG .
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Docker build failed."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Authenticate to ECR
|
||||
echo "Authenticating to ECR..."
|
||||
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REPO_URI
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Failed to authenticate to ECR."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Tag the image for ECR
|
||||
echo "Tagging image for ECR..."
|
||||
docker tag $ECR_REPO_NAME:$VERSION_TAG $ECR_REPO_URI:$VERSION_TAG
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Failed to tag image."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Push the image to ECR
|
||||
echo "Pushing image to ECR..."
|
||||
docker push $ECR_REPO_URI:$VERSION_TAG
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Failed to push image to ECR."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Also tag and push as 'latest' if we're using a specific version
|
||||
if [ "$VERSION_TAG" != "latest" ]; then
|
||||
echo "Also tagging as 'latest'..."
|
||||
docker tag $ECR_REPO_NAME:$VERSION_TAG $ECR_REPO_URI:latest
|
||||
docker push $ECR_REPO_URI:latest
|
||||
fi
|
||||
|
||||
echo "Verifying upload..."
|
||||
aws ecr describe-images --repository-name $ECR_REPO_NAME --region $AWS_REGION --query "imageDetails[?contains(imageTags, '$VERSION_TAG')]"
|
||||
|
||||
echo "================================================"
|
||||
echo "✅ Success! Image uploaded to $ECR_REPO_URI:$VERSION_TAG"
|
||||
echo "================================================"
|
||||
|
||||
# Print helpful deployment instructions
|
||||
echo "To deploy this image to your EC2 instance, SSH into your instance and run:"
|
||||
echo "docker pull $ECR_REPO_URI:$VERSION_TAG"
|
||||
echo "docker stop \$(docker ps -q --filter ancestor=$ECR_REPO_URI)"
|
||||
echo "docker run -d -p 80:80 $ECR_REPO_URI:$VERSION_TAG"
|
||||
Reference in New Issue
Block a user