experiments.ajax 11.2 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
# 
# {{{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 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
chdir("apt");
include_once("profile_defs.php");
include_once("instance_defs.php");
include_once("ajax-routines.ajax");

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

    if (!(ISADMIN() || ISFOREIGN_ADMIN())) {
	SPITAJAX_ERROR(-1, "Not enough permission");
	return -1;
    }
    return 0;
}

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

    if (CheckPageArgs()) {
        return;
    }

    $query_result = 
        DBQueryFatal("select a.*,s.expires,s.hrn,u.email,users.usr_name, ".
                     " (UNIX_TIMESTAMP(now()) > ".
                     "  UNIX_TIMESTAMP(s.expires)) as expired, ".
                     "  truncate(a.physnode_count * ".
                     "   ((UNIX_TIMESTAMP(now()) - ".
63
                     "     UNIX_TIMESTAMP(a.started)) / 3600.0),2) as phours, ".
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                     " IFNULL(aggs.count,0) as aggrows, ".
                     " agg.aggregate_urn as aggrow_urn ".
                     "  from apt_instances as a ".
                     "left join (".
                     "    select uuid, COUNT(*) AS count ".
                     "    from apt_instance_aggregates group by uuid) AS aggs ".
                     "  on aggs.uuid=a.uuid ".
                     "left join apt_instance_aggregates as agg ".
                     "     on agg.uuid=a.uuid ".
                     "left join geni.geni_slices as s on ".
                     "     s.uuid=a.slice_uuid ".
                     "left join geni.geni_users as u on u.uuid=a.creator_uuid ".
                     "left join users on users.uid_idx=a.creator_idx");

    while ($row = mysql_fetch_array($query_result)) {
        $profile_id   = $row["profile_id"];
        $version      = $row["profile_version"];
        $uuid         = $row["uuid"];
        $name         = $row["name"];
        $status       = $row["status"];
        $canceled     = $row["canceled"];
85
        $created      = DateStringGMT($row["started"]);
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 131 132 133 134 135 136 137 138 139 140 141 142
        $expires      = DateStringGMT($row["expires"]);
        $creator_idx  = $row["creator_idx"];
        $profile_name = "$profile_id:$version";
        $creator_uid  = $row["creator"];
        $pid          = $row["pid"];
        $pcount       = $row["physnode_count"];
        $vcount       = $row["virtnode_count"];
        $user_lockdown= $row["user_lockdown"];
        $admin_lockdown= $row["admin_lockdown"];
        $lockdown     = ($user_lockdown || $admin_lockdown ? 1 : 0);
        $phours       = $row["phours"];
        $email        = $row["email"];
        $extending    = $row["extension_requested"];
        $blob         = array();

        # If a guest user, use email instead.
        if (isset($email)) {
            $blob["creator"] = $email;
        }
        else {
            $blob["creator"] =
                "<a href='user-dashboard.php?user=$creator_uid'>".
                "$creator_uid</a>";
        }
        if ($row["expired"]) {
            $status = "expired";
        }
        elseif ($canceled) {
            $status = "canceled";
        }
        $blob["status"] = $status;
        
        $profile = Profile::Lookup($profile_id, $version);
        if ($profile) {
            $blob["profile_name"] = $profile->name();
            $blob["profile_uuid"] = $profile->uuid();
        }
        else {
            $blob["profile_name"] = $profile_name;
        }
        
        #
        # If arows non-zero, then we use that for aggregate_urn,
        # and if its more then 1, we need to consume the extras rows
        # to get the rest of the aggregate urns.
        #
        if ($row["aggrows"] > 0) {
            $cluster = $urn_mapping[$row["aggrow_urn"]];

            for ($i = 1; $i < $row["aggrows"]; $i++) {
                $row = mysql_fetch_array($query_result);
                $cluster .= "," . $urn_mapping[$row["aggrow_urn"]];
            }
        }
        else {
            $cluster = $urn_mapping[$row["aggregate_urn"]];
        }
143 144 145 146 147 148 149 150 151
        $namefrag = "<a href='adminextend.php?uuid=$uuid'>$name</a>
               <a href='status.php?uuid=$uuid' target=_blank>
	       <span class='pull-right glyphicon glyphicon-eye-open'>
               </span></a>";
        $pidfrag = "<a href='show-project.php?project=$pid'>$pid</a>";

        $blob["uuid"]           = $uuid;
        $blob["name"]           = $namefrag;
        $blob["project"]        = $pidfrag;
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        $blob["cluster"]        = $cluster;
        $blob["pcount"]         = $pcount;
        $blob["phours"]         = $phours;
        $blob["vcount"]         = $vcount;
        $blob["created"]        = $created;
        $blob["expires"]        = $expires;
        $blob["user_lockdown"]  = ($user_lockdown ? 1 : 0);
        $blob["admin_lockdown"] = ($admin_lockdown ? 1 : 0);
        $blob["lockdown"]       = $lockdown;
        $blob["extension_requested"] = $extending;

        #
        # For admins, we includes a filters field.
        #
        $filters = array();
        if ($lockdown) {
            $filters[] = "locked";
        }
        if ($status == "expired") {
            $filters[] = "expired";
        }
        if ($extending) {
            $filters[] = "extending";
        }
        if (time() - strtotime($created) > (45 * 24 * 3600)) {
            $filters[] = "old";
        }
        $blob["filter"] = implode(",", $filters);
        
        $results["$pid:$name"] = $blob;
    }
    SPITAJAX_RESPONSE($results);    
}

