setup-lib.sh 45.8 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
#
# 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

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
# 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

49 50 51 52 53 54
mkdir -p $OURDIR
touch $SETTINGS
touch $LOCALSETTINGS
cd $OURDIR

#LOCKFILE="lockfile -1 -r -1 "
55
LOCKFILE="lockfile-create --retry 65535 "
56
RMLOCKFILE="lockfile-remove "
57
PSWDGEN="openssl rand -hex 10"
58 59
SSH="ssh -o StrictHostKeyChecking=no"
SCP="scp -p -o StrictHostKeyChecking=no"
60

61 62 63
#
# Our default configuration
#
64 65 66
CONTROLLER="ctl"
NETWORKMANAGER="nm"
STORAGEHOST="ctl"
67
SHAREHOST="ctl"
68
OBJECTHOST="ctl"
69
COMPUTENODES=""
70
BAREMETALNODES=""
71 72 73 74 75 76
BLOCKNODES=""
OBJECTNODES=""
DATALAN="lan-1"
MGMTLAN="lan-2"
BLOCKLAN=""
OBJECTLAN=""
77 78 79
DATATUNNELS=1
DATAFLATLANS="lan-1"
DATAVLANS=""
80
DATAVXLANS=0
81
DATAOTHERLANS=""
82
USE_EXISTING_IPS=1
83
DO_APT_INSTALL=1
84
DO_APT_UPGRADE=0
85
DO_APT_DIST_UPGRADE=0
86
DO_APT_UPDATE=1
87 88
UBUNTUMIRRORHOST=""
UBUNTUMIRRORPATH=""
David Johnson's avatar
David Johnson committed
89
ENABLE_NEW_SERIAL_SUPPORT=0
David Johnson's avatar
David Johnson committed
90
DO_UBUNTU_CLOUDARCHIVE=0
91
DO_UBUNTU_CLOUDARCHIVE_STAGING=0
92
BUILD_AARCH64_FROM_CORE=0
93
DISABLE_SECURITY_GROUPS=0
94
ENABLE_HOST_PASSTHROUGH=0
95
DEFAULT_SECGROUP_ENABLE_SSH_ICMP=1
96 97
VERBOSE_LOGGING="False"
DEBUG_LOGGING="False"
98
SUPPORT_DYNAMIC_NODES=0
99 100 101 102
KEYSTONEAPIVERSION=""
TOKENTIMEOUT=14400
SESSIONTIMEOUT=14400
CEILOMETER_USE_WSGI=0
103
QUOTASOFF=1
104 105
# Off by default; seems to cause intermittent keystone unavailability.
KEYSTONEUSEMEMCACHE=0
106 107
# Off by default for Juno; on for Kilo and on by default.
KEYSTONEUSEWSGI=""
108 109 110
# 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"
111 112 113
# Support linuxbridge plugin too, but still default to openvswitch.
ML2PLUGIN="openvswitch"
MANILADRIVER="generic"
114
EXTRAIMAGEURLS=""
115
LINUXBRIDGE_STATIC=0
116 117 118 119 120
# If set to 1, and if OSRELEASE >= OSNEWTON, the physical machines will
# use the MGMTIP as the primary DNS server (in preference to the real
# control net DNS server).  The local domain will also be searched prior
# to the cluster's domain.
USE_DESIGNATE_AS_RESOLVER=1
121 122 123
# We are not currently using the ceilometer stats, and they do not work
# as of Pike due to the switch to Gnocchi as the measurement DB.
ENABLE_OPENSTACK_SLOTHD=0
David Johnson's avatar
David Johnson committed
124 125
# The input OpenStack release, if any, from profile params.
OSRELEASE=""
126

127 128 129 130 131 132 133 134 135 136 137 138
#
# 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'
139 140 141
ADMIN_PASS=''
#ADMIN_PASS_HASH='$6$kOIVUcvsnrD/hETx$JahyKoIJf1EFNI2AWCtfzn3ZBoBfaJrRQkjC0kW6VkTwPI9K3TtEWTh/axrHP.e5mmcM96/bTQs1.e7HSKIk10'
ADMIN_PASS_HASH=''
142 143 144

SWAPPER=`cat $BOOTDIR/swapper`

145 146 147 148 149
##
## Are we updating?
##
if [ "x$UPDATING" = "x" ]; then
    UPDATING=0
150
elif [ ! $UPDATING -eq 0 ]; then
151 152 153 154 155 156 157
    $LOCKFILE $OURDIR/UPDATING
fi
# We might store any new nodes here
NEWNODELIST=""
# We might store any missing nodes here
OLDNODELIST=""

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
##
## 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

