node_control.in 6.97 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/perl -wT
use English;
use Getopt::Std;

#
# usage: node_control [options] node [node ...]
#        node_control [options] -e pid,eid
#
9 10
# XXX def_boot_osid and virt_nodes osname are not handled properly. 
#
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
sub usage()
{
    print("Usage: node_control name=value [name=value ...] node [node ...]\n".
	  "       node_control -e pid,eid name=value [name=value ...]\n".
	  "       node_control -l\n".
	  "For multiword values, use name='word0 ... wordN'\n".
	  "Use -l to get a list of operational parameters you can change.\n".
	  "Use -e to change parameters of all nodes in an experiment.\n");
    exit(-1);
}
my  $optlist = "de:l";

#
# Array of allowed names. All of these values are text, so no need to
# worry about distinguishing numeric stuff.
#
# XXX This should be in the library.
#
my %controlset =
(
 #
 #  Symbolic name   =>  Admin, Multi args, nodes DB field, virt_nodes DB field
 #
34
 default_boot_osid	=> [0, 0, "def_boot_osid",	undef],
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
 default_boot_path	=> [0, 0, "def_boot_path",	undef],
 default_boot_cmdline	=> [0, 0, "def_boot_cmd_line",  "cmd_line"],
 startup_command	=> [0, 0, "startupcmd",		"startupcmd"],
 tarfiles		=> [0, 1, "tarballs",		"tarfiles"],
 rpms			=> [0, 1, "rpms",		"rpms"],
 deltas			=> [0, 1, "deltas",		"deltas"],
 next_boot_osid		=> [1, 0, "next_boot_osid",	undef],
 next_boot_path		=> [1, 0, "next_boot_path",	undef],
 next_boot_cmdline	=> [1, 0, "next_boot_cmd_line",	undef],
 pxe_boot_path          => [1, 0, "pxe_boot_path",	undef],
 next_pxe_boot_path     => [1, 0, "next_pxe_boot_path",	undef],
 bios_version		=> [1, 0, "bios_version",	undef],
);
  
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

my $ssh		= "$TB/bin/sshtb -n";
my @nodes       = ();
my %controls    = ();
my $eidmode	= 0;
my $debug       = 0;
my $errors	= 0;
my $pid;
my $eid;
my $dbuid;

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

my $IsAdmin    = TBAdmin($UID);

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

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"l"})) {
    foreach my $option (keys(%controlset)) {
	my ($admin, $multi, $dbkey) = @{ $controlset{$option} };
	
	printf("  %-25s ", $option);
	if ($multi || $admin) {
	    print "- ";
	}
	if ($multi) {
	    print "(multiple options allowed) ";
	}
	if ($admin) {
	    print "(administrators only)";
	}
	print "\n";
    }
    exit(0);
}

if (defined($options{"e"})) {
    $eidmode = $options{"e"};
    if ($eidmode =~ /([-\w]*),([-\w]*)/) {
	$pid = $1;
	$eid = $2;
    }
    else {
	usage();
    }
}

if (! @ARGV) {
    usage();
}

#
# Shift off the set strings (name=value). Verify that each one is in the
# proper format.
#
while (@ARGV) {
    my $string = $ARGV[0];

    if (! ($string =~ /([-\w]*)=[\']?([^\']*)[\']?/)) {
	last;
    }
    shift;

    if (! defined($controlset{$1})) {
	die("*** $0:\n".
	    "    Illegal control setting: $1='$2'. Try the -l option!\n");
    }
    my ($admin, $multi, $dbkey) = @{ $controlset{$1} };

    if ($admin && ! $IsAdmin) {
	die("*** $0:\n".
	    "    You do not have permission to set $1. Try the -l option!\n");
    }

    if ($multi && defined($controls{$1})) {
	$controls{$1} = "$controls{$1}:$2";
    }
    else {
	$controls{$1} = "$2";
    }
}

if ($debug) {
    foreach my $option (keys(%controls)) {
	print "Will set $option to '$controls{$option}'\n";
    }
}

#
# In eidmode, check the access permission for the experiment, and then
# get the nodes. Otherwise, check access for each node given on the
# command line.
#
if ($eidmode) {
    # Permission check.
    if ($UID && !TBAdmin($UID) &&
	! TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
	die("*** $0:\n".
	    "    You do not have permission to control nodes in $pid/$eid!\n");
    }

    if (! (@nodes = ExpNodes($pid, $eid))) {
	die("*** $0:\n".
	    "    There are no nodes in $pid/$eid!\n");
    }
}
else {
    if (! @ARGV) {
	usage();
    }
    
    # Untaint the nodes.
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\@\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("Bad node name: $node.");
	}
    
	push(@nodes, $node);
    }
}

