dataset.ajax 29.9 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2018 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 25
# 
# {{{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("..");
include_once("lease_defs.php");
26
include_once("imageid_defs.php");
27
include_once("blockstore_defs.php");
28
include_once("node_defs.php");
29
include_once("webtask.php");
30 31
chdir("apt");
include_once("dataset_defs.php");
32
include_once("instance_defs.php");
33 34 35 36 37 38 39 40 41

#
# Server side of creating a dataset.
#
function Do_CreateDataSet()
{
    global $this_user;
    global $ajax_args;
    global $DBFieldErrstr, $TBDIR, $APTBASE, $embedded;
Leigh Stoller's avatar
Leigh Stoller committed
42
    global $suexec_output, $suexec_output_array;
43
    global $urn_mapping;
44 45 46 47 48

    $this_idx   = $this_user->uid_idx();
    $this_uid   = $this_user->uid();
    # Allow for form precheck only. So JS code knows it will be fast. 
    $checkonly  = isset($ajax_args["checkonly"]) && $ajax_args["checkonly"];
49
    $embedded   = isset($ajax_args["embedded"]) && $ajax_args["embedded"];
50 51 52 53 54 55 56

    if (!isset($ajax_args["formfields"])) {
	SPITAJAX_ERROR(1, "Missing formfields");
	return;
    }
    $formfields = $ajax_args["formfields"];
    $errors  = array();
57 58
    $options = "";
    
59
    $required = array("dataset_pid", "dataset_name", "dataset_type",
60
		      "dataset_fstype", "dataset_read", "dataset_modify");
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

    foreach ($required as $field) {
	if (!isset($formfields[$field]) || $formfields[$field] == "") {
	    $errors[$field] = "Missing field";
	}
    }
    if (count($errors)) {
	SPITAJAX_ERROR(2, $errors);
	return;
    }

    if (!TBvalid_pid($formfields["dataset_pid"])) {
	$errors["dataset_pid"] = $DBFieldErrstr;
    }
    $project = Project::LookupByPid($formfields["dataset_pid"]);
    if (!$project) {
	$errors["dataset_pid"] = "No such project";
    }
    elseif (!ISADMIN() &&
	    (!$project->IsMember($this_user, $isapproved) || !$isapproved)) {
	$errors["dataset_pid"] = "Not a project member";
    }
    $pid = $project->pid();
    
    if (!TBvalid_vnode_id($formfields["dataset_name"])) {
	$errors["dataset_name"] = $DBFieldErrstr;
    }
    if (! ($formfields["dataset_type"] == "stdataset" ||
89 90
	   $formfields["dataset_type"] == "ltdataset" ||
	   $formfields["dataset_type"] == "imdataset")) {
91 92 93
	$errors["dataset_type"] = "Illegal value";
    }
    else {
94
	$options .= " -t " . $formfields["dataset_type"];
95 96 97 98 99 100 101 102 103 104
    }
    if ($formfields["dataset_type"] == "stdataset") {
	if (!isset($formfields["dataset_expires"]) ||
	    $formfields["dataset_expires"] == "") {
	    $errors["dataset_expires"] = "Missing field";
	}
	elseif (!strtotime($formfields["dataset_expires"])) {
	    $errors["dataset_expires"] = "Illegal value";
	}
	else {
105
	    $options .= " -e " .
106 107 108
		escapeshellarg($formfields["dataset_expires"]);
	}
    }
109 110 111 112 113 114
    if ($formfields["dataset_type"] != "imdataset") {
        if (! preg_match('/^(none|ext2|ext3|ext4|ufs|ufs2)$/',
                         $formfields["dataset_fstype"])) {
            $errors["dataset_fstype"] = "Illegal value";
        }
        elseif ($formfields["dataset_fstype"] != "none") {
115
            $options .= " -f " . $formfields["dataset_fstype"];
116 117 118 119 120 121 122 123 124 125
        }
        if (! (isset($formfields["dataset_size"]) &&
               $formfields["dataset_size"] != "")) {
            $errors["dataset_size"] = "Missing field";
        }
        elseif (! preg_match('/^\d+(MB|GB|TB)$/i',
                         $formfields["dataset_size"])) {
            $errors["dataset_size"] = "Illegal value";
        }
        else {
126
            $options .= " -s " . $formfields["dataset_size"];
127
        }
Leigh Stoller's avatar
Leigh Stoller committed
128 129 130 131 132 133 134 135 136 137
        if (!$embedded) {
            if (! (isset($formfields["dataset_cluster"]) &&
                   $formfields["dataset_cluster"] != "")) {
                $errors["dataset_cluster"] = "Missing field";
            }
            elseif (!array_key_exists($formfields["dataset_cluster"],
                                      $urn_mapping)) {
                $errors["dataset_cluster"] = "Illegal value";
            }
            else {
138
                $options .= " -a '" . $formfields["dataset_cluster"] . "' ";
Leigh Stoller's avatar
Leigh Stoller committed
139
            }
140
        }
141 142
    }
    else {
143
        #
144
        # Not going to allow descriptor only image datasets, must
Leigh Stoller's avatar
Leigh Stoller committed
145
        # provide the node and bsname, and the instance if on the APT path.
146
        #
147 148 149
        if (! (isset($formfields["dataset_node"]) &&
            $formfields["dataset_node"] != "")) {
            $errors["dataset_node"] = "Must provide a node";
150
        }
151 152 153 154 155 156 157 158 159 160
        elseif (! (isset($formfields["dataset_bsname"]) &&
               $formfields["dataset_bsname"] != "")) {
            $errors["dataset_bsname"] = "Must provide bsname";
        }
        else {
            $nodeid = $formfields["dataset_node"];
            $bsname = $formfields["dataset_bsname"];

            if (!TBvalid_node_id($nodeid)) {
                $errors["dataset_node"] = TBFieldErrorString();
161
            }
162 163
            elseif (!TBvalid_vnode_id($bsname)) {
                $errors["dataset_bsname"] = TBFieldErrorString();
164
            }
165 166 167 168 169 170 171 172 173
            if (!count($errors)) {
                if ($embedded) {
                    if ($node = Node::Lookup($nodeid)) {
                        $reservation = $node->Reservation();
                        if (!$reservation ||
                            !$reservation->pid() == $pid) {
                            $errors["dataset_node"] =
                                "Node not reserved to an experiment in the ".
                            "same project as your dataset";
174
                        }
175 176 177 178 179 180 181 182 183 184 185
                        else {
                            $blockstore =
                                $reservation->LookupBlockstore($bsname);
                            if (!$blockstore) {
                                $errors["dataset_bsname"] =
                                    "No such blockstore";
                            }
                            elseif ($blockstore['fixed'] != $node->VirtName()) {
                                $errors["dataset_bsname"] =
                                    "Blockstore does not exist on node $nodeid";
                            }
186 187
                        }
                    }
188 189 190
                    else {
                        $errors["dataset_node"] = "No such node";
                    }
191 192
                }
                else {
193 194 195 196 197 198 199 200 201 202
                    if (! (isset($formfields["dataset_instance"]) &&
                           $formfields["dataset_instance"] != "")) {
                        $errors["dataset_instance"] = "Must provide instance";
                    }
                    elseif (!preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/",
                                        $formfields["dataset_instance"])) {
                        $errors["dataset_instance"] = "Invalid instance uuid";
                    }
                    elseif ($instance =
                            Instance::Lookup($formfields["dataset_instance"])){
203
                        $options .= " -i " . $instance->uuid();
204 205 206 207
                    }
                    else {
                        $errors["dataset_instance"] = "No such instance";
                    }
208
                }
209
                $options .= " -I ${nodeid},${bsname}";
210 211
            }
        }
212
    }
