-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-lib.sh
executable file
·827 lines (748 loc) · 21.1 KB
/
setup-lib.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
#!/bin/sh
DIRNAME=`dirname $0`
#
# Setup our core vars
#
OURDIR=/local/setup
WWWPRIV=$OURDIR
WWWPUB=/local/profile-public
SETTINGS=$OURDIR/settings
LOCALSETTINGS=$OURDIR/settings.local
TOPOMAP=$OURDIR/topomap
BOOTDIR=/var/emulab/boot
TMCC=/usr/local/etc/emulab/tmcc
SWAPPER=`geni-get user_urn | cut -f4 -d+`
if [ -z "$EUID" ]; then
EUID=`id -u`
fi
SUDO=
if [ ! $EUID -eq 0 ] ; then
SUDO=sudo
fi
[ ! -d $OURDIR ] && ($SUDO mkdir -p $OURDIR && $SUDO chown $SWAPPER $OURDIR)
[ ! -e $SETTINGS ] && touch $SETTINGS
[ ! -e $LOCALSETTINGS ] && touch $LOCALSETTINGS
cd $OURDIR
# Setup time logging stuff early
TIMELOGFILE=$OURDIR/setup-time.log
FIRSTTIME=0
if [ ! -f $OURDIR/setup-lib-first ]; then
touch $OURDIR/setup-lib-first
FIRSTTIME=`date +%s`
fi
logtstart() {
area=$1
varea=`echo $area | sed -e 's/[^a-zA-Z_0-9]/_/g'`
stamp=`date +%s`
date=`date`
eval "LOGTIMESTART_$varea=$stamp"
echo "START $area $stamp $date" >> $TIMELOGFILE
}
logtend() {
area=$1
#varea=${area//-/_}
varea=`echo $area | sed -e 's/[^a-zA-Z_0-9]/_/g'`
stamp=`date +%s`
date=`date`
eval "tss=\$LOGTIMESTART_$varea"
tsres=`expr $stamp - $tss`
resmin=`perl -e 'print '"$tsres"' / 60.0 . "\n"'`
echo "END $area $stamp $date" >> $TIMELOGFILE
echo "TOTAL $area $tsres $resmin" >> $TIMELOGFILE
}
if [ $FIRSTTIME -ne 0 ]; then
logtstart "libfirsttime"
fi
#LOCKFILE="lockfile -1 -r -1 "
LOCKFILE="lockfile-create --retry 65535 "
RMLOCKFILE="lockfile-remove "
PSWDGEN="openssl rand -hex 10"
SSH="ssh -o StrictHostKeyChecking=no"
SCP="scp -p -o StrictHostKeyChecking=no"
#
# Our default configuration
#
HEAD="node-0"
DO_APT_INSTALL=1
DO_APT_UPGRADE=0
DO_APT_DIST_UPGRADE=0
DO_APT_UPDATE=1
UBUNTUMIRRORHOST=""
UBUNTUMIRRORPATH=""
KUBESPRAYREPO="https://github.com/kubernetes-incubator/kubespray.git"
KUBESPRAYUSEVIRTUALENV=1
KUBESPRAY_VIRTUALENV=kubespray-virtualenv
KUBESPRAYVERSION=release-2.20
CONTAINERMANAGER=docker
DOCKERVERSION=
DOCKEROPTIONS=
KUBEVERSION=
HELMVERSION=
KUBENETWORKPLUGIN="calico"
KUBEENABLEMULTUS=0
KUBEPROXYMODE="ipvs"
KUBEPODSSUBNET="192.168.0.0/17"
KUBESERVICEADDRESSES="192.168.128.0/17"
KUBEDOMETALLB=1
KUBEACCESSIP="mgmt"
KUBEFEATUREGATES="[EphemeralContainers=true]"
KUBELETCUSTOMFLAGS=""
KUBELETMAXPODS=0
KUBEALLWORKERS=0
SSLCERTTYPE="self"
SSLCERTCONFIG="proxy"
MGMTLAN="datalan-1"
DATALAN="datalan-1"
DATALANS="datalan-1"
SINGLENODE_MGMT_IP=10.10.1.1
SINGLENODE_MGMT_NETMASK=255.255.0.0
SINGLENODE_MGMT_NETBITS=16
SINGLENODE_MGMT_CIDR=${SINGLENODE_MGMT_IP}/${SINGLENODE_MGMT_NETBITS}
DOLOCALREGISTRY=1
STORAGEDIR=/storage
DONFS=1
NFSEXPORTDIR=$STORAGEDIR/nfs
NFSMOUNTDIR=/nfs
NFSASYNC=0
YQFROMPKG=0
#
# We have an 'admin' user that gets a random password that comes in from
# geni-lib/rspec as a hash.
#
ADMIN='admin'
ADMIN_PASS=''
ADMIN_PASS_HASH=''
#
# Setup apt-get to not prompt us
#
if [ ! -e $OURDIR/apt-configured ]; then
echo "force-confdef" | $SUDO tee -a /etc/dpkg/dpkg.cfg.d/cloudlab
echo "force-confold" | $SUDO tee -a /etc/dpkg/dpkg.cfg.d/cloudlab
touch $OURDIR/apt-configured
fi
export DEBIAN_FRONTEND=noninteractive
# -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef"
DPKGOPTS=''
APTGETINSTALLOPTS='-y'
APTGETINSTALL="$SUDO apt-get $DPKGOPTS install $APTGETINSTALLOPTS"
# Don't install/upgrade packages if this is not set
if [ ${DO_APT_INSTALL} -eq 0 ]; then
APTGETINSTALL="/bin/true ${APTGETINSTALL}"
fi
do_apt_update() {
if [ ! -f $OURDIR/apt-updated -a "${DO_APT_UPDATE}" = "1" ]; then
while true ; do
$SUDO apt-get update && break
echo "ERROR: apt-get update failed; pausing to try again"
sleep 60
done
touch $OURDIR/apt-updated
fi
}
are_packages_installed() {
retval=1
while [ ! -z "$1" ] ; do
dpkg -s "$1" >/dev/null 2>&1
if [ ! $? -eq 0 ] ; then
retval=0
fi
shift
done
return $retval
}
maybe_install_packages() {
if [ ! ${DO_APT_UPGRADE} -eq 0 ] ; then
# Just do an install/upgrade to make sure the package(s) are installed
# and upgraded; we want to try to upgrade the package.
$APTGETINSTALL $@
return $?
else
# Ok, check if the package is installed; if it is, don't install.
# Otherwise, install (and maybe upgrade, due to dependency side effects).
# Also, optimize so that we try to install or not install all the
# packages at once if none are installed.
are_packages_installed $@
if [ $? -eq 1 ]; then
return 0
fi
retval=0
while [ ! -z "$1" ] ; do
are_packages_installed $1
if [ $? -eq 0 ]; then
$APTGETINSTALL $1
retval=`expr $retval \| $?`
else
retval=0
fi
shift
done
return $retval
fi
}
##
## Figure out the system python version.
##
python --version
if [ ! $? -eq 0 ]; then
python3 --version
if [ $? -eq 0 ]; then
PYTHON=python3
else
are_packages_installed python3
success=`expr $? = 0`
# Keep trying again with updated cache forever;
# we must have python.
while [ ! $success -eq 0 ]; do
do_apt_update
$SUDO apt-get $DPKGOPTS install $APTGETINSTALLOPTS python3
success=$?
done
PYTHON=python3
fi
else
PYTHON=python
fi
$PYTHON --version | grep -q "Python 3"
if [ $? -eq 0 ]; then
PYVERS=3
PIP=pip3
PYTHONPKGPREFIX=python3
else
PYVERS=2
PIP=pip
PYTHONPKGPREFIX=python
fi
PYTHONBIN=`which $PYTHON`
# Some kubesprays uninstall python-is-python3 and replace with
# python-is-python2; we cannot have that.
if [ ! -e $OURDIR/python-is-what ]; then
are_packages_installed python-is-python3
if [ $? -eq 1 ]; then
echo python-is-python3 > $OURDIR/python-is-what
else
are_packages_installed python-is-python2
if [ $? -eq 1 ]; then
echo python-is-python2 > $OURDIR/python-is-what
fi
fi
touch $OURDIR/python-is-what
fi
##
## Grab our geni creds, and create a GENI credential cert
##
are_packages_installed ${PYTHONPKGPREFIX}-cryptography ${PYTHONPKGPREFIX}-future \
${PYTHONPKGPREFIX}-six ${PYTHONPKGPREFIX}-lxml ${PYTHONPKGPREFIX}-pip
success=`expr $? = 0`
# Keep trying again with updated cache forever;
# we must have this package.
while [ ! $success -eq 0 ]; do
do_apt_update
$SUDO apt-get $DPKGOPTS install $APTGETINSTALLOPTS ${PYTHONPKGPREFIX}-cryptography \
${PYTHONPKGPREFIX}-future ${PYTHONPKGPREFIX}-six ${PYTHONPKGPREFIX}-lxml ${PYTHONPKGPREFIX}-pip
success=$?
done
if [ ! -e $OURDIR/geni.key ]; then
geni-get key > $OURDIR/geni.key
cat $OURDIR/geni.key | grep -q END\ .\*\PRIVATE\ KEY
if [ ! $? -eq 0 ]; then
echo "ERROR: could not get geni key; aborting!"
exit 1
fi
fi
if [ ! -e $OURDIR/geni.certificate ]; then
geni-get certificate > $OURDIR/geni.certificate
cat $OURDIR/geni.certificate | grep -q END\ CERTIFICATE
if [ ! $? -eq 0 ]; then
echo "ERROR: could not get geni cert; aborting!"
exit 1
fi
fi
if [ ! -e ~/.ssl/encrypted.pem ]; then
mkdir -p ~/.ssl
chmod 700 ~/.ssl
cat $OURDIR/geni.key > ~/.ssl/encrypted.pem
cat $OURDIR/geni.certificate >> ~/.ssl/encrypted.pem
fi
if [ ! -e $OURDIR/manifests.xml ]; then
$PYTHON $DIRNAME/getmanifests.py $OURDIR/manifests
if [ ! $? -eq 0 ]; then
# Fall back to geni-get
echo "WARNING: falling back to getting manifest from AM, not Portal -- multi-site experiments will not work fully!"
geni-get manifest > $OURDIR/manifests.0.xml
fi
fi
if [ ! -e $OURDIR/encrypted_admin_pass ]; then
cat $OURDIR/manifests.0.xml | perl -e '@lines = <STDIN>; $all = join("",@lines); if ($all =~ /^.+<[^:]+:password[^>]*>([^<]+)<\/[^:]+:password>.+/igs) { print $1; }' > $OURDIR/encrypted_admin_pass
fi
if [ ! -e $OURDIR/decrypted_admin_pass -a -s $OURDIR/encrypted_admin_pass ]; then
openssl smime -decrypt -inform PEM -inkey geni.key -in $OURDIR/encrypted_admin_pass -out $OURDIR/decrypted_admin_pass
fi
#
# Suck in user parameters, if we haven't already. This also pulls in
# global labels.
#
if [ ! -e $OURDIR/parameters ]; then
$PYTHON $DIRNAME/manifest-to-parameters.py $OURDIR/manifests.0.xml > $OURDIR/parameters
fi
. $OURDIR/parameters
#
# Grab our public addrs.
#
if [ ! -f $OURDIR/publicaddrs ]; then
$PYTHON $DIRNAME/manifest-to-publicaddrs.py $OURDIR/manifests.0.xml $CLUSTER > $OURDIR/publicaddrs.raw
PUBLICADDRS=`cat $OURDIR/publicaddrs.raw | sed -e 's|^\([^/]*\)/.*$|\1|' | xargs`
PUBLICADDRCOUNT=`cat $OURDIR/publicaddrs.raw | wc -l`
PUBLICADDRNETMASK=`cat $OURDIR/publicaddrs.raw | sed -e 's|^[^/]*/\(.*\)$|\1|' | head -1`
cat <<EOF > $OURDIR/publicaddrs
PUBLICADDRS="$PUBLICADDRS"
PUBLICADDRCOUNT=$PUBLICADDRCOUNT
PUBLICADDRNETMASK="$PUBLICADDRNETMASK"
EOF
fi
. $OURDIR/publicaddrs
#
# Ok, to be absolutely safe, if the ADMIN_PASS_HASH we got from params was "",
# and if admin pass wasn't sent as an encrypted string to us, we have we have
# to generate a random admin pass and hash it.
#
if [ "x${ADMIN_PASS_HASH}" = "x" ] ; then
DEC_ADMIN_PASS=`cat $OURDIR/decrypted_admin_pass`
if [ "x${DEC_ADMIN_PASS}" = "x" ]; then
ADMIN_PASS=`$PSWDGEN`
ADMIN_PASS_HASH="`echo \"${ADMIN_PASS}\" | openssl passwd -1 -stdin`"
# Save it off so we can email the user -- because nobody has the
# random pass we just generated!
echo "${ADMIN_PASS}" > $OURDIR/random_admin_pass
else
ADMIN_PASS="${DEC_ADMIN_PASS}"
ADMIN_PASS_HASH="`echo \"${ADMIN_PASS}\" | openssl passwd -1 -stdin`"
fi
#
# Overwrite the params.
#
echo "ADMIN_PASS='${ADMIN_PASS}'" >> $OURDIR/parameters
echo "ADMIN_PASS_HASH='${ADMIN_PASS_HASH}'" >> $OURDIR/parameters
fi
EXPTTYPE="Kubernetes"
CREATOR=`geni-get user_urn | cut -f4 -d+`
SWAPPER=`geni-get user_urn | cut -f4 -d+`
NODEID=`cat $BOOTDIR/nickname | cut -d . -f 1`
PNODEID=`cat $BOOTDIR/nodeid`
EEID=`cat $BOOTDIR/nickname | cut -d . -f 2`
EPID=`cat $BOOTDIR/nickname | cut -d . -f 3`
OURDOMAIN=`cat $BOOTDIR/mydomain`
FULLDOMAIN="${EEID}.${EPID}.$OURDOMAIN"
NFQDN="`cat $BOOTDIR/nickname`.$OURDOMAIN"
PFQDN="`cat $BOOTDIR/nodeid`.$OURDOMAIN"
MYIP=`cat $BOOTDIR/myip`
EXTERNAL_NETWORK_INTERFACE=`cat $BOOTDIR/controlif`
HOSTNAME=`cat ${BOOTDIR}/nickname | cut -f1 -d.`
ARCH=`uname -m`
# Check if our init is systemd
dpkg-query -S /sbin/init | grep -q systemd
HAVE_SYSTEMD=`expr $? = 0`
. /etc/lsb-release
DISTRIB_MAJOR=`echo $DISTRIB_RELEASE | cut -d. -f1`
if [ -e /etc/emulab/bossnode ]; then
BOSSNODE=`cat /etc/emulab/bossnode`
fi
if [ -n "$BOSSNODE" ]; then
SWAPPER_EMAIL=`geni-get -s $BOSSNODE slice_email`
else
SWAPPER_EMAIL=`geni-get slice_email`
fi
#
# Grab our topomap so we can see how many nodes we have.
# NB: only safe to use topomap for non-fqdn things.
#
if [ ! -f $TOPOMAP ]; then
if [ -f $TOPOMAP ]; then
cp -p $TOPOMAP $TOPOMAP.old
fi
# First try via manifest; fall back to tmcc if necessary (although
# that will break multisite exps with >1 second cluster node(s)).
$PYTHON $DIRNAME/manifest-to-topomap.py $OURDIR/manifests.0.xml > $TOPOMAP
if [ ! $? -eq 0 ]; then
echo "ERROR: could not extract topomap from manifest; aborting to tmcc"
rm -f $TOPOMAP
$TMCC topomap | gunzip > $TOPOMAP
fi
# Filter out blockstore nodes
cat $TOPOMAP | grep -v '^bsnode,' > $TOPOMAP.no.bsnode
mv $TOPOMAP.no.bsnode $TOPOMAP
cat $TOPOMAP | grep -v '^bslink,' > $TOPOMAP.no.bslink
mv $TOPOMAP.no.bslink $TOPOMAP
if [ -f $TOPOMAP.old ]; then
diff -u $TOPOMAP.old $TOPOMAP > $TOPOMAP.diff
#
# NB: this does assume that nodes either leave all the lans, or join
# all the lans. We don't try to distinguish anything else.
#
NEWNODELIST=`cat topomap.diff | sed -n -e 's/^\+\([a-zA-Z0-9\-]*\),.*:.*$/\1/p' | uniq | xargs`
OLDNODELIST=`cat topomap.diff | sed -n -e 's/^\-\([a-zA-Z0-9\-]*\),.*:.*$/\1/p' | uniq | xargs`
# Just remove the fqdn map and let it be recalculated below
rm -f $OURDIR/fqdn.map
rm -f $OURDIR/fqdn.physical.map
fi
fi
#
# Create a map of node nickname to FQDN (and another one of pnode id to FQDN).
# This supports geni multi-site experiments.
#
if [ \( -s $OURDIR/manifests.xml \) -a \( ! \( -s $OURDIR/fqdn.map \) \) ]; then
cat $OURDIR/manifests.xml | tr -d '\n' | sed -e 's/<node /\n<node /g' | sed -n -e "s/^<node [^>]*client_id=['\"]*\([^'\"]*\)['\"].*<host name=['\"]\([^'\"]*\)['\"].*$/\1\t\2/p" > $OURDIR/fqdn.map
# Add a newline if we wrote anything.
if [ -s $OURDIR/fqdn.map ]; then
echo '' >> $OURDIR/fqdn.map
fi
# Filter out any blockstore nodes
# XXX: this strategy doesn't work, because only the NM node makes
# the fqdn.map file. So, just look for bsnode for now.
#BSNODES=`cat /var/emulab/boot/tmcc/storageconfig | sed -n -e 's/^.* HOSTID=\([^ \t]*\) .*$/\1/p' | xargs`
#for bs in $BSNODES ; do
# cat $OURDIR/fqdn.map | grep -v "^${bs}"$'\t' > $OURDIR/fqdn.map.tmp
# mv $OURDIR/fqdn.map.tmp $OURDIR/fqdn.map
#done
# XXX: why doesn't the tab grep work here, sigh...
#cat $OURDIR/fqdn.map | grep -v '^bsnode'$'\t' > $OURDIR/fqdn.map.tmp
cat $OURDIR/fqdn.map | grep -v '^bsnode' > $OURDIR/fqdn.map.tmp
mv $OURDIR/fqdn.map.tmp $OURDIR/fqdn.map
cat $OURDIR/fqdn.map | grep -v '^fw[ \t]*' > $OURDIR/fqdn.map.tmp
mv $OURDIR/fqdn.map.tmp $OURDIR/fqdn.map
cat $OURDIR/fqdn.map | grep -v '^fw-s2[ \t]*' > $OURDIR/fqdn.map.tmp
mv $OURDIR/fqdn.map.tmp $OURDIR/fqdn.map
cat $OURDIR/manifests.xml | tr -d '\n' | sed -e 's/<node /\n<node /g' | sed -n -e "s/^<node [^>]*component_id=['\"]*[a-zA-Z0-9:\+\.]*node+\([^'\"]*\)['\"].*<host name=['\"]\([^'\"]*\)['\"].*$/\1\t\2/p" > $OURDIR/fqdn.physical.map
# Add a newline if we wrote anything.
if [ -s $OURDIR/fqdn.physical.map ]; then
echo '' >> $OURDIR/fqdn.physical.map
fi
# Filter out any blockstore nodes
cat $OURDIR/fqdn.physical.map | grep -v '[ \t]bsnode\.' > $OURDIR/fqdn.physical.map.tmp
mv $OURDIR/fqdn.physical.map.tmp $OURDIR/fqdn.physical.map
# Filter out any firewall nodes
cat $OURDIR/fqdn.physical.map | grep -v '[ \t]*fw\.' > $OURDIR/fqdn.physical.map.tmp
mv $OURDIR/fqdn.physical.map.tmp $OURDIR/fqdn.physical.map
cat $OURDIR/fqdn.physical.map | grep -v '[ \t]*fw-s2\.' > $OURDIR/fqdn.physical.map.tmp
mv $OURDIR/fqdn.physical.map.tmp $OURDIR/fqdn.physical.map
fi
#
# Grab our list of short-name and FQDN nodes. One way or the other, we have
# an fqdn map. First we tried the GENI way; then the old Emulab way with
# topomap.
#
NODES=`cat $OURDIR/fqdn.map | cut -f1 | sort -n | xargs`
FQDNS=`cat $OURDIR/fqdn.map | cut -f2 | sort -n | xargs`
NODEIPS=""
NODECOUNT=0
for node in $NODES ; do
ip=`grep "${node}-" /etc/hosts | cut -f1`
NODEIPS="$NODEIPS $ip"
NODECOUNT=`expr $NODECOUNT + 1`
done
# Construct parallel-ssh hosts files
if [ ! -e $OURDIR/pssh.all-nodes ]; then
echo > $OURDIR/pssh.all-nodes
echo > $OURDIR/pssh.other-nodes
for node in $NODES ; do
echo $node >> $OURDIR/pssh.all-nodes
[ "$node" = "$NODEID" ] && continue
echo $node >> $OURDIR/pssh.other-nodes
done
fi
OTHERNODES=""
for node in $NODES ; do
[ "$node" = "$NODEID" ] && continue
OTHERNODES="$OTHERNODES $node"
done
##
## Setup our Ubuntu package mirror, if necessary.
##
grep MIRRORSETUP $SETTINGS
if [ ! $? -eq 0 ]; then
if [ ! "x${UBUNTUMIRRORHOST}" = "x" ]; then
oldstr='us.archive.ubuntu.com'
newstr="${UBUNTUMIRRORHOST}"
if [ ! "x${UBUNTUMIRRORPATH}" = "x" ]; then
oldstr='us.archive.ubuntu.com/ubuntu'
newstr="${UBUNTUMIRRORHOST}/${UBUNTUMIRRORPATH}"
fi
echo "*** Changing Ubuntu mirror from $oldstr to $newstr ..."
$SUDO sed -E -i.us.archive.ubuntu.com -e "s|(${oldstr})|$newstr|" /etc/apt/sources.list
fi
echo "MIRRORSETUP=1" >> $SETTINGS
fi
if [ ! -f $OURDIR/apt-updated -a "${DO_APT_UPDATE}" = "1" ]; then
#
# Attempt to handle old EOL releases; so far only need to handle utopic
#
. /etc/lsb-release
grep -q old-releases /etc/apt/sources.list
if [ $? != 0 -a "x${DISTRIB_CODENAME}" = "xutopic" ]; then
sed -i -re 's/([a-z]{2}\.)?archive.ubuntu.com|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list
fi
$SUDO apt-get update
touch $OURDIR/apt-updated
fi
if [ ! -f $OURDIR/apt-dist-upgraded -a "${DO_APT_DIST_UPGRADE}" = "1" ]; then
# First, mark grub packages not to be upgraded; we don't want an
# install going to the wrong place.
PKGS="grub-common grub-gfxpayload-lists grub-pc grub-pc-bin grub2-common"
for pkg in $PKGS; do
$SUDO apt-mark hold $pkg
done
$SUDO apt-get dist-upgrade -y
for pkg in $PKGS; do
$SUDO apt-mark unhold $pkg
done
touch $OURDIR/apt-dist-upgraded
fi
#
# Process our network information.
#
netmask2prefix() {
nm=$1
bits=0
IFS=.
read -r i1 i2 i3 i4 <<EOF
$nm
EOF
unset IFS
for n in $i1 $i2 $i3 $i4 ; do
v=128
while [ $v -gt 0 ]; do
bits=`expr $bits + \( \( $n / $v \) % 2 \)`
v=`expr $v / 2`
done
done
echo $bits
}
getnodeip() {
node=$1
network=$2
if [ -z "$node" -o -z "$network" ]; then
echo ""
return
fi
ip=`sed -ne "s/^\([0-9\.]*\)[ \t]*${node}-${network}[ \t]*.*$/\1/p" /etc/hosts`
if [ "$network" = "$MGMTLAN" -a -z "$ip" ]; then
echo $SINGLENODE_MGMT_IP
else
echo $ip
fi
}
getnetmask() {
network=$1
if [ -z "$network" ]; then
echo ""
return
fi
nm=`sed -ne "s/^${network},\([0-9\.]*\),.*$/\1/p" $TOPOMAP`
if [ "$network" = "$MGMTLAN" -a -z "$nm" ]; then
echo $SINGLENODE_MGMT_NETMASK
else
echo $nm
fi
}
getnetmaskprefix() {
netmask=`getnetmask $1`
if [ -z "$netmask" ]; then
echo ""
return
fi
prefix=`netmask2prefix $netmask`
echo $prefix
}
getnetworkip() {
node=$1
network=$2
nodeip=`getnodeip $node $network`
netmask=`getnetmask $network`
IFS=.
read -r i1 i2 i3 i4 <<EOF
$nodeip
EOF
read -r m1 m2 m3 m4 <<EOF
$netmask
EOF
unset IFS
printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"
}
#
# Note that the `.`s are escaped enough to make it from shell into yaml into
# ansible and eventually into the golang regexp used by flanneld. Not
# generic.
#
getnetworkregex() {
node=$1
network=$2
nodeip=`getnodeip $node $network`
netmask=`getnetmask $network`
IFS=.
read -r i1 i2 i3 i4 <<EOF
$nodeip
EOF
read -r m1 m2 m3 m4 <<EOF
$netmask
EOF
unset IFS
REGEX=""
if [ $m1 -ge 255 ]; then
REGEX="${REGEX}$i1"
else
REGEX="${REGEX}[0-9]{1,3}"
fi
REGEX="${REGEX}\\\\\\\\."
if [ $m2 -ge 255 ]; then
REGEX="${REGEX}$i2"
else
REGEX="${REGEX}[0-9]{1,3}"
fi
REGEX="${REGEX}\\\\\\\\."
if [ $m3 -ge 255 ]; then
REGEX="${REGEX}$i3"
else
REGEX="${REGEX}[0-9]{1,3}"
fi
REGEX="${REGEX}\\\\\\\\."
if [ $m4 -ge 255 ]; then
REGEX="${REGEX}$i4"
else
REGEX="${REGEX}[0-9]{1,3}"
fi
echo "$REGEX"
}
##
## Util functions.
##
getfqdn() {
n=$1
fqdn=`cat $OURDIR/fqdn.map | grep -E "$n\s" | cut -f2`
echo $fqdn
}
getcontrolip() {
n=$1
fqdn=`getfqdn $n`
ip=`host -4 $fqdn | sed -nre 's/.* has address ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/\1/p'`
echo $ip
}
service_init_reload() {
if [ ${HAVE_SYSTEMD} -eq 1 ]; then
$SUDO systemctl daemon-reload
fi
}
service_enable() {
service=$1
if [ ${HAVE_SYSTEMD} -eq 0 ]; then
$SUDO update-rc.d $service enable
else
$SUDO systemctl enable $service
fi
}
service_disable() {
service=$1
if [ ${HAVE_SYSTEMD} -eq 0 ]; then
$SUDO update-rc.d $service disable
else
$SUDO systemctl disable $service
fi
}
service_restart() {
service=$1
if [ ${HAVE_SYSTEMD} -eq 0 ]; then
$SUDO service $service restart
else
$SUDO systemctl restart $service
fi
}
service_stop() {
service=$1
if [ ${HAVE_SYSTEMD} -eq 0 ]; then
$SUDO service $service stop
else
$SUDO systemctl stop $service
fi
}
service_start() {
service=$1
if [ ${HAVE_SYSTEMD} -eq 0 ]; then
$SUDO service $service start
else
$SUDO systemctl start $service
fi
}
GETTER=`which wget`
if [ -n "$GETTER" ]; then
GETTEROUT="$GETTER --remote-encoding=unix -c -O"
GETTER="$GETTER --remote-encoding=unix -c -N"
GETTERLOGARG="-o"
else
GETTER="/bin/false NO WGET INSTALLED!"
GETTEROUT="/bin/false NO WGET INSTALLED!"
fi
get_url() {
if [ -z "$GETTER" ]; then
/bin/false
return
fi
urls="$1"
outfile="$2"
if [ -n "$3" ]; then
retries=$3
else
retries=3
fi
if [ -n "$4" ]; then
interval=$4
else
interval=5
fi
if [ -n "$5" ]; then
force="$5"
else
force=0
fi
if [ -n "$outfile" -a -f "$outfile" -a $force -ne 0 ]; then
rm -f "$outfile"
fi
success=0
tmpfile=`mktemp /tmp/wget.log.XXX`
for url in $urls ; do
tries=$retries
while [ $tries -gt 0 ]; do
if [ -n "$outfile" ]; then
$GETTEROUT $outfile $GETTERLOGARG $tmpfile "$url"
else
$GETTER $GETTERLOGARG $tmpfile "$url"
fi
if [ $? -eq 0 ]; then
if [ -z "$outfile" ]; then
# This is the best way to figure out where wget
# saved a file!
outfile=`bash -c "cat $tmpfile | sed -n -e 's/^.*Saving to: '$'\u2018''\([^'$'\u2019'']*\)'$'\u2019''.*$/\1/p'"`
if [ -z "$outfile" ]; then
outfile=`bash -c "cat $tmpfile | sed -n -e 's/^.*File '$'\u2018''\([^'$'\u2019'']*\)'$'\u2019'' not modified.*$/\1/p'"`
fi
fi
success=1
break
else
sleep $interval
tries=`expr $tries - 1`
fi
done
if [ $success -eq 1 ]; then
break
fi
done
rm -f $tmpfile
if [ $success -eq 1 ]; then
echo "$outfile"
/bin/true
else
/bin/false
fi
}
# Time logging
if [ $FIRSTTIME -ne 0 ]; then
logtend "libfirsttime"
fi