libadminmfs.pm.in 15.5 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
# All rights reserved.
#
# Admin MFS library.  Routines related to getting into and out of the
# admin MFS and executing programs within it.
#
# Note that none of these routines are graceful about errors.  If some/all
# nodes fail, their bootinfo states may be modified and not all the same;
# i.e., some nodes may be set to boot into the MFS (or may even have booted
# into the MFS) and others not.  These routines are used primarily at swap
# out time where all inconsistent state will get fixed up anyway, or by
# node_admin where the caller is assumed to be saavy.
#
package libadminmfs;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( TBAdminMfsBoot TBAdminMfsSelect TBAdminMfsRunCmd );

# Must come after package declaration!
use lib '@prefix@/lib';
use libdb;
29
use libtestbed;
30 31
use StateWait;

32 33 34 35 36 37 38
#
# Function prototypes
#
sub TBAdminMfsBoot($$@);
sub TBAdminMfsSelect($$@);
sub TBAdminMfsRunCmd($$@);

39 40 41 42 43 44 45 46 47 48
# Configure variables
my $TB		= "@prefix@";
my $TESTMODE    = @TESTMODE@;
my $TBOPS       = "@TBOPSEMAIL@";
my $ELABINELAB  = @ELABINELAB@;

my $nodereboot	= "$TB/bin/node_reboot";
my $power	= "$TB/bin/power";
my $osselect    = "$TB/bin/os_select";

49 50 51 52 53 54 55 56
#
# The number of nodes we will power on at a time and the time to wait
# between batches.  These values are NOT the same as in libreboot.pm.
# I had to reduce the batch count to prevent problems.
#
my $BATCHCOUNT  = 8;
my $BATCHSLEEP	= 5;

57 58 59 60
#
# Timeout for a node to reboot into (or out of) the admin MFS
# XXX we could calculate this from node_type and os_info reboot times
#
61
my $reboottimo  = (6 * 60);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
my $commandtimo = (2 * 60);
my $sleepwait   = 10;