213
    # Permission bits.
214 215 216 217 218
    if ($formfields["dataset_read"] != "global" &&
        $formfields["dataset_read"] != "project") {
        $errors["dataset_read"] = "Illegal value";
    }
    else {
219
        $options .= " -R " . $formfields["dataset_read"];
220 221 222 223 224 225
    }
    if ($formfields["dataset_modify"] != "creator" &&
        $formfields["dataset_modify"] != "project") {
        $errors["dataset_modify"] = "Illegal value";
    }
    else {
226
        $options .= " -W " . $formfields["dataset_modify"];
227
    }
228 229 230 231 232 233 234 235
    if (count($errors)) {
	SPITAJAX_ERROR(2, $errors);
	return;
    }
    if ($checkonly) {
	SPITAJAX_RESPONSE(0);
	return;
    }
236
    $options .= " " . escapeshellarg($pid . "/" .
237 238
				     $formfields["dataset_name"]);

239 240 241 242 243 244 245 246 247 248 249 250
    # Need a webtask for policy messages.
    $webtask = WebTask::CreateAnonymous();
    
    if ($embedded) {
        $wopt    = "-C -b -T " . $webtask->task_id();
	$command = "webcreatedataset $wopt $options";
    }
    else {
        $wopt     = "-t " . $webtask->task_id();
	$command  = "webmanage_dataset $wopt -- create $options";
    }
    
