os_select.in 7.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
#!/usr/bin/perl -wT

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

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

sub usage() {
    print <<"EOF";
Usage: os_select [-h] [-d] [-v] [-1] [-p | -m] <osid> <node> [<node> ...]
 -h    Display this help message
 -d    Debug mode
 -v    Verbose mode
 -p    Path mode: osid is really a path to a kernel
 -1    Set up one-time boot to OS instead of changing default OS
 -m    MFS mode: osid={"PXEFBSD","PXEFRISBEE","PXEBOOT","host:/tftpboot/file"}
 osid  OS identifier for the selected OS (see web interface for listing)
 node  Node identifiers (ie pcXX)
EOF
    exit(-1);
}

use Getopt::Std;

# 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@";
my $TBOPS       = "@TBOPSEMAIL@";

# Testbed Support libraries
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use English;

# Constants
my %osidmap = # Map some magic OSIDs to op_modes
    ( (TB_OSID_MBKERNEL) => "MINIMAL");

# Global vars
my $d=0; # debug/verbose
my $oneshot=0; # is this a one-shot OS boot?
my $pathmode=0; # is osid really a path instead of an osid?
my $mfs=0; # is this for a pxe_boot_path change?

# Find default pxe boot path
my $cmd = "select path from os_info where osid='".TB_OSID_PXEBOOT."'";
my $q = DBQueryFatal($cmd);
my @r = $q->fetchrow_array();
my $pxebootpath = $r[0];

# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
my  $optlist = "hdvp1m";
%options = ();
if (! getopts($optlist, \%options)) { usage(); }
if (defined($options{"h"})) { usage(); }
if (defined($options{"d"})) { $d++; }
if (defined($options{"v"})) { $d++; }
if (defined($options{"p"})) { $pathmode=1; }
if (defined($options{"1"})) { $oneshot=1; }
if (defined($options{"m"})) { $mfs=1; }
print "DEBUG LEVEL $d\n" if $d;

print "ARGV= ".join(" ",@ARGV)."\n" if $d>1;
if (@ARGV < 2) { warn("An osid and a node list are required.\n"); usage(); }

my $osid = shift;
my @nodes = @ARGV;

# Untaint args.
if ($osid =~ /^([-\@\w\/]+)$/) { $osid = $1; }
elsif (!$mfs) { die("Bad data in osid: '$osid'\n"); }

if ($mfs) {
    if ($osid =~ /^[-0-9a-z\.]+:\/tftpboot\/[-0-9a-z\.]+$/) {
	print "MFS osid '$osid' is a PXE path\n" if $d>1;
	$pxebootpath=$osid;
	my $cmd = "select osid from os_info where path='$osid';";
	my $q = DBQueryFatal($cmd);
	if ($q->numrows() > 0) {
	    my @r = $q->fetchrow_array();
	    $osid=$r[0];
	} else { $pathmode=1; }
    } else {
	my $cmd = "select path from os_info where osid='$osid';";
	my $q = DBQueryFatal($cmd);
	if ($q->numrows() < 1) {
	    die("Invalid osid '$osid' is non-existent.\n");
	}
	my @r = $q->fetchrow_array();
	my $path=$r[0];
	if (defined($path) && $path ne "") {
	    $pxebootpath=$path;
	} else {
	    die("Invalid osid for use with -m: '$osid'\n");
	}
    }
}

my @temp;
foreach $n (@nodes) {
    if ($n =~ /^([\w]+)$/) { push(@temp,$1); }
    else { warn("Ignoring bad data in node_id: '$n'\n"); }
}
@nodes=@temp;

if (@nodes < 1) { die("No valid nodes supplied.\n"); }

# Figure out who called us. Only root, people with admin status
# in the DB, or members of the right project can do this.
if ($UID && !TBAdmin($UID)) {
    my ($me) = getpwuid($UID)
        or die "$UID not in passwd file";
    print "Not an admin.\n" if $d>1;
    if (! TBNodeAccessCheck($UID,TB_NODEACCESS_MODIFYINFO,@nodes)) {
        die("os_select: You do not have permission to modify ".
	    "one or more of the nodes.\n");
    }
    print "Access granted to all nodes requested.\n" if $d>1;
} else { print "Running as an admin.\n" if $d>1; }

