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

#
11
# Turn on/off admin mode for nodes and optionally reboot/wait.
12 13 14
#
sub usage()
{
15
    print STDOUT "Usage: node_admin [-h] [-n | -w] <on | off> [node ....]\n";
16
    print STDOUT "       node_admin [-h] [-n | -w] -e pid,eid <on | off>\n";
17 18 19 20 21 22
    print STDOUT "-h     This message\n";
    print STDOUT "-n     Do not reboot node\n";
    print STDOUT "-w     Wait for node to come back up if rebooted\n";
    print STDOUT "-e     Operate on all nodes in an experiment\n";
    print STDOUT "-c cmd Run command in MFS and wait for completion\n".
		 "       (-n and -w apply after the command is run).\n";
23 24
    exit(-1);
}
25
my $optlist  = "hnwe:c:";
26 27
my $waitmode = 0;
my $reboot   = 1;
28
my $runcmd   = "";
29 30 31 32 33 34 35 36 37 38 39 40

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
41
use libadminmfs;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# 
#
my $nodereboot	= "$TB/bin/node_reboot";
58
my $osselect    = "$TB/bin/os_select";
59
my @nodes       = ();
60 61 62 63 64 65 66 67 68

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
69
if (defined($options{h})) {
70 71
    usage();
}
72 73 74 75 76 77 78
if (defined($options{"n"})) {
    $reboot = 0;
}
if (defined($options{"w"})) {
    $waitmode = 1;
}

79
my $onoff;
80

81 82 83 84 85 86 87 88 89
if (defined($options{"c"})) {
    $runcmd = $options{"c"};
    $onoff = "on";
} else {
    if (!@ARGV) {
	usage();
    }
    $onoff  = shift(@ARGV);
}
90 91 92 93 94

if ($onoff ne "on" && $onoff ne "off") {
    usage();
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
if (defined($options{"e"})) {
    #
    # Reboot all nodes in an experiment
    #
    if (@ARGV) {
	usage();
    }

    my $eidmode = $options{"e"};
    my $pid;
    my $eid;
    
    if ($eidmode =~ /([-\w]*),([-\w]*)/) {
	$pid = $1;
	$eid = $2;
    }
    else {
	die("*** $0:\n".
	    "    Invalid argument to -e option: $eidmode\n");
    }

    #
    # Verify permission to muck with this experiment. This is to head off
    # permission problems early; the nodes are indvidually checked later
    # in the library.
    #
    if ($UID && !TBAdmin($UID) &&
	! TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
	die("*** $0:\n".
	    "    You not have permission to reboot nodes in $pid/$eid!\n");
    }

    my $query_result =
	DBQueryFatal("select node_id from reserved where ".
		     "pid='$pid' and eid='$eid'");

    if ($query_result->numrows == 0) {
	die("*** $0:\n".
            "    There are no nodes reserved in pid/eid $pid/$eid\n");
    }
    
    while (my ($nodeid) = $query_result->fetchrow_array()) {
	push(@nodes, $nodeid);
    }
139 140
}
else {
141 142 143
    if (! @ARGV) {
	usage();
    }
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158
    # Untaint the nodes.
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("*** Tainted node name: $node\n");
	}
	if (!TBValidNodeName($node)) {
	    die("*** $0:\n".
		"    Node does not exist: $node\n");
	}
	push(@nodes, $node);
    }
159

160 161 162 163 164 165 166 167 168
    #
    # Root and admin types can do whatever they want. Normal users
    # can only run this on nodes in their own experiments.
    #
    if ($UID && !TBAdmin($UID)) {
	if (! TBNodeAccessCheck($UID, TB_NODEACCESS_LOADIMAGE, @nodes)) {
	    die("*** $0:\n".
		"    You are not allowed to put some nodes into admin mode\n");
	}
169 170 171
    }
}

172 173 174 175 176 177 178 179 180 181 182
my @bad;
my %args;

if ($runcmd ne "") {
    #
    # Reboot into admin more and run the command
    #
    %args = ();
    $args{'name'} = $0;
    $args{'command'} = $runcmd;
    if (TBAdminMfsRunCmd(\%args, undef, @nodes)) {
183
	die("*** $0:\n".
184
	    "    Failed to run '$runcmd' on some of @nodes!\n");
185
    }
186

187 188 189 190 191 192 193 194 195 196 197
    #
    # Turn admin mode back off and optionally reboot back to the old OS
    #
    %args = ();
    $args{'name'} = $0;
    $args{'on'} = 0;
    $args{'clearall'} = 0;
    if (TBAdminMfsSelect(\%args, \@bad, @nodes)) {
	die("*** $0:\n".
	    "    Could not turn admin mode off for @bad!\n");
    }
198

199 200 201 202 203 204 205
    if ($reboot) {
	%args = ();
	$args{'name'} = $0;
	$args{'on'} = 0;
	$args{'reboot'} = $reboot;
	$args{'wait'} = $waitmode;
	if (TBAdminMfsBoot(\%args, \@bad, @nodes)) {
206
	    die("*** $0:\n".
207
		"    Did not properly reboot @bad after command!\n");
208 209
	}
    }
210 211
    exit(0);
}
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226
$args{'name'} = $0;
$args{'on'} = ($onoff eq "on");
$args{'clearall'} = 0;
if (TBAdminMfsSelect(\%args, \@bad, @nodes)) {
    die("*** $0:\n".
	"    Could not turn admin mode $onoff for @bad!\n");
}
if ($reboot) {
    %args = ();
    $args{'name'} = $0;
    $args{'on'} = ($onoff eq "on");
    $args{'reboot'} = $reboot;
    $args{'wait'} = $waitmode;
    if (TBAdminMfsBoot(\%args, \@bad, @nodes)) {
227
	die("*** $0:\n".
228
	    "    Did not properly reboot @bad!\n");
229
    }
230 231
}
exit(0);