parse-ns.in 18.4 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9 10
use English;
use Getopt::Std;
11
use Socket;
Kevin Atkinson's avatar
 
Kevin Atkinson committed
12
use File::Basename;
13 14
    
#
15 16 17 18 19 20
# Parse an ns file. Since the parser runs arbitrary NS file for the user,
# this cannot be safely done on boss without jumping through huge hoops
# to secure tcl and the DB. Yuck! So, instead of running the parser on boss,
# we run it over on ops. This first version operates like this:
#
# NB: This script is setuid.
21 22 23 24
#
sub usage()
{
    print STDOUT
25
	"Usage: parse-ns [options] pid gid eid nsfile\n".
26 27 28 29
        "Where options and arguments are those required by parse.tcl\n";
 
    exit(-1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30
my $optlist  = "nqap";
31
my $anonmode = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
my $passmode = 0;
33 34
my $impotent = 0;
my $debug    = 0;
35 36 37 38 39 40

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
41 42 43 44 45 46 47 48 49 50 51 52
my $CONTROL  = "@USERNODE@";
my $TESTMODE = @TESTMODE@;

# Locals
my $infile   = "parse.input";
my $outfile  = "parse.output";
my $SAVEUID  = $UID;
my $dbuid;
my $pid;
my $gid;
my $eid;
my $nsfile;
53
my $experiment;
54 55 56 57 58 59 60 61 62

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

#
# Untaint the path
# 
63
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
64 65
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

66 67 68 69 70 71
if ($TESTMODE) {
    # In testmode, drop privs (my devel tree at home is TESTMODE=1)
    $EUID = $UID;
}
elsif ($EUID != 0) {
    # We don't want to run this script unless its the real version.
72
    die("Must be root! Maybe its a development version?");
73 74 75 76 77
}

# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
if ($UID == 0) {
78
    die("Please do not run this as root! Its already setuid!");
79 80
}

81 82 83 84
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
85
use libdb;
86
use libtestbed;
Kevin Atkinson's avatar
 
Kevin Atkinson committed
87 88
use libtblog;
use libtblog qw(dblog *SERR);
89
use NodeType;
90 91
use Template;
use Experiment;
92

93 94 95 96 97
use constant false => 0;
use constant true  => 1;

sub parse_error($);

98
#
99 100
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
101
#
102 103 104 105 106 107 108 109 110 111
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"a"})) {
    $anonmode = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
112 113 114
if (defined($options{"p"})) {
    $passmode = 1;
}
115 116 117
if (@ARGV != 4 && @ARGV != 1) {
    usage();
}
118

119 120 121 122 123 124 125 126
if (@ARGV == 1) {
    $nsfile = $ARGV[0];
}
else {
    $pid   = $ARGV[0];
    $gid   = $ARGV[1];
    $eid   = $ARGV[2];
    $nsfile= $ARGV[3];
127 128 129
}

#
130
# Must taint check!
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
if ($nsfile =~ /^([-\w\/\.]+)$/) {
    $nsfile = $1;
}
else {
    die("Bad data in argument: $nsfile.");
}
   
if (defined($pid)) {
    if ($pid =~ /^([-\w]+)$/) {
	$pid = $1;
    }
    else {
	die("Bad data in argument: $pid.");
    }
    if ($eid =~ /^([-\w]+)$/) {
	$eid = $1;
    }
    else {
	die("Bad data in argument: $eid.");
    }
    if ($gid =~ /^([-\w]+)$/) {
	$gid = $1;
    }
    else {
	die("Bad data in argument: $gid.");
    }
158 159 160 161 162 163

    # Slowly convert to new libraries ...
    $experiment = Experiment->Lookup($pid, $eid);
    if (! defined($experiment)) {
	tbdie("Cannot find the experiment object for $pid/$eid");
    }
164 165 166 167 168 169 170 171 172 173 174 175
}

# Remove temps cause of swapin by alternates.
unlink($infile);
unlink($outfile);

if (!$TESTMODE) {
    #
    # Get DB uid for sending over to ops. In testmode, it runs locally
    # as the user running the testsuite, obviously.
    #
    if (! UNIX2DBUID($UID, \$dbuid)) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
176
	tbdie("You do not exist in the Emulab Database!");
177
    }
178

179 180
    # Run as the user for most of this script.
    $EUID = $UID;
181 182
}

