Commit f3432688 authored by Leigh B Stoller's avatar Leigh B Stoller

More "improvements" to ajax server side. Convert status.php to use

new ajax model.
parent 557260ef
......@@ -610,7 +610,9 @@ require(['jquery', 'underscore', 'js/quickvm_sup',
setTimeout(function f() { ShowProgressModal() }, 5000);
}
var $xmlthing = sup.CallServerMethod(ajaxurl, "SnapShotStatus",
var $xmlthing = sup.CallServerMethod(ajaxurl,
"manage_profile",
"SnapShotStatus",
{"uuid" : uuid});
$xmlthing.done(callback);
}
......
......@@ -56,6 +56,10 @@ function ($, sup)
var index;
var callback = function(json) {
if (json.code) {
alert("Failed to get rspec for topology viewer: " + json.value);
return;
}
var xmlDoc = $.parseXML(json.value.rspec);
var xml = $(xmlDoc);
var topo = sup.ConvertManifestToJSON(profile, xml);
......@@ -67,7 +71,9 @@ function ($, sup)
($("#showtopo_nopicker").outerWidth() - 2),
300, topo, null);
};
var $xmlthing = sup.CallServerMethod(ajaxurl, "GetProfile",
var $xmlthing = sup.CallServerMethod(ajaxurl,
"myprofiles",
"GetProfile",
{"uuid" : profile});
$xmlthing.done(callback);
}
......
......@@ -35,7 +35,7 @@ function CallMethod(method, callback, uuid, arg)
});
}
function CallServerMethod(url, method, args)
function CallServerMethod(url, route, method, args)
{
if (url == null) {
url = 'https://' + window.location.host + '/apt/server-ajax.php';
......@@ -46,7 +46,7 @@ function CallServerMethod(url, method, args)
// the data to send (will be converted to a query string)
data: {
ajax_request: 1,
ajax_route: route,
ajax_method: method,
ajax_args: args,
},
......
......@@ -7,10 +7,12 @@ function ($, sup, moment)
'use strict';
var CurrentTopo = null;
var nodecount = 0;
var ajaxurl = null;
function initialize()
{
window.APT_OPTIONS.initialize(sup);
ajaxurl = window.APT_OPTIONS.AJAXURL;
// This activates the popover subsystem.
$('[data-toggle="popover"]').popover({
......@@ -64,9 +66,11 @@ function ($, sup, moment)
}
sup.ShowModal("#waitwait");
var $xmlthing = sup.CallMethod("terminate", null,
window.APT_OPTIONS.uuid, null);
$xmlthing.done(callback);
var xmlthing = sup.CallServerMethod(ajaxurl,
"status",
"TerminateInstance",
{"uuid" : window.APT_OPTIONS.uuid});
xmlthing.done(callback);
});
StartCountdownClock(window.APT_OPTIONS.sliceExpires);
......@@ -79,8 +83,11 @@ function ($, sup, moment)
var callback = function(json) {
StatusWatchCallBack(uuid, json);
}
var $xmlthing = sup.CallMethod("status", null, uuid, null);
$xmlthing.done(callback);
var xmlthing = sup.CallServerMethod(ajaxurl,
"status",
"GetInstanceStatus",
{"uuid" : uuid});
xmlthing.done(callback);
}
// Call back for above.
......@@ -376,8 +383,12 @@ function ($, sup, moment)
}
sup.HideModal('#extend_modal');
sup.ShowModal("#waitwait");
var $xmlthing = sup.CallMethod("request_extension", null, uuid, reason);
$xmlthing.done(callback);
var xmlthing = sup.CallServerMethod(ajaxurl,
"status",
"RequestInstanceExtension",
{"uuid" : uuid,
"reason" : reason});
xmlthing.done(callback);
}
//
......@@ -502,8 +513,11 @@ function ($, sup, moment)
StartSSH(tabname, json.value);
}
}
var xmlthing = sup.CallMethod("ssh_authobject", null,
window.APT_OPTIONS.uuid, hostport);
var xmlthing = sup.CallServerMethod(ajaxurl,
"status",
"GetSSHAuthObject",
{"uuid" : window.APT_OPTIONS.uuid,
"hostport" : hostport});
xmlthing.done(callback);
}
//
......@@ -606,8 +620,11 @@ function ($, sup, moment)
NewSSHTab(nodehostport, nodename);
}
}
var $xmlthing = sup.CallMethod("manifest", null, uuid, null);
$xmlthing.done(callback);
var xmlthing = sup.CallServerMethod(ajaxurl,
"status",
"GetInstanceManifest",
{"uuid" : uuid});
xmlthing.done(callback);
}
function ReDrawTopoMap()
......
......@@ -30,14 +30,19 @@ include_once("webtask.php");
chdir("apt");
include_once("profile_defs.php");
# Logged in users only. This will not return if it fails.
CheckLoginForAjax();
#
# Return snapshot status.
#
function Do_SnapShotStatus()
{
global $this_idx;
global $this_user;
global $ajax_args;
$this_idx = $this_user->uid_idx();
if (!isset($ajax_args["uuid"])) {
SPITAJAX_ERROR(1, "Missing profile uuid");
return;
......
......@@ -30,13 +30,18 @@ include_once("webtask.php");
chdir("apt");
include_once("profile_defs.php");
# Logged in users only. This will not return if it fails.
CheckLoginForAjax();
#
# Return info about specific profile.
#
function Do_GetProfile()
{
global $this_idx;
global $this_user;
global $ajax_args;
$this_idx = $this_user->uid_idx();
if (!isset($ajax_args["uuid"])) {
SPITAJAX_ERROR(1, "Missing profile uuid");
......
......@@ -26,6 +26,30 @@ include("defs.php3");
chdir("apt");
include("quickvm_sup.php");
#
# Poor man routing description.
#
$routing = array("myprofiles" =>
array("file" => "myprofiles.ajax",
"methods" => array("GetProfile" =>
"Do_GetProfile")),
"manage_profile" =>
array("file" => "manage_profile.ajax",
"methods" => array("SnapShotStatus" =>
"Do_SnapShotStatus")),
"status" =>
array("file" => "status.ajax",
"methods" => array("GetInstanceStatus" =>
"Do_GetInstanceStatus",
"TerminateInstance" =>
"Do_TerminateInstance",
"GetInstanceManifest" =>
"Do_GetInstanceManifest",
"GetSSHAuthObject" =>
"Do_GetSSHAuthObject",
"RequestExtension" =>
"Do_RequestExtension")));
#
# Redefine this so we return XML instead of html for all errors.
#
......@@ -38,34 +62,64 @@ $PAGEERROR_HANDLER = function($msg, $status_code = 0) {
};
#
# At this point, must always be a logged in user.
# Included file determines if guest user okay.
#
$this_user = CheckLogin($check_status);
if (!$this_user) {
SPITAJAX_ERROR(2, "Your login has timed out");
return;
#
# Check user login, called by included code. Basically just a
# way to let guest users pass through when allowed, without
# duplicating the code in each file.
#
function CheckLoginForAjax($guestokay = 0)
{
global $this_user, $check_status;
# Known user, but timed out.
if ($check_status & CHECKLOGIN_TIMEDOUT) {
SPITAJAX_ERROR(2, "Your login has timed out");
exit(2);
}
# Logged in user always okay.
if (isset($this_user)) {
if ($check_status & CHECKLOGIN_MAYBEVALID) {
SPITAJAX_ERROR(2, "Your login cannot be verified. Cookie problem?");
exit(2);
}
return;
}
if (!$guestokay) {
SPITAJAX_ERROR(2, "Your are not logged in");
exit(2);
}
# Guest user okay, but the cookie must exist.
if (isset($_COOKIE['quickvm_user'])) {
return;
}
SPITAJAX_ERROR(2, "Who are you?");
exit(2);
}
$this_idx = $this_user->uid_idx();
#
# Verify page arguments.
#
$optargs = RequiredPageArguments("ajax_request", PAGEARG_BOOLEAN,
$optargs = RequiredPageArguments("ajax_route", PAGEARG_STRING,
"ajax_method", PAGEARG_STRING,
"ajax_args", PAGEARG_ARRAY);
#
# If we get more then a few, this will be annyoing.
# Verify page and method.
#
if ($ajax_method == "SnapShotStatus") {
include("manage_profile.ajax");
Do_SnapShotStatus();
if (! array_key_exists($ajax_route, $routing)) {
SPITAJAX_ERROR(1, "Invalid route: $ajax_route");
exit(1);
}
if ($ajax_method == "GetProfile") {
include("myprofiles.ajax");
Do_GetProfile();
}
else {
SPITAJAX_ERROR(1, "Unknown request");
if (! array_key_exists($ajax_method, $routing[$ajax_route]["methods"])) {
SPITAJAX_ERROR(1, "Invalid method: $ajax_route,$ajax_method");
exit(1);
}
include($routing[$ajax_route]["file"]);
call_user_func($routing[$ajax_route]["methods"][$ajax_method]);
?>
<?php
#
# Copyright (c) 2000-2014 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/>.
#
# }}}
#
# Local Variables:
# mode:php
# End:
#
chdir("..");
include_once("geni_defs.php");
chdir("apt");
include_once("profile_defs.php");
include_once("instance_defs.php");
# Guest users okay. This will not return if it fails.
CheckLoginForAjax(1);
# Set these globals below.
$instance = null;
$creator = null;
#
# Locate the objects and check permission.
#
function StatusSetupAjax()
{
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;
}
# Admin users do whatever the like.
if (isset($this_user) && ISADMIN()) {
return 0;
}
# An experiment created by a real user, can be accessed by that user only.
# Ditto a guest user; must be the same guest.
if (! ((get_class($creator) == "User" &&
isset($this_user) && $creator->uuid() == $this_user->uuid()) ||
(get_class($creator) == "GeniUser" &&
isset($_COOKIE['quickvm_user']) &&
$_COOKIE['quickvm_user'] == $creator->uuid()))) {
SPITAJAX_ERROR(1, "You do not have permission!");
return 1;
}
return 0;
}
#
# Status/
#
function Do_GetInstanceStatus()
{
global $instance, $creator;
if (StatusSetupAjax()) {
return;
}
SPITAJAX_RESPONSE($instance->status());
}
#
# Terminate.
#
function Do_TerminateInstance()
{
global $instance, $creator;
if (StatusSetupAjax()) {
return;
}
$uuid = $instance->uuid();
# This needs work.
SUEXEC("nobody", "nobody", "webquickvm -k $uuid",
SUEXEC_ACTION_IGNORE);
SPITAJAX_RESPONSE("");
}
#
# Manifest.
#
function Do_GetInstanceManifest()
{
global $instance, $creator;
if (StatusSetupAjax()) {
return;
}
SPITAJAX_RESPONSE($instance->manifest());
}
#
# SSH Auth Object
#
function Do_GetSSHAuthObject()
{
global $instance, $creator;
global $ajax_args;
if (!isset($ajax_args["hostport"])) {
SPITAJAX_ERROR(1, "Missing hostport");
return 1;
}
$hostport = $ajax_args["hostport"];
if (StatusSetupAjax()) {
return;
}
$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.
#
function Do_RequestInstanceExtension()
{
global $instance, $creator;
global $ajax_args;
if (!isset($ajax_args["reason"])) {
SPITAJAX_ERROR(1, "Missing reason");
return 1;
}
$reason = $ajax_args["reason"];
if (StatusSetupAjax()) {
return;
}
$uuid = $instance->uuid();
$slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
if (!slice) {
SPITAJAX_ERROR(1, "no slice for instance");
return 1;
}
# Only extend for 24 hours. More later.
$expires_time = strtotime($slice->expires());
if ($expires_time > time() + (3600 * 24)) {
sleep(2);
SPITAJAX_ERROR(1, "You still have lots of time left!");
return;
}
$retval = SUEXEC("nobody", "nobody",
"webquickvm -e " . 3600 * 24 . " $uuid",
SUEXEC_ACTION_CONTINUE);
if ($retval == 0) {
# Refresh.
$slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
$new_expires = gmdate("Y-m-d\TH:i:s\Z", strtotime($slice->expires()));
SPITAJAX_RESPONSE($new_expires);
TBMAIL($creator->email(),
"APT Extension: $uuid",
"A request to extend your APT experiment was made and ".
"granted.\n".
"Your reason was:\n\n". $reason . "\n\n".
"Your experiment will now expire at $new_expires.\n\n".
"You can request another extension tomorrow.\n",
"CC: $TBMAIL_OPS");
}
elseif ($retval > 0) {
SPITAJAX_ERROR(1, $suexec_output);
}
else {
SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
}
}
?>
......@@ -40,15 +40,9 @@ $this_user = CheckLogin($check_status);
#
# Verify page arguments.
#
$reqargs = OptionalPageArguments("uuid", PAGEARG_STRING,
"ajax_request", PAGEARG_BOOLEAN,
"ajax_method", PAGEARG_STRING,
"ajax_argument", PAGEARG_STRING);
$reqargs = OptionalPageArguments("uuid", PAGEARG_STRING);
if (!isset($uuid)) {
if ($ajax_request) {
SPITAJAX_ERROR(1, "must provide uuid");
exit();
}
SPITHEADER(1);
echo "<div class='align-center'>
<p class='lead text-center'>
......@@ -64,10 +58,6 @@ if (!isset($uuid)) {
#
$instance = Instance::Lookup($uuid);
if (!$instance) {
if ($ajax_request) {
SPITAJAX_ERROR(1, "no such instance uuid: $uuid");
exit();
}
SPITHEADER(1);
echo "<div class='align-center'>
<p class='lead text-center'>
......@@ -85,10 +75,6 @@ if (! $creator) {
$creator = User::LookupByUUID($instance->creator_uuid());
}
if (!$creator) {
if ($ajax_request) {
SPITAJAX_ERROR(1, "no such instance creator uuid");
exit();
}
SPITHEADER(1);
echo "<div class='align-center'>
<p class='lead text-center'>
......@@ -110,87 +96,11 @@ if (! (isset($this_user) && ISADMIN())) {
(get_class($creator) == "GeniUser" &&
isset($_COOKIE['quickvm_user']) &&
$_COOKIE['quickvm_user'] == $creator->uuid()))) {
if ($ajax_request) {
if ($check_status & CHECKLOGIN_TIMEDOUT) {
SPITAJAX_ERROR(2, "Your login has timed out!");
}
else {
SPITAJAX_ERROR(1, "You do not have permission!");
}
exit();
}
PAGEERROR("You do not have permission to look at this experiment!");
}
}
$slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
#
# Deal with ajax requests.
#
if (isset($ajax_request)) {
if ($ajax_method == "status") {
SPITAJAX_RESPONSE($instance->status());
}
elseif ($ajax_method == "terminate") {
SUEXEC("nobody", "nobody", "webquickvm -k $uuid",
SUEXEC_ACTION_IGNORE);
SPITAJAX_RESPONSE("");
}
elseif ($ajax_method == "manifest") {
SPITAJAX_RESPONSE($instance->manifest());
}
elseif ($ajax_method == "ssh_authobject") {
#
#
#
SPITAJAX_RESPONSE(SSHAuthObject($creator->uid(), $ajax_argument));
}
elseif ($ajax_method == "request_extension") {
if (!isset($slice)) {
SPITAJAX_ERROR(1, "Nothing to extend!");
return;
}
# Only extend for 24 hours. More later.
$expires_time = strtotime($slice->expires());
if ($expires_time > time() + (3600 * 24)) {
sleep(2);
SPITAJAX_ERROR(1, "You still have lots of time left!");
return;
}
$retval =
SUEXEC("nobody", "nobody", "webquickvm -e " . 3600 * 24 . " $uuid",
SUEXEC_ACTION_CONTINUE);
if ($retval == 0) {
# Refresh.
$slice = GeniSlice::Lookup("sa", $instance->slice_uuid());
$new_expires = gmdate("Y-m-d\TH:i:s\Z",
strtotime($slice->expires()));
SPITAJAX_RESPONSE($new_expires);
TBMAIL($creator->email(),
"APT Extension: $uuid",
"A request to extend your APT experiment was made and ".
"granted.\n".
"Your reason was:\n\n". $ajax_argument . "\n\n".
"Your experiment will now expire at $new_expires.\n\n".
"You can request another extension tomorrow.\n",
"CC: $TBMAIL_OPS");
}
elseif ($retval > 0) {
SPITAJAX_ERROR(1, $suexec_output);
}
else {
SPITAJAX_ERROR(-1, "Internal Error. Please try again later");
}
}
elseif ($ajax_method == "extend") {
SPITAJAX_ERROR(1, "Not implemented yet!");
}
exit();
}
SPITHEADER(1);
$style = "style='border: none;'";
......@@ -394,6 +304,7 @@ echo " window.APT_OPTIONS.uuid = '" . $uuid . "';\n";
echo " window.APT_OPTIONS.sliceExpires = '" . $slice_expires . "';\n";
echo " window.APT_OPTIONS.creatorUid = '" . $creator_uid . "';\n";
echo " window.APT_OPTIONS.creatorEmail = '" . $creator_email . "';\n";
echo " window.APT_OPTIONS.AJAXURL = 'server-ajax.php';\n";
echo "</script>\n";
echo "<script src='js/lib/require.js' data-main='js/status'></script>";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment