-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
23 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,60 @@ | ||
#!/bin/bash | ||
|
||
NGINX_CONFIG_PATH="/etc/nginx/sites-available/api.fluffy.run" | ||
HOST_HEALTH_CHECK_ENDPOINT="http://localhost:8082/actuator/health" | ||
HEALTH_CHECK_ATTEMPTS=5 | ||
HEALTH_CHECK_DELAY=3 | ||
|
||
health_check() { | ||
local endpoint=$1 | ||
for i in $(seq 1 $HEALTH_CHECK_ATTEMPTS); do | ||
echo "Health check attempt ($i/$HEALTH_CHECK_ATTEMPTS)" | ||
response=$(curl -s -o /dev/null -w "%{http_code}" $HOST_HEALTH_CHECK_ENDPOINT) | ||
echo "Health check attempt ($i/$HEALTH_CHECK_ATTEMPTS) for $endpoint" | ||
response=$(curl -s -o /dev/null -w "%{http_code}" "$endpoint") | ||
|
||
if [ $response -eq 200 ]; then | ||
echo "Health check passed" | ||
if [ "$response" -eq 200 ]; then | ||
echo "Health check passed for $endpoint" | ||
return 0 | ||
fi | ||
|
||
sleep $HEALTH_CHECK_DELAY | ||
done | ||
|
||
echo "Health check failed" | ||
echo "Health check failed for $endpoint" | ||
return 1 | ||
} | ||
|
||
switch_container() { | ||
local prev_container=$1 | ||
local next_container=$2 | ||
local next_container_port=$3 | ||
|
||
docker compose -f compose.yml up $next_container -d | ||
echo "Starting $next_container..." | ||
docker compose -f compose.yml up "$next_container" -d | ||
|
||
if ! health_check; then | ||
echo "Health check failed, rolling back" | ||
docker compose -f compose.yml down $next_container | ||
local health_check_url="http://localhost:${next_container_port}/health" | ||
|
||
if ! health_check "$health_check_url"; then | ||
echo "Health check failed, rolling back..." | ||
docker compose -f compose.yml down "$next_container" | ||
return 1 | ||
fi | ||
|
||
sed -i "s/server $prev_container:8080;/server $next_container:8080;/" "$NGINX_CONFIG_PATH" | ||
|
||
sudo nginx -s reload | ||
echo "Reloading Nginx configuration..." | ||
if ! sudo nginx -s reload; then | ||
echo "Failed to reload Nginx" | ||
return 1 | ||
fi | ||
|
||
echo "$next_container is now live!" | ||
} | ||
|
||
IS_GREEN=$(docker container ps | grep app-green) | ||
|
||
if [ -z "$IS_GREEN" ]; then | ||
echo "### BLUE >> GREEN ###" | ||
switch_container "app-blue" "app-green" | ||
switch_container "app-blue" "app-green" "8081" | ||
else | ||
echo "### GREEN >> BLUE ###" | ||
switch_container "app-green" "app-blue" | ||
switch_container "app-green" "app-blue" "8080" | ||
fi |