setup-lib.sh 34 KB
Newer Older
1 2
#!/bin/sh

David Johnson's avatar
David Johnson committed
3 4
DIRNAME=`dirname $0`

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# Setup our core vars
#
OURDIR=/root/setup
SETTINGS=$OURDIR/settings
LOCALSETTINGS=$OURDIR/settings.local
TOPOMAP=$OURDIR/topomap
BOOTDIR=/var/emulab/boot
TMCC=/usr/local/etc/emulab/tmcc

mkdir -p $OURDIR
touch $SETTINGS
touch $LOCALSETTINGS
cd $OURDIR

#LOCKFILE="lockfile -1 -r -1 "
LOCKFILE="lockfile-create --retry-count 65535 "
RMLOCKFILE="lockfile-remove "
23
PSWDGEN="openssl rand -hex 10"
24 25
SSH="ssh -o StrictHostKeyChecking=no"
SCP="scp -p -o StrictHostKeyChecking=no"
26

27 28 29
#
# Our default configuration
#
30 31 32 33
CONTROLLER="ctl"
NETWORKMANAGER="nm"
STORAGEHOST="ctl"
OBJECTHOST="ctl"
34
COMPUTENODES=""
35
BAREMETALNODES=""
36 37 38 39 40 41
BLOCKNODES=""
OBJECTNODES=""
DATALAN="lan-1"
MGMTLAN="lan-2"
BLOCKLAN=""
OBJECTLAN=""
42 43 44
DATATUNNELS=1
DATAFLATLANS="lan-1"
DATAVLANS=""
45
DATAVXLANS=0
46
DATAOTHERLANS=""
47
USE_EXISTING_IPS=1
48
DO_APT_INSTALL=1
49
DO_APT_UPGRADE=0
50
DO_APT_UPDATE=1
David Johnson's avatar
David Johnson committed
51
ENABLE_NEW_SERIAL_SUPPORT=0
52
DO_UBUNTU_CLOUDARCHIVE=1
53
BUILD_AARCH64_FROM_CORE=0
54 55
DISABLE_SECURITY_GROUPS=0
DEFAULT_SECGROUP_ENABLE_SSH_ICMP=1
56 57
VERBOSE_LOGGING="False"
DEBUG_LOGGING="False"
58
SUPPORT_DYNAMIC_NODES=0
59 60 61 62
KEYSTONEAPIVERSION=""
TOKENTIMEOUT=14400
SESSIONTIMEOUT=14400
CEILOMETER_USE_WSGI=0
63
QUOTASOFF=1
64 65
# Off by default; seems to cause intermittent keystone unavailability.
KEYSTONEUSEMEMCACHE=0
66 67
# Off by default for Juno; on for Kilo and on by default.
KEYSTONEUSEWSGI=""
68 69 70 71
# On by default; users will have to take full disk images of the
# compute nodes if they have this enabled.
COMPUTE_EXTRA_NOVA_DISK_SPACE="1"

72 73 74 75 76 77 78 79 80 81 82 83
#
# We have an 'adminapi' user that gets a random password.  Then, we have
# the dashboard and instance password, that comes in from geni-lib/rspec as a
# hash, that defaults to the same as the old default profile.
#
# /root/setup/admin-openrc.sh contains the adminapi user, not admin!  We do it
# this way because we need a real passwd so that various CLI tools and openstack
# components have a real user/pass to auth as.
#
ADMIN_API='adminapi'
ADMIN_API_PASS=`$PSWDGEN`
ADMIN='admin'
84 85 86
ADMIN_PASS=''
#ADMIN_PASS_HASH='$6$kOIVUcvsnrD/hETx$JahyKoIJf1EFNI2AWCtfzn3ZBoBfaJrRQkjC0kW6VkTwPI9K3TtEWTh/axrHP.e5mmcM96/bTQs1.e7HSKIk10'
ADMIN_PASS_HASH=''
87 88 89

SWAPPER=`cat $BOOTDIR/swapper`

90 91 92 93 94 95 96 97 98 99 100 101 102
##
## Are we updating?
##
if [ "x$UPDATING" = "x" ]; then
    UPDATING=0
else
    $LOCKFILE $OURDIR/UPDATING
fi
# We might store any new nodes here
NEWNODELIST=""
# We might store any missing nodes here
OLDNODELIST=""

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
##
## Detect if this was a geni experiment
##
grep GENIUSER $SETTINGS
if [ ! $? -eq 0 ]; then
    geni-get slice_urn >/dev/null 2>&1
    if [ $? -eq 0 ]; then
	GENIUSER=1
	echo "GENIUSER=1" >> $SETTINGS
    else
	GENIUSER=0
	echo "GENIUSER=0" >> $SETTINGS
    fi
else
    grep GENIUSER=1 $SETTINGS
    if [ $? -eq 0 ]; then
	GENIUSER=1
    else
	GENIUSER=0
    fi
fi

125 126 127 128 129 130
##
## Grab our geni creds, and create a GENI credential cert
##
#
# NB: force the install of python-m2crypto if geniuser
#
131
if [ $GENIUSER -eq 1 ]; then
132 133
    dpkg -s python-m2crypto >/dev/null 2>&1
    if [ ! $? -eq 0 ]; then
134 135 136 137 138
	apt-get install python-m2crypto
    fi

    if [ ! -e $OURDIR/geni.key ]; then
	geni-get key > $OURDIR/geni.key
139 140 141 142 143 144
	cat $OURDIR/geni.key | grep -q END\ .\*\PRIVATE\ KEY
	if [ $? -eq 0 ]; then
	    HAS_GENI_KEY=1
	else
	    HAS_GENI_KEY=0
	fi
145 146
    else
	HAS_GENI_KEY=1
147 148 149
    fi
    if [ ! -e $OURDIR/geni.certificate ]; then
	geni-get certificate > $OURDIR/geni.certificate
150 151 152 153 154 155
	cat $OURDIR/geni.certificate | grep -q END\ CERTIFICATE
	if [ $? -eq 0 ]; then
	    HAS_GENI_CERT=1
	else
	    HAS_GENI_CERT=0
	fi
