Skip to content

Commit

Permalink
Updates openresty config
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmerfield committed Jan 24, 2025
1 parent a301a47 commit 20409a1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions config/openresty/conf/reverse-proxy.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_header 'Blot-Server' '{{server_label}}' always;
add_header 'Blot-Upstream' $upstream_server always;
add_header 'Blot-Upstream-Addr' $upstream_addr always;

set $upstream_server blot_node;

Expand Down
35 changes: 23 additions & 12 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ echo "Deploying commit: $GIT_COMMIT_HASH"
echo "Commit message: $(git log -1 --pretty=%B)"

# Check that a multi-architecture image is available for the commit hash
# and echo the output, if none is found, the script will fail
echo "Checking for multi-architecture image..."
docker manifest inspect ghcr.io/davidmerfield/blot:$GIT_COMMIT_HASH
if ! docker manifest inspect ghcr.io/davidmerfield/blot:$GIT_COMMIT_HASH > /dev/null 2>&1; then
echo "Error: Multi-architecture image for commit $GIT_COMMIT_HASH does not exist."
exit 1
else
echo "Multi-architecture image found."
fi

# Define container names and ports
BLUE_CONTAINER="blot-container-blue"
Expand All @@ -31,9 +35,13 @@ DOCKER_RUN_COMMAND="docker run --pull=always -d \
--memory=1g --cpus=1 \
ghcr.io/davidmerfield/blot:$GIT_COMMIT_HASH"

# Configurable health check timeout
timeout=${HEALTH_CHECK_TIMEOUT:-30} # Default to 30 seconds
interval=2 # Interval between health checks

# Function to run a command over SSH
ssh_blot() {
ssh blot "$@"
ssh blot "$@" || { echo "SSH command failed: $@"; exit 1; }
}

# Function to remove a container if it exists
Expand All @@ -47,8 +55,6 @@ remove_container_if_exists() {
check_health() {
local container_name=$1
echo "Waiting for $container_name to become healthy..."
timeout=30 # Maximum wait time in seconds
interval=2 # Interval between health checks
elapsed=0

while [[ $elapsed -lt $timeout ]]; do
Expand All @@ -57,7 +63,7 @@ check_health() {
echo "$container_name is healthy."
return 0
fi
echo "Still waiting for $container_name to become healthy..."
echo "Still waiting for $container_name to become healthy (elapsed: $elapsed seconds)..."
sleep $interval
elapsed=$((elapsed + interval))
done
Expand All @@ -66,17 +72,23 @@ check_health() {
return 1
}

# Function to replace placeholders in the docker run command
replace_placeholders() {
local template=$1
local container_name=$2
local container_port=$3
echo "$template" | sed "s/{{CONTAINER_NAME}}/$container_name/g" | sed "s/{{CONTAINER_PORT}}/$container_port/g"
}

# Function to deploy a container
deploy_container() {
local container_name=$1
local container_port=$2
echo "Starting deployment for $container_name on port $container_port..."
remove_container_if_exists $container_name

# Replace placeholders in the docker run command using sed
local run_command=$(echo "$DOCKER_RUN_COMMAND" | \
sed "s/{{CONTAINER_NAME}}/$container_name/g" | \
sed "s/{{CONTAINER_PORT}}/$container_port/g")
# Replace placeholders
local run_command=$(replace_placeholders "$DOCKER_RUN_COMMAND" "$container_name" "$container_port")

echo "Running the following command on the server:"
echo "$run_command"
Expand All @@ -94,9 +106,8 @@ if [[ $? -eq 0 ]]; then
echo "$BLUE_CONTAINER is healthy. Proceeding to replace $GREEN_CONTAINER on port $GREEN_CONTAINER_PORT..."
deploy_container $GREEN_CONTAINER $GREEN_CONTAINER_PORT

# Ensure the green container is healthy before declaring success
if [[ $? -ne 0 ]]; then
echo "Deployment failed. $GREEN_CONTAINER did not become healthy. Rolling back to $BLUE_CONTAINER."
echo "Deployment failed. $GREEN_CONTAINER did not become healthy. Blue container is running."
exit 1
fi
else
Expand Down

0 comments on commit 20409a1

Please sign in to comment.