status.ajax 28.9 KB
Newer Older
1
2
<?php
#
3
# Copyright (c) 2000-2015 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
chdir("..");
25
include_once("webtask.php");
26
27
28
29
30
31
32
33
34
35
36
37
include_once("geni_defs.php");
chdir("apt");
include_once("profile_defs.php");
include_once("instance_defs.php");

# Set these globals below.
$instance = null;
$creator  = null;

#
# Locate the objects and check permission.
#
38
function StatusSetupAjax($needmodify)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{
    global $this_user, $ajax_args;
    global $instance, $creator;

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing instance uuid");
	return 1;
    }
    $uuid = $ajax_args["uuid"];
    $instance = Instance::Lookup($uuid);
    if (!$instance) {
	SPITAJAX_ERROR(1, "no such instance uuid: $uuid");
	return 1;
    }
    $creator = GeniUser::Lookup("sa", $instance->creator_uuid());
    if (! $creator) {
	$creator = User::LookupByUUID($instance->creator_uuid());
    }
    if (!$creator) {
	SPITAJAX_ERROR(1, "no such instance creator");
	return 1;
    }
61
    # Admin users do whatever they like.
62
63
64
    if (isset($this_user) && ISADMIN()) {
	return 0;
    }
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    # For a guest user; must be the same guest that created experiment.
    if (get_class($creator) == "GeniUser") {
        if (isset($_COOKIE['quickvm_user']) &&
            $_COOKIE['quickvm_user'] == $creator->uuid()) {
            return 0;
        }
	SPITAJAX_ERROR(1, "You do not have permission!");
	return 1;
    }
    # An experiment created by a real user, can be accessed by other
    # members of the project, subject to modify restrictions.
    if (! (isset($this_user) && get_class($creator) == "User" &&
           $instance->CanView($this_user) &&
           (!$needmodify || $instance->CanModify($this_user)))) {
79
80
81
82
83
84
85
86
87
88
89
	SPITAJAX_ERROR(1, "You do not have permission!");
	return 1;
    }
    return 0;
}

#
# Status/
#
function Do_GetInstanceStatus()
{
90
    global $instance, $creator, $APTBASE;
91

92
    if (StatusSetupAjax(0)) {
93
94
	return;
    }
95
96
    $blob = array();
    $blob["status"] = $instance->status();
97
98
    $blob["sliverstatus"] = array();
    $blob["sliverurls"]   = array();
99
100
101
102
103

    if ($instance->logfileid()) {
        $blob["logfile_url"] =
            "$APTBASE/spewlogfile.php?logfile=" . $instance->logfileid();
    }
104
    
105
106
107
108
109
110
111
112
113
114
115
    #
    # If we have all of our manifests, the client can request them
    # and show the topology.
    #
    $havemanifests = 1;

    foreach ($instance->slivers() as $sliver) {
        if (!$sliver->manifest()) {
            $havemanifests = 0;
        }
        else {
116
117
            if ($sliver->webtask_id() &&
                $webtask = WebTask::Lookup($sliver->webtask_id())) {
118
119
120
121
122
                $sliverstatus = $webtask->TaskValue("sliverstatus");
                if ($sliverstatus) {
                    $blob["sliverstatus"][$sliver->aggregate_urn()] =
                        $sliverstatus;
                }
123
124
            }
        }
125
        if ($sliver->public_url()) {
126
127
            $blob["sliverurls"][] = array("name" => $sliver->aggregate_name(),
                                          "url"  => $sliver->public_url());
128
        }
129
    }
130
    $blob["havemanifests"] = $havemanifests;
131
132
133
134
135

    # Reflect errors back to the user.
    $webtask = WebTask::LookupByObject($instance->uuid());
    if ($webtask && $webtask->exited()) {
        if ($webtask->exitcode() == 26) {
136
137
            $blob["reason"]  = "Not enough free nodes, please try again later.";
            $blob["reason"] .= "\n\n";
138
        }
139
        elseif ($webtask->exitcode() == 25) {
140
141
            $blob["reason"]  = "Not enough available bandwidth for a link.\n\n";
        }
142
143
        elseif ($webtask->exitcode() == 28) {
            $blob["reason"]  = "Your topology could not be mapped to physical ".
Leigh B Stoller's avatar
Leigh B Stoller committed
144
145
146
                "resources. Clicking the 'Sliver' button above will provide ".
                "lots of info, some of which might be useful in figuring out ".
                "why it failed.\n\n";
147
148
149
150
151
        }
        elseif ($webtask->exitcode() == 8) {
            $blob["reason"]  = "Your experiment timed out while setting up, ".
                "most likely because one or more nodes failed to boot. The ".
                "node consoles may provide more information.\n\n";
152
153
154
155
156
157
158
159
        }
        elseif ($webtask->exitcode() == 1) {
            #
            # This is GENIRESPONSE_BADARGS which typically means an error
            # in the input rspec, something the user must have put in it.
            #
            $blob["reason"]  = "Your topology source contains invalid ".
                "values.\n\n";
160
        }
161
        else {
162
            $blob["reason"] = "";
163
        }
164
        $blob["reason"] .= htmlentities($webtask->TaskValue("output"));
165
    }
166
    SPITAJAX_RESPONSE($blob);
167
168
169
170
171
172
173
}