156 157
    else
	HAS_GENI_CERT=1
158 159 160 161 162 163 164 165 166 167
    fi

    if [ ! -e /root/.ssl/encrypted.pem ]; then
	mkdir -p /root/.ssl
	chmod 600 /root/.ssl

	cat $OURDIR/geni.key > /root/.ssl/encrypted.pem
	cat $OURDIR/geni.certificate >> /root/.ssl/encrypted.pem
    fi

168
    if [ ! -e $OURDIR/manifests.xml -o $UPDATING -ne 0 ]; then
169 170 171 172 173 174 175
	if [ $HAS_GENI_CERT -eq 1 ]; then
	    python $DIRNAME/getmanifests.py $OURDIR/manifests
	else
	    # 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
176
    fi
177 178 179 180 181

    if [ ! -e $OURDIR/encrypted_admin_pass ]; then
	cat /root/setup/manifests.0.xml | perl -e '@lines = <STDIN>; $all = join("",@lines); if ($all =~ /^.+<[^:]+:password[^>]*>([^<]+)<\/[^:]+:password>.+/igs) { print $1; }' > $OURDIR/encrypted_admin_pass
    fi

182
    if [ ! -e $OURDIR/decrypted_admin_pass -a -s $OURDIR/encrypted_admin_pass ]; then
183 184
	openssl smime -decrypt -inform PEM -inkey geni.key -in $OURDIR/encrypted_admin_pass -out $OURDIR/decrypted_admin_pass
    fi
185 186
fi

187 188 189 190 191
#
# Suck in user configuration overrides, if we haven't already
#
if [ ! -e $OURDIR/parameters ]; then
    touch $OURDIR/parameters
192
    if [ $GENIUSER -eq 1 ]; then
193
	cat $OURDIR/manifests.0.xml | sed -n -e 's/^[^<]*<[^:]*:parameter>\([^<]*\)<\/[^:]*:parameter>/\1/p' > $OURDIR/parameters
194 195 196
    fi
fi
. $OURDIR/parameters
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
#
# 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

224 225 226 227 228 229 230 231 232 233
CREATOR=`cat $BOOTDIR/creator`
SWAPPER=`cat $BOOTDIR/swapper`
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`
NFQDN="`cat $BOOTDIR/nickname`.$OURDOMAIN"
PFQDN="`cat $BOOTDIR/nodeid`.$OURDOMAIN"
MYIP=`cat $BOOTDIR/myip`
234
EXTERNAL_NETWORK_INTERFACE=`cat $BOOTDIR/controlif`
David Johnson's avatar
David Johnson committed
235
HOSTNAME=`cat ${BOOTDIR}/nickname | cut -f1 -d.`
236
ARCH=`uname -m`
237

David Johnson's avatar
David Johnson committed
238 239 240 241
# Check if our init is systemd
dpkg-query -S /sbin/init | grep -q systemd
HAVE_SYSTEMD=`expr $? = 0`

242 243 244 245 246 247 248
#
# Figure out which OS/OpenStack this is.
#
OSJUNO=10
OSKILO=11
OSLIBERTY=12

David Johnson's avatar
David Johnson committed
249
. /etc/lsb-release
250 251 252 253 254
if [ ${DISTRIB_CODENAME} = "wily" ]; then
    OSCODENAME="liberty"
    OSVERSION=$OSLIBERTY
    REGION="RegionOne"
elif [ ${DISTRIB_CODENAME} = "vivid" ]; then
David Johnson's avatar
David Johnson committed
255
    OSCODENAME="kilo"
256 257
    OSVERSION=$OSKILO
    REGION="RegionOne"
David Johnson's avatar
David Johnson committed
258 259
else
    OSCODENAME="juno"
260 261
    OSVERSION=$OSJUNO
    REGION="regionOne"
David Johnson's avatar
David Johnson committed
262 263
fi

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
#
# Figure out if we got told to use keystone v2 or v3, or what our
# default should be if not.
#
if [ "x$KEYSTONEAPIVERSION" = "x3" ]; then
    # Let them force v3.
    KAPISTR='v3'
elif [ "$KEYSTONEAPIVERSION" != "2" -a $OSVERSION -ge $OSLIBERTY ]; then
    # If they didn't force v2 or v3, if we're on Liberty or higher, make
    # v3 the default
    KAPISTR='v3'
    KEYSTONEAPIVERSION=3
else
    # Otherwise, use version 2 by default (or choice)
    KEYSTONEAPIVERSION=2
    KAPISTR='v2.0'
fi

282 283 284 285 286 287 288 289 290 291 292 293
#
# Figure out if we got told to use keystone wsgi or not, or what our
# default should be if not.
#
if [ "x$KEYSTONEUSEWSGI" = "x" -a $OSVERSION -ge $OSKILO ]; then
    KEYSTONEUSEWSGI=1
elif [ "x$KEYSTONEUSEWSGI" = "x1" ]; then
    # Let them force WSGI
    KEYSTONEUSEWSGI=1
else
    KEYSTONEUSEWSGI=0
fi
294

295
if [ $GENIUSER -eq 1 ]; then
David Johnson's avatar
David Johnson committed
296
    SWAPPER_EMAIL=`geni-get slice_email`
297 298 299 300
else
    SWAPPER_EMAIL="$SWAPPER@$OURDOMAIN"
fi

301
if [ $GENIUSER -eq 1 ]; then
302
    PUBLICADDRS=`cat $OURDIR/manifests.0.xml | perl -e 'while (<STDIN>) { while ($_ =~ m/\<emulab:ipv4 address="([\d.]+)\" netmask=\"([\d\.]+)\"/g) { print "$1\n"; } }' | xargs`
303 304 305 306 307 308 309
    PUBLICCOUNT=0
    for ip in $PUBLICADDRS ; do
	PUBLICCOUNT=`expr $PUBLICCOUNT + 1`
    done
else
    PUBLICADDRS=""
    PUBLICCOUNT=0
310 311
fi

312 313
#
# Grab our topomap so we can see how many nodes we have.
David Johnson's avatar
David Johnson committed
314
# NB: only safe to use topomap for non-fqdn things.
315
#
316 317 318 319
if [ ! -f $TOPOMAP -o $UPDATING -ne 0 ]; then
    if [ -f $TOPOMAP ]; then
	cp -p $TOPOMAP $TOPOMAP.old
    fi
320
    $TMCC topomap | gunzip > $TOPOMAP
321 322 323 324 325
    # 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
326 327 328 329 330 331 332 333 334 335 336
    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
337
	rm -f $OURDIR/fqdn.physical.map
338
    fi
339
fi
340 341

#
342 343 344
# Since some of our testbeds only have one experiment interface per node,
# just do this little hack job -- if MGMTLAN was specified, but it's not in
# the topomap, we null it out and use the VPN Management setup instead.
345
#
346 347 348 349 350 351 352
if [ ! -z "$MGMTLAN" ] ; then
    cat $TOPOMAP | grep "$MGMTLAN"
    if [ $? -ne 0 ] ; then
	echo "*** Cannot find Management LAN $MGMTLAN ; falling back to VPN!"
	MGMTLAN=""
    fi
fi
353

David Johnson's avatar
David Johnson committed
354
#
355 356
# Create a map of node nickname to FQDN (and another one of pnode id to FQDN).
# This supports geni multi-site experiments.
David Johnson's avatar
David Johnson committed
357
#
358 359 360 361 362 363
if [ \( -s $OURDIR/manifests.xml \) -a \( ! \( -s $OURDIR/fqdn.map \) \) ]; then
    cat 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
364 365 366 367 368 369 370 371 372 373 374 375
    # 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
376 377 378 379 380 381 382 383 384

    cat 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 '^bsnode' > $OURDIR/fqdn.physical.map.tmp
    mv $OURDIR/fqdn.physical.map.tmp $OURDIR/fqdn.physical.map
David Johnson's avatar
David Johnson committed
385 386 387 388 389 390
fi

#
# Setup the fqdn map the non-geni way if necessary!
#
if [ ! -s $OURDIR/fqdn.map ]; then
391
    TNODES=`cat $TOPOMAP | grep -v '^#' | sed -n -e 's/^\([a-zA-Z0-9\-]*\),.*:.*$/\1/p' | xargs`
David Johnson's avatar
David Johnson committed
392
    FQDNS=""
393 394 395 396 397 398 399 400
    NODES=""
    for n in $TNODES ; do
	# Filter out any blockstore nodes
	grep -q "HOSTID=$n " /var/emulab/boot/tmcc/storageconfig
	if [ $? -eq 0 ] ; then
	    continue
	fi
	
David Johnson's avatar
David Johnson committed
401 402
	fqdn="$n.$EEID.$EPID.$OURDOMAIN"
	FQDNS="${FQDNS} $fqdn"
403
	NODES="${NODES} $n"
David Johnson's avatar
David Johnson committed
404 405 406 407 408 409 410 411 412 413 414 415 416

	/bin/echo -e "$n\t$fqdn" >> $OURDIR/fqdn.map
    done
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 | xargs`
FQDNS=`cat $OURDIR/fqdn.map | cut -f2 | xargs`

