template_exprun.php 16.5 KB
Newer Older
1
2
3
4
5
6
7
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
8
include_once("template_defs.php");
9
10
11
12
13
14
15
16
17
18
19
20

#
# No PAGEHEADER since we spit out a Location header later. See below.
#

#
# Only known and logged in users.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);

21
22
23
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

24
25
26
27
# Used below
unset($parameter_xmlfile);
$deletexmlfile = 0;

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#
# Run the script backend
#
function DOIT($instance, $action, $command_options)
{
    global $guid, $version, $pid, $gid, $eid, $uid;
    global $deletexmlfile, $parameter_xmlfile;
    $message = "";

    $command_options = "-e $eid " . $command_options;
    
    if ($action == "start") {
	PAGEHEADER("Start new Experiment Run");
	$message = "Starting new experiment run";
	$command_options = "-a start " . $command_options;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
45
46
47
48
    elseif ($action == "abort") {
	PAGEHEADER("Abort Experiment Run");
	$message = "Aborting experiment run";
	$command_options = "-a abort " . $command_options;
    }
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
    else {
	PAGEHEADER("Stop current Experiment Run");
	$message = "Stopping current experiment run";
	$command_options = "-a stop " . $command_options;
    }	

    #
    # Grab the unix GID for running scripts.
    #
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

    #
    # Avoid SIGPROF in child.
    #
    set_time_limit(0);

    echo "<font size=+2>Template <b>" .
            MakeLink("template",
		     "guid=$guid&version=$version", "$guid/$version") .
            "</b>, Instance <b>" .
            MakeLink("project", "pid=$pid", $pid) . "/" .
            MakeLink("experiment", "pid=$pid&eid=$eid", $eid);
    echo "</b></font>\n";
    echo "<br><br>\n";

    echo "<script type='text/javascript' language='javascript' ".
	 "        src='template_sup.js'>\n";
    echo "</script>\n";

    STARTBUSY($message);

    #
    # Run the backend script.
    #
    $retval = SUEXEC($uid, "$pid,$unix_gid",
		     "webtemplate_exprun $command_options $guid/$version",
		     SUEXEC_ACTION_IGNORE);

    CLEARBUSY();

    if ($deletexmlfile) {
	unlink($parameter_xmlfile);
    }

    #
    # Fatal Error. Report to the user, even though there is not much he can
    # do with the error. Also reports to tbops.
    # 
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
    }

    # User error. Tell user and exit.
    if ($retval) {
	SUEXECERROR(SUEXEC_ACTION_USERERROR);
	return;
    }

    STARTLOG($pid, $eid);
}

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
#
# Run the script backend
#
function DOTIME($instance, $action)
{
    global $guid, $version, $pid, $gid, $eid, $uid;
    $message = "";

    if ($action == "pause") {
	PAGEHEADER("Pause Experiment Time");
	$message = "Pausing experiment runtime";
    }
    else {
	PAGEHEADER("Continue Experiment Time");
	$message = "Continuing experiment runtime";
    }	

    $command_options = "-e $eid -a $action ";
    
    #
    # Grab the unix GID for running scripts.
    #
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

    #
    # Avoid SIGPROF in child.
    #
    set_time_limit(0);

    echo "<font size=+2>Template <b>" .
            MakeLink("template",
		     "guid=$guid&version=$version", "$guid/$version") .
            "</b>, Instance <b>" .
            MakeLink("project", "pid=$pid", $pid) . "/" .
            MakeLink("experiment", "pid=$pid&eid=$eid", $eid);
    echo "</b></font>\n";
    echo "<br><br>\n";

    echo "<script type='text/javascript' language='javascript' ".
	 "        src='template_sup.js'>\n";
    echo "</script>\n";

    STARTBUSY($message);

    #
    # Run the backend script.
    #
    $retval = SUEXEC($uid, "$pid,$unix_gid",
		     "webtemplate_exprun $command_options $guid/$version",
		     SUEXEC_ACTION_IGNORE);

    CLEARBUSY();

    #
    # Fatal Error. Report to the user, even though there is not much he can
    # do with the error. Also reports to tbops.
    # 
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
    }

    # User error. Tell user and exit.
    if ($retval) {
	SUEXECERROR(SUEXEC_ACTION_USERERROR);
	return;
    }

    PAGEREPLACE("showexp.php3?pid=$pid&eid=$eid");
}

