linktest.php3 9.53 KB
Newer Older
1
2
3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
5
6
7
8
9
10
# All rights reserved.
#
include("defs.php3");
include("showstuff.php3");

#
11
# This script uses Sajax ... BEWARE!
12
#
13
14
require("Sajax.php");
sajax_init();
15
sajax_export("stop_linktest");
16

17
#
18
19
20
21
22
23
# We need all errors to come back to us so that we can report the error
# to the user even when its from within an exported sajax function.
# 
function handle_error($message, $death)
{
    echo "failed:$message";
24
    TBERROR("failed:$message", 0);
25
26
27
28
29
30
31
32
33
34
35
36
37
    # Always exit; ignore $death.
    exit(1);
}
$session_errorhandler = 'handle_error';

# If this call is to client request function, then turn off interactive mode.
# All errors will go to above function and get reported back through the
# Sajax interface.
if (sajax_client_request()) {
    $session_interactive = 0;
}

# Now check login status.
38
39
40
41
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

#
42
43
# Need this argument checking in a function so it can called from the
# request handlers.
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
function CHECKPAGEARGS() {
    global $uid, $TB_EXPTSTATE_ACTIVE, $TB_EXPT_MODIFY;
    
    #
    # Check to make sure a valid experiment.
    #
    $pid = $_GET['pid'];
    $eid = $_GET['eid'];
    
    if (isset($pid) && strcmp($pid, "") &&
	isset($eid) && strcmp($eid, "")) {
	if (! TBvalid_eid($eid)) {
	    PAGEARGERROR("$eid is contains invalid characters!");
	}
	if (! TBvalid_pid($pid)) {
	    PAGEARGERROR("$pid is contains invalid characters!");
	}
	if (! TBValidExperiment($pid, $eid)) {
	    USERERROR("$pid/$eid is not a valid experiment!", 1);
	}
	if (TBExptState($pid, $eid) != $TB_EXPTSTATE_ACTIVE) {
	    USERERROR("$pid/$eid is not currently swapped in!", 1);
	}
	if (! TBExptAccessCheck($uid, $pid, $eid, $TB_EXPT_MODIFY)) {
	    USERERROR("You do not have permission to run linktest on ".
		      "$pid/$eid!", 1);
	}
72
    }
73
74
    else {
	PAGEARGERROR("Must specify pid and eid!");
75
76
    }
}
77

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# Grab DB data we need.
#
function GRABDBDATA() {
    global $pid, $eid;
    global $gid, $linktest_level, $linktest_pid;
    
    # Need the GID, plus current level and the pid.
    $query_result =
	DBQueryFatal("select gid,linktest_level,linktest_pid ".
		     "  from experiments ".
		     "where pid='$pid' and eid='$eid'");
    $row = mysql_fetch_array($query_result);
    $gid            = $row[0];
    $linktest_level = $row[1];
    $linktest_pid   = $row[2];
}
95
96

#
97
# Stop a running linktest.
98
# 
99
100
101
function stop_linktest() {
    global $linktest_pid;
    global $uid, $pid, $gid, $eid, $suexec_output;
102

103
104
105
    # Must do this!
    CHECKPAGEARGS();
    GRABDBDATA();
106

107
108
109
110
111
112
113
114
    if (! $linktest_pid) {
	return "stopped:Linktest is not running on experiment $pid/$eid!";
    }
    # For backend script call.
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);
    
    $retval = SUEXEC($uid, "$pid,$unix_gid", "weblinktest -k $pid $eid",
		     SUEXEC_ACTION_IGNORE);
115

116
117
118
119
    if ($retval < 0) {
	return "failed:$suexec_output";
    }
    return "stopped:Linktest has been stopped.";
120
121
}

122
123
124
#
# If user hits stop button in the output side, stop linktest.
#
125
$linktest_running = 0;
126
127
128

function SPEWCLEANUP()
{
129
    global $pid, $gid, $linktest_running;
130
131

    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);
132

133
    if (connection_aborted() && $linktest_running) {
134
	SUEXEC($uid, "$pid,$unix_gid", "weblinktest -k $pid $eid",
135
136
137
138
139
	       SUEXEC_ACTION_IGNORE);
    }
}