417
if [ -z "${COMPUTENODES}" ]; then
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    # Figure out which networkmanager (netmgr) and controller (ctrl) names we have
    for node in $NODES
    do
	if [ "$node" = "networkmanager" ]; then
	    NETWORKMANAGER="networkmanager"
	fi
	if [ "$node" = "controller" ]; then
	    # If we were using the controller as storage and object host, then
	    # keep using it (just use the old name we've detected).
	    if [ "$STORAGEHOST" = "$CONTROLLER" ]; then
		STORAGEHOST="controller"
	    fi
	    if [ "$OBJECTHOST" = "$CONTROLLER" ]; then
		OBJECTHOST="controller"
	    fi
	    CONTROLLER="controller"
	fi
    done
    # Figure out which ones are the compute nodes
437 438 439 440 441 442 443 444
    for node in $NODES
    do
	if [ "$node" != "$CONTROLLER" -a "$node" != "$NETWORKMANAGER" \
	     -a "$node" != "$STORAGEHOST" -a "$node" != "$OBJECTHOST" ]; then
	    COMPUTENODES="$COMPUTENODES $node"
	fi
    done
fi
445

446 447 448 449
OTHERNODES=""
for node in $NODES
do
    [ "$node" = "$NODEID" ] && continue
450

451 452
    OTHERNODES="$OTHERNODES $node"
done
453

454 455 456 457 458 459 460 461
# Save the node stuff off to settings
grep CONTROLLER $SETTINGS
if [ ! $? = 0 ]; then
    echo "CONTROLLER=\"${CONTROLLER}\"" >> $SETTINGS
    echo "NETWORKMANAGER=\"${NETWORKMANAGER}\"" >> $SETTINGS
    echo "STORAGEHOST=\"${STORAGEHOST}\"" >> $SETTINGS
    echo "OBJECTHOST=\"${OBJECTHOST}\"" >> $SETTINGS
    echo "COMPUTENODES=\"${COMPUTENODES}\"" >> $SETTINGS
462 463 464 465 466 467
elif [ $UPDATING -ne 0 ]; then
    sed -i -e "s/^\(CONTROLLER=\"[^\"]*\"\)\$/CONTROLLER=\"$CONTROLLER\"/" $SETTINGS
    sed -i -e "s/^\(NETWORKMANAGER=\"[^\"]*\"\)\$/NETWORKMANAGER=\"$NETWORKMANAGER\"/" $SETTINGS
    sed -i -e "s/^\(STORAGEHOST=\"[^\"]*\"\)\$/STORAGEHOST=\"$STORAGEHOST\"/" $SETTINGS
    sed -i -e "s/^\(OBJECTHOST=\"[^\"]*\"\)\$/OBJECTHOST=\"$OBJECTHOST\"/" $SETTINGS
    sed -i -e "s/^\(COMPUTENODES=\"[^\"]*\"\)\$/COMPUTENODES=\"$COMPUTENODES\"/" $SETTINGS
