libTemplates.pm.in 19.5 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
#
# Delete a template (all tables).
#
sub DeleteTemplateRecord($$)
{
    my ($guid, $vers) = @_;

178
179
180
    DeleteTemplateMetadata($guid, $vers) == 0
	or return -1;

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

    foreach my $table (@tables) {
186
187
188
189
190
191
192
193
        if ($table eq "experiment_templates") {
            DBQueryFatal("delete from $table ".
                         "where guid='$guid' and vers='$vers'");
        }
        else {
            DBQueryFatal("delete from $table ".
                         "where parent_guid='$guid' and parent_vers='$vers'");
        }
194
195
196
197
198
199
    }
}

#
# Create a new template instance record from the args provided. 
#
200
# Usage: int NewTemplateInstanceRecord(\%args, \$idx)
201
#
202
sub NewTemplateInstanceRecord($$)
203
{
204
    my ($args, $pidx) = @_;
205
206
207
208

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

209
    my $query_result = DBQueryWarn($query);
210
    return -1
211
212
213
214
	if (! $query_result);

    # Grab the insert record.
    $$pidx = $query_result->insertid;
215
216
217
218
219
220
221
222
    return 0;
}

#
# Delete a template instance record.
#
sub DeleteTemplateInstanceRecord($$$)
{
223
    my ($guid, $vers, $idx) = @_;
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
    #
    # Delete the run records first since they will not mean much after
    # this record is gone.
    #
    my $query_result =
	DBQueryWarn("select exptidx from experiment_template_instances ".
		    "where parent_guid='$guid' and parent_vers='$vers' and ".
		    "      idx='$idx'");

    return -1
	if (!$query_result);

    while (my ($exptidx) = $query_result->fetchrow_array()) {
	DBQueryWarn("delete from experiment_run_bindings ".
		    "where exptidx='$exptidx'")
	    or return -1;

	DBQueryWarn("delete from experiment_runs ".
		    "where exptidx='$exptidx'")
	    or return -1;
    }

    #
    # Also delete the binding records for the instance.
    #
    DeleteTemplateInstanceBindingRecords($guid, $vers, $idx) == 0
	or return -1;

    # And finally the instance record.
    DBQueryWarn("delete from experiment_template_instances ".
		"where parent_guid='$guid' and parent_vers='$vers' and ".
		"      idx='$idx'")
	or return -1;
258
259
260
261
262
263
264
265
266
267
268

    return 0;
}

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

271
272
273
274
    #
    # Get the runidx and do it first.
    #
    my $runidx;
275
    my $exptidx;
276
    return -1
277
	if (TemplateInstanceInfo($idx, \$runidx, undef, \$exptidx) < 0);
278
279
280
281
282
283

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

284
285
286
287
288
289
290
    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 ".
291
	      "       idx='$idx'";
292
293
294
295
296
297

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

298
299
300
#
# Update an instance record.
#
301
sub UpdateTemplateInstanceRecord($$$$$)
302
{
303
304
305
    my ($guid, $vers, $idx, $start_time, $args) = @_;

    my $query = "update experiment_template_instances set ";
306

307
308
    $query .= "start_time=now() "
	if ($start_time);
309
310

    if (defined($args) && scalar(keys%{$args})) {
311
312
313
	$query .= ","
	    if ($start_time);
	
314
315
316
	$query .= join(",", map("$_='" . $args->{$_} . "'", keys(%{$args})));
    }
    $query .= " where parent_guid='$guid' and parent_vers='$vers' and ".
317
	      "       idx='$idx'";
318
319
320
321
322
323

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

324
325
326
#
# Get info about a template run.
#
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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($$;$$)
349
{
350
    my ($exptidx, $prunidx, $peid, $pidx) = @_;
351
352

    my $query_result =
353
354
	DBQueryWarn("select eid,runidx,idx ".
		    "  from experiment_template_instances ".
355
356
357
358
359
		    "where exptidx='$exptidx'");

    return -1
	if (! $query_result);

360
    my ($eid,$runidx,$idx) = $query_result->fetchrow_array();
361
    $$prunidx = $runidx;
362
363
    $$peid    = $eid
	if (defined($peid));
364
365
    $$pidx    = $idx
	if (defined($pidx));
366
367
368
    return 0;
}

369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#
# 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($$$)
{
391
    my ($guid, $vers, $idx) = @_;
392

393
394
395
396
    DBQueryWarn("delete from experiment_template_instance_bindings ".
		"where parent_guid='$guid' and parent_vers='$vers' and ".
		"      instance_idx='$idx'")
	or return -1;
397
398
399
400

    return 0;
}

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
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
#
# 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;
}

490
491
492
#
# Utility routine to get the pid,tid,gid of a template.
#
493
sub TemplateInfo($$;$$$$)
494
{
495
    my ($guid, $version, $ppid, $ptid, $pgid, $peid) = @_;
496
497

    my $query_result =
498
	DBQueryWarn("select pid,tid,gid,eid from experiment_templates ".
499
500
501
502
503
		    "where guid='$guid' and vers='$version'");

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

504
505
506
507
508
    my ($pid, $tid, $gid, $eid) = $query_result->fetchrow_array();
    $$ppid = $pid
	if (defined($ppid));
    $$ptid = $tid
	if (defined($ptid));
509
510
    $$pgid = $gid
	if (defined($pgid));
511
512
    $$peid = $eid
	if (defined($peid));
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
    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) = @_;