#
140
141
142
# Start linktest running.
# 
function start_linktest($level) {
143
144
    global $linktest_pid, $linktest_running, $TBSUEXEC_PATH;
    global $uid, $pid, $gid, $eid, $suexec_output;
145

146
147
148
    # Must do this!
    CHECKPAGEARGS();
    GRABDBDATA();
149

150
151
    if ($linktest_pid) {
	return "failed:Linktest is already running on experiment $pid/$eid!";
152
    }
153
154
155
156
157
158
159
160
161
162
    if (! TBvalid_tinyint($level) ||
	$level < 0 || $level > TBDB_LINKTEST_MAX) {
	return "failed:Linktest level ($level) must be an integer ".
	    "1 <= level <= ". TBDB_LINKTEST_MAX;
    }
    
    # For backend script call.
    TBGroupUnixInfo($pid, $gid, $unix_gid, $unix_name);

    # Make sure we shutdown if client goes away.
163
164
165
    register_shutdown_function("SPEWCLEANUP");
    set_time_limit(0);

166
167
168
169
170
171
    # XXX Hackish!
    $linktest_running = 1;
    $fp = popen("$TBSUEXEC_PATH $uid $pid,$unix_gid ".
		"weblinktest -l $level $pid $eid", "r");
    if (! $fp) {
	USERERROR("Could not start linktest!", 1);
172
    }
173

174
175
176
177
    header("Content-Type: text/plain");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
178

179
180
181
182
183
    echo "Starting linktest run at level $level on " .
	date("D M d G:i:s T");
    # Oh, this is soooooo bogus!
    for ($i = 0; $i < 1024; $i++) {
	echo " ";
184
    }
185
186
187
188
189
190
191
192
193
194
195
196
    echo "\n";
    
    flush();
    
    while (!feof($fp)) {
	$string = fgets($fp, 1024);
	echo "$string";
	flush();
    }
    $retval = pclose($fp);
    $linktest_running = 0;

197
    if ($retval > 0) {
198
	echo "Linktest reported errors! Stopped at " .
199
200
201
	    date("D M d G:i:s T") . "\n";
    }
    elseif ($retval < 0) {
202
	echo $suexec_output;
203
    }
204
205
206
207
208
209
    else {
        # Success.
	echo "Linktest run was successful! Stopped at " .
	    date("D M d G:i:s T") . "\n";
    }
    exit(0);
210
211
}

212
213
214
215
# See if this request is to one of the above functions. Does not return
# if it is. Otherwise return and continue on.
sajax_handle_client_request();

216
#
217
218
# In plain kill mode, just use the stop_linktest function and then
# redirect back to the showexp page.
219
#
220
221
if (isset($kill) && $kill == 1) {
    stop_linktest();
222
    
223
    header("Location: showexp.php3?pid=$pid&eid=$eid");
224
225
    return;
}
226
227
228
229
if (isset($start) && isset($level)) {
    start_linktest($level);
    return;
}
230

231
232
233
234
235
236
237
238
239
240
241
242
#
# Okay, this is the initial page.
# 
PAGEHEADER("Run Linktest");

# Must do this!
CHECKPAGEARGS();
GRABDBDATA();

echo "<script>\n";
sajax_show_javascript();
if ($linktest_pid) {
243
    echo "var curstate    = 'running';\n";
244
245
}
else {
246
    echo "var curstate    = 'stopped';\n";
247
248
249
250
}
?>

// Linktest stopped. No need to do anything since if a request was running,
251
// it will have returned prematurely.
252
253
254
255
function do_stop_cb(msg) {
    if (msg == '') {
	return;
    }
256

257
258
259
260
261
262
263
264
265
266
267
268
269
    //
    // The first part of the message is an indicator whether command
    // was sucessful. The rest of it (after the ":" is the text itself).
    //
    var offset = msg.indexOf(":");
    if (offset == -1) {
	return;
    }
    var status = msg.substring(0, offset);
    var output = msg.substring(offset + 1);

    // If we got an error; throw up something useful.
    if (status != 'stopped') {
270
	alert("Linktest could not be stopped: " + output);
271
272
273
    }
}

