-
Notifications
You must be signed in to change notification settings - Fork 0
/
fly-machine.sh
executable file
·229 lines (212 loc) · 6.4 KB
/
fly-machine.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
# fly.io constrained instance helper wrapper
# Dayton Pidhirney
set -e -o pipefail
# Default app target config
INSTANCE_DEFAULT_CONFIG="ol2"
# Default prefix for all apps
INSTANCE_PREFIX=${INSTANCE_PREFIX:-"tt09"}
# Default instance cores
INSTANCE_DEFAULT_CORES=${INSTANCE_DEFAULT_CORES:-"4"}
# Default instance type
INSTANCE_DEFAULT_TYPE="performance-${INSTANCE_DEFAULT_CORES}x"
# Better interrupt handling
CMDLINE_OPTIONS="${CMDLINE_OPTIONS} tsc=noirqtime skew_tick=1 nohz=on no-steal-acc"
# Create a base app image which all other
# app instances are based off of. Only runs once.
check_build_base() {
if ! fly image show -a "$INSTANCE_DEFAULT_CONFIG-base" >/dev/null 2>&1; then
fly app create "$INSTANCE_DEFAULT_CONFIG-base"
fly deploy \
--verbose \
--config /dev/null \
--app "$INSTANCE_DEFAULT_CONFIG-base" \
--build-only \
--dockerfile ./docker/base.dockerfile \
--image-label "latest" \
--push || fly app destroy "$INSTANCE_DEFAULT_CONFIG-base" -y
fi
}
# $1: app_name
# $2: state
wait_machine_state() {
while true; do
if [[ "$(fly machine list -j -a $1 | jq -r '.[0].state')" != $2 ]]; then
sleep 1
echo -n '.'
else
echo
break
fi
done
}
if [[ $# -gt 1 || "$1" = "list" ]]; then
declare -a CURRENT_APPS=$(
fly app list | grep $INSTANCE_DEFAULT_CONFIG | grep -v base | tr -d ' ' | tr '\t' ','
)
fi
# Parse options:
case $1 in
-h|help)
echo "$0 [command] [options]"
echo "Commands:"
echo "* create [type]"
echo "* scale <name> <count>"
echo "* update <name>"
echo "* destroy <name>"
echo "* start <name>"
echo "* stop <name>"
echo "* ssh <name>"
echo "* list"
;;
create)
# ensure the base is built
check_build_base
shift
app_config=$1
if [[ "$app_config" = "" ]]; then
app_config=$INSTANCE_DEFAULT_CONFIG
elif [[ ! -f ./docker/$app_config.dockerfile ]]; then
echo "unknown app config: $app_config"
echo "please choose from:"
for config in $(ls -p docker/*.dockerfile | grep -v base | cut -d/ -f2 | cut -d. -f1); do
echo " - $config"
done
exit 1
fi
app_name="$app_config-$INSTANCE_PREFIX"
app_volname="$(echo $app_name | tr '-' '_')"
# Create app config from base
fly apps create $app_name
# Build app config image
fly deploy \
--verbose \
--config /dev/null \
--app $app_name \
--build-only \
--build-arg REGISTRY="registry.fly.io" \
--regions iad \
--vm-size $INSTANCE_DEFAULT_TYPE \
--dockerfile "./docker/$app_config.dockerfile" \
--image-label "latest" \
--push || fly app destroy $app_name -y
# Create volume for app instance
fly volume create -y \
--app $app_name \
--size 30 \
$app_volname
# Create machine app instance
fly machine create "registry.fly.io/$app_name:latest" \
--verbose \
--name "$app_name-${#CURRENT_APPS[@]}-machine" \
--app $app_name \
--region iad \
--port 1122:22 \
--vm-size $INSTANCE_DEFAULT_TYPE \
--kernel-arg "$CMDLINE_OPTIONS" \
--volume $app_volname:/mnt/output:rw \
--autostart=false
# Wait for machine to be created
wait_machine_state $app_name stopped
# XXX: we probably shouldn't assume it needs to be started
# Start app instance
# fly machine start -a $app_name
# Wait for machine to be deployed
# wait_machine_state $app_name started
;;
scale)
shift
app_name=$1
if [[ "$app_name" = "" ]]; then
echo "scale what? (run list command)"
exit 1
fi
shift
app_scale=$1
if [[ $app_scale -le 0 ]]; then
echo "invalid app scale value"
exit 1
fi
fly scale count -a $app_name $app_scale
;;
update)
shift
app_name=$1
if [[ "$app_name" = "" ]]; then
echo "update what? (run list command)"
exit 1
fi
app_config=$(echo $app_name | cut -d- -f1)
# Re-build app config image
fly deploy \
--verbose \
--config /dev/null \
--app $app_name \
--dockerfile "./docker/$app_config.dockerfile" \
--image-label "latest" \
--build-only \
--build-arg REGISTRY="registry.fly.io" \
--push
# Update machines in app
fly machine update -y \
--verbose \
--app $app_name \
--image "registry.fly.io/$app_name:latest" \
--metadata "DEPLOY_ID=$RANDOM"
wait_machine_state $app_name started
;;
destroy)
shift
app_name=$1
if [[ "$app_name" = "" ]]; then
echo "destroy what? (run list command)"
exit 1
fi
fly app destroy $app_name ${@:2}
;;
start)
shift
app_name=$1
if [[ "$app_name" = "" ]]; then
echo "start what? (run list command)"
exit 1
fi
fly machine start -a $app_name
wait_machine_state $app_name started
;;
stop)
shift
app_name=$1
if [[ "$app_name" = "" ]]; then
echo "stop what? (run list command)"
exit 1
fi
fly machine stop -a $app_name
wait_machine_state $app_name stopped
;;
ssh)
shift
app_name=$1
if [[ "$app_name" = "" ]]; then
echo "ssh what? (run list command)"
exit 1
fi
fly ssh console -a $app_name
;;
list)
if [[ "${CURRENT_APPS[@]}" = "" ]]; then
echo "no machines currently"
exit 0
fi
for app in ${CURRENT_APPS[@]}; do
app_name=$(echo $app | cut -d, -f1)
app_state=$(echo $app | cut -d, -f3)
echo "$app_name:$app_state"
done
;;
*)
echo 'Unknown option: (See help using `-h` or simply `help`)'
exit 1
;;
esac
exit 0