-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm-cluster.sh
executable file
·301 lines (260 loc) · 6.88 KB
/
vm-cluster.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
#!/bin/bash
debuglevel=0
if [ ! -z ${VM_CLUSTER_DEBUG} ];then
debuglevel=1
fi
if [ -z ${VM_CLUSTER_CONF} ]; then
dir=$(pwd)
else
dir=${VM_CLUSTER_CONF}
fi
source $dir/vars_default.sh
cn_init=0 ## 1 when the cluster is first setup, automatically changed by the script
debug(){
if [ ${debuglevel} == 1 ]; then
echo "debug info: ${1}"
fi
}
prepare_bridge(){
ip link add name $1 type bridge
ip link set $1 up
ip addr add ${ip24}.1/24 dev $1
debug "finish setting up bridge ${1} on host"
}
download_image(){
curl -o ${dir}/os.img $imageurl
}
prepare_dir(){
mkdir -p ${dir}/${1}
cat << END > ${dir}/${1}/meta-data
instance-id: $1
local-hostname: $1
END
cat << END > ${dir}/${1}/user-data
#cloud-config
users:
- name: ${muser}
groups: sudo
shell: /bin/bash
ssh_authorized_keys:
- ${sshkey}
sudo: ALL=(ALL) NOPASSWD:ALL
END
cp ${dir}/os.img ${dir}/${1}/os.qcow2
genisoimage -o ${dir}/${1}/config.iso -V cidata -r -J ${dir}/${1}/meta-data ${dir}/${1}/user-data
}
install_vm(){
if [ $1 == 0 ]; then
networkopt="--network network=default,mac=${macwan}"
#master should has nat connection to the internet via default network in virsh
specopt="-r 1024"
#I have no attention to generalize the hardware spec settings, but you could hack there
resizeopt="+5G"
else
networkopt=""
specopt="-r 1024"
resizeopt="+2G"
fi
qemu-img resize ${dir}/${vmprefix}${1}/os.qcow2 $resizeopt
virt-install -n ${vmprefix}$1 $specopt --disk ${dir}/${vmprefix}${1}/os.qcow2 --import --disk path=${dir}/${vmprefix}${1}/config.iso,device=cdrom $networkopt --network bridge=$br,mac=${macprefix}:$(printf "%02x" $1) --noautoconsole
}
ansible_init(){
slurmdbpass="bigbrowatchu"
userpass="123456"
cat << END > ${dir}/bootstrap/roles/slurm/defaults/main.yml
---
# defaults file for slurm
db_user: slurm
slurm_user: ${muser}
db_pass: ${slurmdbpass}
END
cat << END > ${dir}/bootstrap/roles/user/defaults/main.yml
---
# defaults file for user
users:
- name: "${muser}"
uid: 1000
password: "${userpass}"
END
cat << END > ${dir}/bootstrap/group_vars/all.yml
ansible_python_interpreter: "/usr/bin/python3"
timezone: "Asia/Shanghai"
admin: ${muser}
netmask: 255.255.255.0
mask: 24
ip_range: ${ip24}.0
ntp_server: ntp.tuna.tsinghua.edu.cn
wan_mask: 24
master_ip: ${ip24}.100
master_name: ${vmprefix}0
dhcp_start_ip: ${ip24}.20
dhcp_end_ip: ${ip24}.90
dns_server:
- 166.111.8.28
cluster_domain: hpc.cluster
cluster_name: hpc
#wan_ip
#wan_gateway
END
for i in $(seq 1 ${nocn})
do
cat << END > ${dir}/bootstrap/host_vars/${vmprefix}${i}.yml
ip: ${ip24}.1$(printf "%02d" ${i})
mac: ${macprefix}:$(printf "%02x" ${i})
END
done
}
add_cns(){
for i in $@
do
exist=$(virsh list --all|grep ${vmprefix}${i}|wc -l)
if [ $exist -gt 0 ]; then
echo "compute node instance ${vmprefix}${i} already exists, exiting"
exit 1
fi
done
if [ ${cn_init} == 0 ]; then
ansible_bootstrap_pre $@
fi
for i in $@
do
prepare_dir ${vmprefix}${i}
install_vm ${i}
done
ansible_bootstrap_post $@
}
reduce_cns(){
for i in $@
do
exist=$(virsh list --all|grep ${vmprefix}${i}|wc -l)
if [ $exist == 0 ]; then
echo "compute node instance ${vmprefix}${i} doesn't exist, exiting"
exit 1
fi
done
for i in $@
do
virsh destroy ${vmprefix}${i}
virsh undefine ${vmprefix}${i}
rm -r ${dir}/${vmprefix}${i}
rm ${dir}/bootstrap/host_vars/${vmprefix}${i}.yml
sed -i "/${vmprefix}${i}/d" ${dir}/bootstrap/hosts
done
cd ${dir}/bootstrap && $ansibleplaybook -i ${dir}/bootstrap/hosts ${dir}/bootstrap/site.yml --key-file "${keyfile}"
}
ansible_bootstrap_head(){
wanip=
while [ -z "$wanip" ]; do
sleep 10
wanip=$(virsh net-dhcp-leases default|grep "${macwan}"|tail -n1|awk '{print $5}'|awk -F/ '{print $1}')
done
cat << END > ${dir}/bootstrap/hosts
[ln]
${vmprefix}0 ansible_ssh_host=${wanip} ansible_ssh_user=${muser} ansible_sudo_pass=
[cn]
END
for i in $(seq 1 ${nocn})
do
echo "${vmprefix}${i} ansible_ssh_host=${ip24}.1$(printf "%02d" ${i}) ansible_ssh_user=${muser} ansible_sudo_pass=" >> ${dir}/bootstrap/hosts
done
echo "wan_ip: ${wanip}" >> ${dir}/bootstrap/group_vars/all.yml
echo "wan_gateway: $(echo $wanip|awk -F "." '{print $1"."$2"."$3"."1}')" >> ${dir}/bootstrap/group_vars/all.yml
while [ $(nc -zv $wanip 22 2>&1|grep succeeded|wc -l) == 0 ]; do
sleep 10
done
sleep 20
cd ${dir}/bootstrap && $ansibleplaybook -i ${dir}/bootstrap/hosts ${dir}/bootstrap/site.yml --key-file "${keyfile}"
}
ansible_bootstrap_pre(){
for i in $@
do
echo "${vmprefix}${i} ansible_ssh_host=${ip24}.1$(printf "%02d" ${i}) ansible_ssh_user=${muser} ansible_sudo_pass=" >>${dir}/bootstrap/hosts
cat << END > ${dir}/bootstrap/host_vars/${vmprefix}${i}.yml
ip: ${ip24}.1$(printf "%02d" ${i})
mac: ${macprefix}:$(printf "%02x" ${i})
END
done
cd ${dir}/bootstrap && $ansibleplaybook -i ${dir}/bootstrap/hosts -l ln ${dir}/bootstrap/site.yml --key-file "${keyfile}"
}
ansible_bootstrap_post(){
for i in $@; do
while [ $(nc -zv ${ip24}.1$(printf "%02d" $i ) 22 2>&1|grep succeeded|wc -l) == 0 ];do
sleep 5
done
done
sleep 15
cd ${dir}/bootstrap && $ansibleplaybook -i ${dir}/bootstrap/hosts ${dir}/bootstrap/site.yml --key-file "${keyfile}"
}
check_command(){
if [ $(which ${1}|wc -l) == 0 ]; then
echo "${1} is not installed in the host system, exiting..."
exit 2
fi
}
check(){
check_command $ansibleplaybook
check_command curl
check_command virsh
check_command virt-install
check_command qemu-img
check_command ip
check_command genisoimage
exist_br=$(ip link show|grep ${br}|wc -l)
if [ $mkbr == 1 ] && [ ${exist_br} -gt 0 ]; then
echo "the bridge $br already exists and you insist on creating it, exiting..."
exit 1
fi
exist_ln=$(virsh list --all|grep ln0|wc -l)
if [ ${exist_ln} -gt 0 ]; then
echo "head node ln0 already exists, exiting..."
exit 1
fi
exist_img=$(ls ${dir}|grep os.img|wc -l)
if [ ${download} == 0 ] && [ ${exist_img} == 0 ]; then
echo "You turned off download image, but we cannot find os.img in ${dir}, exiting..."
exit 1
fi
if [ ! -d "${dir}/bootstrap" ]; then
echo "Bootstrap folder hosting ansible playbooks not exsist in ${dir}, exiting..."
exit 1
fi
}
cluster_spin_up(){
check
if [ $download == 1 ];then
download_image
fi
prepare_dir ${vmprefix}0
if [ $mkbr == 1 ];then
prepare_bridge $br
fi
install_vm 0
ansible_init
ansible_bootstrap_head
cn_init=1
add_cns $(seq 1 $nocn)
}
cluster_tear_down(){
vmlist=($(virsh list --all|grep "${vmprefix}"|awk '{print $2}'))
for i in ${vmlist[@]}
do
virsh destroy $i
virsh undefine $i
rm -r ${dir}/$i
rm ${dir}/bootstrap/host_vars/${i}.yml
done
ip link del $br
debug "finish cluster ${vmprefix} teardown"
}
if [ "$1" == "cluster-up" ]; then
cluster_spin_up
elif [ "$1" == "cluster-down" ]; then
cluster_tear_down
elif [ "$1" == "nodes-add" ]; then
add_cns ${@:2}
elif [ "$1" == "nodes-del" ]; then
reduce_cns ${@:2}
else
echo "unrecognized action, exiting..."
exit 4
fi