#
# Terminate.
#
function Do_TerminateInstance()
{
174
    global $instance, $creator, $this_user, $ajax_args;
175

176
    if (StatusSetupAjax(1)) {
177
178
179
	return;
    }
    $uuid = $instance->uuid();
180
181
182
183
184
185
186
187
188
189
    $webtask_id = WebTask::GenerateID();

    if ($instance->admin_lockdown() || $instance->user_lockdown()) {
        if (! (isset($this_user) &&
               ($instance->creator() == $this_user->uid() || ISADMIN()))) {
            SPITAJAX_ERROR(1, "Not enough permission to terminate; ".
                           "experiment is locked down");
            return;
        }
        $override = substr($uuid, 2, 5);
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        if (! (isset($ajax_args["lockdown_override"]) &&
               $ajax_args["lockdown_override"] == $override)) {
            SPITAJAX_ERROR(1, "Lockdown override is incorrect");
            return;
        }
        $retval = SUEXEC($this_user->uid(), "nobody",
                         "webmanage_instance -t $webtask_id -- ".
                         "  lockdown $uuid clear all",
                         SUEXEC_ACTION_CONTINUE);
        if ($retval) {
            $webtask = WebTask::Lookup($webtask_id);

            if ($webtask && $webtask->exited()) {
                SPITAJAX_ERROR(1, $webtask->TaskValue("output"));
                $webtask->Delete();
            }
            else {
                SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
            }
            return;
        }
    }
213
214
215
    # We do not want to see the busy errors, and that is the only error
    # that gets returned, that is not also emailed by the script. So just
    # use the ignore option.
216
    $retval = SUEXEC("nobody", "nobody",
217
                     "webmanage_instance terminate $uuid",
218
                     SUEXEC_ACTION_IGNORE);
219
220
221
222
223

    if ($retval == 0) {
	SPITAJAX_RESPONSE("");
	return;
    }
224
225
    SPITAJAX_ERROR(-1, "Your experiment is probably busy, ".
        "please try again later.");
226
227
228
229
230
231
232
233
234
}

#
# Manifest.
#
function Do_GetInstanceManifest()
{
    global $instance, $creator;

235
    if (StatusSetupAjax(0)) {
236
237
238
	return;
    }

239
240
241
242
243
244
245
246
    $blob = array();

    foreach ($instance->slivers() as $sliver) {
        if ($sliver->manifest()) {
            $blob[$sliver->aggregate_urn()] = $sliver->manifest();
        }
    }
    SPITAJAX_RESPONSE($blob);
247
248
249
250
251
252
253
}

#
# SSH Auth Object
#
function Do_GetSSHAuthObject()
{
254
    global $instance, $creator, $this_user;
255
256
257
258
259
260
261
262
    global $ajax_args;

    if (!isset($ajax_args["hostport"])) {
	SPITAJAX_ERROR(1, "Missing hostport");
	return 1;
    }
    $hostport = $ajax_args["hostport"];
    
263
    if (StatusSetupAjax(1)) {
264
265
	return;
    }
266
267
268
    #
    # XXX Need to deal with multiple members of an experiment.
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
269
270
271
    if (! ((isset($this_user) && $this_user->SameUser($creator)) ||
	   (isset($_COOKIE['quickvm_user']) &&
	    $_COOKIE['quickvm_user'] == $creator->uuid()))) {
272
273
274
275
	SPITAJAX_ERROR(1, "Not allowed to ssh; only the creator");
	return;
    }
    
276
277
278
279
280
281
282
283
284
285
286
287
    $nodeid = $ajax_args["nodeid"];
    $auth   = SSHAuthObject($creator->uid(), $hostport);
    if (!$auth) {
	SPITAJAX_ERROR(1, "Could not create authentication object");
	return;
    }
    SPITAJAX_RESPONSE($auth);
}