183
# Create the defs files.
184
sub GenDefsFile($);
185 186 187 188 189 190
GenDefsFile($infile);

# Now append the NS file to. This part is hokey. Fix later.
system("cat $nsfile >> $infile") == 0
    or die("Could not combine TCL defs file and the NS file!\n");

191 192 193 194 195
#
# Touch the output file, to avoid a root owned, 644 file. 
#
if (-e $outfile) {
    unlink($outfile) or
Kevin Atkinson's avatar
 
Kevin Atkinson committed
196
	tbdie("Could not unlink $outfile");
197 198
}
system("touch $outfile") == 0 or
Kevin Atkinson's avatar
 
Kevin Atkinson committed
199
    tbdie("Could not create $outfile");
200

201
#
202 203 204
# Build up a new command line to run the parser on ops, writing the
# result back to a file if not in anonmode. Remember to tack on the
# user ID to flip to, when not in testmode.
205
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
206
my $cmdargs = "$TB/libexec/ns2ir/parse.proxy ";
207 208
$cmdargs .= " -u $dbuid"
    if (defined($dbuid));
Timothy Stack's avatar
 
Timothy Stack committed
209
$cmdargs .= " -- ";
210
$cmdargs .= " -a"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
    if ($anonmode);
212 213 214 215
$cmdargs .= " -n"
    if (defined($options{"n"}));
