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.01 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

######################################################################
# 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
######################################################################
71

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

74
foreach my $n (@node_names) {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    # 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
90
    }
91 92 93

    my $result = DBQueryFatal("select * from nodes where node_id='$n'");
    if ($result->numrows() < 1) {
94
      print "Node '$n' does not exist.\n";
95
      $error++;
96
      next;
mac's avatar
mac committed
97
    } else {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	# 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...
113
	    push(@vals,"('$n','$pid','$eid','$n')");
114
	    push(@nodes,"$n");
115
	}
Mac Newbold's avatar
Mac Newbold committed
116 117
    }
}
118

119 120 121 122 123 124 125
if ($debug) { print "List Ready: @vals\nError=$error\n"; }

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

mac's avatar
mac committed
127
if (!$error && @vals) {
128 129 130 131 132 133 134 135
    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...

136 137 138
    if ($debug) {
	print "Reserving nodes...\n";
    }
139
    $cmd = "insert into reserved (node_id,pid,eid,vname) values ".
140
      join(",",@vals);
141 142 143
    if (!DBQueryWarn($cmd)) {
	$error++;
    }
mac's avatar
mac committed
144 145
}

146 147 148 149 150 151 152
DBQueryFatal("unlock tables");

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

154
if (!$error && @nodes) {
155 156
    system("$consetup @nodes") == 0 or
	print STDERR "WARNING: $consetup @nodes failed!\n";
Mac Newbold's avatar
Mac Newbold committed
157 158
}

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