#
# Request automatic extension.
#
Leigh B Stoller's avatar
Leigh B Stoller committed
288
function Do_RequestExtension()
289
{
290
    global $instance, $creator, $this_user, $suexec_output;
291
    global $ajax_args;
292
    global $TBMAIL_OPS, $APTMAIL, $EXTENSIONS, $APTBASE;
293
294
    $autoextend_maximum = TBGetSiteVar("aptui/autoextend_maximum");
    $autoextend_maxage  = TBGetSiteVar("aptui/autoextend_maxage");
295
    $reason  = "";
296
    $message = "";
297
    $needapproval = 0;
298
    $granted = 0;
299

300
    if (StatusSetupAjax(1)) {
301
	goto bad;
302
303
304
305
306
    }
    $uuid = $instance->uuid();
    $slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
    if (!slice) {
	SPITAJAX_ERROR(1, "no slice for instance");
307
	goto bad;
308
309
    }
    $expires_time = strtotime($slice->expires());
310
    $created_time = strtotime($instance->created());
311

312
313
314
    if ($autoextend_maximum == 0 && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Extensions are currenly disabled; please ".
                       "contact us if you need to make special arrangements.");
315
316
317
	goto bad;
    }

318
319
320
321
    if (!isset($ajax_args["howlong"]) || $ajax_args["howlong"] == "") {
	SPITAJAX_ERROR(1, "Missing number of days");
	goto bad;
    }
322
323
    $granted = $wanted = $ajax_args["howlong"];
    if (! preg_match("/^\d+$/", $wanted)) {
324
325
326
327
	SPITAJAX_ERROR(1, "Invalid characters in days");
	goto bad;
    }

328
    if (ISADMIN()) {
329
	if ($wanted < 1 || $wanted > 365) {
330
	    SPITAJAX_ERROR(1, "Must be an integer 1 <= days <= 365");
331
	    goto bad;
332
	}
333
334
335
336
        $reason = "Your experiment was extended by the site administrator.\n";
	if (isset($ajax_args["reason"]) && $ajax_args["reason"] != "") {
            $reason .= "\n" . $ajax_args["reason"];
        }
337
338
339
340
    }
    else {
	if (!isset($ajax_args["reason"]) || $ajax_args["reason"] == "") {
	    SPITAJAX_ERROR(1, "Missing reason");
341
	    goto bad;
342
343
344
	}
	$reason  = $ajax_args["reason"];

345
346
347
348
349
        if (!TBvalid_fulltext($reason)) {
	    SPITAJAX_ERROR(1, "Illegal characters in reason");
	    goto bad;
        }

350
351
352
353
354
	#
	# Guest users are treated differently.
	#
	if (!isset($this_user)) {
            # Only extend for 24 hours. More later.
355
            $granted = 1;
356

357
	    if ($expires_time > time() + (3600 * 24 * $granted)) {
358
		SPITAJAX_ERROR(1, "You still have a day left. ".
359
360
			       "Try again tomorrow");
		goto delay;
361
362
363
	    }
	}
	else {
364
            $diff  = $expires_time - time();
Leigh B Stoller's avatar
Leigh B Stoller committed
365
            $cdiff = time() - $created_time;
Leigh B Stoller's avatar
Leigh B Stoller committed
366
367
368
369
370
371
372
373
374
375

            #
            # If admin lockout, we are refusing any more free time.
            #
            if ($instance->extension_lockout()) {
                $needapproval = 1;
                $granted = 0;
                $message = "because you are not allowed any more ".
                    "free extensions";
            }
376
            #
377
            # After maxage, all extension requests require admin approval.
378
            #
Leigh B Stoller's avatar
Leigh B Stoller committed
379
            elseif ($cdiff > (3600 * 24 * $autoextend_maxage)) {
380
381
382
383
                #
                # Well, unless they asked for less then the free grant,
                # which is a nice loophole people will probably notice.
                #
384
                $granted = 2;
385
386
387
388
389
390
                
                if ($wanted > $granted) {
                    $needapproval = 1;
                    $message = "because it was started more then ".
                        "$autoextend_maxage days ago";
                }
391
            }
392
393
394
            #
            # Temporary for GEC23
            #
Leigh B Stoller's avatar
Leigh B Stoller committed
395
396
            elseif (0 && (time() + ($wanted * 3600 * 24) >
                          strtotime("2015-06-15 12:00:00"))) {
397
398
399
400
                $granted = 1;
                $needapproval = 1;
                $message = "because the testbed is mostly reserved for GEC23";
            }
401
	    #
402
403
404
405
406
	    # Registered users are granted up to the autoextend_maximum
            # automatically. Beyond that, requires approval, but we still
            # give them whatever the free extension is, since we want
            # to give them extra time until the next meeting of the
            # "resource management committee."
407
	    #
408
409
410
411
412
413
414
415
416
417
418
419
420
421
	    elseif ($wanted > $autoextend_maxage) {
                #
                # We have plenty of time left, no extension just a message.
                #
                if ($diff > (3600 * 24 * 7)) {
                    needAdminApproval($wanted, 0, $reason,
                                      "because it was for longer then ".
                                      "$autoextend_maximum days");
                    goto delay;
                }
                $needapproval = 1;
                $granted = $autoextend_maximum;
                $message = "because it was for longer then ".
                    "$autoextend_maximum days";
422
	    }
423
424
425
426
427
428
            else {
                if ($diff > (3600 * 24 * 7)) {
                    SPITAJAX_ERROR(1, "You still have a week left!");
                    goto bad;
                }
            }
429
	    #
430
431
432
433
	    # The most we allow is the autoextend_maximum out, no
	    # matter what they asked for. So, if the autoextend_maximum
	    # is a week and there are five days left and they asked
	    # for seven, we give them two.
434
	    #
435
436
437
            if ($expires_time + ($granted * 3600 * 24) >
                time() + (3600 * 24 * $autoextend_maximum)) {
		$seconds = (3600 * 24 * $autoextend_maximum) - $diff;
438
                $granted = intval($seconds / (3600 * 24));
439
	    }
440
        }
441
    }
