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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2004, 2007 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 52 53 54 55 56 57 58 59 60 61
# Functions

sub set_nextmode($;$);
sub set_boot_osid($);
sub node_opmode($);
sub debug($;$);
sub notify($);
sub info($);
sub fatal($);
sub warning($);

62
# Global vars
63
my $debug	= 0; # debug/verbose
64 65 66 67 68 69
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;
70

71
# Set up syslog
72
openlog("osselect", "pid", $TBLOG);
73

74
#
75 76
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
77
#
78 79 80
%options = ();
if (! getopts($optlist, \%options)) { usage(); }
if (defined($options{"h"})) { usage(); }
81
if (defined($options{"d"})) { $debug++; }
82
if (defined($options{"1"})) { $oneshot=1; }
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
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'");
98 99
    }
}
100 101 102
else {
    usage()
	if (@ARGV < 1);
103 104
}

105 106 107 108 109 110 111 112 113 114
# Untaint the nodes.
foreach my $node ( @ARGV ) {
    if ($node =~ /^([-\@\w]+)$/) {
	$node = $1;
    }
    else {
	fatal("Bad node name: $node");
    }
    push(@nodes, $node);
}
115

116
#
117 118
# Figure out who called us. Only root, people with admin status
# in the DB, or members of the right project can do this.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#
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");
134
    }
135 136
    my %dbrow  = $query_result->fetchhash();
    $opmode = $dbrow{"op_mode"};
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 166 167 168 169 170 171 172 173 174 175 176
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);
177 178 179 180 181

#
# Subroutines
#

182 183 184 185
# Set the next_op_mode field for a node.
sub set_nextmode($;$)
{
    my ($node, $opmode) = @_;
186

187 188
    if ($opmode eq "") {
	debug("Clearing next_op_mode for $node.\n");
189
    }
190 191
    else {
	debug("Setting next_op_mode for $node to $opmode.\n");    
192
    }
193 194 195
    
    DBQueryFatal("update nodes set next_op_mode='$opmode' ".
		 "where node_id='$node'");
196 197 198
    return 0;
}

199 200 201 202
# Set (or clear) the boot osid.
sub set_boot_osid($) {
    my ($node) = @_;
    my $field  = "def_boot_osid";
203
    my $value;
204 205 206 207 208 209 210 211

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

    if ($clear) {
	debug("Clearing $field for $node.\n");
212
	$value = "NULL";
213
    }
214 215
    else {
	debug("Setting $field for $node to $osid.\n");
216
	$value = "'$osid'";
217 218
    }
    
219
    DBQueryFatal("update nodes set ${field}=${value} where node_id='$node'");
220 221
}

222 223 224 225 226 227 228 229 230 231 232 233 234
# 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;
235
    }
236
    return $opmode;
237 238
}

239 240 241 242 243 244 245 246 247 248
sub debug($;$)
{
    my $msg = shift;
    my $notice = shift || 0;
    my $prio="info";
    
    if ($notice) { $prio = "notice"; }
    
    syslog($prio, $msg);
    if ($debug) { print $msg; }
249 250
}

251 252 253 254 255 256 257 258
sub notify($)
{
    my $msg = shift;
    
    if (!$debug) {
	SENDMAIL($TBOPS, "os_select error", $msg);
    } 
    debug($msg, 1);
259 260
}

261 262 263 264 265
sub info($)
{
    my $msg = shift;
    
    debug($msg);
266 267
}

268 269 270 271 272 273
sub fatal($)
{
    my $msg = shift;
    
    notify("FATAL: $msg\n");
    exit(1);
274 275
}

276 277 278 279 280
sub warning($)
{
    my $msg = shift;
    
    info("WARNING: $msg\n");
281 282 283 284 285 286
}

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