os_select.in 5.76 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -wT

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
6 7 8 9
# All rights reserved.
#

# os_select sets the os that should boot next on a node, and sets
10
# next_op_mode accordingly.
11 12 13

sub usage() {
    print <<"EOF";
14
Usage: os_select [-h] [-d] [-c] [-1 | -t] [<osid>] <node> [<node> ...]
15 16
 -h    Display this help message
 -d    Debug mode
17 18 19
 -c    Clear the specified boot osid for nodes. Do not provide an osid.
 -1    Apply change to one-time boot field
 -t    Apply change to temporary boot field
20 21 22 23 24
 osid  OS identifier for the selected OS (see web interface for listing)
 node  Node identifiers (ie pcXX)
EOF
    exit(-1);
}
25
my $optlist = "hdc1t";
26 27 28 29 30 31 32 33 34

# 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

# Configure variables
my $TB          = "@prefix@";
35
my $TBOPS       = "@TBSTATEDEMAIL@";
36
my $TBLOG	= "@TBLOGFACIL@";
37 38 39 40 41 42

# Testbed Support libraries
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use English;
43 44
use Getopt::Std;
use Sys::Syslog;
45 46

# Constants
47
my $MBKERNEL = TB_OSID_MBKERNEL;
48
my %osidmap = # Map some magic OSIDs to op_modes
49
    ( $MBKERNEL => "MINIMAL");
50 51

# Global vars
52
my $debug	= 0; # debug/verbose
53 54 55 56 57 58
my $oneshot	= 0; # apply change to next_boot_osid.
my $tempmode	= 0; # apply change to temp_boot_osid.
my $clear       = 0; # Clear the selected boot (def,temp,next).
my @nodes       = ();
my $osid	= "";
my $opmode;
59

60
# Set up syslog
61
openlog("osselect", "pid", $TBLOG);
62

63
#
64 65
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
66
#
67 68 69
%options = ();
if (! getopts($optlist, \%options)) { usage(); }
if (defined($options{"h"})) { usage(); }
70
if (defined($options{"d"})) { $debug++; }
71
if (defined($options{"1"})) { $oneshot=1; }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
if (defined($options{"t"})) { $tempmode=1; }
if (defined($options{"c"})) { $clear=1; }

# In clearmode, there is no OSID. Just a list of nodes.
if (! $clear) {
    usage()
	if (@ARGV < 2);
    $osid = shift();

    # Untaint args.
    if ($osid =~ /^([-\w\+\.]+)$/) {
	$osid = $1;
    }
    else {
	fatal("Bad data in osid: '$osid'");
87 88
    }
}
89 90 91
else {
    usage()
	if (@ARGV < 1);
92 93
}

94 95 96 97 98 99 100 101 102 103
# Untaint the nodes.
foreach my $node ( @ARGV ) {
    if ($node =~ /^([-\@\w]+)$/) {
	$node = $1;
    }
    else {
	fatal("Bad node name: $node");
    }
    push(@nodes, $node);
}
104

105
#
106 107
# Figure out who called us. Only root, people with admin status
# in the DB, or members of the right project can do this.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
#
if ($UID && !TBAdmin($UID) &&
    !TBNodeAccessCheck($UID, TB_NODEACCESS_MODIFYINFO, @nodes)) {
    fatal("os_select: You do not have permission to modify ".
	  "one or more of the nodes.\n");
}

#
# Grab the info for the OSID. 
#
if (! $clear) {
    my $query_result =
	DBQueryFatal("select * from os_info where osid='$osid'");
    if ($query_result->numrows != 1) {
	fatal("Improper DB entry for OSID: $osid");
123
    }
124 125
    my %dbrow  = $query_result->fetchhash();
    $opmode = $dbrow{"op_mode"};
126 127
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
foreach my $node (@nodes) {
    my $curmode = node_opmode($node);

    # Why? When will this happen?
    next
	if (!$curmode);
    
    debug("Current opmode for $node is $curmode.\n");

    # Set/Clear the osid.
    set_boot_osid($node);

    #
    # Determine what osid the node will now boot. We need to know this so we
    # can set the next opmode. This call has to return *something* or we are
    # screwed since we will not be able to figure out the opmode.
    #
    my ($bootosid, $bootopmode) = TBBootWhat($node, $debug);

    fatal("Bootwhat query failed for $node!")
	if (!$bootosid);
    debug("Bootwhat says: $node => $bootosid\n");

    #
    # If its different then what the node is currently booting, then
    # set up a transition in stated. If no change, be sure to clear
    # is since stated does not like a transition to be specified when
    # none is actually going to be made.
    #
    if ($curmode ne $bootopmode) {
	set_nextmode($node, $bootopmode);
    }
    else {
	# Must clear it.
	set_nextmode($node, "");
    }
}
exit(0);
166 167 168 169 170

#
# Subroutines
#

171 172 173 174
# Set the next_op_mode field for a node.
sub set_nextmode($;$)
{
    my ($node, $opmode) = @_;
175

176 177
    if ($opmode eq "") {
	debug("Clearing next_op_mode for $node.\n");
178
    }
179 180
    else {
	debug("Setting next_op_mode for $node to $opmode.\n");    
181
    }
182 183 184
    
    DBQueryFatal("update nodes set next_op_mode='$opmode' ".
		 "where node_id='$node'");
185 186 187
    return 0;
}

188 189 190 191 192 193 194 195 196 197 198 199
# Set (or clear) the boot osid.
sub set_boot_osid($) {
    my ($node) = @_;
    my $field  = "def_boot_osid";

    $field = "next_boot_osid"
	if ($oneshot);
    $field = "temp_boot_osid"
	if ($tempmode);

    if ($clear) {
	debug("Clearing $field for $node.\n");
200
    }
201 202 203 204 205
    else {
	debug("Setting $field for $node to $osid.\n");
    }
    
    DBQueryFatal("update nodes set ${field}='$osid' where node_id='$node'");
206 207
}

208 209 210 211 212 213 214 215 216 217 218 219 220
# Return current opmode that a node is set to.
sub node_opmode($)
{
    my ($node) = @_;
    my $opmode;

    if (! TBGetNodeOpMode($node, \$opmode)) {
	fatal("Could not get opmode for node $node!");
    }
    if (!defined($opmode) || $opmode eq "" ||
	$opmode eq TBDB_NODEOPMODE_UNKNOWN) {
	warning("Invalid opmode $opmode for $node.\n");
	return 0;
221
    }
222
    return $opmode;
223 224
}

225 226 227 228 229 230 231 232 233 234
sub debug($;$)
{
    my $msg = shift;
    my $notice = shift || 0;
    my $prio="info";
    
    if ($notice) { $prio = "notice"; }
    
    syslog($prio, $msg);
    if ($debug) { print $msg; }
235 236
}

237 238 239 240 241 242 243 244
sub notify($)
{
    my $msg = shift;
    
    if (!$debug) {
	SENDMAIL($TBOPS, "os_select error", $msg);
    } 
    debug($msg, 1);
245 246
}

247 248 249 250 251
sub info($)
{
    my $msg = shift;
    
    debug($msg);
252 253
}

254 255 256 257 258 259
sub fatal($)
{
    my $msg = shift;
    
    notify("FATAL: $msg\n");
    exit(1);
260 261
}

262 263 264 265 266
sub warning($)
{
    my $msg = shift;
    
    info("WARNING: $msg\n");
267 268 269 270 271 272
}

# This is called when we exit with exit() or die()
END {
  closelog();
}