180 181 182 183 184 185
##
## Grab our geni creds, and create a GENI credential cert
##
#
# NB: force the install of python-m2crypto if geniuser
#
186
if [ $GENIUSER -eq 1 ]; then
187 188
    dpkg -s python-m2crypto >/dev/null 2>&1
    if [ ! $? -eq 0 ]; then
189
	apt-get install python-m2crypto
190 191 192 193 194 195 196 197
	# Keep trying again with updated cache forever;
	# we must have this package.
	success=$?
	while [ ! $success -eq 0 ]; do
	    apt-get update
	    apt-get install python-m2crypto
	    success=$?
	done
198 199 200 201
    fi

    if [ ! -e $OURDIR/geni.key ]; then
	geni-get key > $OURDIR/geni.key
202 203 204 205 206 207
	cat $OURDIR/geni.key | grep -q END\ .\*\PRIVATE\ KEY
	if [ $? -eq 0 ]; then
	    HAS_GENI_KEY=1
	else
	    HAS_GENI_KEY=0
	fi
208 209
    else
	HAS_GENI_KEY=1
210 211 212
    fi
    if [ ! -e $OURDIR/geni.certificate ]; then
	geni-get certificate > $OURDIR/geni.certificate
213 214 215 216 217 218
	cat $OURDIR/geni.certificate | grep -q END\ CERTIFICATE
	if [ $? -eq 0 ]; then
	    HAS_GENI_CERT=1
	else
	    HAS_GENI_CERT=0
	fi
219 220
    else
	HAS_GENI_CERT=1
221 222 223 224 225 226 227 228 229 230
    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

231
    if [ ! -e $OURDIR/manifests.xml -o $UPDATING -ne 0 ]; then
232 233 234 235 236 237 238
	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
239
    fi
240 241 242 243 244

    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

245
    if [ ! -e $OURDIR/decrypted_admin_pass -a -s $OURDIR/encrypted_admin_pass ]; then
246 247
	openssl smime -decrypt -inform PEM -inkey geni.key -in $OURDIR/encrypted_admin_pass -out $OURDIR/decrypted_admin_pass
    fi
248 249
fi

250 251 252 253 254
#
# Suck in user configuration overrides, if we haven't already
#
if [ ! -e $OURDIR/parameters ]; then
    touch $OURDIR/parameters
255
    if [ $GENIUSER -eq 1 ]; then
256
	cat $OURDIR/manifests.0.xml | sed -n -e 's/^[^<]*<[^:]*:parameter>\([^<]*\)<\/[^:]*:parameter>/\1/p' > $OURDIR/parameters
257 258 259
    fi
fi
. $OURDIR/parameters
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
#
# 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

