-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle.sh
More file actions
executable file
·130 lines (127 loc) · 4.3 KB
/
cycle.sh
File metadata and controls
executable file
·130 lines (127 loc) · 4.3 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -e
#DO_TOKEN should be pre-defined in the env.
export publicIP=""
source send_msg.sh
write_msg() {
echo "---> $@"
}
exec_doctl() {
eval "doctl $@ -t $DO_TOKEN"
}
validate_prereqs() {
if [ -z "$dropletId" ] && [ -z "$snapshotId" ]; then
echo "need a dropletId or a snapshotId to proceed"
exit 99
elif [ -z "$DO_TOKEN" ]; then
echo "missing DO token. cannot use API."
exit 99
fi
write_msg "all args present"
}
get_droplet() {
up_id=$(exec_doctl compute droplet list --format ID --no-header --tag-name factorio)
if [ "$up_id" != "$dropletId" ]; then
echo "running droplet .${up_id}. does not match requested ID .${dropletId}."
exit 99
fi
write_msg "got droplet with id $dropletId"
}
power_down() {
send_msg "Shutting down droplet $dropletId"
write_msg "shutting down $dropletId"
exec_doctl compute droplet-action power-off $dropletId --wait --no-header --format Status,CompletedAt
write_msg "done"
}
power_up() {
write_msg "powering up $dropletId"
exec_doctl compute droplet-action power-on $dropletId --wait --no-header --format Status,CompletedAt
write_msg "done"
}
take_snapshot() {
send_msg "Taking snapshot. This might take a minute."
write_msg "taking snapshot of droplet $dropletId"
exec_doctl compute droplet-action snapshot $dropletId --no-header --format ID --snapshot-name $snapshotName --wait
write_msg "done"
write_msg "getting snapshot id"
snapshotId=$(doctl compute image list --no-header --format ID,Name -t $DO_TOKEN | grep $snapshotName | awk '{print $1}')
write_msg "done"
write_msg "snapshot ID is: $snapshotId"
}
destroy_snapshot() {
write_msg "removing snapshot"
exec_doctl compute snapshot delete $snapshotId -f --no-header
write_msg "done"
snapshotId=""
}
destroy_droplet() {
send_msg "Destroying droplet $dropletId"
write_msg "destroying droplet $dropletId"
exec_doctl compute droplet delete $dropletId -f
write_msg "done"
dropletId=""
send_msg "Droplet has been destroyed."
}
restore_snapshot() {
write_msg "restoring droplet from snapshot $snapshotId"
exec_doctl compute droplet create factorio --image $snapshotId --tag-name factorio --region $region --size $size --wait --ssh-keys $SHIP_FINGERPRINT --ssh-keys $HOME_FINGERPRINT
write_msg "done"
write_msg "finding new dropletId and publicIP"
result=$(doctl compute droplet list --no-header --format ID,PublicIPv4 --tag-name factorio -t $DO_TOKEN)
dropletId=$(awk '{print $1}' <<< $result)
publicIP=$(awk '{print $2}' <<< $result)
write_msg "found new droplet id: $dropletId"
write_msg "publicIP is $publicIP"
shipctl put_resource_state $JOB_NAME versionName "$publicIP"
send_msg "New droplet started at IP: $publicIP"
write_msg "done"
}
run_container() {
write_msg "starting factorio container"
shipctl replace start.sh
doctl compute ssh $dropletId --ssh-command 'mkdir -p /home/factorio' --ssh-key-path $FACTORIODOKEYS_PRIVATE_KEY_PATH -t $DO_TOKEN
ssh-add $FACTORIODOKEYS_PRIVATE_KEY_PATH
ls -latr
scp_cmd="scp -i $FACTORIODOKEYS_PRIVATE_KEY_PATH start.sh root@$publicIP:/home/factorio"
write_msg "about to execute: $scp_cmd"
shipctl retry "eval $scp_cmd"
doctl compute ssh $dropletId --ssh-command 'sh /home/factorio/start.sh' --ssh-key-path $FACTORIODOKEYS_PRIVATE_KEY_PATH -t $DO_TOKEN
sleep 5
doctl compute ssh $dropletId --ssh-command 'sudo docker ps -a' --ssh-key-path $FACTORIODOKEYS_PRIVATE_KEY_PATH -t $DO_TOKEN
write_msg "done"
}
write_state() {
write_msg "writing new values to state:"
write_msg "dropletId: $dropletId"
write_msg "snapshotId: $snapshotId"
echo "export snapshotId=\"$snapshotId\"" > state.env
echo "export dropletId=\"$dropletId\"" >> state.env
shipctl copy_file_to_state state.env
write_msg "done"
}
validate_prereqs
if [ -n "$dropletId" ] && [ -z "$snapshotId" ]; then
get_droplet
power_down
sleep 2
take_snapshot
sleep 2
destroy_droplet
sleep 2
write_state
elif [ -n "$snapshotId" ] && [ -z "$dropletId" ]; then
send_msg "Restoring droplet from snapshot $snapshotId"
restore_snapshot
#sleep 10
#run_container
sleep 2
destroy_snapshot
sleep 2
write_state
else
send_msg "Action '$message' failed. Error in droplet/snapshot configuration <$BUILD_URL|logs>"
write_msg "bad state"
write_msg "dropletId: $dropletId"
write_msg "snapshotId: $snapshotId"
fi
write_msg "script complete"