Created
September 24, 2024 21:41
-
-
Save kevinlu1248/e105029c64ed1a380472ec6f58be234e to your computer and use it in GitHub Desktop.
0DD redeploy script
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
# !/bin/bash | |
# Remove old docker images only after 2 runs to allow for rollbacks. | |
# Docker images also need to finish processing their requests before they can be removed. | |
docker pull sweepai/sweep:$TAG | |
containers_to_remove=$(docker ps -q --filter "ancestor=sweepai/sweep" | awk 'NR>2') | |
if [ ! -z "$containers_to_remove" ]; then | |
echo "Removing old docker runs" | |
echo "$containers_to_remove" | while read -r container; do | |
if [ ! -z "$container" ]; then | |
docker kill "$container" | |
docker rm "$container" | |
fi | |
done | |
else | |
echo "No old docker runs to remove" | |
fi | |
# Find next available port to deploy to | |
PORT=8081 | |
is_port_free() { | |
if curl -s http://localhost:$1 > /dev/null; then | |
return 0 # Port is in use | |
else | |
return 1 # Port is free | |
fi | |
} | |
while is_port_free $PORT || is_port_free $FRONTEND_PORT; do | |
((PORT++)) | |
done | |
echo "Found open port: $PORT" | |
if [ ! -f .env ]; then | |
echo "Error: .env file not found" >&2 | |
exit 1 | |
fi | |
if ! docker network inspect $NETWORK_NAME >/dev/null 2>&1; then | |
docker network create $NETWORK_NAME || { echo "Failed to create network"; exit 1; } | |
fi | |
docker run --env-file .env -p $PORT:8080 -v $PWD/caches:/mnt/caches --network $NETWORK_NAME -d sweepai/sweep:latest | |
echo "Backend accessible at: http://localhost:$PORT" | |
# Wait until webhook is available before rerouting traffic to it | |
echo "Waiting for server to start..." | |
while true; do | |
curl --output /dev/null --silent --head --fail http://localhost:$PORT/health | |
result=$? | |
if [[ $result -eq 0 || $result -eq 22 ]]; then | |
echo "Received a good response!" | |
break | |
else | |
printf '.' | |
sleep 5 | |
fi | |
done | |
# Reroute traffic to new docker container | |
sudo iptables -t nat -L PREROUTING --line-numbers | grep 'REDIRECT' | tail -n1 | awk '{print $1}' | xargs -I {} sudo iptables -t nat -D PREROUTING {} | |
sudo iptables -t nat -L PREROUTING --line-numbers | grep 'REDIRECT' | tail -n1 | awk '{print $1}' | xargs -I {} sudo iptables -t nat -D PREROUTING {} | |
# redirect internal 8080 to internal docker container | |
sudo iptables -t nat -L OUTPUT --line-numbers | grep 'REDIRECT' | tail -n1 | awk '{print $1}' | xargs -I {} sudo iptables -t nat -D OUTPUT {} | |
sudo iptables -t nat -A OUTPUT -p tcp -o lo --dport 8080 -j REDIRECT --to-port $PORT | |
containers_to_remove=$(docker ps -q --filter "ancestor=sweepai/sweep" | awk 'NR>1') | |
# kill previous docker image after 20 min | |
if [ ! -z "$containers_to_remove" ]; then | |
( | |
sleep 1200 | |
echo "$containers_to_remove" | while read -r container; do | |
if [ ! -z "$container" ]; then | |
docker kill "$container" | |
fi | |
done | |
) & | |
echo "Scheduled removal of old containers after 20 minutes" | |
else | |
echo "No old containers to remove after 20 minutes" | |
fi | |
echo "Command sent to screen session on port: $PORT" | |
echo "Success!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment