nalloc.in 5.04 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3
#
# EMULAB-COPYRIGHT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
4
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6
# All rights reserved.
#
7
use English;
8
use Getopt::Std;
Mac Newbold's avatar
Mac Newbold committed
9

10 11
#
# nalloc - allocate nodes to an experiment. Takes a pid, and eid, and
Leigh B. Stoller's avatar
Leigh B. Stoller committed
12
# a list of nodes. Only nodes that are free can be allocated.
13
#
14 15 16
# Exit status is important! Exit with -1 if an error, else the number
# of nodes that could not be allocated. Otherwise zero.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
17 18 19 20 21 22 23 24 25 26 27
sub usage()
{
    print("Usage: nalloc [-v] [-p] <pid> <eid> <node> <node> <...>\n".
	  "		-p enables partial allocation mode\n".
	  "		-v enables debugging output\n");
    exit(-1);
}
my $optlist = "dp";
my $debug   = 0;
my $partial = 0;

28 29 30
#
# Configure variables
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31 32 33 34 35 36 37
my $TB          = "@prefix@";
my $consetup    = "$TB/libexec/console_setup";
my $exportsetup = "$TB/sbin/exports_setup";

#
# Testbed Support libraries
#
38 39
use lib '@prefix@/lib';
use libdb;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
40
use libtestbed;
41

Leigh B. Stoller's avatar
Leigh B. Stoller committed
42 43 44 45
#
# Turn off line buffering on output
#
$| = 1; 
46

Leigh B. Stoller's avatar
Leigh B. Stoller committed
47 48
# For perf analysis.
#TBDebugTimeStampsOn();
49

Leigh B. Stoller's avatar
Leigh B. Stoller committed
50 51 52 53 54 55 56 57 58
#
# 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{"d"})) {
59 60
    $debug = 1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61
if (defined($options{"p"})) {
62 63
    $partial = 1;
}
64
if (@ARGV < 2) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65
    usage();
66
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67 68 69
my $pid		= shift;
my $eid		= shift;
my @node_names	= @ARGV;
Mac Newbold's avatar
Mac Newbold committed
70

Leigh B. Stoller's avatar
Leigh B. Stoller committed
71 72 73 74 75 76 77 78
#
# Locals
# 
my $error	  = 0;
my $noalloc	  = 0;
my @vals	  = ();
my @nodes	  = ();
my @need_consetup = 0;
79

