-
Notifications
You must be signed in to change notification settings - Fork 4
/
build-final.sh
executable file
·97 lines (80 loc) · 3.19 KB
/
build-final.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
#!/bin/bash
set -xeuo pipefail
# Default group for the VM in ansible. This lets you use group_vars/$VARIANT for site specific configuration
VARIANT=${1:-all}
SSHPORT=2222
SSH_BUILD_KEY="configs/imageadmin-ssh_key"
SSH_ICPCADMIN_KEY="files/secrets/icpcadmin@contestmanager"
PIDFILE="tmp/qemu.pid"
ALIVE=0
IMGFILE="output/$(date +%Y-%m-%d)_image-amd64.img"
if [[ $IMGFILE != 'all' ]]; then
IMGFILE="output/$VARIANT-$(date +%Y-%m-%d)_image-amd64.img"
fi
BASEIMG="base-amd64.img"
# Copy to a raw disk image
qemu-img convert -O raw output/$BASEIMG $IMGFILE
function runssh() {
ssh -i $SSH_BUILD_KEY -o BatchMode=yes -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes imageadmin@localhost -p$SSHPORT $@ 2>/dev/null
}
function cleanup() {
if [ $ALIVE -eq 1 ]; then
echo "Attempting graceful shutdown"
runssh sudo poweroff
else
echo "Forcing shutdown(poweroff)"
kill "$(cat $PIDFILE)"
fi
rm -f $PIDFILE
}
function waitforssh() {
# wait for it to boot
echo -n "Waiting for ssh "
TIMEOUT=60
X=0
while [[ $X -lt $TIMEOUT ]]; do
let X+=1
set +e
OUT=$(ssh -i $SSH_BUILD_KEY -o BatchMode=yes -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes imageadmin@localhost -p$SSHPORT echo "ok" 2>/dev/null)
set -e
if [[ "$OUT" == "ok" ]]; then
ALIVE=1
break
fi
echo -n "."
sleep 1
done
echo ""
if [ $ALIVE -eq 0 ]; then
echo "Timed out waiting for host to respond"
cleanup
exit 1
else
echo "Host is alive! You can ssh in now"
fi
}
qemu-system-x86_64 -smp 1 -m 1024 -drive file="$IMGFILE",index=0,media=disk,format=raw -global isa-fdc.driveA= --enable-kvm -net user,hostfwd=tcp::$SSHPORT-:22 -net nic --daemonize --pidfile $PIDFILE -vnc :0 -vga qxl -spice port=5901,disable-ticketing -usbdevice tablet
ALIVE=0
waitforssh
echo "Running ansible"
INVENTORY_FILE=$(mktemp)
cat <<EOF > $INVENTORY_FILE
vm ansible_port=$SSHPORT ansible_host=127.0.0.1
[$VARIANT]
vm
EOF
ANSIBLE_HOST_KEY_CHECKING=False time ansible-playbook -i $INVENTORY_FILE --ssh-extra-args="-o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --diff --become -u imageadmin --private-key $SSH_BUILD_KEY main.yml
rm -f $INVENTORY_FILE
ssh -i $SSH_ICPCADMIN_KEY -o BatchMode=yes -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes icpcadmin@localhost -p$SSHPORT sudo reboot
# Wait 5 seconds for reboot to happen so we don't ssh back in before it actually reboots
sleep 5
ALIVE=0
waitforssh
echo "Preparing image for distribution"
set -x
ssh -i $SSH_ICPCADMIN_KEY -o BatchMode=yes -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes icpcadmin@localhost -p$SSHPORT sudo bash -c "/icpc/scripts/makeDist.sh"
ssh -i $SSH_ICPCADMIN_KEY -o BatchMode=yes -o ConnectTimeout=1 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes icpcadmin@localhost -p$SSHPORT sudo shutdown --poweroff --no-wall +1
# Dig holes in the file to make it sparse (i.e. smaller!)
fallocate -d $IMGFILE
echo "Image file created: $IMGFILE($(du -h $IMGFILE | cut -f1))"
exit 0