Skip to content

Commit a82c271

Browse files
committed
Reworked the check image script (now checking hostname).
1 parent de3083d commit a82c271

File tree

1 file changed

+130
-90
lines changed

1 file changed

+130
-90
lines changed

examples/image-quick-checks.sh

Lines changed: 130 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,153 @@
11
#!/usr/bin/env bash
22

3-
# I'm a script used to check the state of images.
3+
# I'm a script used to check some basic operations with images.
44

5-
# parameters
5+
# Params
66
if [ $# -ne 1 ]; then
77
echo "usage: $0 image-id"
88
exit 1
99
fi
1010

11-
IMAGE_ID=$1
12-
NB_INSTANCES=16
11+
# Globals
12+
13+
IMAGE_UUID=$1
14+
IMAGE_NAME=""
15+
SERVER_UUID=""
16+
SERVER_NAME=""
1317
WORKDIR=$(mktemp -d 2>/dev/null || mktemp -d -t /tmp)
14-
INSTANCE_NAME='check-image'
1518

16-
# destroy all existing servers matching name
17-
function cleanup {
18-
echo >&2 '[+] cleaning up existing servers...'
19-
for uuid in $(scw ps -a --no-trunc | tail -n +2 | awk '// { print $1, $NF; }' | grep "^.* ${INSTANCE_NAME}\-" | awk '// { print $1; }'); do
20-
scw stop -t $uuid
21-
done
22-
23-
touch $WORKDIR/uuids.txt
24-
touch $WORKDIR/ips.txt
25-
}
26-
27-
# create $NB_INSTANCES servers using the image
28-
function boot {
29-
echo >&2 "[+] creating $NB_INSTANCES servers..."
30-
for i in $(eval echo {1..$NB_INSTANCES}); do
31-
scw create --volume 1G --name "$INSTANCE_NAME-$i" $IMAGE_ID >> $WORKDIR/uuids.txt
32-
done
33-
cat $WORKDIR/uuids.txt
34-
35-
echo >&2 "[+] booting $NB_INSTANCES servers..."
36-
for uuid in $(cat $WORKDIR/uuids.txt); do
37-
scw start -s --boot-timeout=120 --ssh-timeout=600 $uuid &
38-
done
39-
wait `jobs -p`
40-
41-
echo >&2 "[+] fetching IPs..."
42-
for uuid in $(cat $WORKDIR/uuids.txt); do
43-
scw inspect $uuid | grep address | awk '// { print $2; }' | tr -d '"' | awk '// { print $1; }' >> $WORKDIR/ips.txt
44-
done
45-
}
46-
47-
# run several tests and output a Markdown report
48-
function report {
49-
# status
50-
echo >&2 "[+] report status"
51-
echo "## Status of instances"
52-
echo ""
53-
NB_INSTANCES_OK=$(wc -l $WORKDIR/ips.txt | awk '// { print $1; }')
54-
echo "- $NB_INSTANCES_OK / $NB_INSTANCES have correctly booted"
55-
echo ""
19+
# Printing helpers
5620

57-
# fping
58-
echo >&2 "[+] report fping"
59-
echo "## fping"
60-
echo ""
61-
fping $(cat $WORKDIR/ips.txt) | sed 's/\(.*\)/ \1/' > $WORKDIR/fping
62-
NB_INSTANCES_OK=$(wc -l $WORKDIR/fping | awk '// { print $1; }')
63-
echo "- $NB_INSTANCES_OK / $NB_INSTANCES respond to ping"
64-
echo ""
65-
cat $WORKDIR/fping
66-
echo ""
21+
function _log_msg {
22+
echo -ne "${*}" >&2
23+
}
6724

68-
# reboot
69-
echo >&2 "[+] reboot"
70-
echo "## reboot"
71-
echo ""
72-
for uuid in $(cat $WORKDIR/uuids.txt); do
73-
scw exec --wait --timeout 60 $uuid '(which systemctl &>/dev/null && systemctl reboot) || reboot'
74-
done
25+
function einfo {
26+
_log_msg "\033[1;36m>>> \033[0m${*}\n"
27+
}
28+
29+
function echeck {
30+
einfo $@
7531
echo ""
32+
}
7633

77-
sleep 120
34+
function esuccess {
35+
_log_msg "\033[1;32m>>> \033[0m${*}\n"
36+
}
7837

79-
# fping
80-
echo >&2 "[+] report fping 120 sec after reboot"
81-
echo "## fping after reboot"
82-
echo ""
83-
fping $(cat $WORKDIR/ips.txt) | sed 's/\(.*\)/ \1/' > $WORKDIR/fping
84-
NB_INSTANCES_OK=$(wc -l $WORKDIR/fping | awk '// { print $1; }')
85-
echo "- $NB_INSTANCES_OK / $NB_INSTANCES respond to ping"
86-
echo ""
87-
cat $WORKDIR/fping
88-
echo ""
38+
function ewarn {
39+
_log_msg "\033[1;33m>>> \033[0m${*}\n"
40+
}
8941

90-
# uptime
91-
echo >&2 "[+] uptime"
92-
echo "## uptime"
93-
echo ""
94-
for uuid in $(cat $WORKDIR/uuids.txt); do
95-
scw exec --wait --timeout 600 $uuid 'uptime' 1>&2
96-
failed=$?
97-
if [ $failed -ne 0 ]
98-
then
99-
echo " - $uuid is DOWN"
100-
else
101-
echo " - $uuid is UP"
102-
fi
103-
done
104-
echo ""
42+
function eerror {
43+
_log_msg "\033[1;31m>>> ${*}\033[0m\n"
44+
}
45+
46+
function eedie_on_error {
47+
failed=$1
48+
if [ $failed -ne 0 ]
49+
then
50+
eerror $2
51+
cleanup
52+
exit 1
53+
fi
10554
}
10655

56+
# Super helper to print server output to stdout
57+
58+
function watch_server {
59+
einfo "Attaching to server $SERVER_UUID"
60+
mkfifo ${WORKDIR}/stop_watching_server
61+
scw attach $SERVER_UUID 2>/dev/null &
62+
killme=$!
63+
sleep 5
64+
(
65+
cat ${WORKDIR}/stop_watching_server
66+
kill -9 $killme
67+
echo ""
68+
) &>/dev/null &
69+
}
70+
71+
function stop_watching {
72+
echo "" > ${WORKDIR}/stop_watching_server
73+
rm -f ${WORKDIR}/stop_watching_server
74+
sleep 1
75+
}
76+
77+
# Checks
78+
79+
function check_image {
80+
echeck "Checking image..."
81+
IMAGE_NAME=$(scw inspect -f '{{ .Name }}' $IMAGE_UUID 2> /dev/null)
82+
eedie_on_error $? "Unable to find image behind $IMAGE_UUID"
83+
esuccess "Image name is $IMAGE_NAME"
84+
SERVER_NAME="qa-image-$$"
85+
}
86+
87+
function check_create {
88+
echeck "Checking server creation with image..."
89+
SERVER_UUID=$(scw create --name "$SERVER_NAME" $IMAGE_UUID)
90+
eedie_on_error $? "Unable to create server with image $IMAGE_NAME"
91+
esuccess "Created server $SERVER_UUID with image $IMAGE_NAME"
92+
}
93+
94+
function check_boot {
95+
echeck "Checking server boot (can take up to 300 seconds)..."
96+
watch_server $SERVER_UUID
97+
scw start -w -T 300 $SERVER_UUID &> /dev/null
98+
eedie_on_error $? "Unable to boot server $SERVER_UUID"
99+
stop_watching
100+
esuccess "Server $SERVER_UUID properly boots"
101+
}
102+
103+
function check_hostname {
104+
echeck "Checking server hostname..."
105+
HOSTNAME=$(scw exec $SERVER_UUID hostname)
106+
eedie_on_error $? "Unable to fetch hostname from server $SERVER_UUID"
107+
if [ "$HOSTNAME" -ne "$SERVER_NAME" ]
108+
then
109+
ewarn "Server hostname is *NOT* properly set (expected=${SERVR_NAME}, found=${HOSTNAME})"
110+
else
111+
esuccess "Server hostname is properly set"
112+
fi
113+
}
114+
115+
function check_reboot {
116+
echeck "Checking reboot..."
117+
watch_server
118+
scw exec $SERVER_UUID reboot &> /dev/null
119+
scw exec -T 300 -w $SERVER_UUID uptime
120+
stop_watching
121+
eedie_on_error $? "Unable to reboot server $SERVER_NAME"
122+
esuccess "Server $SERVER_NAME properly reboots"
123+
}
124+
125+
# Cleanup
126+
127+
function cleanup {
128+
if [ "$SERVER_UUID" != "" ]
129+
then
130+
einfo "Cleaning up server $SERVER_UUID"
131+
scw stop -t $SERVER_UUID &> /dev/null
132+
scw rm $SERVER_UUID &> /dev/null
133+
SERVER_UUID=""
134+
fi
135+
stop_watching
136+
einfo "Cleaning up temporary directory $WORKDIR"
137+
rm -rf $WORKDIR
138+
kill -9 $(jobs -p)
139+
einfo "Killed all survivor processes"
140+
}
141+
142+
# Main
143+
107144
function main {
145+
check_image
146+
check_create
147+
check_boot
148+
check_hostname
149+
check_reboot
108150
cleanup
109-
boot
110-
report
111151
}
112152

113153
main

0 commit comments

Comments
 (0)