274
275
276
277
278
// onLoad callback for when linktest stops in the iframe.
function linktest_stopped() {
    // Avoid initial outer page load event.
    if (curstate != 'running')
	return;
279
    
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    curstate = 'stopped';	
    getObjbyName('action').value = 'Start';

    var Iframe = document.getElementById('outputarea');
    var html   = Iframe.contentWindow.document.documentElement.innerHTML;

    re = /reported errors/gi
    re.multiline = true;

    if (html.search(re) != -1) {
	getObjbyName('message').innerHTML =
	    '<font size=+1 color=red><blink>' +
	    'Linktest has reported errors! Please examine log below.' +
	    '</blink></font>';
294
295
296
297
298
299
300
301
302
303
304
305
    }
}

function doaction(theform) {
    var levelx = theform['level'].selectedIndex;
    var level  = theform['level'].options[levelx].value;
    var action = theform['action'].value;

    if (curstate == 'stopped') {
	if (level == '0')
	    return;

306
307
308
309
310
311
312
313
314
315
316
317
	// This clears the message area.
	getObjbyName('message').innerHTML = "";

	Iframe = document.getElementById('outputarea');
	// This stuff clears the current contents of the iframe.
	Iframe.contentWindow.document.open();
	Iframe.contentWindow.document.write(" ");
	Iframe.contentWindow.document.close();
	// And this fires it up.
	Iframe.contentWindow.document.location =
	    '<?php echo $REQUEST_URI; ?>&start=1&level=' + level;

318
319
320
	curstate = "running";
	theform['action'].value = "Stop Linktest";
    }
321
322
    else if (curstate == 'running') {
	curstate = 'stopping';	
323
324
325
326
327
328
329
	x_stop_linktest(do_stop_cb);
    }
}
<?php
echo "</script>\n";

echo "<font size=+2>Experiment <b>".
330
331
332
	"<a href='showproject.php3?pid=$pid'>$pid</a>/".
	"<a href='showexp.php3?pid=$pid&eid=$eid'>$eid</a></b></font>\n";

333
334
335
echo "<center><font size=+2><br>
         Are you <b>sure</b> you want to run linktest?
         </font><br><br>\n";
336

337
SHOWEXP($pid, $eid, 1);
338

339
echo "<br>\n";
340
echo "<form action=linktest.php3 method=post name=myform id=myform>";
341
342
echo "<input type=hidden name=pid value=$pid>\n";
echo "<input type=hidden name=eid value=$eid>\n";
343

344
345
346
347
348
349
echo "<table align=center border=1>\n";
echo "<tr>
          <td><a href='$TBDOCBASE/doc/docwrapper.php3?".
                 "docname=linktest.html'>Linktest</a> Option:</td>
          <td><select name=level>
                 <option value=0>Skip Linktest </option>\n";
350

351
352
for ($i = 1; $i <= TBDB_LINKTEST_MAX; $i++) {
    $selected = "";
353

354
355
    if (strcmp("$level", "$i") == 0)
	$selected = "selected";
356
	
357
358
359
360
361
    echo "        <option $selected value=$i>Level $i - " .
	$linktest_levels[$i] . "</option>\n";
}
echo "       </select>";
echo "    </td>
362
363
364
          </tr>
          </table><br>\n";

365
366
367
368
369
370
371
if ($linktest_pid) {
    echo "<input type=button name=action id=action value='Stop Linktest' ".
	         "onclick=\"doaction(myform); return false;\">\n";
}
else {
    echo "<input type=button name=action id=action value=Start ".
	         "onclick=\"doaction(myform); return false;\">\n";
372
373
}

374
echo "</form>\n";
375
376
377
378
379
380
echo "<div id=message></div>\n";
echo "<div id=output style='overflow:auto'>
      <iframe onload=\"linktest_stopped();\"
      width=80% height=400 scrolling=auto id=outputarea frameborder=1>
      </iframe></center>
     </div>\n";
381
382
echo "</center>\n";

383
#
384
385
386
# Standard Testbed Footer
# 
PAGEFOOTER();
387
?>