547
    my $input_data_idx;
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
    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);

574
575
576
577
	DBQueryWarn("lock tables experiment_template_inputs write, ".
		    "            experiment_template_input_data write")
	    or return -1;

578
579
580
581
	my $query_result =
	    DBQueryWarn("select idx from experiment_template_input_data ".
			"where md5='$md5'");

582
583
584
585
	if (!$query_result) {
	    DBQueryWarn("unlock tables");
	    return -1;
	}
586
587

	if ($query_result->numrows) {
588
	    ($input_data_idx) = $query_result->fetchrow_array();
589
590
591
592
593
594
595
	}
	else {
	    $query_result =
		DBQueryWarn("insert into experiment_template_input_data ".
			    "(idx, md5, input) ".
			    "values (NULL, '$md5', $data_string)");
	    
596
597
598
599
600
	    if (!$query_result) {
		DBQueryWarn("unlock tables");
		return -1;
	    }
	    $input_data_idx = $query_result->insertid;
601
602
	}

603
604
605
606
607
608
609
	$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");
610
	return -1
611
	    if (!$query_result);
612
613
614
615
    }
    return 0;
}

616
617
618
619
620
621
622
623
624
#
# 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
625
		"            experiment_template_inputs write, ".
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
654
655
656
657
658
659
660
661
662
		"            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;
}

663
664
665
#
# Add a metadata record. This is used for new templates.
#
666
sub NewTemplateMetadata($$$$$)
667
{
668
    my ($template_guid, $template_version, $name, $value, $creator) = @_;
669
670
671
672
673
674
675
676
677
678
679
    my $guid;
    my $version = 1;

    return -1
	if (libTemplates::NewGUID(\$guid) < 0);

    $name  = DBQuoteSpecial($name);
    $value = DBQuoteSpecial($value);

    my $query_result =
	DBQueryWarn("insert into experiment_template_metadata_items set ".
680
		    "   guid='$guid', vers='$version', uid='$creator', ".
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
		    "   template_guid='$template_guid', ".
		    "   name=$name, value=$value, created=now()");
    return -1
	if (!$query_result);

    DBQueryWarn("insert into experiment_template_metadata set ".
		"   parent_guid='$template_guid', ".
		"   parent_vers='$template_version', ".
		"   metadata_guid='$guid', ".
		"   metadata_vers='$version', ".
		"   internal=1")
	or return -1;
    
    return 0;
}

#
# Delete all template metadata; thats all we need here at the moment
#
sub DeleteTemplateMetadata($$)
{
    my ($template_guid, $template_version) = @_;

    my $query_result =
	DBQueryWarn("select metadata_guid ".
		    "    from experiment_template_metadata ".
		    "where parent_guid='$template_guid' and ".
		    "      parent_vers='$template_version'");

    return -1
	if (!$query_result);

    while (my ($metadata_guid) = $query_result->fetchrow_array()) {
	my @versions = ();

	#
	# Delete all versions for each record. This is wrong if we ever
	# want to share entries between templates.
	#
	my $metadata_result =
	    DBQueryWarn("select vers from experiment_template_metadata_items ".
			"where guid='$metadata_guid'");

	return -1
	    if (!$metadata_result);

	next
	    if (!$metadata_result->numrows);

	while (my ($metadata_vers) = $metadata_result->fetchrow_array()) {
	    push(@versions, $metadata_vers);
	}

	my $clause = join(" or ", map("vers='$_'", @versions));

	DBQueryWarn("delete from experiment_template_metadata_items ".
		    "where guid='$metadata_guid' and ($clause)")
	    or return -1;
    }
    DBQueryWarn("delete from experiment_template_metadata ".
		"where parent_guid='$template_guid' and ".
		"      parent_vers='$template_version'")
	or return -1;

    return 0;
}

#
# Copy exiting template metadata to a child. This is likely to change
# since we probably want to share at some point.
#
752
sub CopyTemplateMetadata($$$$)
753
{
754
    my ($from_guid, $from_version, $to_version, $copier) = @_;
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787

    #
    # Copy the toplevel items.
    #
    my $query_result =
	DBQueryWarn("select name,value ".
		    "  from experiment_template_metadata as m ".
		    "left join experiment_template_metadata_items as i on ".
		    "     i.guid=m.metadata_guid and ".
		    "     i.vers=m.metadata_vers ".
		    "where m.parent_guid='$from_guid' and ".
		    "      m.parent_vers='$from_version' and m.internal=0")
	or return -1;

    while (my ($name, $value) = $query_result->fetchrow_array()) {
	my $guid;
	my $version = 1;
	
	$name   = DBQuoteSpecial($name);
	$value  = DBQuoteSpecial($value);

	return -1
	    if (libTemplates::NewGUID(\$guid) < 0);

	DBQueryWarn("insert into experiment_template_metadata set ".
		    "   parent_guid='$from_guid', ".
		    "   parent_vers='$to_version', ".
		    "   metadata_guid='$guid', ".
		    "   metadata_vers='$version', ".
		    "   internal=0")
	    or return -1;

	DBQueryWarn("insert into experiment_template_metadata_items set ".
788
		    "   guid='$guid', vers='$version', uid='$copier', ".
789
790
791
792
793
794
795
796
		    "   template_guid='$from_guid', ".
		    "   name=$name, value=$value, created=now()")
	    or return -1;
    }

    return 0;
}

797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
#
# 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;
}