update-testbed.in 11.3 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh B Stoller's avatar
Leigh B Stoller committed
3
# Copyright (c) 2010-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26
#
use strict;
use English;
use Getopt::Std;
27
use Data::Dumper;
28 29 30 31 32 33

#
# Update DB.
#
sub usage()
{
34 35 36 37 38 39
    print STDERR "Usage: update-testbed\n";
    print STDERR "-v     - Verify install scripts by loading them only\n";
    print STDERR "-n     - Run install script in impotent mode\n";
    print STDERR "-d     - Debugging mode\n";
    print STDERR "-i     - Run 'gmake boss-install' after running scripts\n";
    print STDERR "-s     - Stop/Restart the testbed before/after running scripts\n";
40 41
    exit(-1);
}
42 43 44 45 46 47 48 49 50 51 52
my $optlist   = "idsvfn";
my $debug     = 0;
my $force     = 0;
my $verify    = 0;
my $install   = 0;
my $stopbed   = 0;
my $stopped   = 0;
my $nologins  = 0;
my $committed = 0;
my $impotent  = 0;
my $forceopt  = "";
53 54 55 56 57 58 59 60 61

#
# Configure variables
#
my $TB	         = "@prefix@";
my $TBOPS        = "@TBOPSEMAIL@";
my $DBNAME       = "@TBDBNAME@";
my $SRCDIR       = "@top_srcdir@";
my $SQLUPDATEDIR = "$SRCDIR/sql/updates";
62
my $PGENIUPDATEDIR = "$SRCDIR/protogeni/updates";
63 64
my $UPDATEDIR    = "$SRCDIR/install/updates";
my $DBUPDATE     = "$TB/sbin/dbupdate";
65 66
my $PGENIUPDATE  = "$TB/sbin/protogeni/update";
my $PGENISUPPORT = @PROTOGENI_SUPPORT@;
67
my $INSTALLUPDATE= "$TB/sbin/update-install";
68
my $SETSITEVAR   = "$TB/sbin/setsitevar";
69
my $MFRISBEED	 = "/usr/local/etc/rc.d/3.mfrisbeed.sh";
70
my $STARTUP      = "/usr/local/etc/rc.d/3.testbed.sh";
71 72 73 74
my $tempfile     = "/var/tmp/update.$$";

# Protos
sub Fatal($);
75
sub CheckForReboot();
76 77

# un-taint path
78
$ENV{'PATH'} = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/site/bin';
79 80 81 82 83 84 85 86 87 88 89 90 91
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# We want to force this to run from the build tree to avoid any
# problems with the install tree not being in a consistent state.
#
BEGIN
{
92
    if (-e "../Makeconf") {
93
	my $srcdir = "@top_srcdir@";
94 95
	my $objdir = `/bin/pwd`;
	chomp($objdir);
96
	# Prior to first install or running from object dir.
97
	unshift(@INC, "$objdir/../db");
98 99 100 101 102 103 104
	if ($srcdir =~ /^\//) {
	    unshift(@INC, "$srcdir/install");
	    unshift(@INC, "$srcdir/tbsetup");
	} else {
	    unshift(@INC, "$objdir/$srcdir/install");
	    unshift(@INC, "$objdir/$srcdir/tbsetup");
	}
105
	unshift(@INC, "$objdir/../tbsetup");
106 107
    }
}
108
use emdb;
109
use libEmulab;
110
use libtestbed;
111
use Image;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

