16
16
17
17
set -Eeuo pipefail
18
18
19
+ script_dir=$( dirname " $( realpath " ${BASH_SOURCE[0]} " ) " )
20
+
19
21
print_help () {
20
22
echo -e ' github.com/shiftstack/shiftstack-ci'
21
23
echo -e ' Spin a server on OpenStack'
@@ -34,25 +36,28 @@ print_help() {
34
36
echo -e ' \t-d\tRun the script in debug mode'
35
37
echo -e ' \t-p\tDo not clean up the server after creation'
36
38
echo -e ' \t\t(will print a cleanup script instead of executing it).'
37
- echo -e ' \t-u\tTest connectivity from the instance by setting the cloud user from the image (e.g. centos)'
39
+ echo -e ' \t-u\tName of the cloud user from the image (e.g. centos) [not used, except to imply -l]'
40
+ echo -e ' \t-l\tInstall and run a connectivity test application'
38
41
echo -e ' \t-t\tRun the script without pause (create/cleanup)'
39
42
}
40
43
41
44
declare \
42
45
debug=' ' \
43
46
persistent=' ' \
44
47
interactive=' ' \
45
- os_user =' ' \
48
+ liveness =' ' \
46
49
server_flavor=' ' \
47
50
server_image=' ' \
48
51
key_name=' ' \
49
52
external_network=' external'
50
- while getopts dtpf:u:i:e:k:h opt; do
53
+ # Note that the $OPTARG to -u is ignored because it is deprecated
54
+ while getopts dtplf:u:i:e:k:h opt; do
51
55
case " $opt " in
52
56
d) debug=' yes' ;;
53
57
p) persistent=' yes' ;;
54
58
t) interactive=' no' ;;
55
- u) os_user=" $OPTARG " ;;
59
+ u) liveness=' yes' ;;
60
+ l) liveness=' yes' ;;
56
61
f) server_flavor=" $OPTARG " ;;
57
62
i) server_image=" $OPTARG " ;;
58
63
e) external_network=" $OPTARG " ;;
@@ -203,14 +208,19 @@ port_id="$(openstack port create -f value -c id \
203
208
" $name " ) "
204
209
>&2 echo " Created port ${port_id} "
205
210
206
- server_id=" $( openstack server create --wait -f value -c id \
207
- --block-device uuid=" $vol_id " \
208
- --image " $server_image " \
209
- --flavor " $server_flavor " \
210
- --nic " port-id=$port_id " \
211
- --security-group " $sg_id " \
212
- --key-name " $key_name " \
213
- " $name " ) "
211
+ declare -a server_create_args
212
+ server_create_args=(
213
+ --block-device uuid=" $vol_id "
214
+ --image " $server_image "
215
+ --flavor " $server_flavor "
216
+ --nic " port-id=$port_id "
217
+ --security-group " $sg_id "
218
+ --key-name " $key_name "
219
+ )
220
+ if [ " $liveness " == ' yes' ]; then
221
+ server_create_args+=(--user-data " ${script_dir} /connectivity-test-cloud-init.yaml" )
222
+ fi
223
+ server_id=$( openstack server create --wait -f value -c id " ${server_create_args[@]} " " $name " )
214
224
# shellcheck disable=SC2086
215
225
server_id=" $( echo $server_id | tr -d ' \r' ) "
216
226
>&2 echo " Created server ${server_id} "
@@ -226,13 +236,13 @@ for driver in "${!drivers[@]}"; do
226
236
>&2 echo " Created loadbalancer ${lb_id} "
227
237
lb_ids+=(" ${lb_id} " )
228
238
229
- lb_listener_id=" $( openstack loadbalancer listener create --wait --name " $name " -f value -c id --protocol TCP --protocol-port 22 " $lb_id " ) "
239
+ lb_listener_id=" $( openstack loadbalancer listener create --wait --name " $name " -f value -c id --protocol TCP --protocol-port 80 " $lb_id " ) "
230
240
>&2 echo " Created loadbalancer listener ${lb_listener_id} "
231
241
232
242
lb_pool_id=" $( openstack loadbalancer pool create --wait --name " $name " -f value -c id --lb-algorithm " ${drivers[$driver]} " --listener " $lb_listener_id " --protocol TCP) "
233
243
>&2 echo " Created loadbalancer pool ${lb_pool_id} "
234
244
235
- lb_member_id=" $( openstack loadbalancer member create --wait -f value -c id --subnet-id " $subnet_id " --address " $server_ip " --protocol-port 22 " $lb_pool_id " ) "
245
+ lb_member_id=" $( openstack loadbalancer member create --wait -f value -c id --subnet-id " $subnet_id " --address " $server_ip " --protocol-port 80 " $lb_pool_id " ) "
236
246
>&2 echo " Created loadbalancer member ${lb_member_id} "
237
247
238
248
fip_id=" $( openstack floating ip create -f value -c id \
@@ -244,17 +254,29 @@ for driver in "${!drivers[@]}"; do
244
254
lb_vip_id=" $( openstack port show -f value -c id " ${ports[$driver]} " -" $lb_id " ) "
245
255
openstack floating ip set --port " $lb_vip_id " " $fip_id "
246
256
247
- if [ " $os_user " != ' ' ]; then
248
- echo " Testing connectivity from the instance ${name} "
249
- sleep 60
250
- if ! ssh -o ConnectTimeout=30 -o StrictHostKeyChecking=no " $os_user " @" $fip_address " ping -c 1 1.1.1.1; then
251
- echo " Error when running a ping from the instance. Dumping load balancer status..."
257
+ if [ " $liveness " == ' yes' ]; then
258
+ echo " Testing connectivity to and from the instance ${name} "
259
+
260
+ # N.B. We use a retry loop here rather than curl's retry
261
+ # options here because it can catch more types of failure. e.g.
262
+ # it can retry on 'No route to host' if the FIP hasn't
263
+ # propagated to the network hardware yet, which curl cannot.
264
+ start=$( date +%s)
265
+ backoff=1
266
+ while ! curl --fail-with-body http://" $fip_address " /; do
267
+ # This normally succeeds immediately, but we allow it up to
268
+ # 300 seconds.
269
+ if [ $(( $(date +% s)- start )) -gt 300 ]; then
270
+ echo " Error checking instance connectivity. Dumping load balancer status and console log."
252
271
openstack loadbalancer status show " $lb_id "
253
- echo " Error when running a ping from the instance. Dumping instance console..."
254
272
openstack console log show " $name " || true
255
273
echo " Done"
256
274
exit 1
257
- fi
275
+ fi
276
+ echo " Backing off for ${backoff} seconds"
277
+ sleep ${backoff}
278
+ backoff=$(( backoff * 2 ))
279
+ done
258
280
fi
259
281
done
260
282
0 commit comments