#
# TBAdminMfsBoot(\%args, \@failed, @nodes)
#
# Boot up (reboot or power on) a set of nodes (@nodes) and optionally wait
# for them to enter or exit the admin MFS.  It is assumed that the node
# has been setup to boot into or out of the admin MFS as appropriate
# (TBAdminMfsSelect). If not, you could be waiting a long time!
# Arguments passed in the $args hash:
#
#	'name'	 string identifying the caller for error messages
#	'on'	 1 to enter MFS, 0 to exit
#	'reboot' 1 to reboot into MFS, -1 to "power on", 0 to do nothing
77
#	'prepare' 1 to run prepare on the way down
78 79
#	'wait'	 1 to wait for all nodes to reach MFS before returning,
#		 0 to return after reboot/power-on
80 81
#	'retry'  1 if we should retry once on nodes that do not make it
#		 into the MFS.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#
# Returns zero if all nodes successfully complete the reboot (wait==0)
# or reach the MFS (wait==1), and non-zero otherwise.  If the $failed ref
# is defined, it is an arrayref in which we return the list of nodes that
# failed.
#
# Note: NO PERMISSION CHECKING IS DONE.  It is assumed that the caller
# has performed all the necessary checks.
#
sub TBAdminMfsBoot($$@)
{
    my ($args, $failedref, @nodes) = @_;

    my $me = $args->{'name'};
    my $on = $args->{'on'};
    my $reboot = $args->{'reboot'};
    my $wait = $args->{'wait'};
99
    my $prepare = $args->{'prepare'};
100 101 102 103
    my $retry = $args->{'retry'};

    $retry = 0
        if (!defined($retry) || !$wait);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    #
    # Reboot or power on nodes...
    #
   if ($wait) {
	$StateWait::debug = 0;
	
	#
	# Initialize the statewait library.
	#
	my @states   = ();

	#
	# Only wait for MFSSETUP when going into the MFS. When coming out
	# of MFS, just wait for generic ISUP.
	# 
	push(@states, TBDB_NODESTATE_MFSSETUP())
	    if ($on);
	push(@states, TBDB_NODESTATE_ISUP());
    
	if (initStateWait(\@states, @nodes)) {
	    print STDERR "*** $me:\n".
		"    Failed to initialize the statewait library!\n";
	    @$failedref = @nodes
		if (defined($failedref));
	    return 1;
	}
    }

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    if ($reboot) {
	#
	# Since the admin MFS is large, we do are own limiting of the number
	# of nodes rebooted in parallel.  This value is considerably lower than
	# that enforced in libreboot.pm.  Also note that we would need to
	# perform batching for the "power on" case anyway, as the power command
	# does not do any batching.
	#
	my @nodelist = @nodes;
	while (@nodelist) {
	    my $batch = "";
	    my $i = 0;
	    while ($i < $BATCHCOUNT && @nodelist > 0) {
		my $node = shift(@nodelist);
		$batch .= " $node";
		$i++;
	    }
	    if ($reboot > 0) {
151 152 153
		my $optarg = ($prepare ? "-p" : "");
		
		if (system("$nodereboot $optarg $batch")) {
154 155 156 157 158 159 160 161 162 163 164 165 166 167
		    print STDERR "*** $me:\n".
			"    WARNING: Could not reboot some of: $batch!\n";
		}
	    } elsif ($reboot < 0) {
		print STDOUT "Powering on nodes:\n";
		if (system("$power on $batch")) {
		    print STDERR "*** $me:\n".
			"    WARNING: Could not power on some of: $batch!\n";
		}
	    }
	    print STDOUT "  $batch ", @nodelist > 0 ? "...\n" : "\n";
	    if (@nodelist) {
		sleep($BATCHSLEEP);
	    }
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	}
    }

    #
    # ...and wait for them to come back up.
    #
    if ($wait) {
	#
	# Initialize the statewait library.
	#
	my @finished = ();
	my @failed   = ();
	my $endtime = time() + $reboottimo;

	print STDOUT "Waiting for nodes to come up.\n";
	
	# Now we can statewait.
	if (waitForState(\@finished, \@failed, $reboottimo)) {
	    print STDERR "*** $me:\n".
		"    Failed in waitForState!\n";
	    @$failedref = @nodes
		if (defined($failedref));
	    return 1;
	}
	endStateWait();
	
	#
	# XXX gak! Our whole state wait system (actually event system)
	# doesn't distinguish timeouts.  A timeout is a failure in this
	# context since it means that some node did not get where it was
	# supposed to go, and that could be dangerous.  Fortunately, nodes
	# that do timeout, while not added to failed, are not added to
	# finished either (ditto for nodes that go through an improper
	# state transition sequence).  So if the sum of @finished + @failed
	# is not @nodes, then we know some nodes timed out.  So we can go
	# back and tease those out.
	#
	if (scalar(@nodes) > scalar(@finished) + scalar(@failed)) {
	    # there is probably a better way to do a set diff...
	    my %seen;
	    for my $n (@finished, @failed) {
		$seen{$n} = 1;
	    }
	    for my $n (@nodes) {
		push(@failed, $n)
		    if (!defined($seen{$n}));
	    }
	}

217 218 219
	if (@failed == 0) {
	    print STDOUT "All nodes are up.\n";
	    return 0;
220 221
	}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	print STDERR "*** $me:\n".
	             "    Failed to boot " . ($on ? "MFS" : "regular OS") .
		     " on: @failed\n";

	#
	# Failures to boot after powering on may be due to issues with
	# power-on and not related to the nodes themselves.  So we will
	# retry once, trying a power cycle the second time around.
	# This addresses cases where I have seen power on leave a machine
	# (pc600) in a hung state that can be cured by cycling (I have no
	# idea why...).
	#
	if ($reboot < 0 && $retry) {
	    print STDERR "Retrying on failed nodes ...\n";
	    my @myfailed;
	    my %myargs;
	    $myargs{'name'} = $me;
	    $myargs{'on'} = $on;
	    $myargs{'reboot'} = 1;
	    $myargs{'wait'} = $wait;
	    $myargs{'prepare'} = $prepare;
	    $myargs{'retry'} = 0;
	    if (TBAdminMfsBoot(\%myargs, \@myfailed, @failed) == 0) {
		return 0;
	    }
	    @failed = @myfailed;
	}

	@$failedref = @failed
	    if (defined($failedref));
	return 1;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    }

    return 0;
}