442
443
    if ($granted > 0) {
        $seconds = 3600 * 24 * $granted;
444
445
446
447
        $retval = SUEXEC("nobody", "nobody",
                         "webmanage_instance extend $uuid $seconds",
                         SUEXEC_ACTION_CONTINUE);
    }
448
449

    if ($retval == 0) {
450
	# Refresh. 
451
	$slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
Leigh B Stoller's avatar
Leigh B Stoller committed
452
453
	$new_expires = date("Y-m-d H:i:s T", strtotime($slice->expires()));
        $created = date("Y-m-d H:i:s T", strtotime($instance->created()));
454
455
        $before = date("Y-m-d H:i:s T", $expires_time);
        $now = date("Y-m-d H:i:s T", time());
Leigh B Stoller's avatar
Leigh B Stoller committed
456
        list($cluster) = Instance::ParseURN($instance->aggregate_urn());
457

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
        #
        # We store each extension request in an ongoing text field.
        #
        $text =
            "Date: $now\n".
            "Wanted: $wanted, Granted: $granted\n".
            "Before: $before\n".
            "After $new_expires\n".
            "Reason:\n".
            $reason . "\n\n".
            "-----------------------------------------------\n";
        $instance->AddExtensionHistory($text);

        if ($needapproval) {
            needAdminApproval($wanted, $granted, $reason, $message);
            if (!$granted)
                goto delay;
            return;
        }
477
478
	SPITAJAX_RESPONSE($new_expires);

479
480
	$instance->SendEmail($creator->email(),
	       "Experiment Extension: $uuid",
481
482
483
484
485
               (ISADMIN() ? $reason :
                "A request to extend your experiment was made and ".
                "granted.\n".
                "Your reason was:\n\n". $reason) .
               "\n\n".
Leigh B Stoller's avatar
Leigh B Stoller committed
486
487
488
               "Your experiment was started on $created\n".
 	       "Your experiment will now expire at $new_expires\n".
               "It is running on $cluster\n\n\n".
489
               "$APTBASE/status.php?uuid=$uuid\n",
490
491
	       "From: $EXTENSIONS\n" .
	       "BCC: $EXTENSIONS");
492
493
494
495
496
497
498
499
        #
        # We do not want to overwrite the reason in the DB if this
        # was an admin extension, we want to keep whatever the user
        # has written previously.
        #
        if (! ISADMIN()) {
            $instance->SetExtensionReason($reason);
        }
500
        else {
501
            $instance->SetExtensionRequested(0);
502
        }
503
        $instance->BumpExtensionCount($granted);
504
505
506
    }
    elseif ($retval > 0) {
	SPITAJAX_ERROR(1, $suexec_output);
507
	goto bad;
508
509
510
    }
    else {
	SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
511
	goto bad;
512
    }