251
    $retval = SUEXEC($this_uid, $project, $command, SUEXEC_ACTION_IGNORE);
252
    if ($retval) {
253 254 255 256 257 258 259 260
        $webtask->Refresh();
        if (!$webtask->exited() || $retval < 0) {
            SUEXECERROR(SUEXEC_ACTION_CONTINUE);
            SPITAJAX_ERROR(-1, "Internal error creating dataset");
        }
        else {
            SPITAJAX_ERROR(1, "Cannot create dataset: " . $webtask->output());
        }
261
        $webtask->Delete();
262
        return;
263 264
    }
    if ($embedded) {
265 266 267 268 269 270 271 272
        if ($formfields["dataset_type"] == "imdataset") {
            $dataset = ImageDataset::LookupByName($project,
                                                  $formfields["dataset_name"]);
        }
        else {
            $dataset = Lease::LookupByName($project,
                                           $formfields["dataset_name"]);
        }
273 274 275 276 277
    }
    else {
	$dataset = Dataset::LookupByName($project, $formfields["dataset_name"]);
    }
    if (!$dataset) {
278
        $webtask->Delete();
279
	SPITAJAX_ERROR(-1, "Could not find dataset after creation");
280 281 282
	return 1;
    }
    $dataset_uuid = $dataset->uuid();
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    $blob = array("dataset_uuid" => $dataset_uuid, "needapproval" => 0);
    
    #
    # If the dataset was not approved, we want to notify the user instead
    # of zapping over to the show dataset page, since we are going to lose
    # the reason. At some point we probably want to store this with the
    # dataset (or local lease).
    #
    $webtask->Refresh();
    if ($webtask->TaskValue("needapproval")) {
        $blob["needapproval"] = 1;
        if ($webtask->TaskValue("unapproved_reason")) {
            $blob["unapproved_reason"] =
                $webtask->TaskValue("unapproved_reason");
        }
    }
299
    $webtask->Delete();
300
    SPITAJAX_RESPONSE($blob);
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 329 330 331 332 333 334 335 336
}

