node_reboot.in 6.66 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2010 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
Leigh Stoller's avatar
Leigh Stoller committed
7
use strict;
8 9 10 11
use English;
use Getopt::Std;

#
12 13
# Reboot a node (or nodes). Will power cycle the node as a last resort.
# Use -e option to reboot all nodes in an experiment.
14
#
15 16
# Exit value is 0 if all nodes reboot okay, or the number of nodes
# could not be rebooted.
17 18 19
#
sub usage()
{
20 21 22 23
    print(STDERR
	  "Usage: node_reboot [-d] [-f] [-w] [-k] node [node ...]\n" .
	  "       node_reboot [-d] [-f] [-w] [-k] -e pid,eid\n".
	  "Use the -d option to turn on debugging\n" .
24
	  "Use the -s option to turn on silent mode\n" .
25 26 27
	  "Use the -e option to reboot all the nodes in an experiment\n" .
	  "Use the -w option to to wait for nodes is come back up\n" .
	  "Use the -k option to power cycle nodes in PXEWAIT mode\n" .
28
	  "Use the -b option to reboot nodes in PXEWAIT mode\n" .
29 30 31
	  "Use the -a option to reboot all free nodes\n".
	  "Use the -c option to reconfig nodes instead of rebooting\n".
	  "Use the -f option to power cycle (and not wait for nodes to die)\n");
32 33
    exit(-1);
}
34
# The hidden -r option runs this in "realmode", ie don't send an event, but
35 36
# really do the work instead.  Hidden -W option specifies the waittime. 
my $optlist     = "dfe:wrkacbpsW:";
37
my $debug       = 0;
38
my $silent      = 0;
39 40 41 42 43
my $powercycle  = 0;
my $waitmode    = 0;
my $realmode    = 1; # XXX Temporary, until we make event sending the default.
my $killmode    = 0;
my $reconfig    = 0;
44
my $rebootmode  = 0;
45
my $prepare     = 0;
46
my $waittime;
47 48 49 50 51

#
# Configure variables
#
my $TB		= "@prefix@";
52
my $CLIENT_BIN  = "@CLIENT_BINDIR@";
53 54

# Locals
Leigh Stoller's avatar
Leigh Stoller committed
55
my @nodes	= ();
56 57

#
58
# Testbed Support libraries
59
#
60 61
use lib "@prefix@/lib";
use libdb;
62
use libreboot;
Leigh Stoller's avatar
Leigh Stoller committed
63 64 65
use Experiment;
use Node;
use User;
66 67 68 69 70 71

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

# Turn off line buffering on output
Mac Newbold's avatar
Mac Newbold committed
72
$| = 1;
73 74 75 76 77

#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
78 79
    die("*** $0:\n".
        "    Must be root! Maybe its a development version?\n");
80 81
}

Leigh Stoller's avatar
Leigh Stoller committed
82 83 84
#
# Verify user and get his DB uid and other info for later.
#
85 86 87 88 89 90 91
my $this_user;
if ($UID) {
    $this_user = User->ThisUser();
    if (! defined($this_user)) {
	die("*** $0:\n".
	    "    You ($UID) do not exist!\n");
    }
Leigh Stoller's avatar
Leigh Stoller committed
92 93
}

94 95 96 97
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
Leigh Stoller's avatar
Leigh Stoller committed
98
my %options = ();
99 100 101 102
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
103 104
    $debug = 1;
}
105 106 107
if (defined($options{"s"})) {
    $silent = 1;
}
108 109 110
if (defined($options{"b"})) {
    $rebootmode = 1;
}
111
if (defined($options{"f"})) {
112
    $powercycle = 1;
113
}
114 115 116
if (defined($options{"k"})) {
    $killmode = 1;
}
117 118 119
if (defined($options{"w"})) {
    $waitmode = 1;
}
120 121 122
if (defined($options{"p"})) {
    $prepare = 1;
}
123 124
if (defined($options{"r"})) {
    $realmode = 1;
125
}
126
if (defined($options{"c"})) {
127 128
    $reconfig = 1;
}
129 130 131
if (defined($options{"W"})) {
    $waittime = $options{"W"};
}
132

