Template.pm.in 14.8 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
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005, 2006 University of Utah and the Flux Group.
# All rights reserved.
#
# XXX Need to deal with locking at some point ...
#
package libTemplates;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use lib '@prefix@/lib';
use libdb;
use libtestbed;
use libtblog;
use English;

# Configure variables
my $TB		= "@prefix@";
my $MD5         = "/sbin/md5";

#
# Grab a new GUID for a template. We do not have to use it of course.
#
sub NewGUID($)
{
    my ($pidx) = @_;
    my $idx;
    
    DBQueryFatal("lock tables emulab_indicies write");

    my $query_result = 
	DBQueryFatal("select idx from emulab_indicies ".
		     "where name='next_guid'");
	
    if (! $query_result->num_rows) {
	$idx = 10000;
	
	DBQueryFatal("insert into emulab_indicies (name, idx) ".
		     "values ('next_guid', $idx)");
    }
    else {
	($idx) = $query_result->fetchrow_array();
    }
    my $nextidx = $idx + 1;
    
    DBQueryFatal("update emulab_indicies set idx='$nextidx' ".
		 "where name='next_guid'");

    DBQueryFatal("unlock tables");

    $$pidx = $idx;
    return 0;
}

#
# Check for valid template.
#
sub TBValidExperimentTemplate($$)
{
    my ($guid, $version) = @_;

    my $query_result =
	DBQueryFatal("select guid from experiment_templates ".
		     "where guid='$guid' and vers='$version'");

    return $query_result->num_rows;
}

#
# Template permission checks. Using the experiment access check stuff!
#
# Usage: TBExptTemplateAccessCheck($uid, $guid, $access_type)
#	 returns 0 if not allowed.
#        returns 1 if allowed.
#
sub TBExptTemplateAccessCheck($$$)
{
    my ($uid, $guid, $access_type) = @_;
    my $mintrust;

    if ($access_type < TB_EXPT_MIN ||
	$access_type > TB_EXPT_MAX) {
	die("*** Invalid access type: $access_type!");
    }

    #
    # Admins do whatever they want!
    #
    if (TBAdmin($uid)) {
	return 1;
    }
    $uid = MapNumericUID($uid);

    my $query_result =
	DBQueryFatal("select pid,gid from experiment_templates where ".
		     "guid='$guid' limit 1");
    
    if ($query_result->numrows == 0) {
	return 0;
    }
    my @row = $query_result->fetchrow_array();
    my $pid = $row[0];
    my $gid = $row[1];

    #
    # An experiment may be destroyed by the experiment creator or the
    # project/group leader.
    #
    if ($access_type == TB_EXPT_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }

    #
    # Either proper permission in the group, or group_root in the project.
    # This lets group_roots muck with other people's experiments, including
    # those in groups they do not belong to.
    #
    return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	TBMinTrust(TBGrpTrust($uid, $pid, $pid), PROJMEMBERTRUST_GROUPROOT);
}

#
# Create a new template record from the args provided. We assume that
# the table is locked by the caller. 
#
# Usage: int NewTemplateRecord(\%args)
#
sub NewTemplateRecord($)
{
    my ($args) = @_;

    my $query = "insert into experiment_templates set ".
	join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));

    # Append some default value stuff.
    $query .= ",created=now()";

    return -1
	if (! DBQueryWarn($query));
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#
# Update a template record
#
sub UpdateTemplateRecord($$$)
{
    my ($guid, $vers, $args) = @_;

    my $query = "update experiment_templates set ".
	join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));

    $query .= " where guid='$guid' and vers='$vers'";

    return -1
	if (! DBQueryWarn($query));
    return 0;
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#
# Delete a template (all tables).
#
sub DeleteTemplateRecord($$)
{
    my ($guid, $vers) = @_;

    # Make sure the experiment_templates table is always last, in case
    # something goes wrong. 
    my @tables = ("experiment_templates");

    foreach my $table (@tables) {
	DBQueryFatal("delete from $table ".
		     "where guid='$guid' and vers='$vers'");
    }
}

#
# Create a new template instance record from the args provided. 
#
191
# Usage: int NewTemplateInstanceRecord(\%args, \$idx)
192
#
193
sub NewTemplateInstanceRecord($$)
194
{
195
    my ($args, $pidx) = @_;
196 197 198 199

    my $query = "insert into experiment_template_instances set ".
	join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));

