powder-shutdown.ajax 3.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
<?php
#
# Copyright (c) 2000-2019 University of Utah and the Flux Group.
# 
# {{{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("webtask.php");
chdir("apt");
include_once("profile_defs.php");
include_once("instance_defs.php");

#
# Need to check the permission, since we allow admins to mess with
# other accounts.
#
function CheckPageArgs()
{
    global $this_user;
    global $ajax_args;

    if (!(ISADMIN())) {
        $approved = 0;
        $project = Project::Lookup("PowderStop");
    
        if (!$project ||
            !$project->IsMember($this_user, $approved) || !$approved) {
            SPITAJAX_ERROR(-1, "Not enough permission");
            return -1;
        }
    }
    return 0;
}

function Do_StartShutdown()
{
    global $this_user, $urn_mapping;
    global $ajax_args;
    $results = array();

    if (CheckPageArgs()) {
        return;
    }
    $webtask = WebTask::CreateAnonymous();
    $retval = SUEXEC($this_user->uid(), "nobody",
63
		     "webpowder_shutdown -t " . $webtask->task_id() . " -b -d -n",
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		     SUEXEC_ACTION_CONTINUE);
    $webtask->Refresh();

    if ($retval != 0) {
        SPITAJAX_ERROR(-1, "Internal error starting emergency stop");
        $webtask->Delete();
	return;
    }
    $instances = $webtask->TaskValue("instanceList");
    if (count($instances)) {
        for($i = 0; $i < count($instances); $i++) {
            $uuid = $instances[$i];
            $instance = Instance::Lookup($uuid);
            if (!$instance) {
                continue;
            }
            $blob = array("name"    => $instance->name(),
                          "uuid"    => $instance->uuid(),
                          "creator" => $instance->creator(),
                          "created" => DateStringGMT($instance->created()),
                          "status"  => $instance->status(),
                          "paniced" => $instance->paniced() ? 1 : 0);

            $clusters = array();
            foreach ($instance->slivers() as $sliver) {
                $clusters[] = $sliver->aggregate_name();
            }
            $blob["clusters"] = join(",", $clusters);
            $results[] = $blob;
        }
    }
    $webtask->Delete();
    SPITAJAX_RESPONSE($results);    
}

function Do_ShutdownStatus()
{
    global $this_user, $urn_mapping;
    global $ajax_args;
    $results = array();

    if (CheckPageArgs()) {
        return;
    }
    if (!isset($ajax_args["instances"])) {
	SPITAJAX_ERROR(1, "Missing instance list");
	return;
    }
    $instances = $ajax_args["instances"];
    for($i = 0; $i < count($instances); $i++) {
        $uuid = $instances[$i];
        $instance = Instance::Lookup($uuid);
        if (!$instance) {
            continue;
        }
        $blob = array("uuid"    => $instance->uuid(),
                      "status"  => $instance->status(),
                      "paniced" => $instance->paniced() ? 1 : 1);
        $results[] = $blob;
    }
    SPITAJAX_RESPONSE($results);    
}

# Local Variables:
# mode:php
# End:
?>