468 469
fi

470 471 472 473 474 475 476 477 478 479 480
#
# 0 (true) if networkmanager node is also the controller; 1 if not.
#
unified() {
    if [ "$NETWORKMANAGER" = "$CONTROLLER" ]; then
	return 0
    else
	return 1
    fi
}

David Johnson's avatar
David Johnson committed
481 482 483 484 485
# Setup apt-get to not prompt us
export DEBIAN_FRONTEND=noninteractive
#  -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"
APTGETINSTALLOPTS='-y'
APTGETINSTALL="apt-get install $APTGETINSTALLOPTS"
486 487
# Don't install/upgrade packages if this is not set
if [ ${DO_APT_INSTALL} -eq 0 ]; then
488 489
    APTGETINSTALL="/bin/true ${APTGETINSTALL}"
fi
David Johnson's avatar
David Johnson committed
490

491
if [ ! -f $OURDIR/apt-updated -a "${DO_APT_UPDATE}" = "1" ]; then
492 493 494 495 496 497 498 499
    #
    # 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
500 501 502
    apt-get update
    touch $OURDIR/apt-updated
fi
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
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 \| $?`
	    fi
	    shift
	done
	return $retval
    fi
}

David Johnson's avatar
David Johnson committed
545
if [ ! -f /etc/apt/sources.list.d/cloudarchive-${OSCODENAME}.list \
546 547 548
    -a ! -f $OURDIR/cloudarchive-added \
    -a "${DO_UBUNTU_CLOUDARCHIVE}" = "1" ]; then
    if [ "${DISTRIB_CODENAME}" = "trusty" ] ; then
David Johnson's avatar
David Johnson committed
549 550
	$APTGETINSTALL install -y ubuntu-cloud-keyring
	echo "deb http://ubuntu-cloud.archive.canonical.com/ubuntu" "${DISTRIB_CODENAME}-updates/${OSCODENAME} main" > /etc/apt/sources.list.d/cloudarchive-${OSCODENAME}.list
551 552 553 554 555 556
	apt-get update
    fi

    touch $OURDIR/cloudarchive-added
fi

557 558 559
#
# We rely on crudini in a few spots, instead of sed whacking.
#
560
maybe_install_packages crudini
561

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
#
# Create IP addresses for the Management and Data networks, as necessary.
#
if [ ! -f $OURDIR/nextsparesubnet ] ; then
    #
    # This is horrible, but for openstack networks that need IP addrs, that
    # were not specified in our experiment description (i.e., by Emulab,
    # Cloudlab, or the geni-lib based rspec file), we have to generate them.
    # We stay away from 172.16 because Emulab/Cloudlab use it internally for
    # virtual machine IP addresses.  We also know that Emulab/Cloudlab/geni-lib
    # always allocate addresses starting at 10.1.1.1, and increment the second
    # octet for a new subnet.  We assume that 1) no subnet will ever be larger
    # than 255*255 hosts, and 2) that we can start our subnets at 10.254.0.0
    # and decrement the second octet any time we need to IP a new openstack
    # network.
    #
    NEXTSPARESUBNET=254
    echo ${NEXTSPARESUBNET} > $OURDIR/nextsparesubnet
else
    NEXTSPARESUBNET=`cat $OURDIR/nextsparesubnet`
fi

584
if [ ! -f $OURDIR/mgmt-hosts -o $UPDATING -ne 0 ] ; then
585 586 587
    echo "*** Setting up Management and Data Network IP Addresses"

    if [ -z "${MGMTLAN}" -o ${USE_EXISTING_IPS} -eq 0 ]; then
588 589 590
	if [ $UPDATING -eq 0 ]; then
	    echo "255.255.0.0" > $OURDIR/mgmt-netmask
	    echo "192.168.0.1 $NETWORKMANAGER" > $OURDIR/mgmt-hosts
591 592 593
	    if ! unified ; then
		echo "192.168.0.3 $CONTROLLER" >> $OURDIR/mgmt-hosts
	    fi
594 595 596 597 598 599 600
	    o3=0
	    o4=5
	else
	    o3=`cat $OURDIR/mgmt-o3`
	    o4=`cat $OURDIR/mgmt-o4`
	fi

601 602 603 604 605
	for node in $NODES
	do
	    [ "$node" = "$CONTROLLER" -o "$node" = "$NETWORKMANAGER" ] \
		&& continue

606 607 608 609 610 611
	    # If it already exists, skip it.
	    grep -q ${node}\$ $OURDIR/mgmt-hosts
	    if [ $? -eq 0 ]; then
		continue
	    fi

612 613 614 615 616 617 618 619 620
	    echo "192.168.$o3.$o4 $node" >> $OURDIR/mgmt-hosts

            # Skip 2 for openvpn tun tunnels
	    o4=`expr $o4 + 2`
	    if [ $o4 -gt 253 ] ; then
		o4=10
		o3=`expr $o3 + 1`
	    fi
	done
621 622 623 624

	# Save off our octets for later
	echo "$o3" > $OURDIR/mgmt-o3
	echo "$o4" > $OURDIR/mgmt-o4
625 626 627 628
    else
	cat $TOPOMAP | grep -v '^#' | sed -e 's/,/ /' \
	    | sed -n -e "s/\([a-zA-Z0-9_\-]*\) .*${MGMTLAN}:\([0-9\.]*\).*\$/\2\t\1/p" \
	    > $OURDIR/mgmt-hosts
David Johnson's avatar
David Johnson committed
629
	cat ${BOOTDIR}/tmcc/ifconfig \
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
	    | sed -n -e "s/^.* MASK=\([0-9\.]*\) .* LAN=${MGMTLAN}.*$/\1/p" \
	    > $OURDIR/mgmt-netmask
    fi

    #
    # If USE_EXISTING_IPS is set to 0, we will re-IP those data lan
    # interfaces: networkmanager:eth1 gets 10.z.0.1/8, and controller gets
    # 10.z.0.3/8; and the compute nodes get 10.z.x.y, where x starts at 1,
    # and y starts at 1 and does not exceed 254.
    #
    # We only assign IPs to flat networks because they are the only networks
    # that the physical hosts (i.e., controller, networkmanager, compute nodes)
    # have an interface on.  Technically, we don't *need* to do this, but do it.
    # One thing it gives us is the ability to run GRE or VXLAN networks over the
    # flat networks.
    #
    if [ ${USE_EXISTING_IPS} -eq 0 ]; then
647
	for lan in $DATAFLATLANS $DATAVLANS $DATAOTHERLANS ; do
648 649 650 651 652 653 654
	    if [ $UPDATING -eq 0 ]; then
		prefix="10.$NEXTSPARESUBNET"
		echo "$prefix" > $OURDIR/data-prefix.$lan
		echo "255.255.0.0" > $OURDIR/data-netmask.$lan
		echo "$prefix.0.0/255.255.0.0" > $OURDIR/data-cidr.$lan
		echo "$prefix.0.0" > $OURDIR/data-network.$lan
		echo "$prefix.0.1 $NETWORKMANAGER" > $OURDIR/data-hosts.$lan
655 656 657
		if ! unified ; then
		    echo "$prefix.0.3 $CONTROLLER" >> $OURDIR/data-hosts.$lan
		fi
658 659 660 661 662 663 664 665 666 667 668 669

                #
                # Now set static IPs for the compute nodes.
                #
		o3=1
		o4=1
	    else
		prefix=`cat $OURDIR/data-prefix.$lan`
		o3=`cat $OURDIR/data-o3.$lan`
		o4=`cat $OURDIR/data-o4.$lan`
	    fi

670 671 672 673 674
	    for node in $NODES
	    do
		[ "$node" = "$CONTROLLER" -o "$node" = "$NETWORKMANAGER" ] \
		    && continue

675 676 677 678 679 680 681
                # If it already exists, skip it.
		grep -q ${node}\$ $OURDIR/data-hosts.$lan
		if [ $? -eq 0 ]; then
		    continue
		fi

		echo "$prefix.$o3.$o4 $node" >> $OURDIR/data-hosts
682 683 684 685 686 687 688 689 690

                # Skip 2 for openvpn tun tunnels
		o4=`expr $o4 + 1`
		if [ $o4 -gt 254 ] ; then
		    o4=10
		    o3=`expr $o3 + 1`
		fi
	    done

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
	    if [ $o3 -gt 128 ]; then
		echo "ERROR: more physical hosts than $prefix.$o3 ; aborting!"
		exit 1
	    fi

	    # Save off our octets for later
	    echo $o3 > $OURDIR/data-o3.$lan
	    echo $o4 > $OURDIR/data-o4.$lan

	    if [ $UPDATING -eq 0 ]; then
	        # Start the pool at the next availble addr!  Don't skip.
	        # XXX: could cause problems for adding new phys hosts... oh well.
		o3=`expr $o3 + 10`
		if [ $SUPPORT_DYNAMIC_NODES -eq 1 ]; then
		    # Well, ok, so, let's just start at 128.  Why?  That
		    # leaves us room for 128*255-N physical hosts, plus
		    # 128*255 virtual machines at any time (and
		    # openstack will reuse ip addrs I'm sure).  So we
		    # have a long time before phys node wraparound...
		    echo "*** Changing from calculated o3=$o3/o4=$o4 to o3=128/o4=$o4 to support dynamic nodes..."
		    o3=128
		fi
	        # Also save two addrs, one for the dhcp agent, and one for the
	        # router interface
		echo "start=$prefix.$o3.3,end=10.$NEXTSPARESUBNET.254.254" \
		    > $OURDIR/data-allocation-pool.$lan
717

718 719
		echo "$prefix.$o3.1" > $OURDIR/router-ipaddr.$lan
		echo "$prefix.$o3.2" > $OURDIR/dhcp-agent-ipaddr.$lan
720

721 722
		NEXTSPARESUBNET=`expr $NEXTSPARESUBNET + 1`
	    fi
723 724
	done
    else
725
	for lan in $DATAFLATLANS $DATAOTHERLANS ; do
726 727 728
	    cat $TOPOMAP | grep -v '^#' | sed -e 's/,/ /' \
		| sed -n -e "s/\([a-zA-Z0-9_\-]*\) .*${lan}:\([0-9\.]*\).*\$/\2\t\1/p" \
		> $OURDIR/data-hosts.$lan
David Johnson's avatar
David Johnson committed
729
	    netmask=`cat ${BOOTDIR}/tmcc/ifconfig \
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  		         | sed -n -e "s/^.* MASK=\([0-9\.]*\) .* LAN=${lan}.*$/\1/p"`
	    echo "$netmask" > $OURDIR/data-netmask.$lan
	    nmdataip=`cat $OURDIR/data-hosts.${lan} | grep ${NETWORKMANAGER} | sed -n -e 's/^\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*$/\1/p'`
	    IFS=.
	    read -r i1 i2 i3 i4 <<EOF
$nmdataip
EOF
	    read -r m1 m2 m3 m4 <<EOF
$netmask
EOF
	    unset IFS
	    network=`printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((i4 & m4))"`
	    echo "$network/$netmask" > $OURDIR/data-cidr.$lan
	    echo "$network" > $OURDIR/data-network.$lan