513
514
    return;
bad:
515
516
517
518
delay:
    sleep(1);
}

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#
# Deny extension,
#
function Do_DenyExtension()
{
    global $instance, $creator, $this_user, $suexec_output;
    global $ajax_args;
    global $TBMAIL_OPS, $APTMAIL, $EXTENSIONS, $APTBASE;
    $reason  = "";
    $message = "";

    if (StatusSetupAjax(1)) {
	goto bad;
    }
    $uuid = $instance->uuid();
    $slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
    if (!slice) {
	SPITAJAX_ERROR(1, "no slice for instance");
	goto bad;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(1, "You do not have permission to do this.");
	goto bad;
    }
    $reason = "Your extension was denied by the site administrator!\n";
    if (isset($ajax_args["message"]) && $ajax_args["message"] != "") {
        $reason .= "\n" . $ajax_args["message"];
    }

    $expires = date("Y-m-d H:i:s T", strtotime($slice->expires()));
    $created = date("Y-m-d H:i:s T", strtotime($instance->created()));
    $now     = date("Y-m-d H:i:s T", time());

    #
    # We store each extension request in an ongoing text field.
    #
    $text =
        "Date: $now\n".
        "Expires: $expires\n".
        "Reason:\n".
        $reason . "\n\n".
        "-----------------------------------------------\n";
    $instance->AddExtensionHistory($text);

    $instance->SendEmail($creator->email(),
	       "Experiment Extension Denied: $uuid",
               $reason .
               "\n\n".
               "Your experiment was started on $created\n".
 	       "Your experiment expires at $expires\n".
               "$APTBASE/status.php?uuid=$uuid\n",
	       "From: $EXTENSIONS\n" .
	       "BCC: $EXTENSIONS");

    $instance->SetExtensionRequested(0);
    SPITAJAX_RESPONSE("Success");
bad:
delay:
    sleep(1);
}

580
581
582
583
584
585
#
# Helper for above.
#
function needAdminApproval($wanted, $granted, $reason, $message)
{
    global $instance, $creator, $this_user, $suexec_output;
586
    global $EXTENSIONS, $APTBASE;
587
588
589
590
591
592
593
594

    $uuid = $instance->uuid();
    # Subtract out the extra time we added above.
    $howlong = $wanted - $granted;
    $url = "$APTBASE/status.php?uuid=$uuid&extend=$howlong";

    # Refresh. 
    $slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
Leigh B Stoller's avatar
Leigh B Stoller committed
595
    $new_expires = date("Y-m-d H:i:s T",
596
                        strtotime($slice->expires()) + ($howlong * 3600 * 24));
Leigh B Stoller's avatar
Leigh B Stoller committed
597
598
    $created = date("Y-m-d H:i:s T", strtotime($instance->created()));
    list($cluster) = Instance::ParseURN($instance->aggregate_urn());
599
		    
600
    $instance->SendEmail($EXTENSIONS,
601
602
603
        "Experiment Extension Request: $uuid",
        "A request to extend this experiment was made but requires\n".
        "administrator approval" .
Leigh B Stoller's avatar
Leigh B Stoller committed
604
        ($message ? " $message" : "") . ".\n\n" .
605
606
607
        "The request was for $wanted days, we granted $granted days, ".
        "the reason given is:\n\n".
        $reason . "\n\n".
608
        "This experiment was started on $created\n".
609
        "Granting the request would set the expiration to $new_expires\n".
Leigh B Stoller's avatar
Leigh B Stoller committed
610
        "It is running on $cluster\n".
611
        "\n\n". $url . "\n\n",
612
613
614
        "From: " . $creator->email());

    $instance->SetExtensionReason($reason);
615
    $instance->BumpExtensionCount($granted);
616
    $instance->SetExtensionRequested(1);
617
618
619
620
            
    # XXX 
    SPITAJAX_ERROR(2, "Your request requires admininstrator approval".
                   ($message ? " because $message" : "") . ". " .
Leigh B Stoller's avatar
Leigh B Stoller committed
621
622
                   "You will receive email if/when your ".
                   "request is granted (or denied). Thanks!");
623
    return;
624
}
625