200
    my $query_result = DBQueryWarn($query);
201
    return -1
202 203 204 205
	if (! $query_result);

    # Grab the insert record.
    $$pidx = $query_result->insertid;
206 207 208 209 210 211 212 213
    return 0;
}

#
# Delete a template instance record.
#
sub DeleteTemplateInstanceRecord($$$)
{
214
    my ($guid, $vers, $idx) = @_;
215 216 217

    DBQueryFatal("delete from experiment_template_instances ".
		 "where parent_guid='$guid' and parent_vers='$vers' and ".
218
		 "      idx='$idx'");
219 220 221 222 223 224 225 226 227 228 229

    return 0;
}

#
# Finalize a template instance record; usage is similar to above
#
# Usage: int FinalizeTemplateInstanceRecord($guid, $vers, $exptidx, \%args)
#
sub FinalizeTemplateInstanceRecord($$$;$)
{
230
    my ($guid, $vers, $idx, $args) = @_;
231

232 233 234 235
    #
    # Get the runidx and do it first.
    #
    my $runidx;
236
    my $exptidx;
237
    return -1
238
	if (TemplateInstanceInfo($idx, \$runidx, undef, \$exptidx) < 0);
239 240 241 242 243 244

    if (defined($runidx)) {
	return -1
	    if (FinalizeTemplateExerimentRunRecord($exptidx, $runidx) < 0);
    }

245 246 247 248 249 250 251
    my $query = "update experiment_template_instances set stop_time=now() ";

    if (defined($args) && scalar(keys%{$args})) {
	$query .= ",";
	$query .= join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));
    }
    $query .= " where parent_guid='$guid' and parent_vers='$vers' and ".
252
	      "       idx='$idx'";
253 254 255 256 257 258

    return -1
	if (! DBQueryWarn($query));
    return 0;
}

259 260 261
#
# Update an instance record.
#
262
sub UpdateTemplateInstanceRecord($$$$$)
263
{
264 265 266
    my ($guid, $vers, $idx, $start_time, $args) = @_;

    my $query = "update experiment_template_instances set ";
267

268 269
    $query .= "start_time=now() "
	if ($start_time);
270 271

    if (defined($args) && scalar(keys%{$args})) {
272 273 274
	$query .= ","
	    if ($start_time);
	
275 276 277
	$query .= join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));
    }
    $query .= " where parent_guid='$guid' and parent_vers='$vers' and ".
278
	      "       idx='$idx'";
279 280 281 282 283 284

    return -1
	if (! DBQueryWarn($query));
    return 0;
}

285 286 287
#
# Get info about a template run.
#
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
sub TemplateInstanceInfo($$;$$)
{
    my ($idx, $prunidx, $peid, $pidx) = @_;

    my $query_result =
	DBQueryWarn("select eid,runidx,exptidx ".
		    "  from experiment_template_instances ".
		    "where idx='$idx'");

    return -1
	if (! $query_result);

    my ($eid,$runidx,$exptidx) = $query_result->fetchrow_array();
    $$prunidx = $runidx;
    $$peid    = $eid
	if (defined($peid));
    $$pidx    = $exptidx
	if (defined($pidx));
    return 0;
}

sub TemplateInstanceInfoByExptidx($$;$$)
310
{
311
    my ($exptidx, $prunidx, $peid, $pidx) = @_;
312 313

    my $query_result =
314 315
	DBQueryWarn("select eid,runidx,idx ".
		    "  from experiment_template_instances ".
316 317 318 319 320
		    "where exptidx='$exptidx'");

    return -1
	if (! $query_result);

321
    my ($eid,$runidx,$idx) = $query_result->fetchrow_array();
322
    $$prunidx = $runidx;
323 324
    $$peid    = $eid
	if (defined($peid));
325 326
    $$pidx    = $idx
	if (defined($pidx));
327 328 329
    return 0;
}

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
#
# Create a new template instance record from the args provided. 
#
# Usage: int NewTemplateInstanceRecord(\%args)
#
sub NewTemplateInstanceBindingRecord($)
{
    my ($args) = @_;

    my $query = "insert into experiment_template_instance_bindings set ".
	join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));

    return -1
	if (! DBQueryWarn($query));
    return 0;
}

#
# Delete template instance binding records
#
sub DeleteTemplateInstanceBindingRecords($$$)
{
352
    my ($guid, $vers, $idx) = @_;
353 354 355

    DBQueryFatal("delete from experiment_template_instance_bindings ".
		 "where parent_guid='$guid' and parent_vers='$vers' and ".
356
		 "      instance_idx='$idx'");
357 358 359 360

    return 0;
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
#
# Create a new experiment run record from the args provided, and return the
# newly generated index for it.
#
# Usage: int NewTemplateExerimentRunRecord(\%args, \$idx)
#
sub NewTemplateExerimentRunRecord($$)
{
    my ($args, $pidx) = @_;

    my $query = "insert into experiment_runs set ".
	join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));

    # Append some default value stuff.
    $query .= ",start_time=now()";

    my $query_result = DBQueryWarn($query);
    return -1
	if (! $query_result);

    # Grab the insert record.
    $$pidx = $query_result->insertid;
    return 0;
}

#
# Delete experiment run record, by its index number.
#
sub DeleteTemplateExerimentRunRecord($$)
{
    my ($exptidx, $runidx) = @_;

    DBQueryFatal("delete from experiment_run_bindings ".
		 "where exptidx='$exptidx' and runidx='$runidx'");

    DBQueryFatal("delete from experiment_runs ".
		 "where exptidx='$exptidx' and idx='$runidx'");

    return 0;
}

#
# Finalize an experiment run record.
#
# Usage: int FinalizeTemplateExerimentRunRecord($exptidx, $runidx)
#
sub FinalizeTemplateExerimentRunRecord($$)
{
    my ($exptidx, $runidx) = @_;

    my $query = "update experiment_runs set stop_time=now() ";
    $query   .= "where exptidx='$exptidx' and idx='$runidx'";

    return -1
	if (! DBQueryWarn($query));
    
    return 0;
}

#
# Insert new experiment run binding records.
#
# Usage: int NewTemplateInstanceRecord(\%args)
#
sub NewExperimentRunBindingRecord($)
{
    my ($args) = @_;

    my $query = "insert into experiment_run_bindings set ".
	join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));

    return -1
	if (! DBQueryWarn($query));
    return 0;
}

#
# Delete template instance binding records
#
sub DeleteExperimentRunBindingRecords($$)
{
    my ($exptidx, $runidx) = @_;

    DBQueryFatal("delete from experiment_run_bindings ".
		 "where exptidx='$exptidx' and runidx='$runidx'");

    return 0;
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
#
# Utility routine to get the pid,tid,gid of a template.
#
sub TemplateInfo($$$$;$)
{
    my ($guid, $version, $ppid, $ptid, $pgid) = @_;

    my $query_result =
	DBQueryWarn("select pid,tid,gid from experiment_templates ".
		    "where guid='$guid' and vers='$version'");

    return -1
	if (!$query_result || !$query_result->numrows);

    my ($pid, $tid, $gid) = $query_result->fetchrow_array();
    $$ppid = $pid;
    $$ptid = $tid;
    $$pgid = $gid
	if (defined($pgid));
    return 0;
}

#
# Create the directory for a template, update the DB, and return that
# path to the caller.
#
#sub CreateTemplateDirectory($$$)
#{
#    my ($guid, $vers, $ppath) = @_;
#
#    # See mkexpdir.
#    my $template_dir = PROJROOT() . "/$pid/templates/$guid/$vers";
#
#    if (! -e $template_dir) {
#	system("/bin/mkdir -p $template_dir") == 0
#	    or return -1;
#    }
#    DBQueryWarn("update experiment_templates set path='$template_dir' ".
#		"where guid='$guid' and vers='$vers'")
#	or return -1;
#
#    $$ppath = $template_dir;
#    return 0;
#}

#
# Add an input file to the template. The point of this is to reduce
# duplication by taking an md5 of the input file, and sharing that
# record/file.
# 
sub AddTemplateInputFile($$$)
{
    my ($guid, $version, $inputfile) = @_;
503
    my $input_data_idx;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    my $data_string = `cat $inputfile`;
    return -1
	if ($?);

    # pid/tid are inserted for debugging.
    my ($pid,$tid) = ();
    if (TemplateInfo($guid, $version, \$pid, \$tid) < 0) {
	tberror("Could not find pid,tid for experiment template '$guid'!");
	return -1;
    }

    if ($data_string) {
	# As you can see, we md5 the raw data.
	$data_string = DBQuoteSpecial($data_string);
	if (length($data_string) >= DBLIMIT_NSFILESIZE()) {
	    tberror("Input file is too big (> " . DBLIMIT_NSFILESIZE() . ")!");
	    return -1;
	}

	#
	# Grab an MD5 of the file to see if we already have a copy of it.
	# Avoids needless duplication.
	#
	my $md5 = `$MD5 -q $inputfile`;
	chomp($md5);

530 531 532 533
	DBQueryWarn("lock tables experiment_template_inputs write, ".
		    "            experiment_template_input_data write")
	    or return -1;

534 535 536 537
	my $query_result =
	    DBQueryWarn("select idx from experiment_template_input_data ".
			"where md5='$md5'");

538 539 540 541
	if (!$query_result) {
	    DBQueryWarn("unlock tables");
	    return -1;
	}
542 543

	if ($query_result->numrows) {
544
	    ($input_data_idx) = $query_result->fetchrow_array();
545 546 547 548 549 550 551
	}
	else {
	    $query_result =
		DBQueryWarn("insert into experiment_template_input_data ".
			    "(idx, md5, input) ".
			    "values (NULL, '$md5', $data_string)");
	    
552 553 554 555 556
	    if (!$query_result) {
		DBQueryWarn("unlock tables");
		return -1;
	    }
	    $input_data_idx = $query_result->insertid;
557 558
	}

559 560 561 562 563 564 565
	$query_result =
	    DBQueryWarn("insert into experiment_template_inputs ".
			" (idx, parent_guid, parent_vers, ".
			"  pid, tid, input_idx) values ".
			" (NULL, '$guid', '$version', '$pid', '$tid', ".
			"  '$input_data_idx')");
	DBQueryWarn("unlock tables");
566
	return -1
567
	    if (!$query_result);
568 569 570 571
    }
    return 0;
}

572 573 574 575 576 577 578 579 580
#
# Delete an input file, say for a template create/modify that fails.
#
sub DeleteTemplateInputFiles($$)
{
    my ($guid, $version) = @_;

    DBQueryWarn("lock tables experiment_template_inputs as i write, ".
		"            experiment_template_inputs as j write, ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
581
		"            experiment_template_inputs write, ".
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
		"            experiment_template_input_data write")
	or return -1;

    #
    # The point of this query is to see if any of the input files in this
    # template are shared with some other template, and thus should not
    # be deleted from the input_data table.
    #
    my $query_result =
	DBQueryWarn("select i.idx,i.input_idx,count(j.input_idx) as count ".
		    "   from experiment_template_inputs as i ".
		    "left join experiment_template_inputs as j on ".
		    "     j.input_idx=i.input_idx ".
		    "where i.parent_guid='$guid' and ".
		    "      i.parent_vers='$version' ".
		    "group by j.input_idx having count > 1");

    if (! $query_result) {
	DBQueryWarn("unlock tables");
	return -1;
    }

    while (my ($input_idx, $data_idx) = $query_result->fetchrow_array()) {
	DBQueryWarn("delete from experiment_template_input_data ".
		    "where idx='$data_idx'");
    }
    $query_result = 
	DBQueryWarn("delete from experiment_template_inputs ".
		    "where parent_guid='$guid' and parent_vers='$version'");
    DBQueryWarn("unlock tables");
    
    return -1
	if (! $query_result);

    return 0;
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
#
# Find out if an experiment is a template instantiation; used by existing
# pages to alter what they do.
#
sub IsTemplateInstanceExperiment($)
{
    my ($exptidx) = @_;
    
    my $query_result =
	DBQueryFatal("select pid,eid from experiment_template_instances ".
		     "where exptidx='$exptidx'");

    return $query_result->numrows;
}

#
# Map an experiment to its template.
#
sub MapExptidxtoTemplate($$$)
{
    my ($exptidx, $pguid, $pvers) = @_;

    my $query_result =
	DBQueryFatal("select parent_guid,parent_vers ".
		     "  from experiment_template_instances ".
		     "where exptidx='$exptidx'");

    return -1
	if (! $query_result->numrows);

    my ($guid,$vers) = $query_result->fetchrow_array();
    $$pguid = $guid;
    $$pvers = $vers;
    return 0;
}