Leigh B. Stoller's avatar
Leigh B. Stoller committed
80
TBDebugTimeStamp("nalloc started");
81
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
82 83 84 85 86
# Experiment must exist.
# 
if (!ExpState($pid,$eid)) {
    die "There is no experiment $eid in project $pid\n";
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
87
TBDebugTimeStamp("nalloc checked exp state");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
88 89 90

#
# User must have permission to modify the experiment.
91
#
92
if ($UID) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
93 94
    if (!TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
        die("*** You not have permission to allocate nodes in $pid/$eid!\n");
95
    }
96
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
97
TBDebugTimeStamp("nalloc checked exp permission");
98

99 100
#
# Before locking any tables, do a quick check to make sure the project
Leigh B. Stoller's avatar
Leigh B. Stoller committed
101
# is allowed to allocate the nodes, by type/class, plus other checks.
102 103
#
foreach my $n (@node_names) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
104 105 106 107 108 109 110
    # Make sure a valid node name first!
    if (! TBValidNodeName($n)) {
	die("*** $0:\n".
	    "    No such node $n!\n");
    }

    # Project allowed to allocated this node type/class?
111 112 113 114 115
    if (! TBNodeAllocCheck($pid, $n)) {
	die("*** $0:\n".
	    "    You are not allowed to allocate $n to project $pid!\n");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129
    # XXX
    # If the node has a tipline, then indicate that we want to call
    # console_setup. We want to avoid calling console_setup if all we
    # got is a zillion virtual nodes on the command line; wasted work.
    #
    my $tiplines_result =
	DBQueryFatal("select tipname,server from tiplines ".
		     "where node_id='$n'");

    if ($tiplines_result->numrows) {
	$need_consetup++;
    }
}
TBDebugTimeStamp("nalloc checked node permission ");
130

Leigh B. Stoller's avatar
Leigh B. Stoller committed
131 132 133
# Must lock this table!
DBQueryFatal("lock tables reserved write");
TBDebugTimeStamp("nalloc locked tables");
mac's avatar
mac committed
134

Leigh B. Stoller's avatar
Leigh B. Stoller committed
135
# Make a list of nodes to reserve.
136
foreach my $n (@node_names) {
137
    my ($r_pid, $r_eid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
138 139
    
    if (NodeidToExp($n, \$r_pid, \$r_eid)) {
140 141
	# Someone has already reserved this node
	if (($r_pid eq $pid) && ($r_eid eq $eid)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142 143
	    print "Already reserved: '$n'\n";
	    # Do not increment error code since that throws off caller.
144
	    next;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145 146 147 148 149 150
	}
	elsif (NodeidToExpOldReserved($n, \$r_pid, \$r_eid) &&
	       (($r_pid eq $pid) && ($r_eid eq $eid))) {
	    print "Already reserved in holding reservation: '$n'\n";
	}
	else {
151
	    print "Someone else has already reserved node '$n'.\n";
152
	    $noalloc++;
153
	    next;
154
	}
155
    }
156

Leigh B. Stoller's avatar
Leigh B. Stoller committed
157 158 159 160 161
    #
    # Add info the list of nodes to reserve; done in a single query below.
    # 
    push(@vals,  "('$n','$pid','$eid','$n','','')");
    push(@nodes, "$n");
Mac Newbold's avatar
Mac Newbold committed
162
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
163
TBDebugTimeStamp("nalloc checked all nodes");
164

Leigh B. Stoller's avatar
Leigh B. Stoller committed
165 166 167
if ($debug) {
    print "List Ready: @vals\nError=$error\n";
}
168

Leigh B. Stoller's avatar
Leigh B. Stoller committed
169 170 171 172
# Now make the reservations in the DB.
if ((!$noalloc || $partial) && @vals) {
    print "Reserving nodes...\n"
	if ($debug);
173

Leigh B. Stoller's avatar
Leigh B. Stoller committed
174 175 176
    if (! DBQueryWarn("replace into reserved ".
		      "  (node_id,pid,eid,vname,old_pid,old_eid) ".
		      "values ". join(",",@vals))) {
177 178
	$error++;
    }
mac's avatar
mac committed
179
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
180
TBDebugTimeStamp("nalloc allocated all nodes");
mac's avatar
mac committed
181

Leigh B. Stoller's avatar
Leigh B. Stoller committed
182
# Unlock tables.
183
DBQueryFatal("unlock tables");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184
TBDebugTimeStamp("nalloc unlocked tables");
185

Leigh B. Stoller's avatar
Leigh B. Stoller committed
186 187 188 189 190 191 192 193 194 195 196
# Update node_activity table; no need to do this with tables locked.
if (!$error && (!$noalloc || $partial) && @nodes) {
    print "Resetting node activity\n"
	if ($debug);

    # It isn't an error if this fails...
    DBQueryWarn("update node_activity set last_ext_act=now() ".
		"where " . join(" or ",map("node_id='$_'", @nodes)));

    TBDebugTimeStamp("nalloc updated node_activity table");
}
mac's avatar
mac committed
197

Leigh B. Stoller's avatar
Leigh B. Stoller committed
198 199
# Now setup consoles if needed.
if ($need_consetup && !$error && @nodes) {
200 201
    system("$consetup @nodes") == 0 or
	print STDERR "WARNING: $consetup @nodes failed!\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
202
    TBDebugTimeStamp("nalloc finished console setup");
Mac Newbold's avatar
Mac Newbold committed
203 204
}

205 206 207 208 209 210 211
#
# Exit with -1 if an error, else the number of nodes that could not
# be allocated.
#
my $exitval = ($error ? -1 : $noalloc);
if ($debug) { print "Returning with value $exitval\n"; }
exit($exitval);