626
627
628
629
630
#
# Request a console URL and pass back to the status page.
#
function Do_ConsoleURL()
{
631
    global $instance, $creator, $this_user, $suexec_output;
632
633
    global $ajax_args;

634
    if (StatusSetupAjax(0)) {
635
636
637
	return;
    }
    if (!isset($this_user)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
638
	SPITAJAX_ERROR(1, "Only registered users can view node consoles");
639
640
	return;
    }
641
    if (!isset($ajax_args["node"])) {
642
	SPITAJAX_ERROR(1, "Missing node argument");
643
644
645
646
647
648
649
650
651
	return 1;
    }
    $node = $ajax_args["node"];
    $uuid = $instance->uuid();
    $slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
    if (!slice) {
	SPITAJAX_ERROR(1, "no slice for instance");
	return 1;
    }
652
    $webtask_id = WebTask::GenerateID();
653
    $retval = SUEXEC("nobody", "nobody",
654
655
		     "webmanage_instance -t $webtask_id -- consoleurl $uuid " .
		     escapeshellarg($node),
656
		     SUEXEC_ACTION_IGNORE);
657
    $webtask = WebTask::Lookup($webtask_id);
658
659

    if ($retval == 0) {
Leigh B Stoller's avatar
Leigh B Stoller committed
660
661
662
663
664
665
        $taskdata = $webtask->TaskData();
        $blob = array();
        $blob["url"] = $taskdata["url"];
        if (isset($taskdata["password"])) {
            $blob["password"] = $taskdata["password"];
        }
666
667
668
        if (isset($taskdata["logurl"])) {
            $blob["logurl"] = $taskdata["logurl"];
        }
Leigh B Stoller's avatar
Leigh B Stoller committed
669
	SPITAJAX_RESPONSE($blob);
670
671
	$webtask->Delete();
	return;
672
    }
673
674
675
    elseif ($retval < 0) {
        SUEXECERROR(SUEXEC_ACTION_CONTINUE);
    }
676
    if ($webtask) {
Leigh B Stoller's avatar
Leigh B Stoller committed
677
	SPITAJAX_ERROR(1, $webtask->TaskValue("output"));
678
	$webtask->Delete();
679
    }
680
681
682
    elseif ($suexec_output != "") {
        SPITAJAX_ERROR(1, $suexec_output);
    }
683
684
685
686
687
    else {
	SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
    }
}