#
# Server side of modifying a dataset.
#
function Do_ModifyDataSet()
{
    global $this_user;
    global $ajax_args;
    global $DBFieldErrstr, $TBDIR, $APTBASE, $embedded;
    global $suexec_output, $suexec_output_array;

    $this_idx   = $this_user->uid_idx();
    $this_uid   = $this_user->uid();
    # Allow for form precheck only. So JS code knows it will be fast. 
    $checkonly  = isset($ajax_args["checkonly"]) && $ajax_args["checkonly"];
    $embedded   = isset($ajax_args["embedded"]) && $ajax_args["embedded"];

    if (!isset($ajax_args["formfields"])) {
	SPITAJAX_ERROR(1, "Missing formfields");
	return;
    }
    $formfields = $ajax_args["formfields"];
    $errors     = array();

    if (!isset($formfields["dataset_uuid"])) {
	SPITAJAX_ERROR(1, "Missing dataset uuid");
	return 1;
    }
    $dataset_uuid = $formfields["dataset_uuid"];

    #
    # Either a local lease or a remote dataset. 
    #
    $dataset = Lease::Lookup($dataset_uuid);
    if (!$dataset) {
337 338 339 340
	$dataset = ImageDataset::Lookup($dataset_uuid);
	if (!$dataset) {
            $dataset = Dataset::Lookup($dataset_uuid);
        }
341 342 343 344 345
    }
    if (!$dataset) {
	SPITAJAX_ERROR(1, "Unknown dataset");
	return;
    }
346 347
    $project = $dataset->Project();
    
348 349 350 351
    if ($this_uid != $dataset->owner_uid() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
352 353 354 355
    if (isset($formfields["dataset_read"])) {
	if ($formfields["dataset_read"] != "global" &&
	    $formfields["dataset_read"] != "project") {
	    $errors["dataset_read"] = "Illegal value";
356 357
	}
    }
358 359 360 361
    if (isset($formfields["dataset_modify"])) {
	if ($formfields["dataset_modify"] != "creator" &&
	    $formfields["dataset_modify"] != "project") {
	    $errors["dataset_modify"] = "Illegal value";
362 363
	}
    }
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    $nodeid = null;
    if ($dataset->type() == "imdataset") {
        #
        # Node and BS name are optional.
        #
        if ((isset($formfields["dataset_node"]) &&
             $formfields["dataset_node"] != "") ||
            (isset($formfields["dataset_bsname"]) &&
             $formfields["dataset_bsname"] != "")) {
            if (! (isset($formfields["dataset_node"]) &&
                   $formfields["dataset_node"] != "")) {
                $errors["dataset_node"] = "Must also provide a node";
            }
            elseif (! (isset($formfields["dataset_bsname"]) &&
                   $formfields["dataset_bsname"] != "")) {
                $errors["dataset_bsname"] = "Must also provide bsname";
            }
            else {
                $nodeid = $formfields["dataset_node"];
                $bsname = $formfields["dataset_bsname"];

                if (!TBvalid_node_id($nodeid)) {
                    $errors["dataset_node"] = TBFieldErrorString();
                }
                elseif (!TBvalid_vnode_id($bsname)) {
                    $errors["dataset_bsname"] = TBFieldErrorString();
                }
391 392
            }
            if (!count($errors)) {
393
                if ($embedded || $dataset->islocal()) {
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
                    if ($node = Node::Lookup($nodeid)) {
                        $reservation = $node->Reservation();
                        if (!$reservation ||
                            $reservation->pid() != $dataset->pid()) {
                            $errors["dataset_node"] =
                                "Node not reserved to an experiment in the ".
                                "same project as your dataset";
                        }
                        else {
                            $blockstore =
                                $reservation->LookupBlockstore($bsname);
                            if (!$blockstore ||
                                $blockstore['fixed'] != $node->VirtName()) {
                                $errors["dataset_bsname"] =
                                    "Blockstore does not exist on node $nodeid";
                            }
                        }
411 412
                    }
                    else {
413
                        $errors["dataset_node"] = "No such node";
414 415 416
                    }
                }
                else {
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
                    if (! (isset($formfields["dataset_instance"]) &&
                           $formfields["dataset_instance"] != "")) {
                        $errors["dataset_instance"] = "Must provide instance";
                    }
                    elseif (!preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/",
                                        $formfields["dataset_instance"])) {
                        $errors["dataset_instance"] = "Invalid instance uuid";
                    }
                    elseif ($instance =
                            Instance::Lookup($formfields["dataset_instance"])){
                        $instance_uuid = $instance->uuid();
                    }
                    else {
                        $errors["dataset_instance"] = "No such instance";
                    }
432 433 434 435
                }
            }
        }
    }
436 437 438 439 440 441 442 443
    if (count($errors)) {
	SPITAJAX_ERROR(2, $errors);
	return;
    }
    if ($checkonly) {
	SPITAJAX_RESPONSE(0);
	return;
    }
444
    $leaseid = $dataset->pid() . "/" . $dataset->id();
445 446
    $opt     = "";

447
    if (isset($formfields["dataset_read"])) {
448
        $perm = $formfields["dataset_read"];
449
        if ($perm != $dataset->read_access()) {
450 451 452 453 454 455 456 457 458 459 460 461
            $opt .= "-R $perm ";
        }
    }
    if (isset($formfields["dataset_modify"])) {
        $perm = $formfields["dataset_modify"];
        if ($perm != $dataset->write_access()) {
            $opt .= "-W $perm ";
        }
    }
    if ($opt != "") {
        if ($dataset->islocal()) {
            $command = $dataset->grantCommand() . " ";
462
            $retval = SUEXEC($this_uid, $project,
463
                             "$command $opt $leaseid",
464 465 466 467 468
                             SUEXEC_ACTION_CONTINUE);
            if ($retval) {
                SPITAJAX_ERROR(1, $suexec_output);
                return;
            }
469
        }
470 471 472
        else {
            $webtask = WebTask::CreateAnonymous();
            $command = $dataset->grantCommand($webtask);
473
            $retval = SUEXEC($this_uid, $project,
474 475
                             "$command $opt $leaseid",
                             SUEXEC_ACTION_IGNORE);
476
            if ($retval) {
477 478 479 480 481 482 483 484 485 486 487
                $webtask->Refresh();
                if (!$webtask->exited() || $retval < 0) {
                    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
                    SPITAJAX_ERROR(-1, "Internal error modifying ".
                                   "dataset permissions");
                }
                else {
                    SPITAJAX_ERROR(1, "Cannot modify dataset permissions: " .
                                   $webtask->output());
                }
                $webtask->Delete();
488 489
                return;
            }
490
            $webtask->Delete();
491
        }
492
    }
493 494 495 496 497
    if ($dataset->type() == "imdataset" && $nodeid) {
        $safe_nodeid = escapeshellarg($nodeid);
        $safe_bsname = escapeshellarg($bsname);
        $dname       = $dataset->id();
        $pid         = $dataset->pid();
498

499
        if ($embedded || $dataset->islocal()) {
500
            $retval = SUEXEC($this_uid, $project,
501
                             "webclone_image -b $safe_bsname ".
Leigh Stoller's avatar
Leigh Stoller committed
502
                             "$dname $safe_nodeid",
503
                             SUEXEC_ACTION_IGNORE);
504 505 506 507 508 509 510
            if ($retval) {
                if ($retval < 1) {
                    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
                }
                SPITAJAX_ERROR(1, $suexec_output);
                return;
            }
511 512
        }
        else {
513
            $webtask = WebTask::CreateAnonymous();
514
            $retval = SUEXEC($this_uid, $project,
515 516
                             "webmanage_dataset -t ". $webtask->task_id() .
                             " snapshot -i $instance_uuid ".
517
                             "   -b $safe_bsname $pid/$dname $safe_nodeid",
518
                             SUEXEC_ACTION_IGNORE);
519 520 521 522 523 524 525 526 527 528 529 530 531
            if ($retval) {
                $webtask->Refresh();
                if (!$webtask->exited() || $retval < 0) {
                    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
                    SPITAJAX_ERROR(-1,
                                   "Internal error taking dataset snapshot");
                }
                else {
                    SPITAJAX_ERROR(1, "Cannot snapshot dataset: " .
                                   $webtask->output());
                }
                $webtask->Delete();
                return;
532
            }
533
            $webtask->Delete();
534 535
        }
    }
536
    SPITAJAX_RESPONSE("show-dataset.php?uuid=$dataset_uuid");
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
}

