waitForState.in 3.47 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2003-2003 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 27 28
#

#
# waitForState - a command line tool that uses the StateWait library
#

29 30 31 32 33 34 35 36 37
use lib '@prefix@/lib';
use StateWait;
use Getopt::Std;

my $debug = 0;
my @states=('ISUP');
my $u = 10;
my $t = 300;
my %opt = ();
38
getopts("hdv:s:u:t:",\%opt);
39 40 41

if ($opt{h}) { usage(); }
if ($opt{d}) { $debug++; }
42
if ($opt{v}) { $debug+=$opt{v}; }
43 44 45 46 47 48 49 50 51 52
if ($opt{u}) { $u = $opt{u}; }
if ($opt{t}) { $t = $opt{t}; }
if ($opt{s}) { @states = split(",",$opt{s}); }
if (!@ARGV) { usage(); }

my @nodes = @ARGV;

sub usage {
    print <<EOF;
Usage:
53
$0 [-h] [-d] [-v <level>]
54 55 56 57 58 59 60 61 62
	[-u <update freq.>] [-t <timeout>]
	[-s state1[,s2]] <node> [<node> ...]

Waits for all of the nodes listed to get to the right state.  For a
list of states, each node must go through those states in the right
order (non-consecutively).  If no state is supplied, ISUP is
used. Update frequency is how many seconds apart status updates should
be (default $u). Timeout is how many seconds we should wait before
giving up and returning (default $t). The -h option shows this
63
message, and -d and -v enable extra debugging output.
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

EOF
    exit(1);
}

if ($debug) { $StateWait::debug = $debug; }
debug("Calling initStateWait([".join(",",@states)."],".join(",",@nodes).")\n");
initStateWait(\@states,@nodes);

my $total = scalar(@nodes);
my $done = 0;
my $start = time();
my $now = $start;
my @finished = ();
my @failed = ();
debug("Start waiting at $start, stop at ".($start+$t)." - ".
        $total." nodes\n");
debug("COND: now-start=".($now-$start).",t=$t,u=$u,done=$done,total=$total\n");
while ( ($now - $start) < $t && $done < $total ) {
    my $wait = min($u,$start + $t - $now);
    debug("Calling waitForState(\@finished,\@failed,$wait) at $now\n");
    waitForState(\@finished, \@failed,$wait);
    $now = time();
    $done = scalar(@finished) + scalar(@failed);
    debug("Returned at $now: finished=".join(",",@finished).
	  "  failed=".join(",",@failed)."\n".
	  "Still waiting for ".($total-$done)." nodes\n");
91
    debug("COND: now-start=".($now-$start).",t=$t,done=$done,total=$total\n");
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

debug("All done at $now: finished=".join(",",@finished).
      "  failed=".join(",",@failed)."\n");

debug("Calling endStateWait()\n");
endStateWait();

my $worked = scalar(@finished);
my $failed = scalar(@finished);
my $remain = $total - $worked - $failed;
if ($worked != $total) {
    print "*** waitForState: Only $worked nodes of $total succeeded!\n";
    if ($failed) { print "\tThere were $failed failures.\n"; }
    if ($remain) { print "\tThere were $remain nodes that timed out.\n"; }
}

exit(0);

sub max ( $$ ) {
    return ($_[0] > $_[1] ? $_[0] : $_[1]);
}

sub min ( $$ ) {
    return ($_[0] < $_[1] ? $_[0] : $_[1]);
}

sub debug {
    if ($debug) { print @_; }
}