readycount.in 2.96 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# All rights reserved.
#

#
# readycount - Command-line tool to get at the ready bits in the database
#


#
# Configure variables
#
use lib '@prefix@/lib';

use libdb;

use English;
use Getopt::Long;
use strict;

sub usage {
    print << "END";
27
Usage: $0 [-h] [-c | -s] [-l] <pid> <eid> [node ...]
Robert Ricci's avatar
Robert Ricci committed
28
-h    This message
29
30
31
32
-s    Set ready bits
-c    Clear ready bits
-p    Use physical node IDs with -c or -s, rather than virtual ones. In
      this case, no pid and eid should be given.
Robert Ricci's avatar
Robert Ricci committed
33
34
35
-l    List ready status for each node in the experiment

If no nodes are given, gives a summary of the nodes that have reported ready.
36
37
38
If nodes are given, reports just status for the listed nodes. If -s or -c is
given, sets or clears ready bits for the listed nodes, or all
them as being ready (or clears their ready bits
Robert Ricci's avatar
Robert Ricci committed
39
40
41
42
43
44
45
46
47
48
if -c is given)
END

    return 1;
}

#
# Process command-line arguments
#
my %opt = ();
49
50
GetOptions(\%opt,'l','h','c','s','p');
if (($opt{p} && (@ARGV < 1)) || (!$opt{p} && (@ARGV < 2))) {
Robert Ricci's avatar
Robert Ricci committed
51
52
    exit &usage;
}
53
54
55
56
57

my ($pid, $eid);
if (!$opt{p}) {
    ($pid, $eid) = (shift @ARGV, shift @ARGV);
}
Robert Ricci's avatar
Robert Ricci committed
58
59
60
61
62
63
my @nodes = @ARGV;

if ($opt{h}) {
    exit &usage;
}

64
if ($opt{l} && ($opt{c} || $opt{s})) {
Robert Ricci's avatar
Robert Ricci committed
65
66
67
    exit &usage;
}

68
69
if ($opt{s} && $opt{c}) {
    die "-s and -c are mutually exclusive\n";
Robert Ricci's avatar
Robert Ricci committed
70
71
}

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Make sure that the user has permission to modify the experiment, if using
# virtual mode, or modify the nodes, if using physical names.
if ($opt{p}) {
    if (!TBNodeAccessCheck($UID,TB_NODEACCESS_MODIFYINFO,@nodes)) {
	die "You do not have permission to modify one or more nodes\n";
    }
} else {
    if (!ExpState($pid,$eid)) {
	die("There is no experiment '$eid' in project '$pid'.\n");
    }

    # Make sure the user has the ability to modify this experiment
    if (!TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
	die("You do not have permission to modify '$eid' in project '$pid'.\n");
    }
Robert Ricci's avatar
Robert Ricci committed
87
88
}

89
90
91
92
my @pnodes;
if ($opt{p}) {
    @pnodes = @nodes;
} else {
Robert Ricci's avatar
Robert Ricci committed
93
94
95
96
97
98
99
100
101
102
103
104
    if (!@nodes) {
	@pnodes = ExpNodes($pid,$eid);
    } else {
	foreach my $vnode (@nodes) {
	    my $pnode;
	    if (VnameToNodeid($pid, $eid, $vnode, \$pnode)) {
		push @pnodes, $pnode;
	    } else {
		die "No such node $vnode in $pid/$eid!\n";
	    }
	}
    }
105
106
107
}

if ($opt{c} || $opt{s}) {
Robert Ricci's avatar
Robert Ricci committed
108
109
110
111
112
113
114
115
116
117
    my $newbit = 1;
    if ($opt{c}) {
	$newbit = 0;
    }
    foreach my $pnode (@pnodes) {
	DBQueryFatal("update nodes set ready=$newbit where node_id='$pnode'");
    }
} else {
    my $ready = 0;
    my $total = 0;
118
119
120
121
122
123
    foreach my $pnode (@pnodes) {
	my $result = DBQueryFatal("select ready, vname, nodes.node_id " .
	    "from nodes left join reserved on nodes.node_id=reserved.node_id ".
	    "where nodes.node_id='$pnode'");
	my ($isready, $vname, $pname) = $result->fetchrow();
	my $name = $opt{p}? $pname : $vname;
Robert Ricci's avatar
Robert Ricci committed
124
	if ($opt{l}) {
125
	    print "$name\t$isready\n";
Robert Ricci's avatar
Robert Ricci committed
126
127
128
129
130
131
132
133
134
135
	}
	if ($isready) {
	    $ready++;
	}
	$total++;
    }
    print "READY=$ready TOTAL=$total\n";
}

exit(0);