function Do_DeleteDataset()
{
    global $this_user;
    global $ajax_args;
    global $suexec_output, $suexec_output_array, $APTBASE;
    
    $this_idx = $this_user->uid_idx();
    $this_uid = $this_user->uid();

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing uuid");
	return;
    }
    #
    # Either a local lease or a remote dataset. 
    #
    $dataset = Lease::Lookup($ajax_args["uuid"]);
    if (!$dataset) {
557 558 559 560 561
	$dataset = ImageDataset::Lookup($ajax_args["uuid"]);
	if (!$dataset) {
            $dataset = Dataset::Lookup($ajax_args["uuid"]);
            $islease = 0;
        }
562 563 564 565 566
    }
    if (!$dataset) {
	SPITAJAX_ERROR(1, "Unknown dataset");
	return;
    }
567 568
    $project = $dataset->Project();
    
569 570 571 572
    if ($this_uid != $dataset->owner_uid() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
573

574 575 576
    #
    # Invoke backend.
    #
577 578
    if ($dataset->islocal()) {
        $command = $dataset->deleteCommand();
579
        $retval = SUEXEC($this_uid, $project, $command,
580 581 582 583 584 585
                         SUEXEC_ACTION_IGNORE);
        if ($retval != 0) {
            if ($retval < 0) {
                SUEXECERROR(SUEXEC_ACTION_CONTINUE);
            }
            $error = "Transient error; please try again later";
586
	
587 588 589 590 591 592 593 594 595 596
            if ($retval && count($suexec_output_array)) {
                $error = $suexec_output;
            }
            SPITAJAX_ERROR(1, $error);
            return;
        }
    }
    else {
        $webtask = WebTask::CreateAnonymous();
        $command = $dataset->deleteCommand($webtask);
597
        $retval = SUEXEC($this_uid, $project, $command,
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
                         SUEXEC_ACTION_IGNORE);
        if ($retval) {
            $webtask->Refresh();
            if (!$webtask->exited() || $retval < 0) {
                SUEXECERROR(SUEXEC_ACTION_CONTINUE);
                SPITAJAX_ERROR(-1, "Internal error deleting dataset");
            }
            else {
                SPITAJAX_ERROR(1, "Cannot delete dataset: " .
                               $webtask->output());
            }
            $webtask->Delete();
            return;
        }
        $webtask->Delete();
613
    }
614
    SPITAJAX_RESPONSE("list-datasets.php");
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
}