688
689
690
691
692
#
# Fire off a snapshot.
#
function Do_Snapshot()
{
693
    global $this_user, $instance, $suexec_output;
694
695
    global $ajax_args;

696
    if (StatusSetupAjax(1)) {
697
698
	return;
    }
699
700
    if (!isset($this_user)) {
	SPITAJAX_ERROR(1, "Only registered users can snapshot nodes");
701
702
	return;
    }
703
704
705
    $this_idx = $this_user->uid_idx();
    $uuid = $ajax_args["uuid"];
    
706
707
708
709
710
711
712
713
714
715
716
717
    if ($this_idx != $instance->creator_idx() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission. Maybe Clone instead?");
	return;
    }
    if ($instance->status() != "ready") {
	SPITAJAX_ERROR(1, "Experiment is currently busy");
	return;
    }
    #
    # The profile also has to belong to the user, since it is
    # going to be modified to use the new image.
    #
718
719
    $profile = Profile::Lookup($instance->profile_id(),
			       $instance->profile_version());
720
721
722
723
724
725
726
727
    if (!$profile) {
	SPITAJAX_ERROR(1, "Cannot lookup profile for instance");
	return;
    }
    if ($this_idx != $profile->creator_idx() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not your profile to change. Clone first!");
	return;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
728
    $optargs = "";
Leigh B Stoller's avatar
Leigh B Stoller committed
729
    if (isset($ajax_args["node_id"]) && $ajax_args["node_id"] != "") {
Leigh B Stoller's avatar
Leigh B Stoller committed
730
731
732
733
734
        $node_id = $ajax_args["node_id"];
        if (!TBvalid_vnode_id($node_id)) {
            SPITAJAX_ERROR(1, "Bad node id");
            return;
        }
735
        $optargs .= " -n $node_id ";
Leigh B Stoller's avatar
Leigh B Stoller committed
736
737
738
739
740
741

        if (isset($ajax_args["update_profile"]) &&
            $ajax_args["update_profile"]) {
            $optargs .= " -u all ";
        }
    }
742
743
744
745
746
747
    else {
        #
        # Single node profile snapshot, we always update the profile.
        #
        $optargs .= " -u node ";
    }
748
749
750
751
    if (isset($ajax_args["update_prepare"]) &&
        $ajax_args["update_prepare"]) {
        $optargs .= " -U ";
    }
752
753
754
755
    
    #
    # Call out to the backend.
    #
756
    $webtask_id = WebTask::GenerateID();
757
    $retval = SUEXEC($this_user->uid(), "nobody",
Leigh B Stoller's avatar
Leigh B Stoller committed
758
		     "webmanage_instance -t $webtask_id -- ".
759
                     "  snapshot $uuid $optargs",
760
		     SUEXEC_ACTION_IGNORE);
761
    $webtask = WebTask::Lookup($webtask_id);
762
763
764

    if ($retval != 0) {
	if ($retval < 0) {
765
	    SPITAJAX_ERROR(-11, "Internal error, cannot proceed.");
766
767
768
	    # Notify tbops.
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	}
769
	elseif ($webtask) {
Leigh B Stoller's avatar
Leigh B Stoller committed
770
	    SPITAJAX_ERROR(1, $webtask->TaskValue("output"));
771
	}
772
773
774
	elseif ($suexec_output != "") {
	    SPITAJAX_ERROR(1, $suexec_output);
        }
775
	else {
776
	    SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
777
	}
778
779
780
	if ($webtask) {
	    $webtask->Delete();
	}
781
	return;
782
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
783
    if ($webtask && $webtask->exited()) {
784
785
	$webtask->Delete();
    }
786
787
788
789
790
791
792
793
    SPITAJAX_RESPONSE("Success");
}

#
# Return snapshot status.
#
function Do_SnapshotStatus()
{
794
    global $this_user, $instance;
795
796
    global $ajax_args;

797
    if (StatusSetupAjax(1)) {
798
799
	return;
    }
800
801
    if (!isset($this_user)) {
	SPITAJAX_ERROR(1, "Only registered users can snapshot nodes");
802
803
	return;
    }
804
805
    $this_idx = $this_user->uid_idx();

806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
    if ($this_idx != $instance->creator_idx() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
    $webtask = WebTask::LookupByObject($instance->uuid());
    if (!$webtask) {
	SPITAJAX_ERROR(1, "No status descriptor found");
	return;
    }
    $taskdata = $webtask->TaskData();
    $blob = array();

    #
    # Size is in KB to avoid bigint problems. But kill the KB.
    # 
    if (isset($taskdata["image_size"])) {
	if (preg_match("/^(\d+)KB$/", $taskdata["image_size"], $matches)) {
	    $taskdata["image_size"] = $matches[1]; 
	}
	$blob["image_size"] = $taskdata["image_size"];
    }
    else {
	$blob["image_size"] = 0;
    }
    $blob["node_status"]  = $taskdata["rawstate"];
    $blob["image_status"] = $taskdata["image_status"];
832
833
834
835
    if ($webtask->exited()) {
	# Success, but not sure what to report. Come back to this later.
	$blob["exited"]   = $webtask->exited();
	$blob["exitcode"] = $webtask->exitcode();
836
837
838
        if (isset($taskdata["image_name"])) {
            $blob["image_name"] = $taskdata["image_name"];
        }
839
840
	$webtask->Delete();
    }
841
842
843
    SPITAJAX_RESPONSE($blob);
}

844
845
846
847
848
#
# Ask the backend to refresh sliverstatus
#
function Do_Refresh()
{
849
    global $this_user, $instance, $suexec_output;
850
851
    global $ajax_args;

852
853
854
855
    #
    # Guest users can do this.
    #
    if (StatusSetupAjax(1)) {
856
857
	return;
    }
858
    $this_idx = $this_user->uid_idx();
859
860
861
862
863
864
865
866
867
    $uuid = $ajax_args["uuid"];
    if ($this_idx != $instance->creator_idx() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission. Maybe Clone instead?");
	return;
    }
    #
    # Call out to the backend.
    #
    $webtask_id = WebTask::GenerateID();
868
    $retval = SUEXEC("nobody", "nobody",
869
870
871
872
873
874
875
876
877
878
879
880
881
		     "webmanage_instance -t $webtask_id -- refresh $uuid",
		     SUEXEC_ACTION_IGNORE);
    $webtask = WebTask::Lookup($webtask_id);

    if ($retval != 0) {
	if ($retval < 0) {
	    SPITAJAX_ERROR(-11, "Internal error, cannot proceed.");
	    # Notify tbops.
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	}
	elseif ($webtask) {
	    SPITAJAX_ERROR(1, $webtask->TaskValue("output"));
	}
882
883
884
	elseif ($suexec_output != "") {
	    SPITAJAX_ERROR(1, $suexec_output);
        }
885
886
887
888
889
890
891
892
	else {
	    SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
	}
	if ($webtask) {
	    $webtask->Delete();
	}
	return;
    }
893
    SPITAJAX_RESPONSE("Success");
894
895
896
}

#
897
# Ask the backend to reboot or reload a node.
898
#
899
function Do_RebootOrReload($which)
900
{
901
    global $this_user, $instance, $suexec_output;
902
903
    global $ajax_args;

904
905
906
    if (StatusSetupAjax(1)) {
	return;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
907
908
909
910
    if (!isset($this_user)) {
	SPITAJAX_ERROR(1, "Only registered users can reboot/reload nodes");
	return;
    }
911
    $this_idx = $this_user->uid_idx();
912
    $uuid = $ajax_args["uuid"];
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936

    if (!isset($ajax_args["node_id"])) {
	SPITAJAX_ERROR(1, "Missing node_id");
	return;
    }
    $node_id = $ajax_args["node_id"];
    if (!preg_match("/^[-\w]+$/", $node_id)) {
        SPITAJAX_ERROR(1, "Illegal characters in node_id");
    }
    if ($this_idx != $instance->creator_idx() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission. Maybe Clone instead?");
	return;
    }
    if ($instance->status() != "ready" && $instance->status() != "failed") {
	SPITAJAX_ERROR(1, "Experiment is currently busy");
	return;
    }

    #
    # Call out to the backend.
    #
    $webtask_id = WebTask::GenerateID();
    $retval = SUEXEC($this_user->uid(), "nobody",
		     "webmanage_instance -t $webtask_id -- ".
937
                     "  $which $uuid " . escapeshellarg($node_id), 
938
939
940
941
942
943
944
945
946
947
948
949
		     SUEXEC_ACTION_IGNORE);
    $webtask = WebTask::Lookup($webtask_id);

    if ($retval != 0) {
	if ($retval < 0) {
	    SPITAJAX_ERROR(-11, "Internal error, cannot proceed.");
	    # Notify tbops.
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
	}
	elseif ($webtask) {
	    SPITAJAX_ERROR(1, $webtask->TaskValue("output"));
	}
950
951
952
953
	elseif ($suexec_output != "") {
	    SPITAJAX_ERROR(1, $suexec_output);
        }
        else {
Leigh B Stoller's avatar
Leigh B Stoller committed
954
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
955
956
957
958
959
960
961
962
963
964
965
966
	    SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
	}
	if ($webtask) {
	    $webtask->Delete();
	}
	return;
    }
    if ($webtask) {
	$webtask->Delete();
    }
    SPITAJAX_RESPONSE("Success");
}
967
968
969
970
971
972
973
974
function Do_Reboot()
{
    Do_RebootOrReload("reboot");
}
function Do_Reload()
{
    Do_RebootOrReload("reload");
}
975

Leigh B Stoller's avatar
Leigh B Stoller committed
976
977
978
979
980
981
982
983
#
# Set or clear the lockout flag
#
function Do_Lockout()
{
    global $this_user;
    global $ajax_args;

984
985
986
987
    if (!isset($this_user) || !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission.");
	return;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
    $this_idx = $this_user->uid_idx();

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing profile uuid");
	return;
    }
    if (!isset($ajax_args["lockout"])) {
	SPITAJAX_ERROR(1, "Missing lockout value");
	return;
    }
    $uuid = $ajax_args["uuid"];
    $instance = Instance::Lookup($uuid);
    if (!$instance) {
	SPITAJAX_ERROR(1, "Unknown instance uuid");
	return;
    }
    $lockout = ($ajax_args["lockout"] ? 1 : 0);
    if (!DBQueryWarn("update apt_instances set extension_adminonly='$lockout' ".
                     "where uuid='$uuid'")) {
	SPITAJAX_ERROR(1, "Database failure.");
	return;
    }
    SPITAJAX_RESPONSE("Success");
}

1013
1014
1015
# Local Variables:
# mode:php
# End:
1016
?>