-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy.sh
executable file
·78 lines (66 loc) · 2.47 KB
/
deploy.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/sh
set +x
cert="./backstroke.pem"
compose="./docker-compose.yml"
haproxy="./haproxy.conf"
postgres_certs="./postgres_certs"
ip="45.55.127.101"
# Create droplet.
if ! doctl compute droplet ls | grep backstroke 2>&1 > /dev/null; then
echo "* Creating compute resources..."
doctl compute droplet create backstroke \
--enable-ipv6 \
--enable-monitoring \
--enable-private-networking \
--tag-names backstroke-compute \
--image 27493072 \
--region nyc3 \
--size 2gb \
--ssh-keys "$(doctl compute ssh-key ls | grep Backstroke | awk '{ print $3 }')" \
--wait
else
echo "* Compute resources already exist."
fi
# If the floating ip is not assigned to backstroke, reassign it to the droplet we just created.
if ! doctl compute floating-ip get $ip | grep backstroke 2>&1 > /dev/null; then
if [ "$(doctl compute floating-ip get $ip --format DropletID --no-header | sed 's/\w//g')" != "" ]; then
echo "* Unassigning floating ip..."
doctl compute floating-ip-action unassign $ip
sleep 3
fi
echo "* Assigning floating ip..."
doctl compute floating-ip-action assign $ip "$(doctl compute droplet ls | grep backstroke | awk '{ print $1 }')"
ssh-keygen -R $ip
# Wait for the ip to point to something
while true; do
sleep 2
echo "\n" | nc -w 1 $ip 22
result=$(($?))
if [ $result -eq 0 ]; then
break
else
printf '.'
fi
done
echo
echo "* Compute resource contected via floating ip!"
else
echo "* Floating ip already assigned."
fi
echo "* Copying docker compose file..."
scp -i $cert $compose root@$ip:/opt/docker-compose.yml
scp -i $cert $compose.production root@$ip:/opt/docker-compose.yml.production
echo "* Copying haproxy file..."
scp -i $cert $haproxy root@$ip:/opt/haproxy.conf
#echo "* Copying postgres certificates and setting permissions..."
#scp -r -i $cert $postgres_certs root@$ip:/opt/postgres_certs/
#ssh -i $cert root@$ip 'sudo chmod 600 /opt/postgres_certs/server.key && sudo chown postgres:postgres /opt/postgres_certs/server.key'
echo "* Disabling local firewall..."
ssh -i $cert root@$ip "ufw disable"
echo "* Pulling services..."
ssh -i $cert root@$ip "cd /opt && docker-compose -f docker-compose.yml -f docker-compose.yml.production pull"
echo "* Stopping services..."
ssh -i $cert root@$ip "cd /opt && docker-compose stop"
echo "* Starting services..."
ssh -i $cert root@$ip "cd /opt && docker-compose -f docker-compose.yml -f docker-compose.yml.production up -d --scale worker=3"
echo "* Done!"