#
# TBAdminMfsSelect(\%args, \@failed, @nodes)
#
# For the given list of nodes, set or clear the temp OSID field so that
# they will or will not boot into the admin MFS.
#
# Arguments passed via the $args hashref:
#
#	'name'	   string identifying the caller for error messages
#	'on'	   1 to set temp OSID to MFS, 0 to clear
#	'clearall' 1 to clear one-shot and partition boot OSIDs
#		   (if 'on' is set), 0 leaves them alone
#
# Returns zero if we successfully set the state for all nodes,
# and non-zero otherwise.  If the $failed ref is defined, it is an
# arrayref in which we return the list of nodes that failed.
#
# Note: NO PERMISSION CHECKING IS DONE.  It is assumed that the caller
# has performed all the necessary checks.
#
# Note: this call will clear any user-set startupcmd and status.  The
# caller is responsible for saving it if they care.
#
sub TBAdminMfsSelect($$@)
{
    my ($args, $failedref, @nodes) = @_;

    return 0
	if (@nodes == 0);

    my $me = $args->{'name'};
    my $on = $args->{'on'};
    my $only = $args->{'clearall'};
    my @good = ();
    my @bad = ();

    #
    # Set/clear the temp OSID for all nodes.
    #
    if ($on) {
	# clear one-shot boots
	if ($only && system("os_select -c -1 @nodes")) {
	    print STDERR "*** $me:\n".
		"    Failed to clear one-shot boot for @nodes!\n";
	    @{$failedref} = @nodes
		if (defined($failedref));
	    return 1;
	}

	# determine the correct admin OSID for all nodes
	my %adminosid = ();
	for my $node (@nodes) {
	    my $osid = TBNodeAdminOSID($node);
	    push @{$adminosid{$osid}}, $node;
	}

	# and set it
	for my $osid (keys %adminosid) {
	    my @n = @{$adminosid{$osid}};
	    if (system("$osselect -t $osid @n")) {
		print STDERR "*** $me:\n".
		    "    Failed to set temp boot to $osid for @n!\n";
		push(@bad, @n);
	    } else {
		push(@good, @n);
	    }
	}

	# clear partition boots for successful nodes
	if ($only && system("os_select -c @good")) {
	    print STDERR "*** $me:\n".
		"    Failed to clear partition boot for @good!\n";
	    @{$failedref} = @nodes
		if (defined($failedref));
	    return 1;
	}
    } else {
	if (system("$osselect -c -t @nodes")) {
	    print STDERR "*** $me:\n".
		"    Failed to clear temp boot for @nodes!\n";
	    @{$failedref} = @nodes
		if (defined($failedref));
	    return 1;
	}
	@good = @nodes;
    }

    #
    # Make sure we don't execute any unintended command, either those
    # set by the user when we enter admin mode, or those set by us when
    # we leave admin mode.
    #
350 351 352 353
    if (@good > 0) {
	my $cond = "node_id in (" . join(",", map("'$_'", @good)) . ")";
	if (!DBQueryWarn("update nodes set startupcmd='', startstatus='none' ".
			 "where $cond")) {
354 355 356
	    print STDERR "*** $me:\n".
		"    Failed to clear startup state for @good!\n";
	    push(@bad, @good);
357
	}
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    }

    @{$failedref} = @bad
	if (defined($failedref));

    return (@bad != 0);
}

