plabinelab.in 6.79 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2004-2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31
#
use English;
use Getopt::Std;

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
32
use User;
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

#
# Do things necessary for setting up inner elab experiment. 
#
sub usage()
{
    print STDOUT "Usage: plabinelab [-d] [-u] pid eid\n";
    print STDOUT "       plabinelab [-d] [-k] pid eid\n";
    print STDOUT "       plabinelab [-d] -r pid eid [node ...]\n";
 
    exit(-1);
}
my $optlist  = "dkur";
my $debug    = 1;
my $killmode = 0;
my $update   = 0;
my $remove   = 0;

#
# Configure variables
#
my $TB		= "@prefix@";
my $makeconf    = "$TB/sbin/dhcpd_makeconf";

# PXE boot path
my $pxeboot     = "/tftpboot/pxelinux.0";

# Locals
my $plabinelab;

# Protos
sub SetUp();
sub TearDown();
sub RemoveNodes(@);
sub AddNodes();
sub RestartDHCPD();

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

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

# Locals
my $query_result;

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"k"})) {
    $killmode = 1;
}
if (defined($options{"u"})) {
    $update = 1;
}
if (defined($options{"r"})) {
    $remove = 1;
}
if (! @ARGV) {
    usage();
}
my ($pid,$eid,@nodelist) = @ARGV;

#
# Untaint the arguments.
#
if ($pid =~ /^([-\w]+)$/) {
    $pid = $1;
}
else {
    die("Tainted argument $pid!\n");
}
if ($eid =~ /^([-\w]+)$/) {
    $eid = $1;
}
else {
    die("Tainted argument $eid!\n");
}

#
124
# Verify user and get his DB uid and other info for later.
125
#
126 127 128
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
}

TBDebugTimeStampsOn();
TBDebugTimeStamp("PlabInElab setup starting");

#
# Get plabinelab status to make sure, and to see if we need to fire off
# an experiment inside once its setup.
#
if (!TBExptIsPlabInElab($pid, $eid, \$plabinelab)) {
    die("*** $0:\n".
	"    Could not get plabinelab status for experiment $pid/$eid\n");
}
exit(0)
    if (!$plabinelab);

#
# Get a list of the nodes involved.  Not all nodes in the experiment
# have to be part of the plab (plab_role == 'none') and one node is the
# distinguished PLC node (plab_role == 'plc').
#
my $plcnode;
my @plabnodes = ();
$query_result =
    DBQueryFatal("select node_id,plab_role from reserved ".
		 "where pid='$pid' and eid='$eid' and plab_role!='none'");
while (my ($node,$role) = $query_result->fetchrow_array()) {
    if ($role eq "plc") {
	$plcnode = $node;
    } else {
	push(@plabnodes, $node);	
    }
}

my $rv;
if ($killmode) {
    $rv = TearDown();
} elsif ($remove) {
    $rv = RemoveNodes(@nodelist);
} elsif ($update) {
    $rv = AddNodes();
} else {
    $rv = SetUp();
}

TBDebugTimeStamp("PlabInElab setup done");
exit($rv);

sub SetUp()
{
    if ($plcnode || @plabnodes > 0) {
	#
	# Currently we jigger the pxe_boot_path field for inner plab nodes
	# so that they run pxelinux when rebooted.  pxelinux is the easiest
	# way of downloading a "Linux MFS".  Maybe someday we'll do it with
	# pxeboot.
	#
	if (@plabnodes > 0) {
	    DBQueryFatal("update nodes set pxe_boot_path='$pxeboot' ".
			 "where (".
			 join(" or ", map("node_id='$_'", @plabnodes)) . ")");
	}
	
	#
	# Now mark all plab involved nodes as being ready to boot inside,
	# so that dhcpd_makeconf knows what nodes to change the entries for.
	#
	push(@plabnodes, $plcnode);
	DBQueryFatal("update reserved set plab_boot=1 ".
		     "where pid='$pid' and eid='$eid' and (".
		     join(" or ", map("node_id='$_'", @plabnodes)) . ")");
	
	#
	# Now that we are done fixing up the DB with info that affects the
	# DHCPD config file, we can restart DHCPD.
	#
	RestartDHCPD();
    }
}

#
# Take down all inner plab nodes.
#
# In theory, nfree will clean up nodes.pxe_boot_path and
# the reserved table as well as recreating the DHCPD config file.
# However, a lot can happen between now and nfree (e.g., forcing a
# node into the MFS which would fail due to pxe_boot_path) so we do
# it ourselves to be safe.
# 
sub TearDown()
{
    #
    # Reset the PXE boot
    #
    if ($plcnode || @plabnodes > 0) {
	if (@plabnodes > 0) {
	    DBQueryFatal("update nodes set pxe_boot_path=NULL ".
			 "where (".
			 join(" or ", map("node_id='$_'", @plabnodes)) . ")");
	}

	#
	# Update the reserved table to mark nodes as no longer part
	# of the plab.
	#
	DBQueryFatal("update reserved set plab_role='none',plab_boot=0 ".
		     "where pid='$pid' and eid='$eid'");

	#
	# and restart DHCPD
	#
	RestartDHCPD();
    }

    return 0;
}

#
# Remove some nodes from an inner plab (on tbswap UPDATE).
# Tricky because the user could remove the PLC node as well.
# 
sub RemoveNodes(@)
{
    my @newnodes   = @_;
    my @nodes      = ();

    #
    # Grab the list of nodes. We want to clear the reserved table bits so
    # that we can regen the DHCPD file. 
    #
    foreach my $node (@newnodes) {
	# Untaint the nodes.
	if ($node =~ /^([-\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("*** Tainted node name: $node\n");
	}
	if (($node eq $plcnode) && @plabnodes > 0) {
	    tbwarn "Removing PLC node, expect problems!";
	}
	push(@nodes, $node);
    }
    return 0
	if (!@nodes);

    #
    # Reset pxe_boot_path
    #
    DBQueryFatal("update nodes set pxe_boot_path=NULL ".
		 "where (".
		 join(" or ", map("node_id='$_'", @nodes)) . ")");

    #
    # Update the reserved table
    #
    DBQueryFatal("update reserved set plab_role='none',plab_boot=0 ".
		 "where pid='$pid' and eid='$eid' and (".
		 join(" or ", map("node_id='$_'", @nodes)) . ")");

    #
    # and restart DHCPD
    #
    RestartDHCPD();

    return 0;
}

#
# Add some nodes as part of a tbswap UPDATE
# No different than a first-time setup right now.
# 
sub AddNodes()
{
    return SetUp();
}

sub RestartDHCPD()
{
    print "Regenerating DHCPD config file and restarting daemon.\n";
    system("$makeconf -i -r");
    if ($?) {
	die("*** $0:\n".
	    "    Failed to reconfig/restart DHCPD.\n");
    }
}