swapexp.php3 9.58 KB
Newer Older
1
<?php
Leigh Stoller's avatar
Leigh Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
5 6
# All rights reserved.
#
7
include("defs.php3");
8
include("showstuff.php3");
9
include_once("template_defs.php");
10 11 12 13 14 15 16

#
# Only known and logged in users can end experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

17 18 19
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

20 21 22 23 24
#
# Must provide the EID!
# 
if (!isset($pid) ||
    strcmp($pid, "") == 0) {
25
    USERERROR("The project ID was not provided!", 1);
26 27 28 29
}

if (!isset($eid) ||
    strcmp($eid, "") == 0) {
30
    USERERROR("The experiment ID was not provided!", 1);
31 32 33
}

if (!isset($inout) ||
34
    (strcmp($inout, "in") && strcmp($inout, "out") &&
35 36 37
     strcmp($inout, "pause") && strcmp($inout, "restart") &&
     strcmp($inout, "instop"))) {
    USERERROR("Improper inout argument!", 1);
38 39
}

40 41 42 43 44 45 46 47 48 49 50
# Canceled operation redirects back to showexp page. See below.
if ($canceled) {
    header("Location: showexp.php3?pid=$pid&eid=$eid");
    return;
}

#
# Standard Testbed Header, after cancel above.
#
PAGEHEADER("Swap Control");

51 52 53 54 55 56 57 58
#
# Only admins can issue a force swapout
# 
if (isset($force) && $force == 1) {
	if (! ISADMIN($uid)) {
		USERERROR("Only testbed administrators can forcibly swap ".
			  "an experiment out!", 1);
	}
59 60
	if (!isset($idleswap)) { $idleswap=0; }
	if (!isset($autoswap)) { $autoswap=0; }
61 62 63
	if (!isset($forcetype)){ $forcetype="force"; }
	if ($forcetype=="idleswap") { $idleswap=1; }
	if ($forcetype=="autoswap") { $autoswap=1; }
64 65 66
}
else {
	$force = 0;
67 68
	$idleswap=0;
	$autoswap=0;
69 70
}

71 72 73 74 75 76 77 78 79 80
$exp_eid = $eid;
$exp_pid = $pid;

#
# Check to make sure thats this is a valid PID/EID tuple.
#
$query_result =
    DBQueryFatal("SELECT * FROM experiments WHERE ".
		 "eid='$exp_eid' and pid='$exp_pid'");
if (mysql_num_rows($query_result) == 0) {
81 82
    USERERROR("The experiment $exp_eid is not a valid experiment ".
	      "in project $exp_pid.", 1);
83
}
84 85 86
$row           = mysql_fetch_array($query_result);
$exp_gid       = $row[gid];
$isbatch       = $row[batchmode];
87
$state         = $row[state];
88
$exptidx       = $row[idx];
89 90 91 92
$swappable     = $row[swappable];
$idleswap_bit  = $row[idleswap];
$idleswap_time = $row[idleswap_timeout];
$idlethresh    = min($idleswap_time/60.0,TBGetSiteVar("idle/threshold"));
93
$lockdown      = $row["lockdown"];
94 95 96 97 98

#
# Verify permissions.
#
if (! TBExptAccessCheck($uid, $exp_pid, $exp_eid, $TB_EXPT_MODIFY)) {
99
    USERERROR("You do not have permission for $exp_eid!", 1);
100 101
}

102
# Template Instance Experiments get special treatment in this page.
103
$instance = TemplateInstance::LookupByExptidx($exptidx);
104
if ($instance && ($inout != "out" && $inout != "in")) {
105
    PAGEARGERROR("Invalid action for template instance");
106 107
}

108 109 110 111 112 113 114 115 116
# Convert inout to informative text.
if (!strcmp($inout, "in")) {
    if ($isbatch) 
	$action = "queue";
    else
	$action = "swapin";
}
elseif (!strcmp($inout, "out")) {
    if ($isbatch) 
117
	$action = "swapout and dequeue";
118
    else {
119
	if ($state == $TB_EXPTSTATE_ACTIVATING) {
120 121 122 123 124 125
	    $action = "cancel";
	}
	else {
	    $action = "swapout";
	}
    }
126 127 128
}
elseif (!strcmp($inout, "pause")) {
    if (!$isbatch)
129
	USERERROR("Only batch experiments can be dequeued!", 1);
130
    $action = "dequeue";
131 132 133 134 135
}
elseif (!strcmp($inout, "restart")) {
    $action = "restart";
}

