Dynamic tunnels (#579)

## Description:

Update deployment:

1. automatically create and configure CF tunnels and point it to
subdomain.domain

2. Send loki logs to remote endpoint

3. create metric-exporter.sh to push prom metrics to remote endpoint

4. update and refactor deployment & env variables

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced
- [x] I understand that submitting code with bugs that could have been
caught through manual testing blocks releases and new features for all
contributors

## Please put your Discord username so you can be contacted if a bug or
regression is found:

<DISCORD USERNAME>

---------

Co-authored-by: Evan Pellegrini <evan@Evans-Air.attlocal.net>
Co-authored-by: evan <openfrontio@gmail.com>
This commit is contained in:
evanpelle
2025-04-20 19:34:17 -07:00
committed by GitHub
parent a4329d42ee
commit 03f7bade7f
11 changed files with 365 additions and 123 deletions
+76 -46
View File
@@ -7,6 +7,27 @@
set -e # Exit immediately if a command exits with a non-zero status
# Check command line arguments
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
echo "Error: Please specify environment and host, with optional subdomain"
echo "Usage: $0 [prod|staging] [eu|us|staging] [subdomain]"
exit 1
fi
# Validate first argument (environment)
if [ "$1" != "prod" ] && [ "$1" != "staging" ]; then
echo "Error: First argument must be either 'prod' or 'staging'"
echo "Usage: $0 [prod|staging] [eu|us|staging] [subdomain]"
exit 1
fi
# Validate second argument (host)
if [ "$2" != "eu" ] && [ "$2" != "us" ] && [ "$2" != "staging" ]; then
echo "Error: Second argument must be either 'eu', 'us', or 'staging'"
echo "Usage: $0 [prod|staging] [eu|us|staging] [subdomain]"
exit 1
fi
# Function to print section headers
print_header() {
echo "======================================================"
@@ -14,59 +35,59 @@ print_header() {
echo "======================================================"
}
# Load environment variables
ENV=$1
HOST=$2
SUBDOMAIN=$3 # Optional third argument for custom subdomain
# Set subdomain - use the custom subdomain if provided, otherwise use REGION
if [ -n "$SUBDOMAIN" ]; then
echo "Using custom subdomain: $SUBDOMAIN"
else
SUBDOMAIN=$HOST
echo "Using host as subdomain: $SUBDOMAIN"
fi
# Load common environment variables first
if [ -f .env ]; then
echo "Loading configuration from .env file..."
echo "Loading common configuration from .env file..."
export $(grep -v '^#' .env | xargs)
fi
# Check command line argument
if [ $# -ne 1 ] || ([ "$1" != "staging" ] && [ "$1" != "eu" ] && [ "$1" != "us" ]); then
echo "Error: Please specify environment (staging, eu, or us)"
echo "Usage: $0 [staging|eu|us]"
# Load environment-specific variables
if [ -f .env.$ENV ]; then
echo "Loading $ENV-specific configuration from .env.$ENV file..."
export $(grep -v '^#' .env.$ENV | xargs)
else
echo "Error: Environment file .env.$ENV not found"
exit 1
fi
REGION=$1
VERSION_TAG="latest"
DOCKER_REPO=""
ENV=""
SSH_KEY=""
# Set environment-specific variables
if [ "$REGION" == "staging" ]; then
print_header "DEPLOYING TO STAGING ENVIRONMENT"
if [ "$HOST" == "staging" ]; then
print_header "DEPLOYING TO STAGING HOST"
SERVER_HOST=$SERVER_HOST_STAGING
DOCKER_REPO=$DOCKER_REPO_STAGING
ENV="staging"
SSH_KEY=$SSH_KEY_STAGING
elif [ "$REGION" == "us" ]; then
print_header "DEPLOYING TO US ENVIRONMENT"
elif [ "$HOST" == "us" ]; then
print_header "DEPLOYING TO US HOST"
SERVER_HOST=$SERVER_HOST_US
DOCKER_REPO=$DOCKER_REPO_PROD # Uses prod Docker repo for alt environment
SSH_KEY=$SSH_KEY_PROD
ENV="prod"
else
print_header "DEPLOYING TO EU ENVIRONMENT"
print_header "DEPLOYING TO EU HOST"
SERVER_HOST=$SERVER_HOST_EU
DOCKER_REPO=$DOCKER_REPO_PROD
SSH_KEY=$SSH_KEY_PROD
ENV="prod"
fi
# Check required environment variables
if [ -z "$SERVER_HOST" ]; then
echo "Error: SERVER_HOST_${REGION^^} not defined in .env file or environment"
echo "Error: ${HOST} not defined in .env file or environment"
exit 1
fi
# Configuration
DOCKER_USERNAME=${DOCKER_USERNAME} # Docker Hub username
UPDATE_SCRIPT="./update.sh" # Path to your update script
REMOTE_USER="openfront"
REMOTE_UPDATE_PATH="/home/$REMOTE_USER"
REMOTE_UPDATE_SCRIPT="$REMOTE_UPDATE_PATH/update-openfront.sh" # Where to place the script on server
IMAGE_NAME="${DOCKER_USERNAME}/${DOCKER_REPO}"
DOCKER_IMAGE="${IMAGE_NAME}:${VERSION_TAG}"
# Check if update script exists
if [ ! -f "$UPDATE_SCRIPT" ]; then
echo "Error: Update script $UPDATE_SCRIPT not found!"
@@ -75,7 +96,9 @@ fi
# Step 1: Build and upload Docker image to Docker Hub
print_header "STEP 1: Building and uploading Docker image to Docker Hub"
echo "Region: ${REGION}"
echo "Environment: ${ENV}"
echo "Host: ${HOST}"
echo "Subdomain: ${SUBDOMAIN}"
echo "Using version tag: $VERSION_TAG"
echo "Docker repository: $DOCKER_REPO"
@@ -107,25 +130,32 @@ chmod +x $UPDATE_SCRIPT
# Copy the update script to the server
scp -i $SSH_KEY $UPDATE_SCRIPT $REMOTE_USER@$SERVER_HOST:$REMOTE_UPDATE_SCRIPT
# Copy environment variables if needed
if [ -f .env ]; then
scp -i $SSH_KEY .env $REMOTE_USER@$SERVER_HOST:$REMOTE_UPDATE_PATH/.env
# Secure the .env file
ssh -i $SSH_KEY $REMOTE_USER@$SERVER_HOST "chmod 600 $REMOTE_UPDATE_PATH/.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 $REMOTE_USER@$SERVER_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && $REMOTE_UPDATE_SCRIPT $REGION $DOCKER_USERNAME $DOCKER_REPO"
ssh -i $SSH_KEY $REMOTE_USER@$SERVER_HOST "chmod +x $REMOTE_UPDATE_SCRIPT && \
cat > $REMOTE_UPDATE_PATH/.env << 'EOL'
GAME_ENV=$ENV
ENV=$ENV
HOST=$HOST
SUBDOMAIN=$SUBDOMAIN
DOCKER_IMAGE=$DOCKER_IMAGE
DOCKER_TOKEN=$DOCKER_TOKEN
ADMIN_TOKEN=$ADMIN_TOKEN
CF_ACCOUNT_ID=$CF_ACCOUNT_ID
R2_ACCESS_KEY=$R2_ACCESS_KEY
R2_SECRET_KEY=$R2_SECRET_KEY
R2_BUCKET=$R2_BUCKET
CF_API_TOKEN=$CF_API_TOKEN
DOMAIN=$DOMAIN
SUBDOMAIN=$SUBDOMAIN
MON_USERNAME=$MON_USERNAME
MON_PASSWORD=$MON_PASSWORD
EOL
chmod 600 $REMOTE_UPDATE_PATH/.env && \
$REMOTE_UPDATE_SCRIPT"
if [ $? -ne 0 ]; then
echo "❌ Failed to execute update script on server."
@@ -133,6 +163,6 @@ if [ $? -ne 0 ]; then
fi
print_header "DEPLOYMENT COMPLETED SUCCESSFULLY"
echo "✅ New version deployed to ${REGION} environment!"
echo "🌐 Check your ${REGION} server to verify the deployment."
echo "✅ New version deployed to ${ENV} environment in ${HOST} with subdomain ${SUBDOMAIN}!"
echo "🌐 Check your server to verify the deployment."
echo "======================================================="