745 746 747 748 749 750 751 752 753 754 755
	    if [ $UPDATING -eq 0 ]; then
	        #
	        # Setup our allocation pool
	        #
	        # First grab all our IP addresses
		allips=`cat $OURDIR/data-hosts.$lan | sed -n -e 's/^\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*$/\1/p'`
		gi1=0; gi2=0; gi3=0; gi4=0;
	        # Figure out the max currently-used IP in this subnet
		for ip in ${allips} ; do
		    IFS=.
		    read -r i1 i2 i3 i4 <<EOF
756 757
$ip
EOF
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
		    unset IFS
		    if [ $i1 -gt $gi1 ]; then gi1=$i1; gi2=0;gi3=0;gi4=0; fi
		    if [ $i2 -gt $gi2 ]; then gi2=$i2; gi3=0;gi4=0; fi
		    if [ $i3 -gt $gi3 ]; then gi3=$i3; gi4=0; fi
		    if [ $i4 -gt $gi4 ]; then gi4=$i4; fi
		done
	        # Get the next available one...
	        # Note, we don't try to stay inside the netmask :(
		gi4=`expr $gi4 + 1`
		if [ $gi4 = 255 ]; then gi4=1; gi3=`expr $gi3 + 1`; fi
		if [ $gi3 = 255 ]; then gi3=1; gi2=`expr $gi2 + 1`; fi
		if [ $gi2 = 255 ]; then gi2=1; gi1=`expr $gi1 + 1`; fi

		endaddr=`printf "%d.%d.%d.%d\n" "$((i1 | ((~m1)+256)))" "$((i2 | ((~m2)+256)))" "$((i3 | ((~m3)+256)))" "$((i4 | ((~m4)+256)))"`

	        #
                # So, this previous calculation is correct, but openstack doesn't
	        # like 255 in any of the octets!  Argh!  So, "fix" any that have it.
                # Argh...
                #
		IFS=.
		read -r i1 i2 i3 i4 <<EOF