#
# TBAdminMfsRunCmd(\%args, \@failed, @nodes)
#
# For the given list of nodes, boot each one into the admin MFS and run the
# indicated command.  Upon successful completion, all nodes are left in the
# MFS with admin mode enabled.  Arguments passed in the $args hash:
#
373 374 375 376 377 378 379 380 381 382 383 384 385 386
#	'name'	    string identifying the caller for error messages
#	'command'   command+args to run on the nodes
#	'timeout'   time in seconds to wait for command completion
#		    (starts when node has booted into MFS)
#	'poweron'   1 if nodes need to be powered on initially
#		    rather than rebooted
#	'pfunc'
#	'pinterval'
#	'pcookie'   Progress tracking.  'pfunc' will be called every
#		    'pinterval' seconds with argument 'pcookie' and a
#		    node-indexed status hash for all nodes.  If
#		    the function returns 0, we abort as we would with
#		    a timeout.
#	'timestamp' if timestamps after significant events are desired
387
#	'prepare'   1 if nodes should be rebooted with "prepare" flag
388 389
#	'retry'     1 if we should retry once on nodes that do not make it
#		    into the MFS.
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
#
# Returns zero if all nodes successfully run the command.
# If the $failed ref is defined, it is an arrayref in which we return the
# list of nodes that failed.
#
# Note: NO PERMISSION CHECKING IS DONE.  It is assumed that the caller
# has performed all the necessary checks.
#
# Note: this call will clear any user-set startupcmd and status.  The
# caller is responsible for saving it if they care.
#
sub TBAdminMfsRunCmd($$@)
{
    my ($args, $failedref, @nodes) = @_;

    my $me = $args->{'name'};
    my $cmd = $args->{'command'};
    my $timeout = $args->{'timeout'};
    my $poweron = $args->{'poweron'};
409 410 411 412
    my $timestamp = $args->{'timestamp'};
    my $pfunc = $args->{'pfunc'};
    my $pinterval = $args->{'pinterval'};
    my $pcookie = $args->{'pcookie'};
413
    my $prepare = $args->{'prepare'};
414
    my $retry = $args->{'retry'};
415 416 417 418 419 420

    # we always need a value
    $timeout = $commandtimo
	if (!defined($timeout));
    $poweron = 0
	if (!defined($poweron));
421 422 423 424
    if ($pfunc) {
	$pinterval = 10
	    if (!defined($pinterval));
    }
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456

    #
    # Arrange for the node to boot into the MFS, but don't boot it yet.
    #
    my %myargs;
    $myargs{'name'} = $me;
    $myargs{'on'} = 1;
    $myargs{'clearall'} = 0;
    if (TBAdminMfsSelect(\%myargs, undef, @nodes)) {
	@{$failedref} = @nodes
	    if (defined($failedref));
	return 1;
    }

    #
    # Record the startup command.  The status has already been set to
    # 'none' by Select above.
    #
    my $allcond = "node_id in (" . join(",", map("'$_'", @nodes)) . ")";
    if (!DBQueryWarn("update nodes set startupcmd='$cmd' where $allcond")) {
	print STDERR "*** $me:\n".
	    "    DB error updating node startupcmd info for @nodes!\n";
	@{$failedref} = @nodes
	    if (defined($failedref));
	return 1;
    }

    #
    # Now reboot into the admin MFS.  TBAdminMfsBoot will return when
    # the nodes report ISUP, which they will do before firing off the
    # startup command.
    #
457 458
    TBDebugTimeStamp("Booting nodes into admin MFS")
	if ($timestamp);
459 460 461 462
    %myargs = ();
    $myargs{'name'} = $me;
    $myargs{'on'} = 1;
    $myargs{'reboot'} = $poweron ? -1 : 1;
463
    $myargs{'retry'} = $retry;
464
    $myargs{'prepare'} = $prepare;
465 466 467 468
    $myargs{'wait'} = 1;
    my @failed = ();
    if (TBAdminMfsBoot(\%myargs, \@failed, @nodes)) {
	print STDERR "*** $me:\n".
469
	    "    failed admin MFS boot for @failed!\n";
470 471 472 473 474 475 476 477
	@{$failedref} = @nodes
	    if (defined($failedref));
	return 1;
    }

    #
    # Wait for the command completion status or til we time out
    #
478 479
    TBDebugTimeStamp("Nodes in admin MFS, running command")
	if ($timestamp);
480 481
    my @running = @nodes;
    my %status = ();
482 483 484
    foreach my $node (@nodes) {
	$status{$node} = "none";
    }
485
    my $endtime = time() + $timeout;
486
    my $interval = $pfunc ? $pinterval : $sleepwait;
487
    while (@running > 0 && time() < $endtime) {
488
	sleep($interval);
489 490 491 492 493 494 495 496 497 498 499 500 501

	my $cond = "node_id in (" . join(",", map("'$_'", @running)) . ")";
	my $query_result =
	    DBQueryWarn("select node_id,startstatus from nodes where $cond");

	if (!$query_result || $query_result->numrows < 1) {
	    print STDERR "*** $me:\n".
		"    DB error getting startstatus for @running!\n";
	    @running = ();
	    last;
	}
 
	while (my ($node, $result) = $query_result->fetchrow_array()) {
502
	    $status{$node} = $result;
503 504
	}

505 506 507 508 509 510 511 512 513 514 515 516 517 518
	#
	# Run the progress function with the updated status of all nodes.
	#
	if ($pfunc && &$pfunc($pcookie, \%status) == 0) {
	    last;
	}

	#
	# Recompute the set of running nodes after any progress function
	# has been called in case that function has updated a node's status.
	# We trust that the function will be well behaved and not add nodes
	# that don't belong!
	#
	@running = grep { $status{$_} eq "none" } keys(%status);
519 520 521 522 523 524
    }

    #
    # Everyone has reported or we timed out.
    # Find out who succeeded, who failed, and who timed out.
    #
525 526
    TBDebugTimeStamp("Node admin MFS commands completed/timedout")
	if ($timestamp);
527 528 529
    my @succeeded = ();
    my @timedout = ();
    for my $node (@nodes) {
530
	if ($status{$node} eq "none" || $status{$node} eq "timeout") {
531
	    push(@timedout, $node);
532
	} elsif (int($status{$node}) != 0) {
533 534 535 536 537 538 539 540 541
	    push(@failed, $node);
	} else {
	    push(@succeeded, $node);
	}
    }

    if (scalar(@nodes) != scalar(@succeeded)) {
	if (scalar(@failed) > 0) {
	    print STDERR "*** $me:\n".
542
		"    Command run failed on @failed\n";
543 544 545 546 547
	    push(@{$failedref}, @failed)
		if (defined($failedref));
	}
	if (scalar(@timedout) > 0) {
	    print STDERR "*** $me:\n".
548
		"    Timed-out while running command on @timedout\n";
549 550 551 552 553 554 555 556 557
	    push(@{$failedref}, @timedout)
		if (defined($failedref));
	}
	return 1;
    }

    print STDOUT "All nodes have completed their command.\n";
    return 0;
}