180
181
182
#
# Spit the form out using the array of data.
#
183
function SPITFORM($instance, $formfields, $parameters, $errors)
184
185
{
    global $TBDB_EIDLEN;
186
    global $guid, $version, $pid, $eid;
187
    global $TBVALIDDIRS_HTML;
188

189
190
191
192
193
194
195
196
197
198
    PAGEHEADER("Start new Experiment Run");

    echo "<font size=+2>Template <b>" .
            MakeLink("template",
		     "guid=$guid&version=$version", "$guid/$version") .
            "</b>, Instance <b>" .
            MakeLink("project", "pid=$pid", $pid) . "/" .
            MakeLink("experiment", "pid=$pid&eid=$eid", $eid);
    echo "</b></font>\n";
    echo "<br><br>\n";
199
200

    echo "<center>\n";
201
202
    $template = $instance->template();
    $template->Show();
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
    echo "</center>\n";
    echo "<br><br>\n";

    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

228
229
230
231
232
233
    #
    # Get the default params for the template, instance and the previous run.
    # These go out for the user to select via a button, which will provide an
    # initial setting for the parameters.
    #
    $template->FormalParameters($formal_parameters);
234
    $template->FormalParameterMouseOvers($mouseovers);
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
    $instance->Bindings($instance_parameters);
    $instance->RunBindings($instance->LastRunIdx(), $lastrun_parameters);

    #
    # Spit out some inline script. Its not as if there will be hundreds
    # of parameters, right?
    #
    echo "<script type='text/javascript' language='javascript' ".
	 "        src='template_sup.js'>\n";
    echo "</script>\n";
    echo "<script language=JavaScript>\n";
    echo "var formal_names  = new Array();\n";
    echo "var template_values = new Array();\n";
    echo "var instance_values = new Array();\n";
    echo "var lastrun_values  = new Array();\n";
    $i = 0;
    while (list ($name, $value) = each ($formal_parameters)) {
	echo "formal_names[$i] = '$name';\n";
	echo "template_values[$i] = '$value';\n";
	$i++;
    }
    $i = 0;
    while (list ($name, $value) = each ($instance_parameters)) {
	echo "instance_values[$i] = '$value';\n";
	$i++;
    }
    $i = 0;
    while (list ($name, $value) = each ($lastrun_parameters)) {
	echo "lastrun_values[$i] = '$value';\n";
	$i++;
    }
    echo "</script>\n";
    

269
    echo "<form action=template_exprun.php".
270
	    "?action=start&guid=$guid&version=$version&eid=$eid ".
271
272
	 "      method=post>\n";
    echo "<table align=center border=1>\n";
273
    
274
275
276
277
278
279
280
281
282
283
284
285
    #
    # RunID:
    #
    echo "<tr>
              <td class='pad4'>ID:
              <br><font size='-1'>(alphanumeric, no blanks)</font></td>
              <td class='pad4' class=left>
                  <input type=text
                         name=\"formfields[runid]\"
                         value=\"" . $formfields[runid] . "\"
	                 size=$TBDB_EIDLEN
                         maxlength=$TBDB_EIDLEN>
286
              &nbsp (optional; we will make up one for you)
287
288
289
              </td>
          </tr>\n";

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    #
    # Clean logs before starting run?
    #
    echo "<tr>
	      <td class='pad4'>Clean Logs:</td>
              <td class='pad4' class=left>
  	          <input type=checkbox name='formfields[clean]' value='Yep'";

    if (isset($formfields[clean]) &&
	strcmp($formfields[clean], "Yep") == 0) {
	echo " checked='1'";
    }
    echo ">";
    echo "&nbsp (run '<tt>loghole clean</tt>' before starting run)
	    </td>
	  </tr>\n";

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
    #
    # Swapmod?
    #
    echo "<tr>
	      <td class='pad4'>Reparse NS file?:</td>
              <td class='pad4' class=left>
  	          <input type=checkbox name='formfields[swapmod]' value='Yep'";

    if (isset($formfields[swapmod]) &&
	strcmp($formfields[swapmod], "Yep") == 0) {
	echo " checked='1'";
    }
    echo ">";
    echo "&nbsp (effectively a '<tt>swap modify</tt>')
	    </td>
	  </tr>\n";

324
325
    echo "<tr>
              <td colspan=2>
326
               Use this text area for an (optional) description:
327
328
329
330
331
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
332
                    rows=4 cols=80>" .
333
334
335
336
337
338
339
340
341
342
	            ereg_replace("\r", "", $formfields[description]) .
	           "</textarea>
              </td>
          </tr>\n";

    if (count($parameters)) {
        #
	# Table of inputs.
	#
	echo "<tr>
343
		  <td class='pad4'>Formal Parameters:</td>
344
345
		  <td>
 		    <table cellpadding=0 cellspacing=0 border=0>\n";
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

	echo "<tr><td>Choose Values:</td><td>";
	echo "<table cellpadding=0 cellspacing=0 border=0>\n";
	echo "<tr><td>\n";
	echo " <button name=formals type=button value=Formals ";
	echo "  onclick=\"SetRunParams(formal_names, template_values);\">";
	echo "Template</button>\n";
	echo "</td><td>\n";
	echo " <button name=instance type=button value=Instance ";
	echo "  onclick=\"SetRunParams(formal_names, instance_values);\">";
	echo "Instance</button>\n";
	echo "</td><td>\n";
	echo " <button name=lastrun type=button value='Previous Run' ";
	echo "  onclick=\"SetRunParams(formal_names, lastrun_values);\">";
	echo "Previous Run</button>\n";
	echo "</tr></table>\n";
362
363
364
365
	
	while (list ($name, $value) = each ($parameters)) {
	    if (!isset($value))
		$value = "&nbsp";
366
	    $mouseover = (isset($mouseovers[$name]) ? $mouseovers[$name] : "");
367
368

	    echo "<tr>
369
                    <td class='pad4' $mouseover>$name</td>
370
371
                    <td class='pad4' class=left>
                        <input type=text
372
                               id='parameter_$name'
373
                               name=\"parameters[$name]\"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
374
                               value=\"" . $value . "\"
375
376
377
378
379
380
381
382
383
	                       size=60
                               maxlength=1024>
                    </td>
                  </tr>\n";
	}
	echo "<tr><td>&nbsp;&nbsp;<b>or</b> XML file:
               </td><td></td></tr>\n";
	echo "<tr>
                  <td class='pad4'>On Server<br>
384
                           <font size='-1'>($TBVALIDDIRS_HTML)</font></td>
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
                  <td class='pad4'>
	              <input type=text
                             name=\"formfields[parameter_xmlfile]\"
                             value=\"" . $formfields[parameter_xmlfile] . "\"
	                     size=60>\n";
	echo "</td></tr>\n";
	echo "</table></td></tr>";
    }

    echo "<tr>
              <td class='pad4' align=center colspan=2>
                <b><input type=submit name=exprun value='Start'></b>
              </td>
         </tr>
        </form>
        </table>\n";
}