$cmdargs .= " -q"
    if (defined($options{"q"}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216 217
$cmdargs .= " -p"
    if ($passmode);
218 219
$cmdargs .= " $pid $gid $eid"
    if (!$anonmode);
220 221

#
222
# Run parser, redirecting stdout to a file to capture the parser results.
Kevin Atkinson's avatar
 
Kevin Atkinson committed
223
# Stderr is redirected to the ERR filehandle
224
# Must flip to real root to run ssh.
225
#
226
if ($TESTMODE) {
227
    #
228 229
    # In testmode, no ssh. Just run locally.
    #
Kevin Atkinson's avatar
 
Kevin Atkinson committed
230
    open ERR, "$cmdargs < $infile 2>&1 >> $outfile |";
231 232
}
else {
233
    #
234 235 236 237
    # In non-testmode, must flip to real root for the ssh, and then flip
    # back afterwards.
    # 
    $EUID = $UID = 0;
Kevin Atkinson's avatar
 
Kevin Atkinson committed
238
    open ERR, "sshtb -host $CONTROL $cmdargs < $infile 2>&1 >> $outfile |";
239 240 241
    $EUID = $UID = $SAVEUID;
}

Kevin Atkinson's avatar
 
Kevin Atkinson committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
#
# Now read in the results from stderr and correctly insert the errors
# into the database under the process name of the script called above.
# Also echo the results to the non-captured stderr (SERR).  This is
# needed because parse.proxy may be run on ops, not boss.
#
{
    my $old_env = \%ENV;
    local %ENV;
    copy_hash %ENV, $old_env;

    my @argv = split /\s+/, $cmdargs;
    my $cmd = basename(shift @argv);
    
    tblog_new_process($cmd, @argv);
Kevin Atkinson's avatar
 
Kevin Atkinson committed
257
    tblog_set_default_cause('user');
Kevin Atkinson's avatar
 
Kevin Atkinson committed
258 259 260 261 262 263 264

    my $err = '';
    
    # assume any output indicates an error
    while (<ERR>) {
	print SERR $_;
	if (/^\s*\*\*\*\s*(.+)/) {
265
	    parse_error($err);
Kevin Atkinson's avatar
 
Kevin Atkinson committed
266 267 268 269 270 271 272 273
	    dblog(TBLOG_ERR, {}, $err) if $err; # flush any previous errors
	    $_ = $1;
	    if (/^\S+:\s*$/) { # error on next line
		$_ = <ERR>;
		print SERR $_;
		s/^\s+//;
	    } 
	    s/\s+$//;
274
	    parse_error($_);
Kevin Atkinson's avatar
 
Kevin Atkinson committed
275 276 277 278 279 280
	    dblog(TBLOG_ERR, {}, $_) if $_;
	    $err = '';
	} else {
	    $err .= $_;
	}
    }
281
    parse_error($err);
Kevin Atkinson's avatar
 
Kevin Atkinson committed
282 283 284 285 286 287 288
    dblog(TBLOG_ERR, {}, $err) if $err;

    tblog_exit();
}
close ERR;


289 290 291
if ($?) {
    my $exit_status = $? >> 8;
    
292 293 294
    tbdie({cause => 'user', type => 'extra', severity => SEV_ERROR,
	   error => ['ns_parse_failed']},
	  "Parsing failed (error code $exit_status)!");
295 296 297 298 299 300 301
}
exit(0)
    if ($impotent);

#
# Run the XML converter on the output. 
#
302
system("$TB/libexec/xmlconvert -p -x $outfile $pid $eid");
303 304 305 306

if ($?) {
    my $exit_status = $? >> 8;
    
307 308 309
    tbdie({type => 'primary', severity => SEV_ERROR,
	   error => ['xml_converter_failed']},
	  "XML converter exited with error code $exit_status!\n");
310 311 312 313 314
}

#
# Now we have to fix up one minor thing; widearea tunnel IPs. These have
# to be unique, but without the DB to ask, there is no easy way to arrange
315 316
# that.

317
my %subnetmap = ();
318
my $WANETMASK = "255.255.255.248";
319 320 321 322 323 324 325 326 327 328 329 330

my $query_result =
    DBQueryFatal("select vname,ips from virt_nodes ".
		 "where pid='$pid' and eid='$eid'");
while (my ($vname,$ips) = $query_result->fetchrow_array()) {
    my @newiplist = ();
    my $newips;

    foreach my $ipinfo (split(" ", $ips)) {
	my ($port,$ip) = split(":", $ipinfo);
	my ($a,$b,$c,$d) = ($ip =~ /(\d+).(\d+).(\d+).(\d+)/);

331
	if ($a eq "69" && $b eq "69") {
332 333 334
	    my $net = inet_ntoa(inet_aton($WANETMASK) & inet_aton($ip));
		
	    if (! defined($subnetmap{$net})) {
335 336 337 338 339 340
		DBQueryFatal("insert into ipsubnets ".
			     "values ('$pid','$eid', NULL)");
		my ($id) =
		    DBQueryFatal("select LAST_INSERT_ID() ".
				 "from ipsubnets")->fetchrow_array();

341 342 343 344 345 346 347 348 349 350
		# We are going to shift the bits up so they do not conflict
		# with the lower part.
		if ($id >= 8192) {
		    die("No more widearea subnets left!\n");
		}
		$id = $id << 3;

		my $cc = ($id & 0xff00) >> 8;
		my $dd = ($id & 0xf8);
		$subnetmap{$net} = inet_aton("192.168.$cc.$dd");
351
	    }
352 353
	    my $newsubnet = inet_ntoa($subnetmap{$net} | inet_aton("$d"));
	    push(@newiplist, "$port:${newsubnet}");
354 355 356 357
	}
	else {
	    push(@newiplist, $ipinfo);
	}
358
    }
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    $newips = join(" ", @newiplist);
    
    if ($newips ne $ips) {
	DBQueryFatal("update virt_nodes set ips='$newips' ".
		     "where vname='$vname' and pid='$pid' and eid='$eid'");
    }
}
#
# So, if we ended up changing any, we have look for corresponding entries
# in virt_trafgens, and fix them too.
#
if (keys(%subnetmap)) {
    $query_result =
	DBQueryFatal("select vnode,vname,ip,target_ip from virt_trafgens ".
		     "where pid='$pid' and eid='$eid'");
    while (my ($vnode,$vname,$ip,$dstip) = $query_result->fetchrow_array()) {
	my ($a,$b,$c,$d) = ($ip =~ /(\d+).(\d+).(\d+).(\d+)/);
	my $newip        = $ip;
	my $newdstip     = $dstip;
378
	my $net		 = inet_ntoa(inet_aton($WANETMASK) & inet_aton($ip));
379
	
380 381
	if (defined($subnetmap{$net})) {
	    $newip = inet_ntoa($subnetmap{$net} | inet_aton("$d"));
382 383
	}
	($a,$b,$c,$d) = ($dstip =~ /(\d+).(\d+).(\d+).(\d+)/);
384 385 386
	$net          = inet_ntoa(inet_aton($WANETMASK) & inet_aton($dstip));
	if (defined($subnetmap{$net})) {
	    $newdstip = inet_ntoa($subnetmap{$net} | inet_aton("$d"));
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	}
	if ($ip ne $newip || $dstip ne $newdstip) {
	    DBQueryFatal("update virt_trafgens set ".
			 "       ip='$newip',target_ip='$newdstip' ".
			 "where pid='$pid' and eid='$eid' and ".
			 "      vnode='$vnode' and vname='$vname'");
	}
    }
}
exit(0);


#
# Open up a TCL file and write a bunch of TCL to it!
#
sub GenDefsFile($)
{
    my($fname) = $_[0];

    open(TCL, "> $fname")
	or die("Could not open TCL defs file for writing: $!\n");

    print TCL "namespace eval TBCOMPAT {\n";

    print TCL "# Event Object Types\n";
    my $query_result =
	DBQueryFatal("select idx,type from event_objecttypes");
    while (my ($idx,$type) = $query_result->fetchrow_array()) {
	print TCL "set objtypes($type) $idx\n";
    }
    print TCL "\n";

    print TCL "# Event Event Types\n";
    $query_result =
	DBQueryFatal("select idx,type from event_eventtypes");
    while (my ($idx,$type) = $query_result->fetchrow_array()) {
	print TCL "set eventtypes($type) $idx\n";
    }
    print TCL "\n";
    
427 428 429 430 431 432 433 434 435 436
    print TCL "# Node Types and Classes\n";
    my @alltypes = NodeType->AllTypes();
    foreach my $typeinfo (@alltypes) {
	my $type   = $typeinfo->type();
	my $class  = $typeinfo->class();
	my $isrem  = $typeinfo->isremotenode();
	my $isvirt = $typeinfo->isvirtnode();
	my $issub  = $typeinfo->issubnode();
	my $osid   = $typeinfo->default_osid();

437
	print TCL "set hwtypes($type) 1\n";
Timothy Stack's avatar
 
Timothy Stack committed
438
	print TCL "set hwtype_class($type) $class\n";
439 440
	print TCL "set isremote($type) $isrem\n";
	print TCL "set isvirt($type) $isvirt\n";
441 442 443 444
	print TCL "set issubnode($type) $issub\n";

	# Since there are multiple types per class, this is probably not
	# the right thing to do.
445 446 447
	print TCL "set hwtypes($class) 1\n";
	print TCL "set isremote($class) $isrem\n";
	print TCL "set isvirt($class) $isvirt\n";
448 449 450 451 452 453 454 455 456 457 458 459 460 461
	print TCL "set issubnode($class) $issub\n";

	if (defined($osid) && $osid ne "") {
	    $query_result =
		DBQueryFatal("select osname from os_info ".
			     "where osid='$osid'");
	    if ($query_result->numrows) {
		my ($osname) = $query_result->fetchrow_array();

		if ($osname) {
		    print TCL "set default_osids($type) \"$osname\"\n"
		}
	    }
	}
462 463
    }
    print TCL "\n";
464
    
465
    print TCL "# Node Aux Types\n";
466 467 468 469 470 471 472 473
    my @auxtypes = NodeType->AuxTypes();
    foreach my $typeinfo (@auxtypes) {
	my $type   = $typeinfo->type();
	my $isrem  = $typeinfo->isremotenode();
	my $isvirt = $typeinfo->isvirtnode();
	my $issub  = $typeinfo->issubnode();
	my $osid   = $typeinfo->default_osid();

474 475 476
	print TCL "set hwtypes($type) 1\n";
	print TCL "set isremote($type) $isrem\n";
	print TCL "set isvirt($type) $isvirt\n";
477 478 479 480 481 482 483 484 485 486 487 488 489 490
	print TCL "set issubnode($type) $issub\n";

	if (defined($osid) && $osid ne "") {
	    $query_result =
		DBQueryFatal("select osname from os_info ".
			     "where osid='$osid'");
	    if ($query_result->numrows) {
		my ($osname) = $query_result->fetchrow_array();

		if ($osname) {
		    print TCL "set default_osids($type) \"$osname\"\n"
		}
	    }
	}
491 492
    }
    print TCL "\n";
493 494 495 496 497 498 499 500 501 502

    print TCL "# Global Vtypes\n";
    $query_result = 
        DBQueryFatal("select vtype, weight, types ".
                     " from global_vtypes");
    while (my ($vtype, $weight, $types) = $query_result->fetchrow_array()) {
        print TCL "set ::GLOBALS::vtypes($vtype) [Vtype $vtype $weight {$types}]\n";
    }
    print TCL "\n";

503
    
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    print TCL "# Node Permissions\n";
    my %nodeperms = ();
    $query_result =
	DBQueryFatal("select type,pid from nodetypeXpid_permissions");
    while (my ($type,$pid) = $query_result->fetchrow_array()) {
	if (! defined($nodeperms{$type})) {
	    $nodeperms{$type} = [];
	}
	push(@{$nodeperms{$type}}, $pid);
    }
    foreach my $type (keys(%nodeperms)) {
	my @pids = @{$nodeperms{$type}};

	print TCL "set nodetypeXpid_permissions($type) [list @pids]\n";
    }
    print TCL "\n";

Timothy Stack's avatar
 
Timothy Stack committed
521 522
    print TCL "# Robot areas\n";
    $query_result =
Timothy Stack's avatar
 
Timothy Stack committed
523
	DBQueryFatal("select distinct building from node_startloc");
Timothy Stack's avatar
 
Timothy Stack committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    while (my ($building,$floor) = $query_result->fetchrow_array()) {
	print TCL "set areas($building) 1\n";
    }
    print TCL "\n";

    print TCL "# Obstacles\n";
    $query_result = DBQueryFatal(
	"select o.obstacle_id,o.building,o.x1,o.x2,o.y1,o.y2,o.description, ".
	" fi.pixels_per_meter ".
	"from obstacles as o ".
	"left join floorimages as fi on fi.building=o.building");
    while (my ($id,$building,$x1,$x2,$y1,$y2,$description,$ppm) = 
	   $query_result->fetchrow_array()) {
	print TCL "set obstacles($id,$building,x1) " . $x1 / $ppm . "\n";
	print TCL "set obstacles($id,$building,x2) " . $x2 / $ppm . "\n";
	print TCL "set obstacles($id,$building,y1) " . $y1 / $ppm . "\n";
	print TCL "set obstacles($id,$building,y2) " . $y2 / $ppm . "\n";
	print TCL "set obstacles($id,$building,description) {$description}\n";
    }
    print TCL "\n";

Timothy Stack's avatar
 
Timothy Stack committed
545 546 547 548 549 550 551 552 553 554 555 556
    print TCL "# Cameras\n";
    $query_result = DBQueryFatal(
	"select name,building,loc_x,loc_y,width,height from cameras");
    while (my ($name,$building,$loc_x,$loc_y,$width,$height) = 
	   $query_result->fetchrow_array()) {
	print TCL "set cameras($name,$building,x) $loc_x\n";
	print TCL "set cameras($name,$building,y) $loc_y\n";
	print TCL "set cameras($name,$building,width) $width\n";
	print TCL "set cameras($name,$building,height) $height\n";
    }
    print TCL "\n";

557 558 559 560 561 562 563 564 565 566
    if (defined($pid)) {
	print TCL "# OSIDs\n";
	$query_result =
	    DBQueryFatal("select osname from os_info ".
			 "where shared=1 or pid='$pid'");
		     
	while (my ($osname) = $query_result->fetchrow_array()) {
	    print TCL "set osids($osname) 1\n";
	}
	print TCL "\n";
567 568
    }

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    if (defined($pid)) {
	# Load reserved nodes, for swapmodify.
	$query_result = 
	    DBQueryFatal("select r.vname,r.node_id,n.type from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "where r.pid='$pid' and r.eid='$eid'");

	print TCL "# Reserved Nodes\n";
	while (($vname,$reserved,$type) = $query_result->fetchrow_array()) {
	    print TCL "lappend reserved_list \"$vname\"\n";
	    print TCL "set reserved_type($vname) \"$type\"\n";
	    print TCL "set reserved_node($vname) \"$reserved\"\n";
	}
    }

584 585 586 587 588 589 590 591 592 593 594
    print TCL "# Physical Node Names\n";
    $query_result =
	DBQueryFatal("select n.node_id,nt.type from nodes as n ".
		     "left join node_types as nt on n.type=nt.type ".
		     "where n.node_id=n.phys_nodeid and nt.isremotenode=0 ".
		     "and n.role='testnode' and nt.type!='dnard'");

    while (($node_id,$type) = $query_result->fetchrow_array()) {
	print TCL "set physnodes($node_id) \"$type\"\n";
    }

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    print TCL "# Location info\n";
    $query_result =
	DBQueryFatal("select li.node_id,li.building,li.loc_x,li.loc_y,".
		     "li.loc_z,fi.pixels_per_meter ".
		     "from location_info as li ".
		     "left join floorimages as fi on fi.building=li.building");

    while (my ($node_id,$building,$loc_x,$loc_y,$loc_z,$ppm) =
	   $query_result->fetchrow_array()) {
	print TCL "set location_info($node_id,$building,x) ". 
	    $loc_x / $ppm ."\n";
	print TCL "set location_info($node_id,$building,y) ". 
	    $loc_y / $ppm ."\n";
	if (!defined($loc_z)) {
	    $loc_z = 0.0;
	}
	print TCL "set location_info($node_id,$building,z) ". 
	    $loc_z / $ppm ."\n";
    }

615 616 617 618 619 620 621 622 623
    #
    # ElabInElab stuff.
    # 
    my $maxnodes   = 0;
    if (defined($pid)) {
	my $elabinelab = 0;
	my $elabinelab_eid;

	TBExptIsElabInElab($pid, $eid, \$elabinelab, \$elabinelab_eid)
Kevin Atkinson's avatar
 
Kevin Atkinson committed
624
	    or tbdie("Failed to get ElabInElab attributes!");
625 626 627

	if ($elabinelab && defined($elabinelab_eid)) {
	    if (! TBExptMinMaxNodes($pid, $elabinelab_eid, undef, \$maxnodes)) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
628
		tbdie("Could not get max nodes from DB!");
629 630 631 632
	    }
	}
    }
    # Be sure to initialize this to something ...
633 634 635 636 637
    print TCL "set elabinelab_maxpcs $maxnodes\n\n";

    #
    # For Templates.
    #
638 639 640 641 642 643 644
    print TCL "# Template goo\n";
    # Does not matter what it is, as long as it is set.
    print TCL "set ::DATASTORE \"/proj\"\n";
    
    if (defined($experiment)) {
	my $instance =
	    Template::Instance->LookupByID($experiment->instance_idx());
645
	
646 647 648
	if (defined($instance)) {
	    my $datastore = $instance->path() . "/template_datastore";
	    my %parameters;
649

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
	    print TCL "set ::DATASTORE \"$datastore\"\n";

	    $instance->BindingList(\%parameters) == 0
		or tbdie("Could not get binding list for $instance");
		
	    foreach my $name (keys(%parameters)) {
		my $value = $parameters{$name};

		print TCL "set parameter_list_defaults($name) \"$value\"\n";
	    }
	    
	    $instance->RunBindingList(\%parameters) == 0
		or tbdie("Could not get run binding list for $instance");
		
	    foreach my $name (keys(%parameters)) {
		my $value = $parameters{$name};
666

667 668
		print TCL "set parameter_list_defaults($name) \"$value\"\n";
	    }
669 670
	}
    }
671
    print TCL "\n\n";
672

673 674
    print TCL "}\n";
    close(TCL);
675
}
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

sub parse_error($) {
    my ($mesg) = @_;

    return if parse_invalid_os_error($mesg);
    return if parse_invalid_variable_error($mesg);

    return;
}

sub parse_invalid_os_error($) {
    my ($mesg) = @_;
    my ($type, $osname);

    if ($mesg =~ /^\[tb-set-(\w+)-os\] Invalid osid (\S+)\.$/) {
	($type, $osname) = ($1, $2);
    } else {
	return false;
    }

    tbreport(SEV_ADDITIONAL, {script => 'parse.tcl'}, 'invalid_os',
	     $type, $osname, undef);

    return true;
}

sub parse_invalid_variable_error($) {
    my ($mesg) = @_;
    my ($type, $var);

    if ($mesg =~ /^\[tb-set-hardware\] Invalid hardware type (\S+)\.$/) {
	$var  = $1;
	$type = 'hardware_type';
    } elsif ($mesg =~ /^\[run\] Invalid lan\/link name (\S+)\./) {
	$var  = $1;
	$type = 'lan_link_name';
    } elsif ($mesg =~ /^\[run\] Invalid node name (\S+)\./) {
	$var  = $1;
	$type = 'node_name';
    } else {
	return false;
    }

    tbreport(SEV_ADDITIONAL, {script => 'parse.tcl'}, 'invalid_variable',
	     $type, $var);

    return true;
}