136
if ($instance) {
Leigh Stoller's avatar
Leigh Stoller committed
137 138 139 140 141 142 143
    $guid = $instance->guid();
    $version = $instance->vers();
    
    echo "<font size=+2>Template <b>" .
          MakeLink("template",
		   "guid=$guid&version=$version", "$guid/$version") .
	"</b>, Instance <b>";
144 145 146 147
}
else {
    echo "<font size=+2>Experiment <b>";
}
Leigh Stoller's avatar
Leigh Stoller committed
148 149 150
echo MakeLink("project", "pid=$pid", $pid) . "/" .
     MakeLink("experiment", "pid=$pid&eid=$eid", $eid);
echo "</b></font><br>\n";
151
flush();
152

153 154 155 156 157 158
# A locked down experiment means just that!
if ($lockdown) {
    echo "<br><br>\n";
    USERERROR("Cannot proceed; the experiment is locked down!", 1);
}

159 160 161
#
# We run this twice. The first time we are checking for a confirmation
# by putting up a form. The next time through the confirmation will be
162 163
# set. Or, the user can hit the cancel button, in which case we 
# redirect the browser back to the experiment page (see above).
164 165 166
#
if (!$confirmed) {
    echo "<center><h2><br>
167 168
          Are you sure you want to ";
    if ($force) {
169
	echo "<font color=red><br>forcibly</br></font> ";
170
    }
171
    if ($instance) {
172
	echo "$action template instance";
173 174 175 176 177
    }
    else {
	echo "$action experiment";
    }
    echo " '$exp_eid?'
178 179
          </h2>\n";
    
180 181
    SHOWEXP($exp_pid, $exp_eid, 1);

182 183
    echo "<form action='swapexp.php3?inout=$inout&pid=$exp_pid&eid=$exp_eid'
                method=post>";
184 185

    if ($force) {
186 187 188
	if (!$swappable) {
	    echo "<h2>Note: This experiment is <em>NOT</em> swappable!</h2>\n";
	}
189 190 191 192 193 194 195 196
	echo "Force Type: <select name=forcetype>\n";
	echo "<option value=force ".($forcetype=="force"?"selected":"").
	    ">Forced Swap</option>\n";
	echo "<option value=idleswap ".($forcetype=="idleswap"?"selected":"").
	    ">Idle-Swap</option>\n";
	echo "<option value=autoswap ".($forcetype=="autoswap"?"selected":"").
	    ">Auto-Swap</option>\n";
	echo "</select><br><br>\n";
197 198 199
	echo "<input type=hidden name=force value=$force>\n";
	echo "<input type=hidden name=idleswap value=$idleswap>\n";
	echo "<input type=hidden name=autoswap value=$autoswap>\n";
200 201
    }
    
202 203
    echo "<b><input type=submit name=confirmed value=Confirm></b>\n";
    echo "<b><input type=submit name=canceled value=Cancel></b>\n";
204
    echo "</form>\n";
205

206
    if ($inout!="out" && $idleswap_bit) {
207 208 209 210
	if ($idleswap_time / 60.0 != $idlethresh) {
	    echo "<p>Note: The Idle-Swap time for your experiment will be
		 reset to $idlethresh hours.</p>\n";
	}
211 212
    }
    
213 214
    if (!strcmp($inout, "restart")) {
	echo "<p>
215
              <a href='$TBDOCBASE/faq.php3#restart'>
216 217 218 219
                 (Information on experiment restart)</a>\n";
    }
    else {
	echo "<p>
220
              <a href='$TBDOCBASE/faq.php3#swapping'>
221 222
                 (Information on experiment swapping)</a>\n";
    }
223 224 225 226 227 228 229 230 231 232 233 234
    echo "</center>\n";

    PAGEFOOTER();
    return;
}