#
# Verify page arguments.
# 
if (!isset($guid) ||
    strcmp($guid, "") == 0) {
    USERERROR("You must provide a template GUID.", 1);
}
if (!isset($version) ||
    strcmp($version, "") == 0) {
    USERERROR("You must provide a template version number", 1);
}
414
415
if (!isset($eid) ||
    strcmp($eid, "") == 0) {
416
417
418
419
420
421
422
423
    USERERROR("You must provide a template instance ID", 1);
}
if (!TBvalid_guid($guid)) {
    PAGEARGERROR("Invalid characters in GUID!");
}
if (!TBvalid_integer($version)) {
    PAGEARGERROR("Invalid characters in version!");
}
424
if (!TBvalid_eid($eid)) {
425
426
427
428
    PAGEARGERROR("Invalid characters in instance ID!");
}

#
429
# Check to make sure this is a valid template and user has permission.
430
#
431
432
$template = Template::Lookup($guid, $version);
if (!$template) {
433
434
435
    USERERROR("The experiment template $guid/$version is not a valid ".
              "experiment template!", 1);
}
436
437
438
if (! $template->AccessCheck($uid, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to modify experiment template ".
	      "$guid/$version!", 1);
439
}
440
441
442
443
444

# Need these below.
$pid = $template->pid();
$gid = $template->gid();

445
if (($exptidx = TBExptIndex($pid, $eid)) < 0) {
446
    TBERROR("No such experiment '$eid' for template $guid/$version/$eid", 1);
447
448
}