function Do_ApproveDataset()
{
    global $this_user;
    global $ajax_args;
    global $suexec_output, $suexec_output_array, $APTBASE;
    
    $this_idx = $this_user->uid_idx();
    $this_uid = $this_user->uid();

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing uuid");
	return;
    }
630 631 632 633 634 635 636 637 638
    $dataset_uuid = $ajax_args["uuid"];
    #
    # Either a local lease or a remote dataset. 
    #
    $dataset = Lease::Lookup($dataset_uuid);
    if (!$dataset) {
        $dataset = Dataset::Lookup($dataset_uuid);
    }
    if (!$dataset) {
639 640 641 642 643 644 645
	SPITAJAX_ERROR(1, "Unknown dataset");
	return;
    }
    if (!ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
646
    if ($dataset->state() != "unapproved") {
647 648 649
	SPITAJAX_ERROR(1, "Dataset does not need to be approved.");
	return;
    }
650
    if ($dataset->islocal()) {
651
        $retval = SUEXEC($this_uid, $dataset->Project(),
652 653 654 655 656 657
                         "webapprovelease " .
                         $dataset->pid() . "/" .
                         $dataset->gid() . "/" . $dataset->lease_id(),
                         SUEXEC_ACTION_CONTINUE);
        if ($retval != 0) {
            $error = "Transient error; please try again later";
658
	
659 660 661 662 663 664
            if ($retval && count($suexec_output_array)) {
                $error = $suexec_output;
            }
            SPITAJAX_ERROR(1, $error);
            return;
        }
665
    }
666
    else {
667 668
        $webtask = WebTask::CreateAnonymous();
    
669
        $retval = SUEXEC($this_uid, $dataset->Project(),
670 671
                         "webmanage_dataset -t ". $webtask->task_id() .
                         " approve " . $dataset->pid() . "/" . $dataset->id(),
672
                         SUEXEC_ACTION_IGNORE);
673 674 675
        if ($retval) {
            $webtask->Refresh();
            if (!$webtask->exited() || $retval < 0) {
676
                SUEXECERROR(SUEXEC_ACTION_CONTINUE);
677
                SPITAJAX_ERROR(-1, "Internal error approving dataset");
678
            }
679 680 681
            else {
                SPITAJAX_ERROR(1, "Cannot approve dataset: " .
                               $webtask->output());
682
            }
683
            $webtask->Delete();
684 685
            return;
        }
686
        $webtask->Delete();
687 688
    }
    SPITAJAX_RESPONSE("show-dataset.php?uuid=$dataset_uuid");
689 690
}