287 288 289 290 291 292 293 294 295 296
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`
297
EXTERNAL_NETWORK_INTERFACE=`cat $BOOTDIR/controlif`
David Johnson's avatar
David Johnson committed
298
HOSTNAME=`cat ${BOOTDIR}/nickname | cut -f1 -d.`
299
ARCH=`uname -m`
300

David Johnson's avatar
David Johnson committed
301 302 303 304
# Check if our init is systemd
dpkg-query -S /sbin/init | grep -q systemd
HAVE_SYSTEMD=`expr $? = 0`

305 306 307 308 309 310
#
# Figure out which OS/OpenStack this is.
#
OSJUNO=10
OSKILO=11
OSLIBERTY=12
311
OSMITAKA=13
David Johnson's avatar
David Johnson committed
312
OSNEWTON=14
313
OSOCATA=15
David Johnson's avatar
David Johnson committed
314
OSPIKE=16
315

David Johnson's avatar
David Johnson committed
316
. /etc/lsb-release
David Johnson's avatar
David Johnson committed
317 318 319 320 321 322 323 324 325 326
#
# Allow a specific release to trump the image defaults, maybe.
#
if [ ! "x$OSRELEASE" = "x" ]; then
    OSCODENAME="$OSRELEASE"
    if [ $OSCODENAME = "juno" ]; then OSVERSION=$OSJUNO ; fi
    if [ $OSCODENAME = "kilo" ]; then OSVERSION=$OSKILO ; fi
    if [ $OSCODENAME = "liberty" ]; then OSVERSION=$OSLIBERTY ; fi
    if [ $OSCODENAME = "mitaka" ]; then OSVERSION=$OSMITAKA ; fi
    if [ $OSCODENAME = "newton" ]; then OSVERSION=$OSNEWTON ; fi
327
    if [ $OSCODENAME = "ocata" ]; then OSVERSION=$OSOCATA ; fi
David Johnson's avatar
David Johnson committed
328
    if [ $OSCODENAME = "pike" ]; then OSVERSION=$OSPIKE ; fi
David Johnson's avatar
David Johnson committed
329 330 331 332 333 334 335 336 337

    #
    # We only use cloudarchive for LTS images!
    #
    echo "$DISTRIB_DESCRIPTION" | grep -qi LTS
    if [ $? -eq 0 ]; then
	DO_UBUNTU_CLOUDARCHIVE=1
    fi
elif [ ${DISTRIB_CODENAME} = "wily" ]; then
338 339 340
    OSCODENAME="liberty"
    OSVERSION=$OSLIBERTY
elif [ ${DISTRIB_CODENAME} = "vivid" ]; then
David Johnson's avatar
David Johnson committed
341
    OSCODENAME="kilo"
342
    OSVERSION=$OSKILO
343 344 345
elif [ ${DISTRIB_CODENAME} = "xenial" ]; then
    OSCODENAME="mitaka"
    OSVERSION=$OSMITAKA
David Johnson's avatar
David Johnson committed
346 347
else
    OSCODENAME="juno"
348
    OSVERSION=$OSJUNO
David Johnson's avatar
David Johnson committed
349 350
fi

351 352 353 354 355 356 357
#
# Default memcached fully on for Mitaka or greater.  Too slow without it.
#
if [ $OSVERSION -ge $OSMITAKA ]; then
    KEYSTONEUSEMEMCACHE=1
fi

David Johnson's avatar
David Johnson committed
358
if [ $OSVERSION -eq $OSJUNO ]; then
359
    REGION="regionOne"
David Johnson's avatar
David Johnson committed
360 361
else
    REGION="RegionOne"
David Johnson's avatar
David Johnson committed
362 363
fi

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#
# 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

David Johnson's avatar
David Johnson committed
382 383 384 385
#
# Figure out Nova API string.
#
NAPISTR="v2"
386
if [ $OSVERSION -ge $OSMITAKA ]; then
David Johnson's avatar
David Johnson committed
387 388 389
    NAPISTR="v2.1"
fi

390 391 392 393 394 395 396 397 398 399 400 401
#
# 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
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
#
# The keystone auth_token parameter names are project_domain_name and
# user_domain_name as of Mitaka.  The auth_token parameter name has
# also changed.
#
if [ $OSVERSION -ge $OSMITAKA ]; then
    PROJECT_DOMAIN_PARAM="project_domain_name"
    USER_DOMAIN_PARAM="user_domain_name"
    AUTH_TYPE_PARAM="auth_type"
else
    PROJECT_DOMAIN_PARAM="project_domain_id"
    USER_DOMAIN_PARAM="user_domain_id"
    AUTH_TYPE_PARAM="auth_plugin"
fi

418 419 420 421 422 423 424 425 426 427
#
# Set the database package name and driver string.
#
if [ $OSVERSION -ge $OSNEWTON ]; then
    DBDPACKAGE="python-pymysql"
    DBDSTRING="mysql+pymysql"
else
    DBDPACKAGE="python-mysqldb"
    DBDSTRING="mysql"
fi
428

429
if [ $GENIUSER -eq 1 ]; then
David Johnson's avatar
David Johnson committed
430
    SWAPPER_EMAIL=`geni-get slice_email`
431 432 433 434
else
    SWAPPER_EMAIL="$SWAPPER@$OURDOMAIN"
fi

435
if [ $GENIUSER -eq 1 ]; then
436
    PUBLICADDRS=`cat $OURDIR/manifests.*.xml | perl -e '$found = 0; while (<STDIN>) { if ($_ =~ /\<[\d\w:]*routable_pool [^\>\<]*\/>/) { print STDERR "DEBUG: found empty pool: $_\n"; next; } if ($_ =~ /\<[\d\w:]*routable_pool [^\>]*client_id=['"'"'"]'$NETWORKMANAGER'['"'"'"]/) { $found = 1; print STDERR "DEBUG: found: $_\n" } if ($found) { while ($_ =~ m/\<emulab:ipv4 address="([\d.]+)\" netmask=\"([\d\.]+)\"/g) { print "$1\n"; } } if ($found && $_ =~ /routable_pool\>/) { print STDERR "DEBUG: end found: $_\n"; $found = 0; } }' | xargs`
437 438 439 440 441 442 443
    PUBLICCOUNT=0
    for ip in $PUBLICADDRS ; do
	PUBLICCOUNT=`expr $PUBLICCOUNT + 1`
    done
else
    PUBLICADDRS=""
    PUBLICCOUNT=0
444 445
fi

446 447
#
# Grab our topomap so we can see how many nodes we have.
David Johnson's avatar
David Johnson committed
448
# NB: only safe to use topomap for non-fqdn things.
449
#
450 451 452 453
if [ ! -f $TOPOMAP -o $UPDATING -ne 0 ]; then
    if [ -f $TOPOMAP ]; then
	cp -p $TOPOMAP $TOPOMAP.old
    fi
454 455 456 457 458 459 460 461 462 463

    # First try via manifest; fall back to tmcc if necessary (although
    # that will break multisite exps with >1 second cluster node(s)).
    python2 $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

464 465 466 467 468
    # 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
469 470 471 472 473 474 475 476 477 478 479
    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
480
	rm -f $OURDIR/fqdn.physical.map
481
    fi
482
fi
483 484

#
485 486 487
# 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.
488
#
489 490 491 492 493 494 495
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
496

David Johnson's avatar
David Johnson committed
497
#
498 499
# 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
500
#
501 502 503 504 505 506
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
507 508 509 510 511 512 513 514 515 516 517 518
    # 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
519 520 521 522
    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
523 524 525 526 527 528 529

    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
530 531 532 533 534 535
    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
536
    mv $OURDIR/fqdn.physical.map.tmp $OURDIR/fqdn.physical.map
David Johnson's avatar
David Johnson committed
537 538 539 540 541 542
fi

#
# Setup the fqdn map the non-geni way if necessary!
#
if [ ! -s $OURDIR/fqdn.map ]; then
543
    TNODES=`cat $TOPOMAP | grep -v '^#' | sed -n -e 's/^\([a-zA-Z0-9\-]*\),.*:.*$/\1/p' | xargs`
David Johnson's avatar
David Johnson committed
544
    FQDNS=""
545 546 547 548 549 550 551
    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
552 553 554 555 556
	# Filter out any firewall nodes
	if [ "$n" = "fw" -o "$n" = "fw-s2" ]; then
	    continue
	fi

David Johnson's avatar
David Johnson committed
557 558
	fqdn="$n.$EEID.$EPID.$OURDOMAIN"
	FQDNS="${FQDNS} $fqdn"
559
	NODES="${NODES} $n"
David Johnson's avatar
David Johnson committed
560 561 562 563 564 565 566 567 568 569 570 571 572

	/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`

