forked from sweepai/sweep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredeploy.sh
executable file
·60 lines (48 loc) · 1.71 KB
/
redeploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# !/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:latest
echo `docker ps`
containers_to_remove=$(docker ps -q | awk 'NR>2')
if [ ! -z "$containers_to_remove" ]; then
echo "Removing old docker runs"
docker kill -f $containers_to_remove
docker rm -f $containers_to_remove
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; do
((PORT++))
done
echo "Found open port: $PORT"
# Start new docker container on the next available port
docker run --env-file env -p $PORT:8080 -v ./caches:/mnt/caches -d sweepai/sweep:latest
# 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 -A PREROUTING -p tcp --dport ${SWEEP_PORT:-8080} -j REDIRECT --to-port $PORT
# kill previous docker image after 20 min
(sleep 1200 && docker kill $(docker ps -q | awk 'NR>1')) &
echo "Command sent to screen session on port: $PORT"
echo "Success!"