#
# Create an update clause for all of the specified values.
#
my $physnodes_updatestr;
my $virtnodes_updatestr;

foreach my $option (keys(%controls)) {
    my ($admin, $multi, $physdbkey, $virtdbkey) = @{ $controlset{$option} };
    my $value = $controls{$option};

    if (defined($physnodes_updatestr)) {
	$physnodes_updatestr = "$physnodes_updatestr, $physdbkey='$value'";
    }
    else {
	$physnodes_updatestr = "$physdbkey='$value'";
    }

    if (defined($virtdbkey)) {
	if (defined($virtnodes_updatestr)) {
	    $virtnodes_updatestr = "$virtnodes_updatestr, $virtdbkey='$value'";
	}
	else {
	    $virtnodes_updatestr = "$virtdbkey='$value'";
	}
    }
}
if (! defined($physnodes_updatestr) && ! defined($virtnodes_updatestr)) {
    exit(0);
}

if ($debug) {
    if (defined($physnodes_updatestr)) {
	print "Phys update will be '$physnodes_updatestr'\n";
    }
    if (defined($virtnodes_updatestr)) {
	print "Virt update will be '$virtnodes_updatestr'\n";
    }
}

#
# Now do it for every node. Do the permission check here to reduce the
# race condition window. Should probably lock instead, but thats a pain.
#
foreach my $node (@nodes) {
    my $node_pid;
    my $node_eid;
    my $node_vname;

    #
    # We need the vname for the node so that we can update the virt_nodes
    # table. This implies that we cannot update the virt_nodes unless the
    # experiment is swapped in (so we can map from phys node name to virt
    # node name). At some point, maybe we want to provide a way to change
    # the params of a swapped out experiment by having the user specify
    # the vname?
    #
    if (! NodeidToExp($node, \$node_pid, \$node_eid, \$node_vname)) {
	print("*** $0:\n".
	      "    Node $node is not a member of a swapped in experiment!\n");
	$errors++;
	next;
    }
    
    if (defined($physnodes_updatestr)) {
	if ($UID && !$IsAdmin &&
	    ! TBNodeAccessCheck($UID, TB_NODEACCESS_MODIFYINFO, $node)) {
	    print("*** $0:\n".
		  "    You do not have permission to modify physical ".
		  "parameters for $node!\n");
	    
	    $errors++;
	    next;
	}
	DBQueryFatal("update nodes set $physnodes_updatestr ".
		     "where node_id='$node'");
    }

    if (defined($node_vname) && defined($virtnodes_updatestr)) {
	if ($UID && !$IsAdmin &&
	    ! TBExptAccessCheck($UID, $node_pid, $node_eid, TB_EXPT_MODIFY)) {
	    print("*** $0:\n".
		  "    You do not have permission to modify virtual ".
		  "parameters for $node!\n");
	    
	    $errors++;
	    next;
	}
	DBQueryFatal("update virt_nodes set $virtnodes_updatestr ".
		     "where pid='$node_pid' and eid='$node_eid' ".
		     " and vname='$node_vname'");
    }
}

exit($errors);