my $pernodeopmode=0;
my $opmode = os_opmode($osid);
print "Found opmode '$opmode' for osid '$osid'.\n" if $d;
if ($opmode eq TBDB_NODEOPMODE_BOOTWHAT ) { $pernodeopmode=1; }

foreach $n (@nodes) {
    my $curmode = node_opmode($n);
    if (!$curmode) { next; }
    print "Current opmode for node '$n' is '$curmode'.\n" if $d;
    if ($mfs) { set_pxe_path($n); } 
    else { set_boot_osid($n); }
    my $boot = TBBootWhat($n,$d);
    if (!$boot) { die("***Bootwhat query failed. Contact testbed-ops.\n"); }
    print "Bootwhat says: $n => $boot\n" if $d>1;
    if ($pernodeopmode) { 
	# in per-node mode, opmode is the mode for the os that we're going 
	# to boot, whatever that may be on each node
	$opmode= os_opmode($boot); 
	print "Desired opmode for node '$n' is '$opmode'.\n" if $d;
    }
    if (!$curmode || ($curmode ne $opmode)) { set_nextmode($n); } 
    else { set_nextmode($n,""); } # Make sure it is clear
}

exit();

#
# Subroutines
#

sub set_nextmode() {
    my $node = shift || "";
    my $mode = shift;
    if (!defined($mode)) { $mode = $opmode; }
    my $cmd = "update nodes set next_op_mode='$mode' where node_id='$node';";
    print "Setting next_op_mode for node '$node' to '$mode'.\n" if $d;
    my $q = DBQueryFatal($cmd);
    return 0;
}

sub set_boot_osid() {
    my $node = shift || "";
    my $field = ($oneshot?"next":"def")."_boot_".($pathmode?"path":"osid");
    my $cmd = "update nodes set $field='$osid'";
    # Clear out the paths and the next_boot_osid except for possibly
    # one that we're going to set. Never clear def_boot_osid, since it
    # won't ever get in the way of what we really want to boot, and we
    # want something to fall back on.
    foreach $f ("next_boot_path", "def_boot_path", "next_boot_osid") {
	if ($f ne $field) { $cmd .= ", $f=''"; }
    }
    $cmd .= " where node_id='$node';";
    print "cmd=$cmd\n" if $d>1;
    print "Setting $field for node '$node' to '$osid'.\n" if $d;
    my $q = DBQueryFatal($cmd);
    return 0;
}

sub set_pxe_path() {
    my $node = shift || "";
    my $field = ($oneshot?"next_":"")."pxe_boot_path";
    my $cmd = "update nodes set $field='$pxebootpath'";
    # Clear out the next_pxe_boot_path if we're not going to set it
    foreach $f ("next_pxe_boot_path") {
	if ($f ne $field) { $cmd .= ", $f=''"; }
    }
    $cmd .= " where node_id='$node';";
    print "cmd=$cmd\n" if $d>1;
    print "Setting $field for node '$node' to '$pxebootpath'.\n" if $d;
    my $q = DBQueryFatal($cmd);
    return 0;
}
    

sub node_opmode() {
    my $node = shift || "";
    my $cmd = "select op_mode from nodes where node_id='$node';";
    my $q = DBQueryFatal($cmd);
    if ($q->numrows() < 1) {
	warn("Ignoring invalid node '$node' (non-existent)\n");
	return 0;
    }
    my @r = $q->fetchrow_array();
    my $opmode=$r[0];
    if (defined($opmode) && $opmode) { return $opmode; }
    warn("Invalid opmode '$opmode' for node '$node'.\n");
    return 0;
}

sub os_opmode() {
    my $osid = shift || "";
    if ($pathmode) { return $osidmap{TB_OSID_MBKERNEL}; }
    if (defined($osidmap{$osid})) { return $osidmap{$osid}; }
    my $cmd = "select op_mode from os_info where osid='$osid';";
    my $q = DBQueryFatal($cmd);
    if ($q->numrows() < 1) {
	die("Invalid osid '$osid' is non-existent.\n");
    }
    my @r = $q->fetchrow_array();
    my $opmode=$r[0];
    print "OpMode query for '$osid' gave '$opmode'\n" if $d>1;
    if (defined($opmode) && $opmode ne "") { return $opmode; }
    die("No opmode found for osid '$osid'.\n");
}