780 781
$endaddr
EOF
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
		unset IFS
		if [ $i1 = 255 ]; then i1=254; fi
		if [ $i2 = 255 ]; then i2=254; fi
		if [ $i3 = 255 ]; then i3=254; fi
		if [ $i4 = 255 ]; then i4=254; fi
		endaddr="$i1.$i2.$i3.$i4"

	        # Also save two addrs, one for the dhcp agent, and one for the
	        # router interface
		echo "$gi1.$gi2.$gi3.$gi4" > $OURDIR/router-ipaddr.$lan
		gi4=`expr $gi4 + 1`
		echo "$gi1.$gi2.$gi3.$gi4" > $OURDIR/dhcp-agent-ipaddr.$lan
		gi4=`expr $gi4 + 1`
	        # Start the pool at the next availble addr!  Don't skip.
	        # XXX: could cause problems for adding new phys hosts... oh well.
		if [ $SUPPORT_DYNAMIC_NODES -eq 1 ]; then
		    # Well, ok, so, let's just start at 128.  Why?  That
		    # leaves us room for 128*255-N physical hosts, plus
		    # 128*255 virtual machines at any time (and
		    # openstack will reuse ip addrs I'm sure).  So we
		    # have a long time before phys node wraparound...
		    echo "*** Changing from gi3=$gi3/gi4=$gi4 to gi3=128/gi4=1 to support dynamic nodes..."
		    gi3=128
		    gi4=1
		fi
		# Write the allocation pool.
		echo "start=$gi1.$gi2.$gi3.$gi4,end=$endaddr" \
		    > $OURDIR/data-allocation-pool.$lan
	    fi
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
	done
    fi

    # Save off nextsparesubnet
    echo "${NEXTSPARESUBNET}" > $OURDIR/nextsparesubnet
fi

#
# Setup IP configuration for neutron tunnel nets
#
if [ ${DATATUNNELS} -gt 0 ]; then
    i=0
    while [ $i -lt ${DATATUNNELS} ]; do
	LAN="tun${i}"
	subnet=${NEXTSPARESUBNET}

	if [ -f $OURDIR/ipinfo.$LAN ]; then
	    i=`expr $i + 1`
	    continue
	fi

	echo "LAN='$LAN'" >> $OURDIR/ipinfo.$LAN
	echo "ALLOCATION_POOL='start=10.${subnet}.1.1,end=10.${subnet}.254.254'" >> $OURDIR/ipinfo.$LAN
	echo "CIDR='10.$subnet.0.0/255.255.0.0'" >> $OURDIR/ipinfo.$LAN

	NEXTSPARESUBNET=`expr ${NEXTSPARESUBNET} - 1`
	i=`expr $i + 1`
    done

    # Save off nextsparesubnet
    echo "${NEXTSPARESUBNET}" > $OURDIR/nextsparesubnet
fi

#
# Setup IP configuration for vlan networks
#
for lan in $DATAVLANS ; do
    LAN="$lan"
    subnet=${NEXTSPARESUBNET}

    if [ -f $OURDIR/ipinfo.$LAN ]; then
	continue
    fi

    echo "LAN='$LAN'" >> $OURDIR/ipinfo.$LAN
    echo "ALLOCATION_POOL='start=10.${subnet}.1.1,end=10.${subnet}.254.254'" >> $OURDIR/ipinfo.$LAN
    echo "CIDR='10.$subnet.0.0/255.255.0.0'" >> $OURDIR/ipinfo.$LAN

    NEXTSPARESUBNET=`expr ${NEXTSPARESUBNET} - 1`
done
# Save off nextsparesubnet
echo "${NEXTSPARESUBNET}" > $OURDIR/nextsparesubnet

864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
#
# Setup IP configuration for neutron vxlan nets
#
if [ ${DATAVXLANS} -gt 0 ]; then
    i=0
    while [ $i -lt ${DATAVXLANS} ]; do
	LAN="vxlan${i}"
	subnet=${NEXTSPARESUBNET}

	if [ -f $OURDIR/ipinfo.$LAN ]; then
	    i=`expr $i + 1`
	    continue
	fi

	echo "LAN='$LAN'" >> $OURDIR/ipinfo.$LAN
	echo "ALLOCATION_POOL='start=10.${subnet}.1.1,end=10.${subnet}.254.254'" >> $OURDIR/ipinfo.$LAN
	echo "CIDR='10.$subnet.0.0/255.255.0.0'" >> $OURDIR/ipinfo.$LAN

	NEXTSPARESUBNET=`expr ${NEXTSPARESUBNET} - 1`
	i=`expr $i + 1`
    done

    # Save off nextsparesubnet
    echo "${NEXTSPARESUBNET}" > $OURDIR/nextsparesubnet
fi