573
if [ -z "${COMPUTENODES}" ]; then
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    # 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
593 594 595 596 597 598 599 600
    for node in $NODES
    do
	if [ "$node" != "$CONTROLLER" -a "$node" != "$NETWORKMANAGER" \
	     -a "$node" != "$STORAGEHOST" -a "$node" != "$OBJECTHOST" ]; then
	    COMPUTENODES="$COMPUTENODES $node"
	fi
    done
fi
601

602 603 604 605
OTHERNODES=""
for node in $NODES
do
    [ "$node" = "$NODEID" ] && continue
606

607 608
    OTHERNODES="$OTHERNODES $node"
done
609

610 611 612 613 614 615 616 617
# 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
618 619 620 621 622 623
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
624 625
fi

626 627 628 629 630 631 632 633 634 635 636
#
# 0 (true) if networkmanager node is also the controller; 1 if not.
#
unified() {
    if [ "$NETWORKMANAGER" = "$CONTROLLER" ]; then
	return 0
    else
	return 1
    fi
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
##
## 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 ..."
	sed -E -i.us.archive.ubuntu.com -e "s|(${oldstr})|$newstr|" /etc/apt/sources.list
    fi

    echo "MIRRORSETUP=1" >> $SETTINGS
fi

David Johnson's avatar
David Johnson committed
658
# Setup apt-get to not prompt us
659 660
echo "force-confdef" > /etc/dpkg/dpkg.cfg.d/cloudlab
echo "force-confold" >> /etc/dpkg/dpkg.cfg.d/cloudlab
David Johnson's avatar
David Johnson committed
661
export DEBIAN_FRONTEND=noninteractive
662 663
# -o Dpkg::Options::="--force-confold" -o Dpkg::Options::="--force-confdef" 
DPKGOPTS=''
David Johnson's avatar
David Johnson committed
664
APTGETINSTALLOPTS='-y'
665
APTGETINSTALL="apt-get $DPKGOPTS install $APTGETINSTALLOPTS"
666 667
# Don't install/upgrade packages if this is not set
if [ ${DO_APT_INSTALL} -eq 0 ]; then
668 669
    APTGETINSTALL="/bin/true ${APTGETINSTALL}"
fi
David Johnson's avatar
David Johnson committed
670

671
if [ ! -f $OURDIR/apt-updated -a "${DO_APT_UPDATE}" = "1" ]; then
672 673 674 675 676 677 678 679
    #
    # 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
680 681 682
    apt-get update
    touch $OURDIR/apt-updated
fi
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
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
725 726 727 728 729 730 731 732 733 734
if [ ! -f $OURDIR/cloudarchive-added -a "${DO_UBUNTU_CLOUDARCHIVE}" = "1" ]; then
    #if [ "${DISTRIB_CODENAME}" = "trusty" ] ; then
    #	$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
    #	apt-get update
    #elif [ "${DISTRIB_CODENAME}" = "xenial" ] ; then
	maybe_install_packages software-properties-common
	# Disable unattended upgrades!
	rm -fv /etc/apt/apt.conf.d/*unattended-upgrades
	add-apt-repository -y cloud-archive:$OSRELEASE
735
	if [ "${DO_UBUNTU_CLOUDARCHIVE_STAGING}" = "1" ]; then
736
	    add-apt-repository -y cloud-archive:${OSRELEASE}-proposed
737
	fi
738
	apt-get update
David Johnson's avatar
David Johnson committed
739
    #fi
740 741 742 743

    touch $OURDIR/cloudarchive-added
fi

744 745 746 747 748 749 750 751 752 753 754 755 756 757
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
	apt-mark hold $pkg
    done
    apt-get dist-upgrade -y
    for pkg in $PKGS; do
	apt-mark unhold $pkg
    done
    touch $OURDIR/apt-dist-upgraded
fi

758 759 760
#
# We rely on crudini in a few spots, instead of sed whacking.
#
761
maybe_install_packages crudini
762

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
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
}

781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
#
# 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

803
if [ ! -f $OURDIR/mgmt-hosts -o $UPDATING -ne 0 ] ; then
804 805 806
    echo "*** Setting up Management and Data Network IP Addresses"

    if [ -z "${MGMTLAN}" -o ${USE_EXISTING_IPS} -eq 0 ]; then
807 808 809
	if [ $UPDATING -eq 0 ]; then
	    echo "255.255.0.0" > $OURDIR/mgmt-netmask
	    echo "192.168.0.1 $NETWORKMANAGER" > $OURDIR/mgmt-hosts
810 811 812
	    if ! unified ; then
		echo "192.168.0.3 $CONTROLLER" >> $OURDIR/mgmt-hosts
	    fi
813 814 815 816 817 818 819
	    o3=0
	    o4=5
	else
	    o3=`cat $OURDIR/mgmt-o3`
	    o4=`cat $OURDIR/mgmt-o4`
	fi

820 821 822 823 824
	for node in $NODES
	do
	    [ "$node" = "$CONTROLLER" -o "$node" = "$NETWORKMANAGER" ] \
		&& continue

825 826 827 828 829 830
	    # If it already exists, skip it.
	    grep -q ${node}\$ $OURDIR/mgmt-hosts
	    if [ $? -eq 0 ]; then
		continue
	    fi

831 832 833 834 835 836 837 838 839
	    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
840 841 842 843

	# Save off our octets for later
	echo "$o3" > $OURDIR/mgmt-o3
	echo "$o4" > $OURDIR/mgmt-o4
844 845 846 847
    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
848
	cat ${BOOTDIR}/tmcc/ifconfig \
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	    | 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
866
	for lan in $DATAFLATLANS $DATAVLANS $DATAOTHERLANS ; do
867 868 869 870
	    if [ $UPDATING -eq 0 ]; then
		prefix="10.$NEXTSPARESUBNET"
		echo "$prefix" > $OURDIR/data-prefix.$lan
		echo "255.255.0.0" > $OURDIR/data-netmask.$lan
871
		echo "$prefix.0.0/16" > $OURDIR/data-cidr.$lan
872 873
		echo "$prefix.0.0" > $OURDIR/data-network.$lan
		echo "$prefix.0.1 $NETWORKMANAGER" > $OURDIR/data-hosts.$lan
874 875 876
		if ! unified ; then
		    echo "$prefix.0.3 $CONTROLLER" >> $OURDIR/data-hosts.$lan
		fi
877 878 879 880 881 882 883 884 885 886 887 888

                #
                # 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

889 890 891 892 893
	    for node in $NODES
	    do
		[ "$node" = "$CONTROLLER" -o "$node" = "$NETWORKMANAGER" ] \
		    && continue

894 895 896 897 898 899 900
                # 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
901 902 903 904 905 906 907 908 909

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

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	    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
936

937 938
		echo "$prefix.$o3.1" > $OURDIR/router-ipaddr.$lan
		echo "$prefix.$o3.2" > $OURDIR/dhcp-agent-ipaddr.$lan
939

940 941
		NEXTSPARESUBNET=`expr $NEXTSPARESUBNET + 1`
	    fi
942 943
	done
    else
944
	for lan in $DATAFLATLANS $DATAOTHERLANS ; do
945 946 947
	    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
948
	    netmask=`cat ${BOOTDIR}/tmcc/ifconfig \
949 950 951 952 953 954 955 956 957 958 959 960
  		         | 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))"`
961 962
	    cidr=`python $DIRNAME/ipcalc.py mask2bits $netmask`
	    echo "$network/$cidr" > $OURDIR/data-cidr.$lan
963 964
	    echo "$network" > $OURDIR/data-network.$lan

965 966 967 968 969 970 971 972 973 974 975
	    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
976 977
$ip
EOF
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
		    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
1000 1001
$endaddr
EOF
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
		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
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	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
1044
	if [ -f "$OURDIR/ipinfo.tun${i}" ]; then
1045 1046 1047 1048
	    i=`expr $i + 1`
	    continue
	fi

1049 1050 1051
	LAN="tun${i}"
	subnet=${NEXTSPARESUBNET}

1052 1053
	echo "LAN='$LAN'" >> $OURDIR/ipinfo.$LAN
	echo "ALLOCATION_POOL='start=10.${subnet}.1.1,end=10.${subnet}.254.254'" >> $OURDIR/ipinfo.$LAN
1054
	echo "CIDR='10.$subnet.0.0/16'" >> $OURDIR/ipinfo.$LAN
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067

	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
1068
    if [ -f $OURDIR/ipinfo.$lan ]; then
1069 1070 1071
	continue
    fi

1072 1073 1074
    LAN="$lan"
    subnet=${NEXTSPARESUBNET}

1075 1076
    echo "LAN='$LAN'" >> $OURDIR/ipinfo.$LAN
    echo "ALLOCATION_POOL='start=10.${subnet}.1.1,end=10.${subnet}.254.254'" >> $OURDIR/ipinfo.$LAN
1077
    echo "CIDR='10.$subnet.0.0/16'" >> $OURDIR/ipinfo.$LAN
1078 1079 1080 1081 1082 1083

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

1084 1085 1086 1087 1088 1089
#
# Setup IP configuration for neutron vxlan nets
#
if [ ${DATAVXLANS} -gt 0 ]; then
    i=0
    while [ $i -lt ${DATAVXLANS} ]; do
1090
	if [ -f "$OURDIR/ipinfo.vxlan${i}" ]; then
1091 1092 1093 1094
	    i=`expr $i + 1`
	    continue
	fi

1095 1096 1097
	LAN="vxlan${i}"
	subnet=${NEXTSPARESUBNET}

1098 1099
	echo "LAN='$LAN'" >> $OURDIR/ipinfo.$LAN
	echo "ALLOCATION_POOL='start=10.${subnet}.1.1,end=10.${subnet}.254.254'" >> $OURDIR/ipinfo.$LAN
1100
	echo "CIDR='10.$subnet.0.0/16'" >> $OURDIR/ipinfo.$LAN
1101 1102 1103 1104 1105 1106 1107 1108 1109

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

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

1110 1111 1112 1113
#
# NB: this IP/mask is only valid after setting up the management network IP
# addresses because they might not be the Emulab ones.
#
1114 1115 1116
if [ ! -e $OURDIR/info.mgmt ]; then
    MGMTIP=`grep -E "$NODEID$" $OURDIR/mgmt-hosts | head -1 | sed -n -e 's/^\\([0-9]*\\.[0-9]*\\.[0-9]*\\.[0-9]*\\).*$/\\1/p'`
    MGMTNETMASK=`cat $OURDIR/mgmt-netmask`
1117
    MGMTPREFIX=`netmask2prefix $MGMTNETMASK`
1118
    if [ -z "$MGMTLAN" ] ; then
David Johnson's avatar
David Johnson committed
1119
	MGMTVLAN=0
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	MVMTVLANDEV=
	MGMTMAC=""
	MGMT_NETWORK_INTERFACE="tun0"
    else
	cat ${BOOTDIR}/tmcc/ifconfig | grep "IFACETYPE=vlan" | grep "${MGMTLAN}"
	if [ $? = 0 ]; then
	    MGMTVLAN=1
	    MGMTMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* VMAC=\([0-9a-f:\.]*\) .* LAN=${MGMTLAN}.*\$/\1/p"`
	    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
	    MGMTMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/.* MAC=\([0-9a-f:\.]*\) .* LAN=${MGMTLAN}/\1/p"`
	    MGMT_NETWORK_INTERFACE=`/usr/local/etc/emulab/findif -m $MGMTMAC`
	    MGMTVLANDEV=
	fi
David Johnson's avatar
David Johnson committed
1136
    fi
1137 1138
    echo "MGMTIP='$MGMTIP'" >> $OURDIR/info.mgmt
    echo "MGMTNETMASK='$MGMTNETMASK'" >> $OURDIR/info.mgmt
1139
    echo "MGMTPREFIX='$MGMTPREFIX'" >> $OURDIR/info.mgmt
1140 1141 1142 1143 1144 1145
    echo "MGMTVLAN=$MGMTVLAN" >> $OURDIR/info.mgmt
    echo "MGMTMAC='$MGMTMAC'" >> $OURDIR/info.mgmt
    echo "MGMT_NETWORK_INTERFACE='$MGMT_NETWORK_INTERFACE'" >> $OURDIR/info.mgmt
    echo "MGMTVLANDEV='$MGMTVLANDEV'" >> $OURDIR/info.mgmt
else
    . $OURDIR/info.mgmt
1146
fi
1147 1148 1149 1150 1151

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

1157
    DATAIP=`cat $OURDIR/data-hosts.$lan | grep -E "$NODEID$" | sed -n -e 's/^\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*$/\1/p'`
1158
    DATANETMASK=`cat $OURDIR/data-netmask.$lan`
David Johnson's avatar
David Johnson committed
1159
    cat ${BOOTDIR}/tmcc/ifconfig | grep "IFACETYPE=vlan" | grep "${lan}"
1160 1161
    if [ $? = 0 ]; then
	DATAVLAN=1
David Johnson's avatar
David Johnson committed
1162
	DATAMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* VMAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*\$/\1/p"`
1163 1164
	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
1165
	DATAVLANTAG=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* LAN=${lan} VTAG=\([0-9]*\).*\$/\1/p"`
1166
	DATAPMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* PMAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*\$/\1/p"`
1167 1168 1169 1170
    else
	DATAVLAN=0
	DATAVLANDEV=""
	DATAVLANTAG=0
David Johnson's avatar
David Johnson committed
1171
	DATAMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* MAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*$/\1/p"`
1172
	DATADEV=`/usr/local/etc/emulab/findif -m $DATAMAC`
1173
	DATAPMAC=
1174 1175 1176 1177 1178 1179 1180 1181 1182
    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
1183
    echo "DATAPMAC=${DATAPMAC}" >> $OURDIR/info.$lan
1184 1185 1186 1187 1188 1189 1190 1191
    echo "DATADEV=${DATADEV}" >> $OURDIR/info.$lan
done

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

1192
    #DATAIP=`cat $OURDIR/data-hosts.$lan | grep -E "$NODEID$" | sed -n -e 's/^\([0-9]*.[0-9]*.[0-9]*.[0-9]*\).*$/\1/p'`
1193
    #DATANETMASK=`cat $OURDIR/data-netmask.$lan`
David Johnson's avatar
David Johnson committed
1194
    DATAVLAN=1
David Johnson's avatar
David Johnson committed
1195
    DATAMAC=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* VMAC=\([0-9a-f:\.]*\) .* LAN=${lan}.*\$/\1/p"`
1196 1197
    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
1198
    DATAVLANTAG=`cat ${BOOTDIR}/tmcc/ifconfig | sed -n -e "s/^.* LAN=${lan} VTAG=\([0-9]*\).*\$/\1/p"`
1199

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    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
##
1214
if [ ! -f $OURDIR/neutron.vars ]; then
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
    #
    # 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
1238 1239 1240 1241 1242 1243
    if [ -n "${DATAVXLANS}" ]; then
	if [ -n "${network_types}" ]; then
	    network_types="${network_types},"
	fi
	network_types="${network_types}vxlan"
    fi
1244

1245
    echo "network_types=\"${network_types}\"" >> $OURDIR/neutron.vars
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

    #
    # 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

1258
    echo "flat_networks=\"${flat_networks}\"" >> $OURDIR/neutron.vars
1259 1260 1261 1262

    #
    # Figure out the bridge mappings
    #
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    # NB: We can only control the name of the external br-ex bridge,
    # because only the Neutron linuxbridge driver accepts both a map of
    # physical networks to physical interfaces; and physical network to
    # bridge names.  Nova assumes that the bridge it must plug a device
    # into is named according to the physical network uuid.  Thus, for
    # the linuxbridge case, we only setup bridge_mappings for
    # br-ex... modulo a flag.  Hopefully in the future they will see the
    # sense in allowing static bridge configurations.
    #
    bridge_mappings="bridge_mappings=external:br-ex"
    extra_mappings=""
    if [ "${ML2PLUGIN}" = "linuxbridge" ]; then
	extra_mappings="physical_interface_mappings=external:${EXTERNAL_NETWORK_INTERFACE}"
1276
    fi
1277 1278
    for lan in $DATAFLATLANS ; do
	. $OURDIR/info.${lan}
1279 1280 1281 1282 1283
	if [ "${ML2PLUGIN}" = "linuxbridge" ]; then
	    extra_mappings="${extra_mappings},${lan}:${DATADEV}"
	    if [ $LINUXBRIDGE_STATIC -eq 1 ]; then
		bridge_mappings="${bridge_mappings},${lan}:${DATABRIDGE}"
	    fi
1284
	else
1285
	    bridge_mappings="${bridge_mappings},${lan}:${DATABRIDGE}"
1286
	fi
1287 1288 1289 1290 1291 1292 1293 1294
    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
1295 1296 1297 1298 1299
	    if [ "${ML2PLUGIN}" = "linuxbridge" ]; then
		extra_mappings="${extra_mappings},${lan}:${DATADEV}"
		if [ $LINUXBRIDGE_STATIC -eq 1 ]; then
		    bridge_mappings="${bridge_mappings},${DATAVLANDEV}:${DATABRIDGE}"
		fi
1300
	    else
1301
		bridge_mappings="${bridge_mappings},${DATAVLANDEV}:${DATABRIDGE}"
1302
	    fi
1303 1304 1305
	fi
    done

1306
    echo "bridge_mappings=\"${bridge_mappings}\"" >> $OURDIR/neutron.vars
1307
    echo "extra_mappings=\"${extra_mappings}\"" >> $OURDIR/neutron.vars
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320

    #
    # 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

1321
    echo "network_vlan_ranges=\"network_vlan_ranges=${network_vlan_ranges}\"" >> $OURDIR/neutron.vars
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

    #
    # 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"
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	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
1350 1351 1352 1353

	break
    done

1354 1355 1356
    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
1357

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
    if [ "${ML2PLUGIN}" = "openvswitch" ]; then
	interface_driver='neutron.agent.linux.interface.OVSInterfaceDriver'
    else
	interface_driver='neutron.agent.linux.interface.BridgeInterfaceDriver'
    fi

    echo "interface_driver=\"${interface_driver}\"" >> $OURDIR/neutron.vars
    
    fwdriver=""
    if [ "${ML2PLUGIN}" = "openvswitch" ]; then
	fwdriver="neutron.agent.linux.iptables_firewall.OVSHybridIptablesFirewallDriver"
    else
	fwdriver="neutron.agent.linux.iptables_firewall.IptablesFirewallDriver"
    fi
    if [ ${DISABLE_SECURITY_GROUPS} -eq 1 ]; then
	fwdriver="neutron.agent.firewall.NoopFirewallDriver"
    fi

    echo "fwdriver=\"${fwdriver}\"" >> $OURDIR/neutron.vars

1378
fi
David Johnson's avatar
David Johnson committed
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388
#
# Emulab tmcc finds the bossip via first server in /etc/resolv.conf,
# ugh, and we might change /etc/resolv.conf if we are installing
# Designate on >= Ocata.  So force it to find the bossip via this file
# instead.  Previously, we had done this near the bottom of
# setup-controller.sh, but this change has to be made in a
# multi-cluster-compatible manner; the bossip could be different for
# phys node at different clusters.
#
David Johnson's avatar
David Johnson committed
1389
if [ ! -f /etc/emulab/bossnode -a $OSVERSION -ge $OSNEWTON -a "${USE_DESIGNATE_AS_RESOLVER}" = "1" ]; then
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
    mydomain=`hostname | sed -n -e 's/[^\.]*\.\(.*\)$/\1/p'`
    mynameserver=`sed -n -e 's/^nameserver \([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*\).*$/\1/p' < /etc/resolv.conf | head -1`
    if [ -z "$mynameserver" ]; then
	mynameserver=`dig +short boss.$mydomain A`
    fi
    if [ -n "$mynameserver" ]; then
	echo $mynameserver > /etc/emulab/bossnode
    fi
fi

1400 1401 1402 1403 1404 1405 1406
##
## 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
1407 1408 1409 1410 1411 1412
##
## Util functions.
##

getfqdn() {
    n=$1
1413
    fqdn=`cat $OURDIR/fqdn.map | grep -E "$n\s" | cut -f2`
David Johnson's avatar
David Johnson committed
1414 1415
    echo $fqdn
}
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460

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
}
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539

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
}
1540 1541 1542 1543 1544

# Time logging
if [ $FIRSTTIME -ne 0 ]; then
    logtend "libfirsttime"
fi