Commit 37d2649e authored by Leigh B. Stoller's avatar Leigh B. Stoller

Convert all of the new template code to PHP "classes." This was not as

much "fun" as the Perl conversion, but nothing about PHP is as much
fun as it is in perl.
parent 7e219a4a
......@@ -34,7 +34,7 @@ sub usage()
"-E <str> - A pithy sentence describing the template\n".
"-g <gid> - The group in which to create the experiment\n".
"<pid> - The project in which to create the experiment\n".
"<tid> - The template name (unique, alphanumeric, no blanks)\n".
"<tid> - The template name (alphanumeric, no blanks)\n".
"<input> - Input file for experiment.\n");
exit(-1);
}
......@@ -305,6 +305,9 @@ system("$makegraph $guid");
fatal(-1, "Error generating template graph.")
if ($?);
# Web interface depends on this line. Bad; need another way to send
# back the newly generated guid/version.
print "Template $guid/$vers has been created\n";
exit(0);
#
......
......@@ -5,7 +5,7 @@
# All rights reserved.
#
include("defs.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# Only known and logged in users ...
......@@ -52,28 +52,26 @@ if (!TBvalid_integer($runidx)) {
PAGEHEADER("Experiment Run");
#
# Check to make sure this is a valid template.
# Check to make sure this is a valid template and user has permission.
#
if (! TBValidExperimentTemplate($guid, $version)) {
$template = Template::Lookup($guid, $version);
if (!$template) {
USERERROR("The experiment template $guid/$version is not a valid ".
"experiment template!", 1);
}
if (! TBIsTemplateInstanceExperiment($exptidx)) {
if (! $template->AccessCheck($uid, $TB_EXPT_READINFO)) {
USERERROR("You do not have permission to view experiment template ".
"$guid/$version!", 1);
}
$instance = TemplateInstance::LookupByExptidx($exptidx);
if (!$instance) {
USERERROR("The instance $exptidx is not a valid instance in ".
"template $guid/$version!", 1);
}
if (! TBValidExperimentRun($exptidx, $runidx)) {
if (! $instance->ValidRun($runidx)) {
USERERROR("The run $runidx is not a valid experiment run!", 1);
}
#
# Verify Permission.
#
if (! TBExptTemplateAccessCheck($uid, $guid, $TB_EXPT_READINFO)) {
USERERROR("You do not have permission to view experiment template ".
"$guid/$version!", 1);
}
echo "<font size=+2>Experiment Run<b> " .
MakeLink("instance",
"guid=$guid&version=$version&exptidx=$exptidx",
......@@ -82,7 +80,7 @@ echo "<font size=+2>Experiment Run<b> " .
echo "<br><br>\n";
SHOWEXPERIMENTRUN($exptidx, $runidx);
$instance->ShowRun($runidx);
#
# Standard Testbed Footer
......
......@@ -5,7 +5,7 @@
# All rights reserved.
#
include("defs.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# Only known and logged in users ...
......@@ -45,24 +45,22 @@ if (!TBvalid_integer($exptidx)) {
PAGEHEADER("Template Instance");
#
# Check to make sure this is a valid template.
# Check to make sure this is a valid template and user has permission.
#
if (! TBValidExperimentTemplate($guid, $version)) {
$template = Template::Lookup($guid, $version);
if (!$template) {
USERERROR("The experiment template $guid/$version is not a valid ".
"experiment template!", 1);
}
if (! TBIsTemplateInstanceExperiment($exptidx)) {
USERERROR("The instance $exptidx is not a valid instance in ".
"template $guid/$version!", 1);
}
#
# Verify Permission.
#
if (! TBExptTemplateAccessCheck($uid, $guid, $TB_EXPT_READINFO)) {
if (! $template->AccessCheck($uid, $TB_EXPT_READINFO)) {
USERERROR("You do not have permission to view experiment template ".
"$guid/$version!", 1);
}
$instance = TemplateInstance::LookupByExptidx($exptidx);
if (!$instance) {
USERERROR("The instance $exptidx is not a valid instance in ".
"template $guid/$version!", 1);
}
echo "<font size=+2>Template Instance <b>" .
MakeLink("template",
......@@ -70,7 +68,7 @@ echo "<font size=+2>Template Instance <b>" .
"</b></font>\n";
echo "<br><br>\n";
SHOWTEMPLATEINSTANCE($guid, $version, $exptidx, 1);
$instance->Show(1);
#
# Standard Testbed Footer
......
......@@ -72,7 +72,7 @@ sajax_handle_client_request();
# Faster to do this after the sajax stuff
include("showstuff.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# Need some DB info.
......@@ -97,11 +97,15 @@ $panic_date = $row["panic_date"];
$lockdown = $row["lockdown"];
# Template Instance Experiments get special treatment in this page.
$isinstance = ($EXPOSETEMPLATES &&
TBIsTemplateInstanceExperiment($expindex) ? 1 : 0);
if ($isinstance) {
$tag = "Template Instance";
TBPidEid2Template($pid, $eid, $guid, $version, $instance_idx);
$instance = NULL;
if ($EXPOSETEMPLATES) {
$instance = TemplateInstance::LookupByExptidx($expindex);
if (! is_null($instance)) {
$tag = "Template Instance";
$guid = $instance->guid();
$vers = $instance->vers();
}
}
#
......@@ -189,7 +193,7 @@ if ($expstate) {
}
elseif ($expstate == $TB_EXPTSTATE_ACTIVE ||
($expstate == $TB_EXPTSTATE_PANICED && $isadmin)) {
WRITESUBMENUBUTTON(($isinstance ?
WRITESUBMENUBUTTON(($instance ?
"Terminate Instance" :
"Swap Experiment Out"),
"swapexp.php3?inout=out&pid=$exp_pid&eid=$exp_eid");
......@@ -201,23 +205,23 @@ if ($expstate) {
}
}
if (!$isinstance && $expstate != $TB_EXPTSTATE_PANICED) {
if (!$instance && $expstate != $TB_EXPTSTATE_PANICED) {
WRITESUBMENUBUTTON("Terminate Experiment",
"endexp.php3?pid=$exp_pid&eid=$exp_eid");
}
# Batch experiments can be modifed only when paused.
if (!$isinstance && ($expstate == $TB_EXPTSTATE_SWAPPED ||
if (!$instance && ($expstate == $TB_EXPTSTATE_SWAPPED ||
(!$isbatch && $expstate == $TB_EXPTSTATE_ACTIVE))) {
WRITESUBMENUBUTTON("Modify Experiment",
"modifyexp.php3?pid=$exp_pid&eid=$exp_eid");
}
}
if ($isinstance && $expstate == $TB_EXPTSTATE_ACTIVE) {
if ($instance && $expstate == $TB_EXPTSTATE_ACTIVE) {
WRITESUBMENUBUTTON("Start New Experiment Run",
"template_exprun.php?action=start&guid=$guid".
"&version=$version&eid=$exp_eid");
"&version=$vers&eid=$exp_eid");
}
if ($expstate == $TB_EXPTSTATE_ACTIVE) {
......@@ -293,7 +297,7 @@ WRITESUBMENUDIVIDER();
WRITESUBMENUBUTTON("Show History",
"showstats.php3?showby=expt&which=$expindex");
if (!$isinstance && STUDLY()) {
if (!$instance && STUDLY()) {
WRITESUBMENUBUTTON("Duplicate Experiment",
"beginexp_html.php3?copyid=${exp_pid},${exp_eid}");
}
......@@ -408,11 +412,11 @@ if (TBExptFirewall($exp_pid, $exp_eid) &&
}
SUBPAGEEND();
if ($isinstance &&
if ($instance &&
($expstate == $TB_EXPTSTATE_ACTIVE ||
$expstate == $TB_EXPTSTATE_PANICED ||
$expstate == $TB_EXPTSTATE_ACTIVATING)) {
SHOWTEMPLATEINSTANCEBINDINGS($guid, $version, $instance_idx);
$instance->ShowBindings();
}
#
......
......@@ -129,7 +129,10 @@ function STARTLOG($pid, $eid)
</script>\n";
echo "<br>\n";
echo "<img id='busy' src='busy.gif'><span id='loading'> Loading...</span>";
echo "<center>\n";
echo "<img id='busy' src='busy.gif'>
<span id='loading'> Working ...</span>";
echo "</center>\n";
echo "<br>\n";
echo "<div><iframe id='outputframe' src='busy.gif' ".
......
......@@ -9,6 +9,7 @@
#
# Functions to dump out various things.
#
include_once("template_defs.php");
#
# A project
......@@ -863,16 +864,17 @@ function SHOWEXP($pid, $eid, $short = 0, $sortby = "") {
</tr>\n";
if (!$short) {
$isinstance = (TBIsTemplateInstanceExperiment($exptidx) ? 1 : 0);
$instance = TemplateInstance::LookupByExptidx($exptidx);
if ($isinstance) {
TBPidEid2Template($pid, $eid, $guid, $version, $instance_idx);
if (! is_null($instance)) {
$guid = $instance->guid();
$vers = $instance->vers();
echo "<tr>
<td>Template: </td>
<td class=\"left\">
<a href='template_show.php?guid=$guid&version=$version'>
$guid/$version</a></td>
<a href='template_show.php?guid=$guid&version=$vers'>
$guid/$vers</a></td>
</tr>\n";
}
......
......@@ -6,7 +6,7 @@
#
include("defs.php3");
include("showstuff.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# Only known and logged in users can do this.
......
......@@ -5,7 +5,7 @@
# All rights reserved.
#
include("defs.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# Only known and logged in users can begin experiments.
......@@ -109,19 +109,17 @@ if (isset($guid) && isset($version)) {
if (!TBvalid_tinyint($version)) {
PAGEARGERROR("Invalid GUID version");
}
#
# Check to make sure this is a valid template.
# Check to make sure this is a valid template and user has permission.
#
if (! TBValidExperimentTemplate($guid, $version)) {
$template = Template::Lookup($guid, $version);
if (!$template) {
USERERROR("The experiment template $guid/$version is not a valid ".
"experiment template!", 1);
}
#
# Verify Permission.
#
if (! TBExptTemplateAccessCheck($uid, $guid, $TB_EXPT_READINFO)) {
USERERROR("You do not have permission to view experiment template ".
if (! $template->AccessCheck($uid, $TB_EXPT_READINFO)) {
USERERROR("You do not have permission to modify experiment template ".
"$guid/$version!", 1);
}
header("Content-Type: text/plain");
......
......@@ -6,7 +6,7 @@
#
include("defs.php3");
include("showstuff.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# Only known and logged in users can end experiments.
......@@ -100,9 +100,13 @@ if (! TBExptAccessCheck($uid, $exp_pid, $exp_eid, $TB_EXPT_MODIFY)) {
}
# Template Instance Experiments get special treatment in this page.
$isinstance = $EXPOSETEMPLATES && TBIsTemplateInstanceExperiment($exptidx);
if ($isinstance && $inout != "out") {
PAGEARGERROR("Invalid action for template instance");
$instance = NULL;
if ($EXPOSETEMPLATES) {
$instance = TemplateInstance::LookupByExptidx($exptidx);
if (! is_null($instance) && $inout != "out") {
PAGEARGERROR("Invalid action for template instance");
}
}
# Convert inout to informative text.
......@@ -133,7 +137,7 @@ elseif (!strcmp($inout, "restart")) {
$action = "restart";
}
if ($isinstance) {
if ($instance) {
echo "<font size=+2>Template Instance <b>";
}
else {
......@@ -162,7 +166,7 @@ if (!$confirmed) {
if ($force) {
echo "<font color=red><br>forcibly</br></font> ";
}
if ($isinstance) {
if ($instance) {
echo "terminate template instance";
}
else {
......@@ -226,10 +230,10 @@ if (!$confirmed) {
#
TBGroupUnixInfo($exp_pid, $exp_gid, $unix_gid, $unix_name);
if ($isinstance) {
if (! TBPidEid2Template($exp_pid, $exp_eid, $guid, $version, $instidx)) {
TBERROR("Could not map $pid/$eid to its template!", 1);
}
if ($instance) {
$guid = $instance->guid();
$version = $instance->vers();
echo "<br>\n";
echo "<b>Terminating template instance!</b> ... ";
echo "this will take a few minutes; please be patient.";
......@@ -253,7 +257,7 @@ $args = ($idleswap ? "-i" : ($autoswap ? "-a" : ""));
$retval = SUEXEC($uid, "$exp_pid,$unix_gid",
($force ?
"webidleswap $args $exp_pid $exp_eid" :
($isinstance ?
($instance ?
"webtemplate_swapout -e $exp_eid $guid/$version" :
"webswapexp -s $inout $exp_pid $exp_eid")),
SUEXEC_ACTION_IGNORE);
......@@ -279,7 +283,7 @@ if ($retval) {
echo "<blockquote><pre>$suexec_output<pre></blockquote>";
}
else {
if ($isinstance) {
if ($instance) {
STARTLOG($pid, $eid);
}
elseif ($isbatch) {
......
......@@ -5,7 +5,7 @@
# All rights reserved.
#
include("defs.php3");
include("template_defs.php");
include_once("template_defs.php");
#
# No PAGEHEADER since we spit out a Location header later. See below.
......@@ -310,9 +310,6 @@ if (!isset($formfields[tid]) || $formfields[tid] == "") {
elseif (!TBvalid_eid($formfields[tid])) {
$errors["Template ID"] = TBFieldErrorString();
}
elseif (TBValidExperimentTemplate($formfields[pid], $formfields[tid])) {
$errors["Template ID"] = "Already in use";
}
#
# Description:
......@@ -507,23 +504,26 @@ if ($retval) {
return;
}
unset($guid);
if (TBPidTid2Template($pid, $tid, $guid, $version)) {
#
# Parse the last line of output. Ick.
#
if (preg_match("/^Template\s+(\w+)\/(\w+)\s+/",
$suexec_output_array[count($suexec_output_array)-1],
$matches)) {
$guid = $matches[1];
$vers = $matches[2];
echo "<script type='text/javascript' language='javascript'>\n";
echo "PageReplace('template_show.php?guid=$guid&version=$version');\n";
echo "PageReplace('template_show.php?guid=$guid&version=$vers');\n";
echo "</script>\n";
}
#
# In case the above fails.
#
echo "Done!";
echo "<center><b>Done!</b></center>";
echo "<br><br>\n";
if (isset($guid)) {
SHOWTEMPLATE($guid, $version);
}
#
# Standard Testbed Footer
#
......
......@@ -4,280 +4,257 @@
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
#
# Grab a new GUID.
# The template class is really just a wrapper around the DB data, plus
# some access routines and printing functions.
#
function TBNewGUID(&$newguid)
class Template
{
DBQueryFatal("lock tables emulab_indicies write");
$query_result =
DBQueryFatal("select idx from emulab_indicies ".
"where name='next_guid'");
$row = mysql_fetch_array($query_result);
$newguid = $row['idx'];
$nextidx = $newguid + 1;
var $template;
DBQueryFatal("update emulab_indicies set idx='$nextidx' ".
"where name='next_guid'");
DBQueryFatal("unlock tables");
return 0;
}
#
# Confirm a valid experiment template
#
# usage TBValidExperimentTemplate($guid, $version)
# returns 1 if valid
# returns 0 if not valid
#
function TBValidExperimentTemplate($guid, $version)
{
$guid = addslashes($guid);
$version = addslashes($version);
$query_result =
DBQueryFatal("select guid from experiment_templates ".
"where guid='$guid' and vers='$version'");
function Template($guid, $vers) {
$guid = addslashes($guid);
$vers = addslashes($vers);
$query_result =
DBQueryWarn("select * from experiment_templates ".
"where guid='$guid' and vers='$vers'");
if (mysql_num_rows($query_result) == 0) {
return 0;
if (!$query_result || !mysql_num_rows($query_result)) {
$this->template = NULL;
return;
}
$this->template = mysql_fetch_array($query_result);
}
return 1;
}
#
# Check if a template is hidden.
#
# usage TBIsExperimentTemplateHidden($guid, $version)
# returns 1 if hidden
# returns 0 if visible
#
function TBIsExperimentTemplateHidden($guid, $version)
{
$guid = addslashes($guid);
$version = addslashes($version);
$query_result =
DBQueryFatal("select hidden from experiment_templates ".
"where guid='$guid' and vers='$version'");
if (mysql_num_rows($query_result) == 0) {
return 0;
# Hmm, how does one cause an error in a php constructor?
function IsValid() {
return !is_null($this->template);
}
$row = mysql_fetch_array($query_result);
return $row[hidden];
}
#
# Check if a template is the root template (cannot be hidden)
#
# usage TBIsRootTemplate($guid, $version)
# returns 1 if hidden
# returns 0 if visible
#
function TBIsRootTemplate($guid, $version)
{
$guid = addslashes($guid);
$version = addslashes($version);
$query_result =
DBQueryFatal("select parent_guid from experiment_templates ".
"where guid='$guid' and vers='$version' and ".
" parent_guid is null");
# Do class level lookup.
function Lookup($guid, $vers) {
$foo = new Template($guid, $vers);
return mysql_num_rows($query_result);
}
if ($foo->IsValid())
return $foo;
return null;
}
# Do class level lookup for the root template.
function LookupRoot($guid) {
$foo = new Template($guid, 1);
#
# Experiment Template permission checks; using the experiment access checks.
#
# Usage: TBExptTemplateAccessCheck($uid, $guid, $access_type)
# returns 0 if not allowed.
# returns 1 if allowed.
#
function TBExptTemplateAccessCheck($uid, $guid, $access_type)
{
global $TB_EXPT_READINFO;
global $TB_EXPT_MODIFY;
global $TB_EXPT_DESTROY;
global $TB_EXPT_UPDATE;
global $TB_EXPT_MIN;
global $TB_EXPT_MAX;
global $TBDB_TRUST_USER;
global $TBDB_TRUST_LOCALROOT;
global $TBDB_TRUST_GROUPROOT;
global $TBDB_TRUST_PROJROOT;
$mintrust;
if ($access_type < $TB_EXPT_MIN ||
$access_type > $TB_EXPT_MAX) {
TBERROR("Invalid access type: $access_type!", 1);
if ($foo->IsValid())
return $foo;
return null;
}
#
# Admins do whatever they want!
#
if (ISADMIN()) {
return 1;
}
$guid = addslashes($guid);
# Refresh a template instance by reloading from the DB.
#
function Refresh() {
if (! $this->IsValidTemplate())
return -1;
$query_result =
DBQueryFatal("select pid,gid,uid from experiment_templates where ".
"guid='$guid' limit 1");
$guid = $this->guid();
$vers = $this->vers();
if (mysql_num_rows($query_result) == 0) {
$query_result =
DBQueryWarn("select * from experiment_templates ".
"where guid='$guid' and vers='$vers'");
if (!$query_result || !mysql_num_rows($query_result)) {
$this->template = NULL;
return -1;
}
$this->template = mysql_fetch_array($query_result);
return 0;
}
$row = mysql_fetch_array($query_result);
$pid = $row[pid];
$gid = $row[gid];
$head = $row[uid];
if ($access_type == $TB_EXPT_READINFO) {
$mintrust = $TBDB_TRUST_USER;
# accessors
function guid() {
return (is_null($this->template) ? -1 : $this->template['guid']);
}
else {
$mintrust = $TBDB_TRUST_LOCALROOT;
function vers() {
return (is_null($this->template) ? -1 : $this->template['vers']);
}
#
# Either proper permission in the group, or group_root in the project.
# This lets group_roots muck with other peoples experiments, including
# those in groups they do not belong to.
#
return TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT);
}
# Helper function
function ShowItem($tag, $value, $default = "&nbsp")
{
if (!isset($value)) {
$value = $default;
function pid() {
return (is_null($this->template) ? -1 : $this->template['pid']);
}
echo "<tr><td>${tag}: </td><td class=left>$value</td></tr>\n";
}
function MakeLink($which, $args, $text)
{
$page = "";
if ($which == "project") {
$page = "showproject.php3";
function gid() {
return (is_null($this->template) ? -1 : $this->template['gid']);
}
elseif ($which == "user") {
$page = "showuser.php3";
function eid() {
return (is_null($this->template) ? -1 : $this->template['eid']);
}
elseif ($which == "template") {
$page = "template_show.php";
function tid() {
return (is_null($this->template) ? -1 : $this->template['tid']);
}
elseif ($which == "metadata") {
$page = "template_metadata.php";
function uid() {
return (is_null($this->template) ? -1 : $this->template['uid']);
}
elseif ($which == "instance") {
$page = "instance_show.php";
function IsHidden() {
return (is_null($this->template) ? -1 : $this->template['hidden']);
}
elseif ($which == "run") {
$page = "experimentrun_show.php";
function created() {
return (is_null($this->template) ? -1 : $this->template['created']);
}
function description() {
return (is_null($this->template) ? -1 :
$this->template['description']);
}
function parent_guid() {
return (is_null($this->template) ? -1 :$this->template['parent_guid']);
}
function parent_vers() {