890 891 892 893
#
# NB: this IP/mask is only valid after setting up the management network IP
# addresses because they might not be the Emulab ones.
#
894
MGMTIP=`cat /etc/hosts | grep -E "$NODEID$" | head -1 | sed -n -e 's/^\\([0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*\\).*$/\\1/p'`
David Johnson's avatar
David Johnson committed
895
MGMTNETMASK=`cat $OURDIR/mgmt-netmask`
896 897 898 899
if [ -z "$MGMTLAN" ] ; then
    MGMTMAC=""
    MGMT_NETWORK_INTERFACE="tun0"
else
David Johnson's avatar
David Johnson committed
900
    cat ${BOOTDIR}/tmcc/ifconfig | grep "IFACETYPE=vlan" | grep "${MGMTLAN}"
David Johnson's avatar
David Johnson committed
901 902
    if [ $? = 0 ]; then
	MGMTVLAN=1
David Johnson's avatar
David Johnson committed
903
	MGMTMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* VMAC=\([0-9a-f:\.]*\) .* LAN=${MGMTLAN}.*\$/\1/p"`
David Johnson's avatar
David Johnson committed
904 905 906 907
	MGMT_NETWORK_INTERFACE=`/usr/local/etc/emulab/findif -m $MGMTMAC`
	MGMTVLANDEV=`ip link show ${MGMT_NETWORK_INTERFACE} | sed -n -e "s/^.*${MGMT_NETWORK_INTERFACE}\@\([0-9a-zA-Z_]*\): .*\$/\1/p"`
    else
	MGMTVLAN=0
David Johnson's avatar
David Johnson committed
908
	MGMTMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/.* MAC=\([0-9a-f:\.]*\) .* LAN=${MGMTLAN}/\1/p"`
David Johnson's avatar
David Johnson committed
909 910
	MGMT_NETWORK_INTERFACE=`/usr/local/etc/emulab/findif -m $MGMTMAC`
    fi
911
fi
912 913 914 915 916

#
# NB: this IP/mask is only valid after data ips have been assigned, because
# they might not be the Emulab ones.
#
917
for lan in $DATAFLATLANS $DATAOTHERLANS ; do
918 919 920 921
    if [ -e $OURDIR/info.$lan ] ; then
	continue
    fi

922
    DATAIP=`cat $OURDIR/data-hosts.$lan | grep -E "$NODEID$" | sed -n -e 's/^\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*$/\1/p'`
923
    DATANETMASK=`cat $OURDIR/data-netmask.$lan`
David Johnson's avatar
David Johnson committed
924
    cat ${BOOTDIR}/tmcc/ifconfig | grep "IFACETYPE=vlan" | grep "${lan}"
925 926
    if [ $? = 0 ]; then
	DATAVLAN=1
David Johnson's avatar
David Johnson committed
927
	DATAMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* VMAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*\$/\1/p"`
928 929
	DATADEV=`/usr/local/etc/emulab/findif -m $DATAMAC`
	DATAVLANDEV=`ip link show ${DATADEV} | sed -n -e "s/^.*${DATADEV}\@\([0-9a-zA-Z_]*\): .*\$/\1/p"`
David Johnson's avatar
David Johnson committed
930
	DATAVLANTAG=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* LAN=${lan} VTAG=\([0-9]*\).*\$/\1/p"`
931 932 933 934
    else
	DATAVLAN=0
	DATAVLANDEV=""
	DATAVLANTAG=0
David Johnson's avatar
David Johnson committed
935
	DATAMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* MAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*$/\1/p"`
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
	DATADEV=`/usr/local/etc/emulab/findif -m $DATAMAC`
    fi

    echo "DATABRIDGE=br-${lan}" >> $OURDIR/info.$lan
    echo "DATAIP=${DATAIP}" >> $OURDIR/info.$lan
    echo "DATANETMASK=${DATANETMASK}" >> $OURDIR/info.$lan
    echo "DATAVLAN=${DATAVLAN}" >> $OURDIR/info.$lan
    echo "DATAVLANTAG=${DATAVLANTAG}" >> $OURDIR/info.$lan
    echo "DATAVLANDEV=${DATAVLANDEV}" >> $OURDIR/info.$lan
    echo "DATAMAC=${DATAMAC}" >> $OURDIR/info.$lan
    echo "DATADEV=${DATADEV}" >> $OURDIR/info.$lan
done

for lan in $DATAVLANS ; do
    if [ -e $OURDIR/info.$lan ] ; then
	continue
    fi

954
    #DATAIP=`cat $OURDIR/data-hosts.$lan | grep -E "$NODEID$" | sed -n -e 's/^\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*$/\1/p'`
955
    #DATANETMASK=`cat $OURDIR/data-netmask.$lan`
David Johnson's avatar
David Johnson committed
956
    DATAVLAN=1
David Johnson's avatar
David Johnson committed
957
    DATAMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* VMAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*\$/\1/p"`
958 959
    DATADEV=`/usr/local/etc/emulab/findif -m $DATAMAC`
    DATAVLANDEV=`ip link show ${DATADEV} | sed -n -e "s/^.*${DATADEV}\@\([0-9a-zA-Z_]*\): .*\$/\1/p"`
David Johnson's avatar
David Johnson committed
960
    DATAVLANTAG=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* LAN=${lan} VTAG=\([0-9]*\).*\$/\1/p"`
961

962 963 964 965 966 967 968 969 970 971 972 973 974 975
    echo "DATABRIDGE=br-${DATAVLANDEV}" >> $OURDIR/info.$lan
    #echo "DATAIP=${DATAIP}" >> $OURDIR/info.$lan
    #echo "DATANETMASK=${DATANETMASK}" >> $OURDIR/info.$lan
    echo "DATAVLAN=${DATAVLAN}" >> $OURDIR/info.$lan
    echo "DATAVLANTAG=${DATAVLANTAG}" >> $OURDIR/info.$lan
    echo "DATAVLANDEV=${DATAVLANDEV}" >> $OURDIR/info.$lan
    echo "DATAMAC=${DATAMAC}" >> $OURDIR/info.$lan
    echo "DATADEV=${DATADEV}" >> $OURDIR/info.$lan