#
# Parse command arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"f"})) {
    $force = 1;
    $forceopt = "-f";
}
if (defined($options{"i"})) {
    $install = 1;
}
if (defined($options{"v"})) {
    $verify = 1;
}
133 134 135 136 137 138
if (defined($options{"s"})) {
    $stopbed = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
139 140 141 142

#
# Must be root if actually doing this.
#
143
if (!($verify || $impotent) && $UID) {
144 145 146 147 148 149 150
    Fatal("This script must be run as root! Maybe use sudo?")
}

if (! (-e "../db/dbupdate" && -e "./update-install")) {
    Fatal("Cannot find dbupdate script! Running from the object dir?");
}

151 152 153
my $objdir = `/bin/pwd`;
chomp($objdir);

154 155 156 157 158 159 160
my $abssrcdir;
if ($SRCDIR =~ /^\//) {
    $abssrcdir = "$SRCDIR";
} else {
    $abssrcdir = "$objdir/$SRCDIR";
}

161
my @INCDIRS = ("-I${objdir}", "-I${objdir}/../tbsetup",
162 163 164
	       "-I${objdir}/../db", "-I${objdir}/../event/lib",
	       "-I${objdir}/../clientside/lib/event", 
	       "-I${objdir}/../clientside/lib/libtb", 
165 166
	       "-I${abssrcdir}/tbsetup",
	       "-I${abssrcdir}/install",
167
	       "-I${abssrcdir}/clientside/lib/event",
168 169 170
	       # To catch a few extra things that do not normally change.
	       "-I@prefix@/lib"
	       );
171 172 173
if ($PGENISUPPORT) {
    @INCDIRS = (@INCDIRS, "-I${objdir}/../protogeni/lib");
}
174

175
# Chicken or Egg.
Mike Hibler's avatar
Mike Hibler committed
176 177 178
# XXX only set specific fields as the ns_include field may not be there yet.
DBQueryFatal("INSERT INTO sitevariables (name,value,defaultvalue,description)".
	     " VALUES ".
179 180 181
	     " ('general/testbed_shutdown',NULL,'0', ".
	     "  'Non-zero value indicates that the testbed is shutdown ".
	     "and scripts should not do anything when they run. ".
Mike Hibler's avatar
Mike Hibler committed
182
	     "DO NOT SET THIS BY HAND!')")
183 184
    if (!SiteVarExists("general/testbed_shutdown"));

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#
# Dump the testbed version numbers in all modes.
#
print "Testbed version info:\n";
system("perl @INCDIRS ./testbed-version");

#
# Impotent mode, just runs the install scripts. 
#
if ($impotent) {
    system("perl @INCDIRS ./update-install -n -p $UPDATEDIR");
    if ($?) {
	exit(1);
    }
    CheckForReboot();
    exit(0);
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
#
# In verify mode, just run the update scripts in verify.
#
if ($verify) {
    my $exitval = 0;
    
    print "Checking for DB updates ...\n";
    system("perl @INCDIRS ../db/dbupdate -q -v -p $SQLUPDATEDIR $DBNAME");
    $exitval++
	if ($?);
    
    print "Checking for install updates ...\n";
    system("perl @INCDIRS ./update-install -q -v -p $UPDATEDIR");
    $exitval++
	if ($?);

219 220 221 222 223 224 225 226
    if ($PGENISUPPORT) {
	print "Checking for ProtoGENI DB updates ...\n";
	system("perl @INCDIRS ../protogeni/scripts/update ".
	       "       -q -v -p $PGENIUPDATEDIR");
	$exitval++
	    if ($?);
    }

227 228 229 230 231 232 233 234
    exit(0)
	if (!$exitval);

    print "*** You have update scripts that have not been run.\n";
    print "    As a safety measure, you will not be able to install until\n";
    print "    this is resolved. Typically, you can resolve this by:\n\n";
    print "        boss> cd /your/object/dir\n";
    print "        boss> gmake update-testbed\n\n";
235 236
    print "    More information is available at:\n";
    print "       https://users.emulab.net/trac/emulab/wiki/install/update-testbed-v2\n\n";
237 238 239
    exit($exitval);
}

240 241 242 243 244 245 246 247 248 249
#
# Cleanup handler when stopping the testbed.
#
sub Restart()
{
    if ($committed) {
	print "*** Update has progressed too far; you must renable by hand\n";
	return;
    }
    if ($stopped) {
Leigh B Stoller's avatar
Leigh B Stoller committed
250 251 252 253 254 255 256 257 258 259 260 261
	#
	# Need to to do this so the rc script knows it is okay to start.
	#  0  - Testbed is enabled.
	#  1  - Testbed is disabled.
	# -1  - Testbed is coming back online, so start up daemons.
	#
	system("$SETSITEVAR general/testbed_shutdown -1");
	if ($?) {
	    print "*** Could not set general/testbed_shutdown to -1\n";
	    exit(1);
	}
	
262
	print "Starting up testbed daemons.\n";
263
	if (system("$MFRISBEED start") || system("$STARTUP start")) {
264 265 266
	    print "*** Could not restart testbed daemons.\n";
	    exit(1);
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
267
	print "\n";
268 269 270
    }
    if ($nologins) {
	print "Turning on the web interface and allowing swaps\n";
271 272
	if (system("$SETSITEVAR general/testbed_shutdown -") ||
	    system("$SETSITEVAR web/nologins -") ||
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	    system("$SETSITEVAR web/message -")) {
	    print "*** Could not renable the web interface and swapping!\n";
	    exit(1);
	}
    }
}

sub Handler()
{
    Restart();
    exit(1);
}

if ($stopbed) {
    $SIG{INT} = \&Handler;
    
    #
    # Stop the testbed before doing the update.
    #
    print "** Putting the testbed to sleep before updating ...\n";
    print "-> Turning off the web interface and disallowing swaps.\n";

    my $msg = "Testbed software update in progress; back in a little while";
    $msg =~ s|\&|&amp;|g;
    $msg =~ s|\<|&lt;|g;
    system("$SETSITEVAR","web/message", $msg);
    if ($?) {
	Fatal("Could not update web message");
    }
    system("$SETSITEVAR web/nologins 1");
    if ($?) {
	Fatal("Could not disable web logins and experiment swaps");
    }
306 307 308 309
    system("$SETSITEVAR general/testbed_shutdown 1");
    if ($?) {
	Fatal("Could not set general/testbed_shutdown");
    }
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    $nologins = 1;
    
    print "-> Waiting a few seconds for testbed to quiet down ...\n";
    sleep(5);
    print "-> Looking for experiments still in transition.\n";

    #
    # Look for any experiments in transition, and wait for them to stop.
    # This could take an arbitrarily long time of course, but its the only
    # safe way to do it. Don't worry about any experiments that are older
    # then 24 hours though; they are likely stuck and not going anywhere.
    #
    while (1) {
	my $query_result =
	    emdb::DBQueryWarn("select pid,eid,idx,state,expt_locked ".
			"   from experiments ".
			"where expt_locked is not null and ".
			"      DATE_SUB(NOW(),INTERVAL 1 DAY) <= expt_locked");
	if (!$query_result) {
	    Fatal("Error getting info from the DB. Stopping.");
	}
	last
	    if (!$query_result->numrows());

	print "-> Experiments still in transition:\n";
	while (my ($pid,$eid,$idx,$state,$expt_locked) =
Leigh B Stoller's avatar
Leigh B Stoller committed
336
	       $query_result->fetchrow_array()) {
337 338 339 340 341 342 343

	    print "  $pid,$eid,$idx -- $state, $expt_locked\n";
	}
	print "-> Waiting 30 seconds before trying again. ^C is okay\n\n";
	sleep(30);
    }
    print "** Testbed is quiet; stopping testbed daemons ... \n";
344
    if (system("$STARTUP stop >/dev/null 2>&1") ||
345 346
	(-x $MFRISBEED &&
	system("$MFRISBEED stop >/dev/null 2>&1"))) {
347 348 349
	Fatal("Could not stop testbed daemons. Stopping.");
    }
    $stopped = 1;
350

351 352 353 354 355 356 357 358 359
    print "** Testbed is stopped. Proceeding to update\n";

    #
    # No going back; we do not want to re-enable the testbed if things fail
    # from this point on.
    #
    $committed = 1;
}

360 361 362 363 364 365 366 367 368 369
print "Running DB updates ...\n";
system("perl @INCDIRS ../db/dbupdate -q -p $SQLUPDATEDIR $DBNAME");
exit(1)
    if ($?);
    
print "Running pre-install updates ...\n";
system("perl @INCDIRS ./update-install $forceopt -q -a pre -p $UPDATEDIR");
exit(1)
    if ($?);

370 371 372 373 374 375 376 377
if ($PGENISUPPORT) {
    print "Running ProtoGENI DB updates ...\n";
    system("perl @INCDIRS ../protogeni/scripts/update ".
	   "       -q -p $PGENIUPDATEDIR");
    exit(1)
	if ($?);
}

378 379 380 381
if ($install) {
    print "Installing testbed software ... this will take a while!\n";
    print "Output saved to $tempfile\n";
    
Leigh B Stoller's avatar
Leigh B Stoller committed
382
    system("(cd ..; gmake; gmake boss-install-noupdatecheck) >$tempfile 2>&1");
383 384 385 386 387 388 389 390 391 392
    if ($?) {
	exit(1);
    }
}

print "Running post-install updates ...\n";
system("perl @INCDIRS ./update-install $forceopt -q -a post -p $UPDATEDIR");
exit(1)
    if ($?);

393 394 395 396 397 398 399 400
if (CheckForReboot()) {
    exit(0);
}
# So we can turn the testbed back on.
elsif ($stopbed) {
    $committed = 0;
    Restart();
}
401 402 403 404 405 406
exit(0);

sub Fatal($)
{
    my ($msg) = @_;

407
    Restart();
408 409 410
    die("*** $0:\n".
	"    $msg\n");
}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437

#
# See if a reboot is needed. Typically not, but sometimes it might.
#
sub CheckForReboot()
{
    my $reboot_query =
	emdb::DBQueryWarn("select value from version_info ".
			  "where name='needreboot'");
    if ($reboot_query->numrows) {
	my ($needreboot) = $reboot_query->fetchrow_array();
	if ($needreboot) {
	    print "********************************************************\n";
	    print "*\n";
	    if ($impotent) {
		print "* THESE UPDATES WILL REQUIRE BOSS REBOOT!\n";
	    }
	    else {
		print "* YOU MUST NOW REBOOT BOSS!\n";
	    }
	    print "*\n";
	    print "********************************************************\n";
	    return 1;
	}
    }
    return 0;
}