forked from kronosnet/ci-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci-setup-anvil-bm-vm-cache
executable file
·160 lines (134 loc) · 4.51 KB
/
ci-setup-anvil-bm-vm-cache
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
#!/bin/bash
set -e
. $HOME/ci-tools/ci-setup-anvil-vm-env
TODAY=$(date +%F)
if [ -f "$DEPLOYDIR/cache/${BUILD}-latest.qcow2-${TODAY}" ]; then
echo "Cache already updated"
exit 0
fi
for i in $ALLNODES $CACHENODES; do
destroy_vm $i
done
cd "$DEPLOYDIR/images"
rsync -av --progress anvil-ci-proxy:ci-cache/* .
if [ -n "$SRCURL" ]; then
echo "Downloading latest $NAME cloud image"
latestimage=$(lftp -e "set ssl:verify-certificate no; cd "$SRCPATH"; cls -1 *${IMG}*.qcow2; quit" "$SRCURL" 2>/dev/null | sort -u | grep -v "\-latest" | tail -n 1)
if [ -z "$latestimage" ]; then
echo "Error determining latest $NAME cloud image"
if [ ! -e "${BUILD}-latest.qcow2" ]; then
echo "No local image found. ABORTING"
exit 1
fi
fi
if [ ! -f "$latestimage" ]; then
wget -4 -O "$latestimage" "$SRCURL/$SRCPATH/$latestimage"
rm -f "${BUILD}-latest.qcow2"
ln -sf "$latestimage" "${BUILD}-latest.qcow2"
fi
else
if [ ! -h "${BUILD}-latest.qcow2" ]; then
echo "Unable to find ${BUILD}-latest.qcow2 image in $DEPLOYDIR/images"
exit 1
fi
fi
# update daily cache
# some code is similar to the one below, but it´s not the same
echo "Generating daily cache"
cd "$DEPLOYDIR/cache"
echo "Updating daily cache for ${BUILD} ${TODAY}"
echo "Destroying VMs (if any)"
rm -rf ${BUILD}-latest.qcow2* an-cache*
qemu-img create -b "$DEPLOYDIR/images/${BUILD}-latest.qcow2" -F qcow2 -f qcow2 "${BUILD}-latest.qcow2" 200G
cat "$TEMPLATESDIR/anvil_striker_net_config.cfg" | sed -e "s#@IFNIP@#192.168.122.2#g" > an-cache_net_config.cfg
cat "$TEMPLATESDIR/anvil_cloud_init_${BUILD}.cfg" | \
sed \
-e "s#@HOSTNAME@#an-cache#g" \
-e "s#@BUILD@#${BUILD}#g" \
-e "s#@SSHKEY@#${SSHKEY}#g" \
-e "s#@KEY@#${KEY}#g" \
-e "s#@ORG@#${ORG}#g" \
-e "s#@AKEY@#${AKEY}#g" \
-e "s#@PRNUM@#${PRNUM}#g" \
-e "s#@PRREPO@#${PRREPO}#g" \
-e "s#@ANVIL@#false#g" \
-e "s#@ANVILDEPS@#false#g" \
-e "s#@ALTEEVE@#false#g" \
-e "s#@ALTEEVESTAGING@#false#g" \
-e "s#@ANVILPKG@##g" \
> "an-cache_cloud_init.cfg"
rm -f "an-cache-seed.img"
echo "instance-id: $(uuidgen)" > "an-cache-metadata"
cloud-localds -v --network-config="an-cache_net_config.cfg" "an-cache-seed.img" "an-cache_cloud_init.cfg" "an-cache-metadata"
virt-install --name an-cache \
--virt-type kvm --memory 16384 --vcpus 6 \
--boot hd,menu=on \
--disk path="$DEPLOYDIR/cache/an-cache-seed.img",device=cdrom \
--disk path="$DEPLOYDIR/cache/${BUILD}-latest.qcow2",device=disk \
--graphics vnc \
--os-variant $KVMVAR \
--network network:ifn1_bridge1,model=e1000 \
--noautoconsole
echo "Waiting for nodes and cloud-init to complete full init"
bootdone=no
expected=1
error=0
while [ "$bootdone" = "no" ]; do
actual=0
for i in $CACHENODES; do
status=$(ssh $i cloud-init status 2>/dev/null | awk '{print $NF}')
echo "cloud-init status node $i: $status"
if [ "$status" = "error" ]; then
error=1
expected=$((expected - 1))
echo "ERROR cloud init log for node $i:"
ssh $i cat /var/log/cloud-init-output.log
echo "---------------------------"
fi
if [ "$status" = "done" ]; then
actual=$((actual + 1))
fi
done
echo "$(date) actual: $actual expected: $expected"
if [ "$actual" = "$expected" ]; then
bootdone=yes
else
sleep 10
fi
done
if [ "$error" = "1" ]; then
destroy_vm an-cache
exit 1
fi
# add c9s dvd iso for caching
ssh $CACHENODES mkdir -p /mnt/shared/files
scp "$DEPLOYDIR/images/CentOS-Stream-9-latest-x86_64-dvd1.iso" $CACHENODES:/mnt/shared/files/CentOS-Stream-9-latest-x86_64-dvd1.iso
# cleanup image
# centos-9-stream cloud init workaround
ssh $CACHENODES "grep -q yum_add_repo /etc/cloud/cloud.cfg || sed -i 's/cloud_config_modules:/&\n - yum_add_repo/' /etc/cloud/cloud.cfg"
# allow cloud-init to regenerate anvil/alteeve repos
ssh $CACHENODES "rm -f /etc/yum.repos.d/alteeve* /etc/yum.repos.d/anvil*"
# extra cleaning after cloud-init
ssh $CACHENODES cloud-init clean
ssh $CACHENODES "rm -f /etc/sysconfig/network-scripts/ifcfg-eth*"
ssh $CACHENODES subscription-manager unregister || true
state=running
virsh shutdown an-cache
loop=0
while ([ "$state" != "" ] && [ "$state" == "running" ]); do
sleep 5
state=$(virsh dominfo an-cache | grep -w "State:" | awk '{ print $2}')
loop=$((loop + 1))
# wait 2 minutes
if [ "$loop" = "24" ]; then
echo "an-cache did not shutdown after 2 minutes!"
virsh destroy an-cache || true
fi
done
virsh undefine an-cache || true
touch "${BUILD}-latest.qcow2-${TODAY}"
# repeat to make sure known_hosts is clean after caching
rm -f $HOME/.ssh/known_hosts
ssh-keyscan github.com > $HOME/.ssh/known_hosts
echo "Done updating daily cache"
exit 0