-
Notifications
You must be signed in to change notification settings - Fork 5
/
start-vm
executable file
·200 lines (178 loc) · 6.19 KB
/
start-vm
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
#!/bin/bash
set -e
export TERM=xterm-256color
ENV_DIR=./environments/vm
# argument parsing from https://stackoverflow.com/a/28466267
die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
needs_arg() { if [ -z "$OPTARG" ]; then die "No arg for --$OPT option"; fi; }
OPTERR=0
OPTPROCESSED=0
while getopts -- '-:h' OPT; do
OPTPROCESSED=$(( $OPTPROCESSED + 1 ))
# support long options: https://stackoverflow.com/a/28466267/519360
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
case "$OPT" in
h | help ) echo "Start SRAM VMs for local testing"
echo "Options:"
echo " --ci: for use in CI runs"
echo " --skip-ansible: only bring up VMs, skip Ansible run"
echo " --skip-vm: skip bringing up VMs, only run Ansible"
echo " --container: Use container-in-container based deploy"
echo " --no-mitogen: Disable use of mitogen"
echo " --stop: Stop all containers"
exit 0
;;
s | stop ) docker exec -ti docker-docker1-1 bash -c 'docker stop $(docker ps -q) || true'
docker exec -ti docker-docker2-1 bash -c 'docker stop $(docker ps -q) || true'
docker compose -f docker/docker-compose.yml stop
exit 0
;;
ci ) CI_OPTION="--ci"
ENV_DIR="./environments/ci"
;;
container ) IN_CONTAINER="--container"
ENV_DIR="./environments/docker"
;;
classic ) CLASSIC="1"
;;
skip-ansible | \
sa | no ) SKIP_ANSIBLE=1
;;
skip-docker | \
no-docker | \
skip-vm | \
no-vm | \
sd | sv | nd | nv ) SKIP_VM=1
;;
no-mitogen) export NO_MITOGEN=1
;;
* ) OPTPROCESSED=$(( $OPTPROCESSED - 1 ))
;;
esac
done
shift $OPTPROCESSED # remove parsed options and args from $@ list
if [ -z "$IN_CONTAINER" ] && [ -z "$CI_OPTION" ] && [ -z "$CLASSIC" ]; then
read -p "Continue without --container? [Yes/No] " yn
if [[ "$yn" == [Nn]* ]]; then
exit 0
fi
fi
if [ "$SKIP_VM" ]
then
echo "Skipping docker spin-up"
else
if [ ! -f docker/ansible_key.pub ]; then
echo "No ansible key found"
ssh-keygen -f docker/ansible_key -N ""
fi
# Build single docker image before docker-compose starts
if [ -z "$REENTRANT" ]; then
echo "Building docker image"
time docker build -t scz-base --target=scz-base docker/
fi
# Bring up the VMs if they're not running
echo "Bringing docker containers up"
echo "./docker/docker-compose.yml.py $CI_OPTION $IN_CONTAINER > ./docker/docker-compose.yml"
./docker/docker-compose.yml.py $CI_OPTION $IN_CONTAINER > ./docker/docker-compose.yml
time docker compose -f docker/docker-compose.yml up --no-recreate -d --remove-orphans
echo "Waiting until containers have booted"
TIMEOUT=120
for c in $(docker compose -f docker/docker-compose.yml ps -q)
do
host=$(docker inspect --format='{{.Config.Hostname}}' $c)
printf " - %-10s" $host
stat=xxx
starttime=$(date +%s)
while true
do
if [ $(( $(date +%s) - $starttime )) -gt $TIMEOUT ]
then
tput setaf 1
echo "timeout"
tput sgr0
exit 1
fi
stat=$(docker inspect --format='{{.State.Health.Status}}' $c)
if [ "$stat" = "healthy" ]
then
tput setaf 2
echo -n "up "
tput sgr0
break
fi
echo -n "."
sleep 2
done
# wait until ssh is running
if [ "$host" != "mail" ] && [ "$host" != "redis" ]
then
echo -n "."
starttime=$(date +%s)
while ! ssh-keyscan -T 1 ${host}.vm.scz-vm.net > /dev/null 2>&1
do
if [ $(( $(date +%s) - $starttime )) -gt $TIMEOUT ]
then
tput setaf 1
echo "timeout"
tput sgr0
ssh-keyscan -T 1 ${host}.vm.scz-vm.net || true
exit 1
fi
sleep 2
echo -n "."
done
tput setaf 2
echo -n "ssh ok"
tput sgr0
fi
echo
done
fi
# try to find mitogen
MITOGEN_PATH=
MITOGEN_SUBPATH=ansible_mitogen/plugins/strategy
for p in ./mitogen /opt/mitogen /usr/local/mitogen /usr/mitogen ~/.mitogen ~/mitogen /usr/lib/python3/dist-packages/
do
if [ -d "$p/$MITOGEN_SUBPATH/" ]
then
MITOGEN_PATH=$p
break
fi
done
if [ -n "$MITOGEN_PATH" ] && [ "$NO_MITOGEN" != "1" ]
then
echo "Found Mitogen at $MITOGEN_PATH"
export ANSIBLE_STRATEGY_PLUGINS=$MITOGEN_PATH/$MITOGEN_SUBPATH/
export ANSIBLE_STRATEGY=mitogen_linear
else
echo "Mitogen not found"
fi
ANSIBLE_VAULT_ARG=
if [ -e "secret_vm" ]
then
ANSIBLE_VAULT_ARG="--vault-password-file=secret_vm"
fi
# skip SBS download on reentrant runs, do we don't accidentally fetch a new SBS version that was updated betweens runs
if [ $REENTRANT ]
then
EXTRA_VARS="--extra-vars=sbs_skip_download=true"
fi
if [ "$SKIP_ANSIBLE" ]
then
echo "Skipping ansible run"
else
ansible-galaxy collection install --upgrade -r requirements.yml || ansible-galaxy collection install --force -r requirements.yml
echo "Starting ansible"
export ANSIBLE_FORCE_COLOR=True
time \
ansible-playbook provision.yml \
-i $ENV_DIR/inventory \
$ANSIBLE_VAULT_ARG \
$EXTRA_VARS \
"$@"
fi
exit 0