sched_reload.in 3.88 KB
Newer Older
1 2
#!/usr/bin/perl -wT
use English;
3
use Getopt::Std;
4 5 6 7 8 9 10
   
#
# Schedule the reloading of a disk partition on a node. If the node is
# currently not reserved, start the loading now after reserving it to 
# testbed:reloading. Otherwise, put the right info into the database, and 
# nfree will do it when the node gets freed.
# 
11
# usage: sched_reload [-f | -p] <imageid> <node> [node ...]
12 13 14
#
sub usage()
{
15 16 17
    print STDOUT "Usage: sched_reload [-f | -p] <imageid> <node> [node ...]\n".
	"Use the -f to force reload. Fail if node cannot be reserved.\n".
	"Use the -p to pend reload for the reload daemon.\n";
18 19
    exit(-1);
}
20
my  $optlist = "fp";
21 22 23 24

#
# Configure variables
#
25
my $TB     = "@prefix@";
26

27 28 29
#
# Load the Testbed support stuff. 
#
30 31
use lib "@prefix@/lib";
use libdb;
32

33
#
34
# These come from the library.
35
# 
36 37 38
my $RELOADPID	= NODERELOADING_PID;
my $RELOADEID	= NODERELOADING_EID;
my $PENDINGEID	= NODERELOADPENDING_EID;
39

40 41
my $osload      = "$TB/bin/os_load";
my $nalloc      = "$TB/bin/nalloc";
42
my $name        = "";
43 44 45
my $error       = 0;
my $debug       = 0;
my $force	= 0;
46
my $pend	= 0;
47
my @nodes       = ();
48
my @row;
49 50 51 52 53 54 55 56

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$| = 1; #Turn off line buffering on output

#
57 58 59 60 61 62 63
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
64
if (@ARGV < 2) {
65
    usage();
66
}
67 68 69
if (defined($options{"f"})) {
    $force = $options{"f"};
}
70 71 72 73 74 75
if (defined($options{"p"})) {
    $pend = $options{"p"};
}
if ($pend and $force) {
    usage();
}
76
my $imageid   = shift;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

#
# Untaint args.
#
if ($imageid =~ /^([-\@\w.\+]+)$/) {
    $imageid = $1;
}
else {
    die("Bad data in $imageid.");
}

foreach my $node ( @ARGV ) {
    if ($node =~ /^([-\@\w]+)$/) {
	$node = $1;
    }
    else {
	die("Bad node name: $node.");
    }
    
    push(@nodes, $node);
}
98

99
#
100 101
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
102
#
103
if ($UID && !TBAdmin($UID)) {
104 105 106 107
    die("Only root or TB administrators can schedule disk reloads.\n");
}

#
108
# A loop.
109 110 111 112
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
113
    my $allocated = 0;
114

115
    $sth = DBQueryFatal("select * from nodes where node_id='$pc'");
116 117 118 119 120
    if ($sth->num_rows() != 1) {
      print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
      next;
    }
    
121
    print STDERR "Checking if $pc is reserved...";
122
    $sth = DBQueryFatal("select * from reserved where node_id='$pc'");
123
   
124 125
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
126 127 128 129 130 131 132 133 134
	my $eid;
	if ($pend) {
	    $eid = $PENDINGEID;
	}
	else {
	    $eid = $RELOADEID;
	}
	my $cmd = "$nalloc $RELOADPID $eid $pc";

135 136 137
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
138 139 140 141
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
142
	    DBQueryFatal("delete from last_reservation where node_id='$pc'");
143
	    push (@load_list,$pc);
144
	    $allocated = 1;
145 146 147 148
	}
    } else {
        print STDERR "Reserved.\n";      
    }
149 150 151 152 153 154 155 156 157 158

    #
    # If force and not able to reserve, do not pend a reload.
    # 
    if ($force && !$allocated) {
	$error++;
	next;
    }

    # Put it in the reloads table so TMCD knows to free it.
159
    print STDERR "Scheduling reload of $imageid for $pc:\n";
160 161
    DBQueryFatal("replace into reloads ".
		 "(node_id, image_id) values ('$pc', '$imageid')");
162 163
}

164 165 166 167 168
if ($pend) {
    print STDOUT "Reload Scheduling Done!\n";
    exit $error;
}

169 170
if (@load_list > 0) {
  print STDERR "Running os_load on ",join(", ",@load_list),":\n";
171
  $cmd = "$osload $imageid @load_list";
172 173 174 175 176 177 178
  print STDERR "Calling '$cmd'\n";
  if ( system($cmd) != 0 ) {
    print STDERR "WARNING: OS_LOAD FAILED ON @load_list!\n";
  }
}

print STDOUT "Reload Scheduling Done!\n";
179
exit $error;