template_exprun.php 10.2 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 28 29 30
# Used below
unset($parameter_xmlfile);
$deletexmlfile = 0;

#
# Spit the form out using the array of data.
#
31
function SPITFORM($instance, $formfields, $parameters, $errors)
32 33
{
    global $TBDB_EIDLEN;
34
    global $guid, $version, $eid;
35 36 37 38

    PAGEHEADER("Start an Experiment Run");

    echo "<center>\n";
39 40
    $template = $instance->template();
    $template->Show();
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
    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";
    }

    echo "<form action=template_exprun.php".
67
	    "?action=start&guid=$guid&version=$version&eid=$eid ".
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	 "      method=post>\n";
    echo "<table align=center border=1>\n";

    #
    # 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>
83
              &nbsp (optional; we will make up one for you)
84 85 86 87 88
              </td>
          </tr>\n";

    echo "<tr>
              <td colspan=2>
89
               Use this text area for an (optional) description:
90 91 92 93 94
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
95
                    rows=4 cols=80>" .
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 154 155 156 157 158 159
	            ereg_replace("\r", "", $formfields[description]) .
	           "</textarea>
              </td>
          </tr>\n";

    if (count($parameters)) {
        #
	# Table of inputs.
	#
	echo "<tr>
		  <td class='pad4'>Formal Parameters:</td>
		  <td>
 		    <table cellpadding=0 cellspacing=0 border=0>\n";
	
	while (list ($name, $value) = each ($parameters)) {
	    if (!isset($value))
		$value = "&nbsp";

	    echo "<tr>
                    <td class='pad4'>$name</td>
                    <td class='pad4' class=left>
                        <input type=text
                               name=\"parameters[$name]\"
                               value=\"" . $value . "\"
	                       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>
                           <font size='-1'>(<code>/proj</code>,
                      <code>/groups</code>, <code>/users</code>)</font></td>
                  <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);
}
160 161
if (!isset($eid) ||
    strcmp($eid, "") == 0) {
162 163 164 165 166 167 168 169
    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!");
}
170
if (!TBvalid_eid($eid)) {
171 172 173 174
    PAGEARGERROR("Invalid characters in instance ID!");
}

#
175
# Check to make sure this is a valid template and user has permission.
176
#
177 178
$template = Template::Lookup($guid, $version);
if (!$template) {
179 180 181
    USERERROR("The experiment template $guid/$version is not a valid ".
              "experiment template!", 1);
}
182 183 184
if (! $template->AccessCheck($uid, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to modify experiment template ".
	      "$guid/$version!", 1);
185
}
186 187 188 189 190

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

191
if (($exptidx = TBExptIndex($pid, $eid)) < 0) {
192
    TBERROR("No such experiment '$eid' for template $guid/$version/$eid", 1);
193 194
}

195 196 197
#
# Check to make sure and a valid instance that is actually swapped in.
#
198 199 200 201
$instance = TemplateInstance::LookupByExptidx($exptidx);
if (!$instance) {
    TBERROR("Template Instance $guid/$version/$exptidx is not ".
	    "a valid experiment template instance!", 1);
202 203 204 205 206 207
}

#
# On first load, display virgin form and exit.
#
if (!isset($exprun)) {
208 209 210
    #
    # 
    #
211
    $defaults['runid'] = $instance->NextRunID();
212 213 214 215

    #
    # Get the current bindings for the template instance.
    #
216
    $instance->Bindings($bindings);
217 218 219 220 221 222 223 224 225 226
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

227
    SPITFORM($instance, $defaults, $bindings, 0);
228 229 230 231 232 233 234 235 236 237 238
    PAGEFOOTER();
    return;
}
elseif (! isset($formfields)) {
    PAGEARGERROR();
}

#
# Okay, validate form arguments.
#
$errors = array();
239 240 241

# Set up command options
$command_options = " -a start -e $eid ";
242 243 244 245 246 247 248 249 250 251

#
# RunID:
#
if (!isset($formfields[runid]) || $formfields[runid] == "") {
    $errors["ID"] = "Missing Field";
}
elseif (!TBvalid_eid($formfields[runid])) {
    $errors["ID"] = TBFieldErrorString();
}
252
elseif (TBValidExperiment($pid, $formfields[runid])) {
253 254 255
    $errors["ID"] = "Already in use";
}
else {
256
    $command_options .= " -r " . escapeshellarg($formfields[runid]);
257 258 259 260 261
}

#
# Description:
# 
262 263 264 265 266 267 268
if (isset($formfields[description]) && $formfields[description] != "") {
    if (!TBvalid_template_description($formfields[description])) {
	$errors["Description"] = TBFieldErrorString();
    }
    else {
	$command_options .= " -E " . escapeshellarg($formfields[description]);
    }
269 270 271 272 273
}

#
# Parameters. The XML file overrides stuff in the form. 
#
274
$template->FormalParameters($parameter_masterlist);
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328
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";
	}
	elseif (! ereg("^$TBPROJ_DIR/.*",  $parameter_xmlfile) &&
		! ereg("^$TBUSER_DIR/.*",  $parameter_xmlfile) &&
		! ereg("^$TBGROUP_DIR/.*", $parameter_xmlfile)) {
	    $errors["Parameter XML File"] = "Must reside in either ".
		"$TBUSER_DIR/, $TBPROJ_DIR/, or $TBGROUP_DIR/";
	}
    	$deletexmlfile = 0;
    }
    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 temporary file $parameter_xmlfile", 1);
	}
	
	fwrite($fp, "<template_parameters>\n");

	while (list ($name, $default_value) = each ($parameter_masterlist)) {
	    if (isset($parameters[$name])) {
		$value = $parameters[$name];
	    }
	    else {
		$value = $default_value;
	    }
	    
	    fwrite($fp, " <parameter name=\"$name\">");
	    fwrite($fp, "<value>$value</value></parameter>\n");
	}
	fwrite($fp, "</template_parameters>\n");
	fclose($fp);
	chmod($parameter_xmlfile, 0666);
    }
    $command_options .= " -p $parameter_xmlfile";
}

if (count($errors)) {
329
    SPITFORM($instance, $formfields, $parameters, $errors);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    PAGEFOOTER();
    exit(1);
}

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

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

# Okay, we can spit back a header now that there is no worry of redirect.
345 346 347 348 349 350 351
PAGEHEADER("Start Experiment Template");

echo "<font size=+2>Experiment Template <b>" .
        MakeLink("template",
		 "guid=$guid&version=$version", "$guid/$version") . 
      "</b></font>\n";
echo "<br><br>\n";
352

353 354 355 356 357 358 359
echo "<script type='text/javascript' language='javascript' ".
     "        src='template_sup.js'>\n";
echo "</script>\n";

echo "<center>\n";
echo "<b>Starting experiment run!</b> ...<br>\n";
echo "This will take a few moments; please be patient.<br>\n";
360
echo "<br>\n";
361
echo "<img id='busy' src='busy.gif'><span id='loading'> Working ...</span>";
362
echo "<br>\n";
363
echo "</center>\n";
364 365 366 367 368 369
flush();

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

373 374 375 376 377
/* Clear the 'loading' indicators above */
echo "<script type='text/javascript' language='javascript'>\n";
echo "ClearLoadingIndicators();\n";
echo "</script>\n";

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
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;
}

396
STARTLOG($pid, $eid);
397

398 399 400 401 402
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>