Leigh Stoller's avatar
Leigh Stoller committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
function Do_RefreshDataset()
{
    global $this_user;
    global $ajax_args;
    global $suexec_output, $suexec_output_array, $APTBASE;
    
    $this_idx = $this_user->uid_idx();
    $this_uid = $this_user->uid();

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing uuid");
	return;
    }
    $uuid    = $ajax_args["uuid"];
    $dataset = Dataset::Lookup($uuid);
    if (!$dataset) {
	SPITAJAX_ERROR(1, "Unknown dataset");
	return;
    }
    if ($this_uid != $dataset->owner_uid() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
    #
    # Invoke backend.
    #
717 718
    $webtask  = WebTask::CreateAnonymous();
    
719
    $retval = SUEXEC($this_uid, $dataset->Project(),
720 721
		     "webmanage_dataset -t ". $webtask->task_id() . 
                     " refresh " . $dataset->pid() . "/" . $dataset->id(),
722
		     SUEXEC_ACTION_IGNORE);
723 724 725
    if ($retval) {
        $webtask->Refresh();
        if (!$webtask->exited() || $retval < 0) {
726
            SUEXECERROR(SUEXEC_ACTION_CONTINUE);
727
            SPITAJAX_ERROR(-1, "Internal error refreshing dataset");
728
        }
729 730 731 732 733 734
        else {
            SPITAJAX_ERROR(1, "Cannot refresh dataset info: " .
                           $webtask->output());
        }
        $webtask->Delete();
        return;
Leigh Stoller's avatar
Leigh Stoller committed
735
    }
736
    $webtask->Delete();
Leigh Stoller's avatar
Leigh Stoller committed
737 738 739
    SPITAJAX_RESPONSE(0);
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
function Do_ExtendDataset()
{
    global $this_user;
    global $ajax_args;
    global $suexec_output, $suexec_output_array, $APTBASE;
    
    $this_idx = $this_user->uid_idx();
    $this_uid = $this_user->uid();
    $islease  = 1;

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing uuid");
	return;
    }
    #
    # Either a local lease or a remote dataset. 
    #
    $dataset = Lease::Lookup($ajax_args["uuid"]);
    if (!$dataset) {
	$dataset = Dataset::Lookup($ajax_args["uuid"]);
	$islease = 0;
    }
    if (!$dataset) {
	SPITAJAX_ERROR(1, "Unknown dataset");
	return;
    }
    if ($this_uid != $dataset->owner_uid() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
770 771 772 773 774

    #
    # For Portal datasets, we use a webtask. The native CLI does not
    # to webtasks.
    #
775
    if ($islease) {
776 777 778
	$command  = "webextendlease ";
        $command .= $dataset->pid() . "/" . $dataset->id();
        
779
        $retval = SUEXEC($this_uid, $dataset->Project(), $command,
780 781 782 783 784 785 786 787 788 789 790 791 792
                         SUEXEC_ACTION_IGNORE);
        if ($retval != 0) {
            if ($retval < 0) {
                SUEXECERROR(SUEXEC_ACTION_CONTINUE);
            }
            $error = "Transient error; please try again later";
	
            if ($retval && count($suexec_output_array)) {
                $error = $suexec_output_array;
            }
            SPITAJAX_ERROR(1, $error);
            return;
        }
793 794
    }
    else {
795
        $webtask  = WebTask::CreateAnonymous();
796
        $retval = SUEXEC($this_uid, $dataset->Project(),
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
                         "webmanage_dataset -t ". $webtask->task_id() .
                         " extend ". $dataset->pid() . "/" . $dataset->id(),
                         SUEXEC_ACTION_IGNORE);
        if ($retval) {
            $webtask->Refresh();
            if (!$webtask->exited() || $retval < 0) {
                SUEXECERROR(SUEXEC_ACTION_CONTINUE);
                SPITAJAX_ERROR(-1, "Internal error extending dataset");
            }
            else {
                SPITAJAX_ERROR(1, "Cannot extend dataset: " .
                               $webtask->output());
            }
            $webtask->Delete();
            return;
812
        }
813
        $webtask->Delete();
814 815 816 817 818
    }
    $dataset_uuid = $dataset->uuid();
    SPITAJAX_RESPONSE("show-dataset.php?uuid=$dataset_uuid");
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
function Do_GetInfo()
{
    global $this_user;
    global $ajax_args;
    
    $this_idx = $this_user->uid_idx();
    $this_uid = $this_user->uid();
    $islease  = 1;

    if (!isset($ajax_args["uuid"])) {
	SPITAJAX_ERROR(1, "Missing uuid");
	return;
    }
    #
    # Either a local lease or a remote dataset. 
    #
    $dataset = Lease::Lookup($ajax_args["uuid"]);
    if (!$dataset) {
	$dataset = Dataset::Lookup($ajax_args["uuid"]);
	$islease = 0;
    }
    if (!$dataset) {
	SPITAJAX_ERROR(1, "Unknown dataset");
	return;
    }
    if ($this_uid != $dataset->owner_uid() && !ISADMIN()) {
	SPITAJAX_ERROR(1, "Not enough permission");
	return;
    }
    $blob = array();
    if ($dataset->state() == "busy" ||
        ($dataset->state() == "unapproved" && $dataset->locked())) {
        $blob["state"] = "allocating";
    }
    else {
        $blob["state"] = $dataset->state();
    }
856
    $blob["size"] = $dataset->size() ? $dataset->size() : 0;
857

858 859 860 861 862 863
    # A local lease via Classic interface (embedded).
    if ($islease) {
        SPITAJAX_RESPONSE($blob);
        return;
    }

864
    #
865
    # If we were generating an image. 
866
    #
867 868 869 870 871
    $webtask = $dataset->WebTask();
    if (!$webtask) {
	SPITAJAX_ERROR(-1, "Internal error getting webtask");
	return;
    }
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    if ($webtask) {
        $taskdata = $webtask->TaskData();
        #
        # 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["image_status"] = $taskdata["image_status"];
888 889 890
        if (isset($taskdata["copyback_uuid"])) {
            $blob["copyback_uuid"] = $taskdata["copyback_uuid"];
        }
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

        #
        # Lets put the node status in too. The backend has helpfully told us
        # the aggregate and node to track down the status.
        #
        if (isset($taskdata["instance"]) &&
            isset($taskdata["aggregate_urn"]) &&
            isset($taskdata["client_id"])) {
            $instance = Instance::Lookup($taskdata["instance"]);
            if (!$instance) {
                SPITAJAX_ERROR(-1, "Instance is gone");
                return;
            }
            $sliver = InstanceSliver::Lookup($instance,
                                             $taskdata["aggregate_urn"]);
            if (!$sliver) {
                SPITAJAX_ERROR(-1, "Sliver is gone");
                return;
            }
910 911 912 913 914
            foreach ($sliver->StatusArray() as $status) {
                if (isset($status["sliver_data"]) &&
                    $status["client_id"] == $taskdata["client_id"]) {
                    $blob["node_status"] = $status["sliver_details"]["rawstate"];
                    break;
915 916 917
                }
            }
        }
918
    }
919 920 921
    SPITAJAX_RESPONSE($blob);
}

922 923 924 925
# Local Variables:
# mode:php
# End:
?>