449
450
451
#
# Check to make sure and a valid instance that is actually swapped in.
#
452
453
454
455
$instance = TemplateInstance::LookupByExptidx($exptidx);
if (!$instance) {
    TBERROR("Template Instance $guid/$version/$exptidx is not ".
	    "a valid experiment template instance!", 1);
456
457
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
458
if (isset($action) && ($action == "stop" || $action == "abort")) {
459
460
461
462
463
    # Run the backend script.
    DOIT($instance, $action, "");
    PAGEFOOTER();
    return;
}
464
465
466
467
468
469
elseif (isset($action) && ($action == "pause" || $action == "continue")) {
    # Run the backend script.
    DOTIME($instance, $action, "");
    PAGEFOOTER();
    return;
}
470
elseif (!isset($exprun)) {
471
    #
472
    # On first load, display virgin form and exit.
473
    #
474
    $defaults['runid'] = $instance->NextRunID();
475
476

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
477
    # Get the current bindings of the current run.
478
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
479
    $instance->RunBindings($instance->LastRunIdx(), $bindings);
480
481
482
483
484
485
486
487
488
489
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

490
    SPITFORM($instance, $defaults, $bindings, 0);
491
492
493
494
495
496
497
498
499
500
501
    PAGEFOOTER();
    return;
}
elseif (! isset($formfields)) {
    PAGEARGERROR();
}

#
# Okay, validate form arguments.
#
$errors = array();
502
503

# Set up command options
504
$command_options = " ";
505
506
507
508
509
510
511
512
513
514

#
# RunID:
#
if (!isset($formfields[runid]) || $formfields[runid] == "") {
    $errors["ID"] = "Missing Field";
}
elseif (!TBvalid_eid($formfields[runid])) {
    $errors["ID"] = TBFieldErrorString();
}
515
elseif (TBValidExperiment($pid, $formfields[runid])) {
516
517
518
    $errors["ID"] = "Already in use";
}
else {
519
    $command_options .= " -r " . escapeshellarg($formfields[runid]);
520
521
}

522
523
524
525
526
527
528
#
# Clean?
#
if (isset($formfields[clean]) && $formfields[clean] == "Yep") {
    $command_options .= " -c";
}

529
530
531
532
533
534
535
#
# Swapmod?
#
if (isset($formfields[swapmod]) && $formfields[swapmod] == "Yep") {
    $command_options .= " -m";
}

536
537
538
#
# Description:
# 
539
540
541
542
543
544
545
if (isset($formfields[description]) && $formfields[description] != "") {
    if (!TBvalid_template_description($formfields[description])) {
	$errors["Description"] = TBFieldErrorString();
    }
    else {
	$command_options .= " -E " . escapeshellarg($formfields[description]);
    }
546
547
548
549
550
}

#
# Parameters. The XML file overrides stuff in the form. 
#
551
$template->FormalParameters($parameter_masterlist);
552
553
554
555
556
557
558
559
560
561
if (count($parameter_masterlist)) {
    if (isset($formfields[parameter_xmlfile]) &&
	$formfields[parameter_xmlfile] != "") {

	$parameter_xmlfile = $formfields[parameter_xmlfile];
	    
	if (!preg_match("/^([-\@\w\.\/]+)$/", $parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Pathname includes illegal characters";
	}
562
563
564
	elseif (! VALIDUSERPATH($parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Must reside in one of: $TBVALIDDIRS";
565
566
567
568
569
	}
    	$deletexmlfile = 0;
    }
    else {
	#
570
	# Lets confirm that the user did not forget to set at least one value. 
571
	#
572
	$gotone = 0;
573
	while (list ($name, $default_value) = each ($parameter_masterlist)) {
574
575
	    if (isset($parameters[$name]) && $parameters[$name] != "") {
		$gotone = 1;
576
	    }
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
	}
	if (! $gotone) {
	    $errors["Parameters"] = "You did not set any values";
	}
	else {
	    #
	    # Generate a temporary file and write in the XML goo.
	    #
	    list($usec, $sec) = explode(' ', microtime());
	    srand((float) $sec + ((float) $usec * 100000));
	    $foo = rand();

	    $parameter_xmlfile = "/tmp/$uid-$foo.xml";
	    $deletexmlfile = 1;
	    
	    if (! ($fp = fopen($parameter_xmlfile, "w"))) {
		TBERROR("Could not create temp file $parameter_xmlfile", 1);
594
	    }
595
596
597
	
	    fwrite($fp, "<template_parameters>\n");

598
	    reset($parameter_masterlist);
599
600
601
602
603
604
605
	    while (list($name,$default_value) = each ($parameter_masterlist)) {
		if (isset($parameters[$name])) {
		    $value = $parameters[$name];
		}
		else {
		    $value = $default_value;
		}
606
	    
607
608
609
610
611
612
		fwrite($fp, " <parameter name=\"$name\">");
		fwrite($fp, "<value>$value</value></parameter>\n");
	    }
	    fwrite($fp, "</template_parameters>\n");
	    fclose($fp);
	    chmod($parameter_xmlfile, 0666);
613
614
	}
    }
615
    $command_options .= " -x $parameter_xmlfile";
616
617
618
}

if (count($errors)) {
619
    SPITFORM($instance, $formfields, $parameters, $errors);
620
621
622
623
624
    PAGEFOOTER();
    exit(1);
}

# Run the backend script.
625
DOIT($instance, $action, $command_options);
626

627
628
629
630
631
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>