-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaf-spine.sh
executable file
·325 lines (268 loc) · 8.68 KB
/
leaf-spine.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
#!/bin/bash
#
# Scripted version of the example leaf/spine setup given by Cumulus
# for learning Cumulus VX networking
#
VBM=/usr/local/bin/VBoxManage
if [[ ! -x $VBM ]]; then
echo "Error : \"$VBM\" not found or not executable"
exit
fi
CUMULUS_IMAGE=cumulus-linux-3.2.1-vx-amd64-1486153138.ac46c24zd00d13e.ova
while [[ -n "$1" ]]; do
if [[ "$1" = "-f" ]]; then
FORCE=true
elif [[ "$1" = "-v" ]]; then
VERBOSE=true
else
echo "Usage: $0 [-f] [-v] "
echo "Unrecognized \"$1\""
exit
fi
shift
done
if [[ ! -f $CUMULUS_IMAGE ]]; then
echo "$0 : Error could not find ${CUMULUS_IMAGE}"
echo "$0 : You have to obtain the Cumulus .ova file yourself by registering with Cumulus and accepting the licensing agreement."
echo "$0 : Please see https://cumulusnetworks.com/products/cumulus-vx/"
exit
fi
# conditional printf
function vftrace() {
if [[ -n "$VERBOSE" ]]; then
printf "$@"
fi
}
function delvm_if_force() {
EXISTS=""
VMNAME="$1"
MATCH=`$VBM list vms | grep "${VMNAME}"`
if [[ -n "${MATCH}" ]]; then
EXISTS=true
if [[ -n "${FORCE}" ]]; then
vftrace "Deleting $VMNAME due to force ... "
$VBM unregistervm "${VMNAME}" --delete >/dev/null 2>&1
vftrace "done\n"
EXISTS=""
fi
else
EXISTS=""
fi
}
VM_PREFIX="Cumulus VX"
COMMON_OPTIONS="--vsys 0"
function check_vm_running() {
RUNNING=""
VMNAME="$1"
RUN=`$VBM list runningvms | grep "${VMNAME}"`
if [[ -n "${RUN}" ]]; then
RUNNING=true
fi
}
function startVM() {
vftrace "startVM ${VMNAME}\n"
VMNAME="$1"
check_vm_running "${VMNAME}"
if [[ -z "$RUNNING" ]]; then
vftrace "Starting $VMNAME\n"
$VBM startvm "${VMNAME}" --type gui
else
vftrace "$VMNAME was already running\n"
fi
}
function stopVM() {
vftrace "stopVM ${VMNAME}\n"
VMNAME="$1"
check_vm_running "${VMNAME}"
if [[ -n "$RUNNING" ]]; then
vftrace "Stopping $VMNAME\n"
$VBM controlvm "${VMNAME}" poweroff
else
vftrace "$VMNAME was already stopped\n"
fi
}
# management network type - bridged or host-only
# bridged networking will allow traffic from the VMs to go via the
# host adapter out to the surrounding network. If there is a DHCP
# server there that gives them an address, then they will be able to
# join the network
# note that hostonly networking will not be even remotely functional
# right now as the VMs will not find a DHCP server and hence will
# never become network accessible. The intention is to develop this
# script further and eventually bootstrap these cumulus VMs within a
# chef-bcpc network
MNET=bridged
function adjust_mgmt() {
VMNAME=$1
NIC=$2
vftrace "adjust_mgmt $VMNAME $NIC\n"
if [[ "${MNET}" = bridged ]]; then
$VBM modifyvm "${VMNAME}" --nic1 bridged --bridgeadapter1 en0 >/dev/null 2>&1
else
$VBM modifyvm "${VMNAME}" --nic"${NIC}" hostonly >/dev/null 2>&1
$VBM modifyvm "${VMNAME}" --hostonlyadapter"${NIC}" vboxnet0 >/dev/null 2>&1
fi
}
MGMT_NIC_TYPE=
for LEAF in 1 2; do
VMNAME="${VM_PREFIX}-leaf${LEAF}"
stopVM "${VMNAME}"
delvm_if_force "$VMNAME"
if [[ -z "$EXISTS" ]]; then
vftrace "Import $CUMULUS_IMAGE for $VMNAME ... "
$VBM import $CUMULUS_IMAGE ${COMMON_OPTIONS} -vmname "${VMNAME}" >/dev/null 2>&1
vftrace "done\n"
vftrace "Modify NICs ... "
adjust_mgmt "${VMNAME}" 1
$VBM modifyvm "${VMNAME}" --nic2 intnet --intnet l${LEAF}s1 >/dev/null 2>&1
$VBM modifyvm "${VMNAME}" --nic2 intnet --intnet l${LEAF}s2 >/dev/null 2>&1
$VBM modifyvm "${VMNAME}" --nic2 intnet --intnet leaf${LEAF} >/dev/null 2>&1
vftrace "done\n"
else
vftrace "${VMNAME} exists\n"
fi
done
#set -x
for SPINE in 1 2; do
VMNAME="${VM_PREFIX}-spine${SPINE}"
stopVM "${VMNAME}"
delvm_if_force "$VMNAME"
if [[ -z "$EXISTS" ]]; then
vftrace "Import $CUMULUS_IMAGE for $VMNAME ... "
$VBM import $CUMULUS_IMAGE ${COMMON_OPTIONS} -vmname "${VMNAME}" >/dev/null 2>&1
vftrace "done\n"
vftrace "Modify NICs ... "
adjust_mgmt "${VMNAME}" 1
$VBM modifyvm "${VMNAME}" --nic2 intnet --intnet l${SPINE}s1 >/dev/null 2>&1
$VBM modifyvm "${VMNAME}" --nic2 intnet --intnet l${SPINE}s2 >/dev/null 2>&1
$VBM modifyvm "${VMNAME}" --nic2 intnet --intnet spine${SPINE} >/dev/null 2>&1
vftrace "done\n"
else
vftrace "${VMNAME} exists\n"
fi
done
function getvminfo {
HOSTNAME="$1"
vftrace "Trying to get IP address for $HOSTNAME\n"
TRIES="$2"
for TRY in `seq -s" " 1 ${TRIES}`; do
# extract the first mac address for this VM
MAC1=`$VBM showvminfo "$HOSTNAME" --machinereadable | grep macaddress1 | tr -d \" | tr = " " | awk '{print $2}'`
# add the customary colons in
MAC1=`echo $MAC1 | sed -e 's/^\([0-9A-Fa-f]\{2\}\)/\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1_/' \
-e 's/_\([0-9A-Fa-f]\{2\}\)/:\1/'`
MAC1=`echo $MAC1 | sed -e 's/^0//' -e 's/:0/:/g'`
IP=`arp -na | grep -i $MAC1 | awk '{print $2}' | tr -d \( | tr -d \)`
if [[ -n "$IP" ]]; then
vftrace "${HOSTNAME} has IP address $IP\n"
break
else
vftrace "$HOSTNAME not up yet.\n"
vftrace "Will retry getting IP address ...\n"
sleep 5
fi
done
}
SOCKETDIR=${HOME}/.ssh/sockets
if [[ ! -d $SOCKETDIR ]]; then
mkdir ${SOCKETDIR}
chmod a+rwx ${SOCKETDIR}
else
ls ${SOCKETDIR}
fi
SSH_COMMAND_OPTS="-o ControlMaster=auto -o ControlPath=${SOCKETDIR}/%r@%h-%p -o ControlPersist=600"
SSHCOMMON="-q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o VerifyHostKeyDNS=no"
SSHCMD="ssh $SSH_COMMAND_OPTS ${SSHCOMMON}"
SCPCMD="scp $SSH_COMMAND_OPTS ${SSHCOMMON}"
# Work out the IP address of this VM and then install an ssh key to
# allow easy access from now on
function preconfig_vm() {
VMNAME="$1"
check_vm_running "$VMNAME"
if [[ -n "$RUNNING" ]]; then
vftrace "$VMNAME is running\n"
getvminfo "${VMNAME}" 5
if [[ -z "$IP" ]]; then
echo "$VMNAME has no IP address!"
else
vftrace "Now configuring ${VMNAME} ($IP) ..."
cat ~/.ssh/id_rsa.pub | sshpass -p 'CumulusLinux!' $SSHCMD cumulus@${IP} 'umask 0077; mkdir -p .ssh; cat >> .ssh/authorized_keys'
vftrace "Key copied\n"
fi
fi
}
# Get the VMs started
for LEAF in 1 2; do
VMNAME="${VM_PREFIX}-leaf${LEAF}"
startVM "$VMNAME"
done
for SPINE in 1 2; do
VMNAME="${VM_PREFIX}-spine${SPINE}"
startVM "$VMNAME"
done
LOCALNET="10.0.1.0/24"
vftrace "Updating arp for $LOCALNET ..."
nmap -sn $LOCALNET >/dev/null 2>&1
vftrace "done\n"
declare -a ADDRS
for LEAF in 1 2; do
VMNAME="${VM_PREFIX}-leaf${LEAF}"
preconfig_vm "${VMNAME}"
if [[ -n "$IP" ]]; then
ADDRS[$LEAF]="$IP"
${SCPCMD} Quagga.conf.leaf${LEAF} interfaces.leaf${LEAF} daemons sudo cumulus@"${IP}":/tmp
echo "CumulusLinux!" | ${SSHCMD} -t cumulus@${IP} "sudo -S cp /tmp/sudo /etc/sudoers.d/cumulus "
${SSHCMD} -t cumulus@"${IP}" "sudo cp /tmp/interfaces.leaf${LEAF} /etc/network/interfaces"
${SSHCMD} -t cumulus@"${IP}" "sudo cp /tmp/daemons /etc/quagga/daemons"
${SSHCMD} -t cumulus@"${IP}" "sudo cp /tmp/Quagga.conf.leaf${LEAF} /etc/network/Quagga.conf"
${SSHCMD} -t cumulus@"${IP}" "sudo systemctl restart networking"
${SSHCMD} -t cumulus@"${IP}" "sudo systemctl restart quagga.service"
else
vftrace "Couldn't finish configuring $VMNAME\n"
exit
fi
done
for SPINE in 1 2; do
VMNAME="${VM_PREFIX}-spine${SPINE}"
preconfig_vm "${VMNAME}"
if [[ -n "$IP" ]]; then
ADDRS["$SPINE"+2]="$IP"
${SCPCMD} Quagga.conf.spine${SPINE} interfaces.spine${SPINE} daemons sudo cumulus@"${IP}":/tmp
echo "CumulusLinux!" | ${SSHCMD} -t cumulus@${IP} "sudo -S cp /tmp/sudo /etc/sudoers.d/cumulus "
${SSHCMD} -t cumulus@"${IP}" "sudo cp /tmp/interfaces.spine${SPINE} /etc/network/interfaces"
${SSHCMD} -t cumulus@"${IP}" "sudo cp /tmp/daemons /etc/quagga/daemons"
${SSHCMD} -t cumulus@"${IP}" "sudo cp /tmp/Quagga.conf.spine${SPINE} /etc/network/Quagga.conf"
${SSHCMD} -t cumulus@"${IP}" "sudo systemctl restart networking"
${SSHCMD} -t cumulus@"${IP}" "sudo systemctl restart quagga.service"
else
vftrace "Couldn't finish configuring $VMNAME\n"
exit
fi
done
# run tests
IP1=${ADDRS[1]}
IP2=${ADDRS[2]}
IP3=${ADDRS[3]}
IP4=${ADDRS[4]}
vftrace "IPs $IP1 $IP2 $IP3 $IP4\n"
if [[ -z "$IP1" && -z "$IP2" && -z "$IP3" && -z "$IP4" ]]; then
vftrace "Some VMs didn't get IP addresses, tests not attempted\n"
exit
else
vftrace "All VMs configured\n"
fi
vftrace "IPs : $IP1 $IP2 $IP3 $IP4\n"
for IP in `seq -s" " 1 4`; do
for OTHER in `seq -s" " 1 4`; do
if [[ "$IP" -ne "$OTHER" ]]; then
CMD="$CMD ping -c1 ${ADDRS[OTHER]} ;"
fi
done
vftrace "${CMD}\n"
${SSHCMD} -t cumulus@"${ADDRS[IP]}" "${CMD}"
CMD=""
done