All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

nalloc.in 4.29 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7
# All rights reserved.
#
8
use English;
9
use Getopt::Std;
Mac Newbold's avatar
Mac Newbold committed
10

11 12 13 14 15 16
#
# nalloc - allocate nodes to an experiment. Takes a pid, and eid, and
# a list of nodes. Only nodes that are free can be allocated, and a
# special hack for sharks is included - allocating 'sh*' expands to
# allocation of the entire rack.
#
17

18 19 20
#
# Configure variables
#
21
my $TB       = "@prefix@";
22 23 24 25 26
use lib '@prefix@/lib';
use libdb;

my %opt = ();
getopts(\%opt,'v');
27

28 29 30 31
my $debug = 0;
if ($opt{v}) {
    $debug = 1;
}
Mac Newbold's avatar
Mac Newbold committed
32

33 34 35 36
if (@ARGV < 2) {
    die("Usage: nalloc [-v] <pid> <eid> <node> <node> <...>\n".
	 "		-v enables debugging output\n");
}
Mac Newbold's avatar
Mac Newbold committed
37

38 39
my $consetup="$TB/libexec/console_setup";
my $exportsetup="$TB/sbin/exports_setup";
40
my $error = 0;
41
my $pid = shift;
Mac Newbold's avatar
Mac Newbold committed
42 43
my $eid = shift;
my @node_names=@ARGV;
mac's avatar
mac committed
44 45
my @vals = ();
my @nodes= ();
Mac Newbold's avatar
Mac Newbold committed
46

47
if ($debug) { print "Expt '$eid', proj '$pid'\n"; }
48

49
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50 51 52 53 54 55 56 57
# Experiment must exist.
# 
if (!ExpState($pid,$eid)) {
    die "There is no experiment $eid in project $pid\n";
}

#
# User must have permission to modify the experiment.
58
#
59
if ($UID) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60 61
    if (!TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
        die("*** You not have permission to allocate nodes in $pid/$eid!\n");
62
    }
63
}
64

65 66 67 68 69 70 71 72 73 74 75
#
# Before locking any tables, do a quick check to make sure the project
# is allowed to allocate the nodes, by type/class. 
#
foreach my $n (@node_names) {
    if (! TBNodeAllocCheck($pid, $n)) {
	die("*** $0:\n".
	    "    You are not allowed to allocate $n to project $pid!\n");
    }
}

76 77 78 79 80 81
######################################################################
# Step 1 - Make a list of nodes to reserve
#
# We prune nodes that are already reserved, etc., from the list, and
# do expansion of shark shelves
######################################################################
82

83
DBQueryFatal("lock tables nodes read, reserved write, node_activity write");
mac's avatar
mac committed
84

85
foreach my $n (@node_names) {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    # Shark hack
    if ($n =~ /(sh\d+)/ ) { $n = $1."-1"; }
    # End shark hack

    my ($r_pid, $r_eid);
    if (NodeidToExp($n,\$r_pid,\$r_eid)) {
	# Someone has already reserved this node
	if (($r_pid eq $pid) && ($r_eid eq $eid)) {
	    print "You have already reserved node '$n'.\n";
	    # Do not increment error code since that throws off tbprerun.
	} else {
	    print "Someone else has already reserved node '$n'.\n";
	    $error++;
	}
	next; # Go on to the next node if this one is reserved
101
    }
102 103 104

    my $result = DBQueryFatal("select * from nodes where node_id='$n'");
    if ($result->numrows() < 1) {
105
      print "Node '$n' does not exist.\n";
106
      $error++;
107
      next;
mac's avatar
mac committed
108
    } else {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	# No one has reserved it, and it exists, so add it to my list
	# Shark hack
	if ($n =~ /(sh\d+)/ ) {
	    # its a shark - do the whole shelf if its not done already.
	    my $shelf = $1;
	    if ( ! (join(",",@nodes) =~ /,$shelf-\d,/)) {
		# Shelf hasn't been done yet...
		foreach my $n ( 1 .. 8 ) {
		    push(@vals,"('$shelf-$n','$pid','$eid')");
		    push(@nodes,"$shelf-$n");
		}
	    }
	    # End shark hack
	} else {
	    # its not a shark - just add it in...
124
	    push(@vals,"('$n','$pid','$eid','$n')");
125
	    push(@nodes,"$n");
126
	}
Mac Newbold's avatar
Mac Newbold committed
127 128
    }
}
129

130 131 132 133 134 135 136
if ($debug) { print "List Ready: @vals\nError=$error\n"; }

######################################################################
# Step 2 - Make the reservations in the database
#
# Uses the list built in step 1
######################################################################
137

mac's avatar
mac committed
138
if (!$error && @vals) {
139 140 141 142 143 144 145 146
    if ($debug) {
	print "Resetting node activity...\n";
    }
    my $cmd = "update node_activity set last_ext_act = now() where ".
      join(" or ",map("node_id='$_'",@nodes));
    DBQueryWarn($cmd);
    # It isn't an error if this fails...

147 148 149
    if ($debug) {
	print "Reserving nodes...\n";
    }
150
    $cmd = "insert into reserved (node_id,pid,eid,vname) values ".
151
      join(",",@vals);
152 153 154
    if (!DBQueryWarn($cmd)) {
	$error++;
    }
mac's avatar
mac committed
155 156
}

157 158 159 160 161 162 163
DBQueryFatal("unlock tables");

######################################################################
# Step 3 - Setup consoles
#
# Uses the list built in step 1
######################################################################
mac's avatar
mac committed
164

165
if (!$error && @nodes) {
166 167
    system("$consetup @nodes") == 0 or
	print STDERR "WARNING: $consetup @nodes failed!\n";
Mac Newbold's avatar
Mac Newbold committed
168 169
}

170
if ($debug) { print "Returning with value $error\n"; }
171
exit($error);