#
# We need the unix gid for the project for running the scripts below.
# Note usage of default group in project.
#
TBGroupUnixInfo($exp_pid, $exp_gid, $unix_gid, $unix_name);

235 236 237
if ($instance) {
    $guid = $instance->guid();
    $version = $instance->vers();
Leigh Stoller's avatar
Leigh Stoller committed
238

239 240
    STARTBUSY(($inout == "out" ? "Terminating" : "Starting") . 
	      " template instance!");    
241 242
}

243 244 245 246 247
#
# Run the scripts. We use a script wrapper to deal with changing
# to the proper directory and to keep some of these details out
# of this. 
#
248 249 250 251
# Avoid SIGPROF in child.
# 
set_time_limit(0);

252 253
# Args for idleswap It passes them on to swapexp, or if it is just a
# plain force swap, it passes -f for us.
254
$args = ($idleswap ? "-i" : ($autoswap ? "-a" : ""));
255

256
$retval = SUEXEC($uid, "$exp_pid,$unix_gid",
257 258
		  ($force ?
		   "webidleswap $args $exp_pid $exp_eid" :
259
		   ($instance ?
260
		    "webtemplate_swap$inout -e $exp_eid $guid/$version" :
261
		    "webswapexp -s $inout $exp_pid $exp_eid")),
262
		 SUEXEC_ACTION_IGNORE);
263

264 265 266 267
if ($instance) {
    CLEARBUSY();
}

268 269 270 271 272 273 274 275 276
#
# 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_DIE);
    #
    # Never returns ...
    #
277 278 279 280 281 282
    die("");
}

#
# Exit status 0 means the experiment is swapping, or will be.
#
283 284
echo "<br>\n";
if ($retval) {
285
    echo "<h3>$action could not proceed</h3>";
286 287 288
    echo "<blockquote><pre>$suexec_output<pre></blockquote>";
}
else {
289
    if ($instance) {
290 291 292
	STARTLOG($pid, $eid);
    }
    elseif ($isbatch) {
293 294 295 296 297 298 299 300 301
	if (strcmp($inout, "in") == 0) {
	    echo "Batch Mode experiments 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. In the meantime, you can
                  check the web page to see how many attempts have been made,
                  and when the last attempt was.\n";
	}
	elseif (strcmp($inout, "out") == 0) {
302

303 304 305 306 307 308 309 310 311 312
	    echo "Batch mode experiments take a few moments to stop. Once
                  it does, the experiment will enter the 'paused' state.
                  You can requeue the batch experiment at that time.\n";

	    echo "<br><br>
                  If you do not receive
                  email notification within a reasonable amount of time,
                  please contact $TBMAILADDR.\n";
	}
	elseif (strcmp($inout, "pause") == 0) {
313
	    echo "Your experiment has been dequeued. You may requeue your
314 315
		  experiment at any time.\n";
	}
316
	STARTWATCHER($pid, $eid);
317 318
    }
    else {
319
	echo "<div>";
320
	if (strcmp($inout, "out") == 0 &&
321
	    strcmp($state, $TB_EXPTSTATE_ACTIVATING) == 0) {
322 323 324 325 326

	    echo "Your experiment swapin has been marked for cancelation.
                  It typically takes a few minutes for this to be recognized,
                  assuming you made your request early enough. You will
                  be notified via email when the original swapin request has
327
                  either aborted or finished. ";
328 329 330 331 332 333
	}
	else {
	    if (strcmp($inout, "in") == 0)
		$howlong = "two to ten";
	    else
		$howlong = "less than two";
334
    
335
	    echo "<b>Your experiment has started its $action.</b> 
336 337
                 You will be notified via email when the operation is complete.
                 This typically takes $howlong minutes, depending on the
338
                 number of nodes in the experiment. ";
339
	}
340
	echo "If you do not receive
341 342 343 344
              email notification within a reasonable amount of time,
              please contact $TBMAILADDR.\n";

	echo "<br><br>
345 346
              While you are waiting, you can watch the log in realtime:<br>\n";
	echo "</div>";
347
	echo "<br>\n";
348
	STARTLOG($pid, $eid);
349
    }
350 351 352 353 354 355 356
}

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