node_statewait.in 1.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Simple utility script that waits for nodes to reach a given state.
# Presently used by elabinelab stuff to determine when nodes have been
# rebooted and are part of inner elab.
#
sub usage()
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
17 18
    print STDOUT
	"Usage: node_statewait [-s state] [-t timeout] [-a] | [node ...]\n";
19 20
    exit(-1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
21
my $optlist = "at:s:";
22 23 24 25 26 27 28 29 30 31
my $timeout = 60 * 6;

#
# Configure variables
#
my $TB		= "@prefix@";

# Locals
my @nodes       = ();
my $failed      = 0;
32
my $state;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"t"})) {
    $timeout = $options{"t"};
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55 56 57
if (defined($options{"s"})) {
    $state = $options{"s"};
}
58 59 60
else {
    $state = TBDB_NODESTATE_PXEWAIT;
}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

#
# All testnodes, or just some nodes.
#
if (defined($options{"a"})) {
    usage()
	if (@ARGV);

    my $query_result =
	DBQueryFatal("select node_id from nodes ".
		     "where role='testnode'");

    while (my ($node) = $query_result->fetchrow_array()) {
	push(@nodes, $node);
    }
    exit(0)
	if (!@nodes);
}
else {
    usage()
	if (!@ARGV);

    # Untaint the nodes.
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("*** Bad node name: $node.");
	}
	push(@nodes, $node);
    }
}

my $waitstart = time;

foreach my $node (sort(@nodes)) {
    #
    # Skip if something failed earlier.
    #
101
    if (!TBNodeStateWait($node, $waitstart, $timeout, undef, ($state))) {
102 103 104 105 106 107 108
	print STDOUT "nodewait ($node): Success\n";
	next;
    }
    $failed++;
}
print "nodewait: Done. There were $failed failures.\n";
exit($failed);