batchexp.php3 5.06 KB
Newer Older
1 2 3 4 5 6
<?php
include("defs.php3");

#
# Standard Testbed Header
#
7
PAGEHEADER("Creating a Batch Mode Experiment");
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

$mydebug = 0;

#
# First off, sanity check the form to make sure all the required fields
# were provided. I do this on a per field basis so that we can be
# informative. Be sure to correlate these checks with any changes made to
# the project form. 
#
if (!isset($exp_pid) ||
    strcmp($exp_pid, "") == 0) {
  FORMERROR("Select Project");
}
if (!isset($exp_id) ||
    strcmp($exp_id, "") == 0) {
23
  FORMERROR("Experiment Name");
24 25 26
}
if (!isset($exp_name) ||
    strcmp($exp_name, "") == 0) {
27
  FORMERROR("Experiment Description");
28 29 30
}

#
31
# Only known and logged in users can begin experiments.
32
#
33
$uid = GETLOGIN();
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
LOGGEDINORDIE($uid);

#
# Database limits
#
if (strlen($exp_id) > $TBDB_EIDLEN) {
    USERERROR("The experiment name \"$exp_id\" is too long! ".
              "Please select another.", 1);
}

#
# Certain of these values must be escaped or otherwise sanitized.
# 
$exp_name = addslashes($exp_name);

#
50 51 52 53 54 55 56 57 58 59 60 61
# Not allowed to specify both a local and an upload!
#
$speclocal  = 0;
$specupload = 0;

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
if (isset($exp_nsfile) && strcmp($exp_nsfile, "") &&
    strcmp($exp_nsfile, "none")) {
    $specupload = 1;
}
62

63 64 65 66 67 68 69 70 71 72 73
if (!$speclocal && !$specupload) {
    USERERROR("You must supply either NS file name (local or remote)", 1);
}
if ($speclocal && $specupload) {
    USERERROR("You may not specify both an uploaded NS file and an ".
	      "NS file that is located on the Emulab server", 1);
}
if (!$specupload && strcmp($exp_nsfile_name, "")) {
    #
    # Catch an invalid filename.
    #
74 75 76 77
    USERERROR("The NS file '$exp_nsfile_name' does not appear to be a ".
	      "valid filename. Please go back and try again.", 1);
}

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
if ($speclocal) {
    #
    # No way to tell from here if this file actually exists, since
    # the web server runs as user nobody. The startexp script checks
    # for the file before going to ground, so the user will get immediate
    # feedback if the filename is bogus.
    #
    # Do not allow anything outside of /users or /proj. I do not think there
    # is a security worry, but good to enforce it anyway.
    #
    if (! ereg("^$TBPROJ_DIR/.*" ,$exp_localnsfile) &&
        ! ereg("^$TBUSER_DIR/.*" ,$exp_localnsfile)) {
	USERERROR("You must specify a server resident in file in either ".
                  "$TBUSER_DIR/ or $TBPROJ_DIR/", 1);
    }
    
    $nsfile = $exp_localnsfile;
}
elseif ($specupload) {
    #
    # XXX
    # Set the permissions on the NS file so that the scripts can get to it.
    # It is owned by nobody, and most likely protected. This leaves the
    # script open for a short time. A potential security hazard we should
    # deal with at some point.
    #
    chmod($exp_nsfile, 0666);
    $nsfile = $exp_nsfile;
}

108 109 110
#
# Make sure the PID/EID tuple does not already exist in the database.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
111 112 113 114 115
$query_result =
    DBQueryFatal("SELECT eid FROM experiments ".
		 "WHERE eid='$exp_id' and pid='$exp_pid'");
if (mysql_num_rows($query_result)) {
    USERERROR("The experiment name '$exp_id' you have chosen is already ".
116 117 118 119
              "in use in project $exp_pid. Please select another.", 1);
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
120
# Check group. If none specified, then use default group.
121
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
122 123 124
if (!isset($exp_gid) ||
    strcmp($exp_gid, "") == 0) {
	$exp_gid = $exp_pid;
125
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
126 127
if (!TBValidGroup($exp_pid, $exp_gid)) {
    USERERROR("No such group $exp_gid in project $exp_gid!", 1);
128 129 130
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
131
# Verify permissions.
132
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
133
if (! TBProjAccessCheck($uid, $exp_pid, $exp_gid, $TB_PROJECT_CREATEEXPT)) {
134
    USERERROR("You do not have permission to begin a batch experiment in ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
135
	      "in Project/Group $exp_pid/$exp_gid!", 1);
136
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
137 138 139 140 141

#
# We need the unix gid for the group for running the scripts below.
#
TBGroupUnixInfo($exp_pid, $exp_gid, $unix_gid, $unix_name);
142 143

echo "<center><br>";
144 145
echo "<h2>Starting batch experiment setup. Please wait a moment ...
      </h2></center>";
146 147 148 149 150 151 152 153 154 155 156 157

flush();

#
# Run the scripts. We use a script wrapper to deal with changing
# to the proper directory and to keep most of these details out
# of this. 
#
$output = array();
$retval = 0;
$last   = time();

158 159 160
$result = exec("$TBSUEXEC_PATH $uid $unix_gid ".
	       "webbatchexp -x \"$exp_expires\" -E \"$exp_name\" ".
	       "-p $exp_pid -g $exp_gid -e $exp_id $nsfile",
161 162 163 164 165 166 167 168 169 170 171 172 173
 	       $output, $retval);

if ($retval) {
    echo "<br><br><h2>
          Setup Failure($retval): Output as follows:
          </h2>
          <br>
          <XMP>\n";
          for ($i = 0; $i < count($output); $i++) {
              echo "$output[$i]\n";
          }
    echo "</XMP>\n";
    
174
    PAGEFOOTER();
175 176 177
    die("");
}

178 179 180 181 182 183 184 185 186
echo "<br><br>";
echo "<h2>
       Experiment `$exp_id' in project `$exp_pid' has been batched!<br><br>
       Your experiment will be run when enough resources become available.
       This might happen immediately, or it may take hours or days.
       You will be notified via email when the experiment has been run.
       If you do not receive email notification within a reasonable amount
       of time, please contact $TBMAILADDR.
      </h2>\n";
187 188 189 190 191 192

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>