-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrunit.sh
executable file
·395 lines (342 loc) · 11.7 KB
/
runit.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/bin/bash
# whether do deploy single node or three node high availability MaaS.
HA=false # single infra node
# whether to use proxy
PROXY=false
PROXY_HTTP="http://100.107.0.4:1080"
PROXY_HTTPS="http://100.107.0.4:1080"
# location of log file
LOG=./output.txt
# directory where qcows2 files for VMs will be located
VMs=~/VMs
# There is no big need to change these variables unless you want different IPs
MY_IP=192.168.210.1
INFRA1=192.168.210.4
INFRA2=192.168.210.5
INFRA3=192.168.210.6
CIDR=192.168.210.0/24
CIDR_bits=24
GW=192.168.210.1
# logging commands and their output
# $1 is a command to execute
logit() {
echo "***************************************************************************" >> $LOG 2>&1
date >> $LOG 2>&1
echo $1 >> $LOG 2>&1
eval ${1} >> $LOG 2>&1
uptime >> $LOG 2>&1
echo "" >> $LOG 2>&1
}
info() {
echo -n $1
}
ok() {
GREEN='\033[0;32m'
NC='\033[0m' # No Color
printf "${GREEN}OK${NC}\n"
}
wait_for_ping() {
result=1
while [ $result -eq 1 ]; do
sleep 5
ping -c 1 ${1} >> $LOG 2>&1
result=$?
done
}
wait_for_ssh() {
result=1
while [ $result -ne 0 ]; do
sleep 5
nc -zvw3 ${1} 22 >> $LOG 2>&1
result=$?
done
}
echo
echo "******************************************************************************"
echo
echo -n "Going to deploy "
if [ "$HA" = true ] ; then
echo "3 infra nodes"
else
echo "1 infra node"
fi
echo
if [ "$PROXY" = true ] ; then
echo "Going to use proxy"
echo " HTTP_PROXY=${PROXY_HTTP}"
echo " HTTPS_PROXY=${PROXY_HTTPS}"
else
echo "No proxy will be configured"
fi
echo
echo "******************************************************************************"
echo
cd
logit "echo \"*** Start ***\""
info "Checking user..."
if [ ${USER} != 'ubuntu' ]; then
echo "Script must run under user ubuntu"
logit "echo This is just a dumb script which assumes it runs under user ubuntu"
return 1
fi
ok
info "Creating ${VMs}..."
mkdir -p ${VMs}
ok
if [ "$PROXY" = true ] ; then
info "Setting proxy in /etc/environment..."
cat <<EOF | sudo tee -a /etc/environment >> $LOG 2>&1
http_proxy="${PROXY_HTTP}"
https_proxy="${PROXY_HTTPS}"
no_proxy=127.0.0.1
EOF
ok
info "Restarting snapd..."
sudo systemctl restart snapd
ok
fi
# install needed packages
logit "echo \"*** packages ***\""
info "Installing necessary packages..."
sudo apt install bridge-utils libvirt-bin qemu-utils virtinst qemu-kvm bind9 bind9utils cloud-image-utils -y >> $LOG 2>&1
sleep 30
ok
logit "echo \"*** Bind ***\""
info "Configuring bind9..."
cat <<EOF | sudo tee /etc/bind/named.conf.options >> $LOG 2>&1
options {
directory "/var/cache/bind";
forwarders {
127.0.0.53;
};
dnssec-validation no;
auth-nxdomain no; # conform to RFC1035
listen-on {${GW};};
listen-on-v6 { any; };
};
EOF
ok
info "Restarting bind9..."
sudo systemctl restart bind9
ok
if [[ $(lscpu | grep Intel) ]]; then
CPU=intel
elif [[ $(lscpu | grep AMD) ]]; then
CPU=amd
else
logit "Could not detect processor type"
return 1
fi
# it happened ONCE nested KVM was not allowed, so these three lines are just in case
sudo modprobe -r kvm_$CPU
sudo modprobe kvm_$CPU nested=1
echo "options kvm_$CPU nested=1" | sudo tee -a /etc/modprobe.d/kvm.conf >> $LOG 2>&1
logit "cat /sys/module/kvm_$CPU/parameters/nested"
# create maasbr0 and setup iptable for NAT and forward
logit "echo \"*** networking ***\""
info "Defining and configuring maasbr0 bridge..."
sudo brctl addbr maasbr0
sudo ip a add ${GW}/${CIDR_bits} dev maasbr0
sudo ip l set maasbr0 up
ok
info "Setting iptables..."
sudo iptables -t nat -A POSTROUTING -s ${CIDR} ! -d ${CIDR} -m comment --comment "network maasbr0" -j MASQUERADE
sudo iptables -t filter -A INPUT -i maasbr0 -p tcp -m tcp --dport 53 -m comment --comment "network maasbr0" -j ACCEPT
sudo iptables -t filter -A INPUT -i maasbr0 -p udp -m udp --dport 53 -m comment --comment "network maasbr0" -j ACCEPT
sudo iptables -t filter -A FORWARD -o maasbr0 -m comment --comment "network maasbr0" -j ACCEPT
sudo iptables -t filter -A FORWARD -i maasbr0 -m comment --comment "network maasbr0" -j ACCEPT
ok
info "Generating ssh keypair..."
# generate keypair
printf 'y\n'|ssh-keygen -t rsa -f ~/.ssh/id_rsa -t rsa -N '' >> $LOG 2>&1
ok
info "Generating ssh keypair for infras..."
# generate keypair
printf 'y\n'|ssh-keygen -t rsa -f id_rsa_persistent -t rsa -N '' >> $LOG 2>&1
cat id_rsa_persistent.pub >> ~/.ssh/authorized_keys
ok
PUBKEY=$(cat ~/.ssh/id_rsa.pub)
logit "echo \"*** create define_infra script ***\""
info "Creating define_infra script..."
cat <<EOF | tee define_infra.sh >> $LOG 2>&1
#!/bin/bash
# \$1 name
# \$2 IP
cat <<EOF1 | tee ci_userdata_\${1}
#cloud-config
hostname: \${1}
users:
- name: ubuntu
sudo: ALL=(ALL) NOPASSWD:ALL
home: /home/ubuntu
shell: /bin/bash
groups: [adm, audio, cdrom, dialout, floppy, video, plugdev, dip, netdev, libvirtd]
lock_passwd: True
gecos: Ubuntu
ssh_authorized_keys:
- ${PUBKEY}
package_update: true
package_upgrade: true
ssh_authorized_keys:
- ${PUBKEY}
packages:
- bridge-utils
- qemu-kvm
- libvirt-bin
power_state:
mode: reboot
write_files:
- encoding: b64
owner: root:root
permissions: '0644'
path: /etc/ssh/id_rsa_persistent
content: $(base64 -w0 ~/id_rsa_persistent)
- encoding: b64
owner: root:root
permissions: '0644'
path: /etc/ssh/id_rsa_persistent.pub
content: $(base64 -w0 ~/id_rsa_persistent.pub)
runcmd:
- systemctl disable cloud-init.service
- systemctl disable cloud-init-local.service
- systemctl disable cloud-final.service
- systemctl disable cloud-config.service
- echo " StrictHostKeyChecking no\\n" >> /etc/ssh/ssh_config
- echo "Host ${MY_IP}\\n" >> /etc/ssh/ssh_config
- echo " IdentityFile /etc/ssh/id_rsa_persistent\\n" >> /etc/ssh/ssh_config
EOF1
cat <<EOF2 | tee ci_network_\${1}
version: 2
ethernets:
ens3:
dhcp4: false
bridges:
broam:
dhcp4: false
interfaces: [ ens3 ]
addresses: [\${2}/${CIDR_bits}]
gateway4: ${GW}
nameservers:
addresses: [${GW}]
parameters:
stp: false
forward-delay: 0
EOF2
cp ubuntu-18.04-server-cloudimg-amd64.img ${VMs}/\${1}-d1.qcow2
qemu-img resize ${VMs}/\${1}-d1.qcow2 50G
cloud-localds -d raw -f iso -m local -H \${1} -N ci_network_\${1} \${1}-cloudinit.iso ci_userdata_\${1}
virt-install --print-xml --noautoconsole --virt-type kvm --boot hd,menu=off --name \${1} --ram 8192 --vcpus 4 --cpu host-passthrough,cache.mode=passthrough --graphics vnc --video=cirrus --os-type linux --os-variant ubuntu18.04 --controller scsi,model=virtio-scsi,index=0 --disk format=qcow2,bus=scsi,cache=writeback,path=${VMs}/\${1}-d1.qcow2 --disk device=cdrom,path=\${1}-cloudinit.iso --network=bridge=maasbr0,mac=\$(date +"18:%y:%m:%H:%M:%S"),model=virtio > \${1}.xml
virsh define \${1}.xml
virsh start \${1}
EOF
ok
logit "cat define_infra.sh"
chmod +x define_infra.sh
logit "Downloading cloud image"
info "Downloading cloud image..."
wget -t3 -c --progress=dot:giga https://cloud-images.ubuntu.com/releases/18.04/release/ubuntu-18.04-server-cloudimg-amd64.img >> $LOG 2>&1
ok
if [ "$HA" = true ] ; then
INFRAS="${INFRA1} ${INFRA2} ${INFRA3}"
else
INFRAS="${INFRA1}"
fi
# call the script to create one to three infra nodes
logit "echo \"*** define infras ***\""
logit "echo \"*** define infra1 ***\""
info "Defining infra1, this will take couple of minutes..."
sudo ./define_infra.sh infra1 ${INFRA1} >> $LOG 2>&1
logit "echo return code $?"
ok
if [ "$HA" = true ] ; then
logit "echo \"*** define infra2 ***\""
info "Defining infra2, this will take couple of minutes..."
sudo ./define_infra.sh infra2 ${INFRA2} >> $LOG 2>&1
logit "echo return code $?"
ok
logit "echo \"*** define infra3 ***\""
info "Defining infra3, this will take couple of minutes..."
sudo ./define_infra.sh infra3 ${INFRA3} >> $LOG 2>&1
logit "echo return code $?"
ok
fi
for i in ${INFRAS} ; do info "Waiting for a successfull ping to ${i}..."; wait_for_ping ${i}; ok ; done
for i in ${INFRAS} ; do info "Waiting for sshd start on ${i}..."; wait_for_ssh ${i}; ok ; done
# setup proxy
if [ "$PROXY" = true ] ; then
logit "echo Adding proxy to infras"
info "Adding proxy to infras..."
for i in ${INFRAS} ; do echo http_proxy=\"${PROXY_HTTP}\"|ssh -o StrictHostKeyChecking=no ${i} "cat - |sudo tee -a /etc/environment"; done >> $LOG 2>&1
for i in ${INFRAS} ; do echo https_proxy=\"${PROXY_HTTPS}\"|ssh -o StrictHostKeyChecking=no ${i} "cat - |sudo tee -a /etc/environment"; done >> $LOG 2>&1
ok
fi
# setup ssh keys as needed
info "Setting ssh stuff..."
logit "echo allow connection from host to ubuntu on infras"
logit "echo get ubuntu public key from infras"
# get ubuntu public key from infras
for i in ${INFRAS} ; do ssh -o StrictHostKeyChecking=no ${i} "printf 'y\n'|ssh-keygen -t rsa -f /home/ubuntu/.ssh/id_rsa -t rsa -N '' >>/dev/null 2>&1 ; cat /home/ubuntu/.ssh/id_rsa.pub"; done > ubuntukeyinfra
logit "echo allow ubuntu from infras to logon to host"
# allow ubuntu from infras to logon to host
cat ubuntukeyinfra >> ~/.ssh/authorized_keys
logit "echo establish first conection from infras to host so that it does not ask next time"
# establish first conection from infras to host so that it does not ask next time
for i in ${INFRAS} ; do ssh ${i} "printf 'yes\n'|ssh -o StrictHostKeyChecking=no ubuntu@${GW} hostname"; done >> $LOG 2>&1
ok
# define VMs for FCE
logit "echo \"*** define VMs ***\""
cat <<EOF |tee define_VMs.sh >> $LOG 2>&1
#!/bin/bash
define() {
# \$1 name
# \$2 id
# \$3 memory
# \$4 - \$8 unique MAC
CPUOPTS="--cpu host"
GRAPHICS="--graphics vnc --video=cirrus"
CONTROLLER="--controller scsi,model=virtio-scsi,index=0"
DISKOPTS="format=qcow2,bus=scsi,cache=writeback"
export CPUOPTS GRAPHICS CONTROLLER DISKOPTS
qemu-img create -f qcow2 ${VMs}/\${1}\${2}d1.qcow2 60G
qemu-img create -f qcow2 ${VMs}/\${1}\${2}d2.qcow2 20G
qemu-img create -f qcow2 ${VMs}/\${1}\${2}d3.qcow2 20G
virt-install --noautoconsole --print-xml --boot network,hd,menu=on \
\$GRAPHICS \$CONTROLLER --name \${1}\${2} --ram \$3 --vcpus 2 \$CPUOPTS \
--disk path=${VMs}/\${1}\${2}d1.qcow2,size=60,\$DISKOPTS \
--disk path=${VMs}/\${1}\${2}d2.qcow2,size=20,\$DISKOPTS \
--disk path=${VMs}/\${1}\${2}d3.qcow2,size=20,\$DISKOPTS \
--network=bridge=maasbr0,mac=18:\${5}:\${6}:\${7}:\${8}:1\${2},model=virtio \
--network=bridge=maasbr0,mac=18:\${5}:\${6}:\${7}:\${8}:2\${2},model=virtio \
--network=bridge=maasbr0,mac=18:\${5}:\${6}:\${7}:\${8}:3\${2},model=virtio \
--network=bridge=maasbr0,mac=18:\${5}:\${6}:\${7}:\${8}:4\${2},model=virtio \
--network=bridge=maasbr0,mac=18:\${5}:\${6}:\${7}:\${8}:5\${2},model=virtio \
> \${1}\${2}.xml
virsh define \${1}\${2}.xml
}
for i in \$(seq 1 9); do
define fe \${i} 4096 \$(date +"%y %m %H %M %S")
done
EOF
logit "cat define_VMs.sh"
info "Creating VMs..."
chmod +x define_VMs.sh
# sudo becasue some weird change in libvirt in 18.04
sudo ./define_VMs.sh >> $LOG 2>&1
logit "echo return code $?"
sleep 10
ok
logit "ls -l"
ssh-keyscan -H ${INFRA1} >> ~/.ssh/known_hosts >> $LOG 2>&1
if [ "$HA" = true ] ; then
ssh-keyscan -H ${INFRA2} >> ~/.ssh/known_hosts >> $LOG 2>&1
ssh-keyscan -H ${INFRA3} >> ~/.ssh/known_hosts >> $LOG 2>&1
fi
echo "******************************************************************************"
virsh list --all
echo
echo "*******************************************************************************************************"
echo "* When you clone cpe-deployments run *"
echo "* git config user.name \"Chuck Norris\"; git config user.email [email protected] *"
echo "* there *"
echo "*******************************************************************************************************"
echo