133
if (defined($options{"a"})) {
Leigh Stoller's avatar
Leigh Stoller committed
134 135 136
    usage()
	if (@ARGV);
    
137 138 139
    #
    # Reboot all free nodes
    #
Leigh Stoller's avatar
Leigh Stoller committed
140
    if ($UID && !$this_user->IsAdmin()) {
141 142
	die("*** $0:\n".
	    "    You not have permission to reboot all free nodes!\n");
143 144 145 146 147 148 149 150 151 152
    }

    my $query_result =
	DBQueryFatal("select n.node_id from nodes as n ".
		     "left join reserved as r on r.node_id=n.node_id ".
		     "left join node_types as nt on nt.type=n.type ".
		     "where nt.class='pc' and n.role='testnode' and ".
		     "      r.pid is NULL");

    if ($query_result->numrows == 0) {
153 154
	die("*** $0:\n".
	    "    There are no free nodes to reboot\n");
155
    }
156

157 158
    while (my ($nodeid) = $query_result->fetchrow_array()) {
	push(@nodes, $nodeid);
159 160
    }
}
161
elsif (defined($options{"e"})) {
162
    #
163
    # Reboot all nodes in an experiment
164
    #
Leigh Stoller's avatar
Leigh Stoller committed
165 166
    usage()
	if (@ARGV);
167

Leigh Stoller's avatar
Leigh Stoller committed
168 169
    my $experiment = Experiment->Lookup($options{"e"});
    if (!defined($experiment)) {
170
	die("*** $0:\n".
Leigh Stoller's avatar
Leigh Stoller committed
171
	    "    Unknown experiment!\n");
172
    }
Leigh Stoller's avatar
Leigh Stoller committed
173 174

    #
175 176 177
    # Verify permission to muck with this experiment. This is to head off
    # permission problems early; the nodes are indvidually checked later
    # in the library.
Leigh Stoller's avatar
Leigh Stoller committed
178
    #
Leigh Stoller's avatar
Leigh Stoller committed
179 180
    if ($UID && !$this_user->IsAdmin() &&
	! $experiment->AccessCheck($this_user, TB_EXPT_MODIFY)) {
181
	die("*** $0:\n".
Leigh Stoller's avatar
Leigh Stoller committed
182
	    "    You not have permission to reboot nodes in $experiment!\n");
Leigh Stoller's avatar
Leigh Stoller committed
183
    }
184
    my @nodelist = $experiment->NodeList(0, 1);
Leigh Stoller's avatar
Leigh Stoller committed
185
    if (! @nodelist) {
186
	die("*** $0:\n".
Leigh Stoller's avatar
Leigh Stoller committed
187
            "    There are no nodes reserved in $experiment\n");
188
    }
189
    
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    #
    # If this is a firewalled experiment, don't reboot the firewall.
    # If you want to reboot the firewall, you have to do it individually.
    #
    # Two reasons.  One is that the firewall is not conceptually part of
    # the experiment and the user should not "be aware" of it.  This
    # is a pretty lame reason because pretty much everywhere else, the
    # firewall IS part of the experiment.  That leads to reason number
    # two: rebooting the firewall causes all other nodes in the experiment
    # to become disconnected until the firewall reboots.  For some machines,
    # that are also rebooting as you recall, not getting PXE info for a
    # significant amount of time causes them to fail to the next boot.
    # For some machines this might mean halting ("Strike any key to continue"),
    # as there is no other boot possibility.  This means ya gotta come back
    # later and reboot all those nodes again.
    #
    my $firewall = "";
Leigh Stoller's avatar
Leigh Stoller committed
207 208 209 210
    if ($experiment->IsFirewalled()) {
	$experiment->FirewallAndPort(\$firewall, undef) == 0 or
	    die("*** $0:\n".
		"    Could not get the firewall node for $experiment\n");
211
    }
Leigh Stoller's avatar
Leigh Stoller committed
212 213 214
    foreach my $node (@nodelist) {
	push(@nodes, $node->node_id())
	    if ($node->node_id() ne $firewall);
215 216 217
    }
}
else {
218 219
    #
    # Reboot nodes listed on command line.
Leigh Stoller's avatar
Leigh Stoller committed
220 221 222
    #
    usage()
	if (!@ARGV);
223

Leigh Stoller's avatar
Leigh Stoller committed
224 225 226 227 228
    foreach my $n (@ARGV) {
	my $node = Node->Lookup($n);
	if (!defined($node)) {
	    die("*** $0:\n".
		"    Node $n does not exist!\n");
Mac Newbold's avatar
Mac Newbold committed
229
	}
Leigh Stoller's avatar
Leigh Stoller committed
230 231 232

	if ($UID && !$this_user->IsAdmin() &&
	    ! $node->AccessCheck($this_user, TB_NODEACCESS_REBOOT)) {
233
	    die("*** $0:\n".
Leigh Stoller's avatar
Leigh Stoller committed
234
		"    You are not allowed to reboot $node!\n");
235
	}
Leigh Stoller's avatar
Leigh Stoller committed
236
	push(@nodes, $node->node_id());
237 238 239
    }
}

240
#
241
# Okay, call into the library using a hash of arguments.
242
#
243 244
my %args   = ();
my %status = ();
245

246
$args{'debug'}       = $debug;
247
$args{'silent'}      = $silent;
248
$args{'powercycle'}  = $powercycle;
249
$args{'rebootmode'}  = $rebootmode;
250 251 252 253
$args{'waitmode'}    = $waitmode;
$args{'realmode'}    = $realmode;
$args{'killmode'}    = $killmode;
$args{'reconfig'}    = $reconfig;
254
$args{'prepare'}     = $prepare;
255
$args{'waittime'}    = $waittime     if defined $waittime;
256
$args{'nodelist'}    = [ @nodes ];
257

258
exit(nodereboot(\%args, \%status));