done

##
## Setup some one-time neutron configuration variables, based on our network
## configuration
##
976
if [ ! -f $OURDIR/neutron.vars ]; then
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    #
    # Which type drives do we want to configure?
    #
    network_types=""
    # NB: we always configure flat, because the external net is flat
    #if [ -n "${DATAFLATLANS}" ]; then
	if [ -n "${network_types}" ]; then
	    network_types="${network_types},"
	fi
	network_types="${network_types}flat"
    #fi
    if [ -n "${DATAFLATLANS}" -a ${DATATUNNELS} -gt 0 ]; then
	if [ -n "${network_types}" ]; then
	    network_types="${network_types},"
	fi
	network_types="${network_types}gre"
    fi
    if [ -n "${DATAVLANS}" ]; then
	if [ -n "${network_types}" ]; then
	    network_types="${network_types},"
	fi
	network_types="${network_types}vlan"
    fi
1000 1001 1002 1003 1004 1005
    if [ -n "${DATAVXLANS}" ]; then
	if [ -n "${network_types}" ]; then
	    network_types="${network_types},"
	fi
	network_types="${network_types}vxlan"
    fi
1006

1007
    echo "network_types=\"${network_types}\"" >> $OURDIR/neutron.vars
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

    #
    # What are our flat networks?
    #
    flat_networks="external"
    for lan in $DATAFLATLANS ; do
	if [ -n "${flat_networks}" ]; then
	    flat_networks="${flat_networks},"
	fi
	flat_networks="${flat_networks}${lan}"
    done

1020
    echo "flat_networks=\"${flat_networks}\"" >> $OURDIR/neutron.vars
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

    #
    # Figure out the bridge mappings
    #
    bridge_mappings="bridge_mappings=external:br-ex"
    for lan in $DATAFLATLANS ; do
	. $OURDIR/info.${lan}
	bridge_mappings="${bridge_mappings},${lan}:${DATABRIDGE}"
    done
    for lan in $DATAVLANS ; do
	. $OURDIR/info.${lan}
	# NB: neutron doesn't like to see the same map entry multiple times...
	echo "$bridge_mappings" | grep -q "${DATAVLANDEV}:"
	if [ $? = 0 ] ; then
	    continue;
	else
	    bridge_mappings="${bridge_mappings},${DATAVLANDEV}:${DATABRIDGE}"
	fi
    done

1041
    echo "bridge_mappings=\"${bridge_mappings}\"" >> $OURDIR/neutron.vars
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

    #
    # Figure out the network_vlan_ranges
    #
    network_vlan_ranges=""
    for lan in $DATAVLANS ; do
	. $OURDIR/info.${lan}
	if [ -n "${network_vlan_ranges}" ] ; then
	    network_vlan_ranges="${network_vlan_ranges},"
	fi
	network_vlan_ranges="${network_vlan_ranges}${DATAVLANDEV}:${DATAVLANTAG}:${DATAVLANTAG}"
    done

1055
    echo "network_vlan_ranges=\"network_vlan_ranges=${network_vlan_ranges}\"" >> $OURDIR/neutron.vars
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

    #
    # What's our first flat network, which will host our GRE tunnels?
    #
    gre_local_ip=""
    enable_tunneling=""
    tunnel_types=""
    for lan in $DATAFLATLANS ; do
	. $OURDIR/info.$lan

        # Just use the first one
	gre_local_ip="local_ip = $DATAIP"
	enable_tunneling="enable_tunneling = True"
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	tunnel_types=""
	if [ ${DATATUNNELS} -gt 0 ]; then
	    if [ -z "${tunnel_types}" ]; then
		tunnel_types="tunnel_types = gre"
	    else
		tunnel_types="${tunnel_types},gre"
	    fi
	fi
	if [ ${DATAVXLANS} -gt 0 ]; then
	    if [ -z "${tunnel_types}" ]; then
		tunnel_types="tunnel_types = vxlan"
	    else
		tunnel_types="${tunnel_types},vxlan"
	    fi
	fi
1084 1085 1086 1087

	break
    done

1088 1089 1090
    echo "gre_local_ip=\"${gre_local_ip}\"" >> $OURDIR/neutron.vars
    echo "enable_tunneling=\"${enable_tunneling}\"" >> $OURDIR/neutron.vars
    echo "tunnel_types=\"${tunnel_types}\"" >> $OURDIR/neutron.vars
1091 1092

fi
David Johnson's avatar
David Johnson committed
1093

1094 1095 1096 1097 1098 1099 1100
##
## Finally, if we had been UPDATING, remove the lockfile!
##
if [ $UPDATING -ne 0 ]; then
    $RMLOCKFILE $OURDIR/UPDATING
fi

David Johnson's avatar
David Johnson committed
1101 1102 1103 1104 1105 1106
##
## Util functions.
##

getfqdn() {
    n=$1
1107
    fqdn=`cat $OURDIR/fqdn.map | grep -E "$n\s" | cut -f2`
David Johnson's avatar
David Johnson committed
1108 1109
    echo $fqdn
}
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

service_enable() {
    service=$1
    if [ ${HAVE_SYSTEMD} -eq 0 ]; then
	update-rc.d $service enable
    else
	systemctl enable $service
    fi
}

service_disable() {
    service=$1
    if [ ${HAVE_SYSTEMD} -eq 0 ]; then
	update-rc.d $service disable
    else
	systemctl disable $service
    fi
}

service_restart() {
    service=$1
    if [ ${HAVE_SYSTEMD} -eq 0 ]; then
	service $service restart
    else
	systemctl restart $service
    fi
}

service_stop() {
    service=$1
    if [ ${HAVE_SYSTEMD} -eq 0 ]; then
	service $service stop
    else
	systemctl stop $service
    fi
}

service_start() {
    service=$1
    if [ ${HAVE_SYSTEMD} -eq 0 ]; then
	service $service start
    else
	systemctl start $service
    fi
}