186 187 188 189 190 191 192 193 194 195 196 197
function Do_ExperimentErrors()
{
    global $this_user, $urn_mapping, $geni_response_codes;
    global $ajax_args;
    $clause  = "";
    $results = array();

    if (CheckPageArgs()) {
        return;
    }
    if (isset($ajax_args["stamp"]) && TBvalid_integer($ajax_args["stamp"])) {
        $stamp = $ajax_args["stamp"];
198
        $clause = "where UNIX_TIMESTAMP(f.started) <= $stamp";
199 200
    }
    $query_result =
201
        DBQueryFatal("select f.name,exitcode,f.started,f.public_url,".
202
	             "  IF(i.uuid,i.uuid,f.uuid) as uuid, ".
203 204 205 206 207 208 209 210 211 212 213 214 215
                     "  IF(i.uuid,1,0) as active,f.exitmessage, ".
                     "  p.uuid as profile_uuid,p.name as profile_name, ".
                     "  IF(i.uuid,i.aggregate_urn,h.aggregate_urn) ".
                     "    as aggregate_urn, ".
                     "  f.slice_uuid,f.logfileid,f.creator ".
                     "from apt_instance_failures as f ".
                     "left join apt_instances as i on ".
                     "     i.uuid=f.uuid ".
                     "left join apt_instance_history as h on ".
                     "     h.uuid=f.uuid ".
                     "left join apt_profiles as p on ".
                     "     p.profileid=f.profile_id ".
                     $clause . " " .
216
                     "order by f.started desc limit 25");
217 218 219 220 221 222 223

    while ($row = mysql_fetch_array($query_result)) {
        $name       = $row["name"];
        $exitcode   = $row["exitcode"];
        $url        = $row["public_url"];
        $logfileid  = $row["logfileid"];
        $uuid       = $row["uuid"];
224
        $active     = $row["active"] ? 1 : 0;
225
        $created    = DateStringGMT($row["started"]);
226 227 228 229 230 231 232
        $creator    = $row["creator"];
        $message    = $row["exitmessage"];
        $reason     = $exitcode;
        $slice_uuid = $row["slice_uuid"];
        $profile_uuid = $row["profile_uuid"];
        $profile_name = $row["profile_name"];
        $aggregate_urn= $row["aggregate_urn"];
233
        $cluster      = null;
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

        if ($exitcode >= 0 && $exitcode <= count($geni_response_codes)) {
            $reason = $geni_response_codes[$exitcode];
        }
        elseif ($exitcode == GENIRESPONSE_STITCHER_ERROR) {
            $reason = "Stitcher Failed";
        }
        if ($aggregate_urn && $aggregate_urn != "") {
            $cluster = $urn_mapping[$aggregate_urn];
        }
        #
        # Ick. If the url is for the showslicepub link, we have a problem;
        # the link is good only while the slice is alive. After that, we have
        # to use a private url (which is okay, cause only admins are going
        # to see it and be able to use it).
        #
        if (ISFOREIGN_ADMIN()) {
            $url = null;
        }
        elseif (!$url) {
            #
            # If there is a log file, show that instead.
            #
            if ($logfileid) {
                $url = $TBBASE . "/" .
                    CreateURL("spewlogfile", "logfile", $logfileid);
            }
        }
        elseif (preg_match("/^(https:.*)\/showslicepub/", $url, $matches)) {
            if (ISADMIN()) {
                $url = $matches[1] . "/showslicelogs.php?slice_uuid=".
                    $slice_uuid;
            }
            else {
                $url = null;
            }
        }
        $results[$uuid] = array("name"     => $name,
                                "uuid"     => $uuid,
                                "exitcode" => $exitcode,
                                "active"   => $active,
                                "created"  => $created,
                                "creator"  => $creator,
                                "reason"   => $reason,
                                "message"  => $message,
                                "url"      => $url,
                                "cluster"  => $cluster,
                                "profile_uuid" => $profile_uuid,
                                "profile_name" => $profile_name);
    }
    SPITAJAX_RESPONSE($results);    
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 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
#
# Search for an IP.
#
function Do_SearchIP()
{
    global $this_user, $urn_mapping;
    global $ajax_args;
    $this_uid = $this_user->uid();

    if (CheckPageArgs()) {
        return;
    }
    $ip = $ajax_args["ip"];
    if (! preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $ip)) {
	SPITAJAX_ERROR(-1, "Bad IP address");
        return;
    }
    $webtask = WebTask::CreateAnonymous();
    $retval = SUEXEC($this_uid, "nobody",
		     "websearchip -t " . $webtask->task_id() . " $ip",
		     SUEXEC_ACTION_IGNORE);
    $webtask->Refresh();
    
    if ($retval != 0) {
        if (!$webtask->exited() || $retval < 0) {
            SUEXECERROR(SUEXEC_ACTION_CONTINUE);
            SPITAJAX_ERROR(-1, "Internal error");
        }
        else {
            SPITAJAX_ERROR($webtask->exitcode(), $webtask->output());
        }
        $webtask->Delete();
        return;
    }
    $uuid = $webtask->TaskValue("instance");
    $webtask->Delete();
    SPITAJAX_RESPONSE($uuid);
}

326 327 328 329
# Local Variables:
# mode:php
# End:
?>