Commit 2cfe4630 authored by Leigh Stoller's avatar Leigh Stoller

Add an export option to the record listing. A new button on the Template

Record page lets you export the contents of the archive that corresponds
to that record, along with an XML file that describes the various DB bits
for the template and instance.

This is just a first cut so that Mike can start playing around. Subject to
change, I'm sure.

The archive is dumped to /proj/$pid/exports/$guid/$vers/$exptidx, which
is basically the last commit of the instance when it was terminated.

The xml file is called export.xml and is placed in the top level directory
of the above directory. The file is created with XML::Simple, and a typical
XML file might look like:

<instance>
  <bindings>
    <name>NodeCount</name>
    <description>Number of nodes!</description>
    <value>1</value>
  </bindings>
  <bindings>
    <name>OS</name>
    <description></description>
    <value>RHL90-STD</value>
  </bindings>
  <bindings>
    <name>ScriptArgs</name>
    <description></description>
    <value>-b</value>
  </bindings>
  <eid>NewOne-V2</eid>
  <guid>10149/2</guid>
  <metadata>
    <name>M1</name>
    <guid>10162/1</guid>
    <value>Some metadata</value>
  </metadata>
  <pid>testbed</pid>
  <runs>
    <name>1</name>
    <archive_tag>T20060526-082533-172_endexp</archive_tag>
    <description></description>
    <exptidx>110</exptidx>
    <idx>1</idx>
    <runid>NewOne-V2</runid>
    <start_time>2006-05-26 08:23:02</start_time>
    <stop_time>2006-05-26 08:25:16</stop_time>
  </runs>
  <uid>stoller</uid>
</instance>
parent a12df9b3
......@@ -23,7 +23,8 @@ BIN_STUFF = power snmpit tbend tbprerun tbreport \
tbswap nseswap tarfiles_setup node_history tbrsync \
node_attributes archive_control template_create \
template_swapin template_swapout template_graph \
template_exprun template_delete template_metadata
template_exprun template_delete template_metadata \
template_export
SBIN_STUFF = resetvlans console_setup.proxy sched_reload named_setup \
batch_daemon exports_setup reload_daemon sched_reserve \
......@@ -53,7 +54,7 @@ LIBEXEC_STUFF = rmproj wanlinksolve wanlinkinfo \
spewrpmtar webtarfiles_setup webfrisbeekiller gentopofile \
webnodeattributes webarchive_control webtemplate_create \
webtemplate_swapin webtemplate_swapout webtemplate_exprun \
webtemplate_graph webtemplate_metadata
webtemplate_graph webtemplate_metadata webtemplate_export
LIB_STUFF = libtbsetup.pm exitonwarn.pm libtestbed.pm snmpit_intel.pm \
snmpit_cisco.pm snmpit_lib.pm snmpit_apc.pm power_rpc27.pm \
......
......@@ -864,6 +864,40 @@ sub FormalParameterList($$)
return 0;
}
#
# Get description for formal parameters; this needs to be its own object.
#
sub FormalParameterDescription($$)
{
my ($self, $name, $prval) = @_;
# Must be a real reference.
return -1
if (! ref($self));
my $result = undef;
my $template_guid = $self->guid();
my $template_vers = $self->vers();
$name = DBQuoteSpecial($name);
my $query_result =
DBQueryWarn("select m.value from experiment_template_parameters as p ".
"left join experiment_template_metadata_items as m on ".
" m.guid=p.metadata_guid and m.vers=p.metadata_vers ".
"where p.parent_guid='$template_guid' and ".
" p.parent_vers='$template_vers' and ".
" p.name=$name");
return -1
if (!$query_result);
if ($query_result->numrows) {
($result) = $query_result->fetchrow_array();
}
$$prval = $result;
return 0;
}
#
# Delete (all) template metadata
#
......@@ -1065,6 +1099,51 @@ sub CopyMetadata($$$)
return 0;
}
#
# Return a list of metadata. This should be its own object.
#
sub MetadataList($$)
{
my ($self, $prval) = @_;
# Must be a real reference.
return -1
if (! ref($self));
my %results = ();
my $template_guid = $self->guid();
my $template_vers = $self->vers();
#
# Copy the toplevel items.
#
my $query_result =
DBQueryWarn("select i.* from experiment_template_metadata as m ".
"left join experiment_template_metadata_items as i on ".
" i.guid=m.metadata_guid and ".
" i.vers=m.metadata_vers ".
"where m.parent_guid='$template_guid' and ".
" m.parent_vers='$template_vers' and ".
" m.hidden=0 and m.metadata_type is null")
or return -1;
while (my $rowref = $query_result->fetchrow_hashref()) {
my $name = $rowref->{"name"};
my $value = $rowref->{"value"};
my $guid = $rowref->{"guid"};
my $vers = $rowref->{"vers"};
my $ref = {'name' => $name,
'value' => $value,
'guid' => "$guid/$vers"};
$results{$name} = $ref;
}
%$prval = %results;
return 0;
}
#
# Create a new Instance record, using the package below.
#
......@@ -1633,24 +1712,17 @@ sub FinalizeCurrentRun($)
my $idx = $self->idx();
my $runidx = $self->runidx();
my $exptidx = $self->exptidx();
my $archive_tag;
return -1
if (!defined($runidx));
#
# Grab the current archive tag. This will correspond to the commit
# done at the end of the run.
# Grab the current archive tag.
#
my $query_result =
DBQueryWarn("select r.archive_tag from experiment_stats as s ".
"left join experiment_resources as r on ".
" r.idx=s.rsrcidx ".
"where s.exptidx='$exptidx'");
return -1
if (!$query_result || !$query_result->numrows);
my ($archive_tag) = $query_result->fetchrow_array();
if (ArchiveTag($self, \$archive_tag) < 0);
DBQueryWarn("update experiment_runs set archive_tag='$archive_tag' ".
"where exptidx='$exptidx' and idx='$runidx'")
or return -1;
......@@ -1662,6 +1734,36 @@ sub FinalizeCurrentRun($)
return Refresh($self);
}
#
# Need to make "run" its own object ...
#
# Return an array of runs, indexed by its idx.
#
sub RunList($$)
{
my ($self, $prval) = @_;
# Must be a real reference.
return -1
if (! ref($self));
my %results = ();
my $exptidx = $self->exptidx();
my $query_result =
DBQueryWarn("select * from experiment_runs ".
"where exptidx='$exptidx'");
return -1
if (!$query_result);
while (my $rowref = $query_result->fetchrow_hashref()) {
$results{$rowref->{'idx'}} = $rowref;
}
%$prval = %results;
return 0;
}
#
# Binding records for each Run.
#
......@@ -1728,6 +1830,44 @@ sub LogHole($)
return 0;
}
#
# Grab the current archive tag. I think this info belongs in the instance
# record.
#
sub ArchiveTag($$)
{
my ($self, $ptag) = @_;
# Must be a real reference.
return -1
if (! ref($self));
my $exptidx = $self->exptidx();
#
# Grab the current archive tag. This will correspond to the commit
# done at the end of the run.
#
my $query_result =
DBQueryWarn("select r.archive_tag from experiment_stats as s ".
"left join experiment_resources as r on ".
" r.idx=s.rsrcidx ".
"where s.exptidx='$exptidx'");
return -1
if (!$query_result || !$query_result->numrows);
my ($archive_tag) = $query_result->fetchrow_array();
return -1
if (!defined($archive_tag));
$$ptag = $archive_tag;
return 0;
}
# _Always_ make sure that this 1 is at the end of the file...
1;
......@@ -1250,6 +1250,18 @@ sub cleanup()
#
if ($inout eq "in") {
GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPIN, $errorstat);
#
# Must clean up the experiment if it managed to swap in.
#
$estate = ExpState($pid, $eid);
if ($estate eq EXPTSTATE_ACTIVE) {
print "Running 'tbswap out -force $pid $eid'\n";
if (system("$tbbindir/tbswap out -force $pid $eid") != 0) {
print "tbswap out failed!\n";
}
}
}
elsif ($inout eq "out") {
GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPOUT, $errorstat);
......
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use strict;
use Getopt::Std;
use POSIX qw(isatty setsid);
use POSIX qw(strftime);
use Errno qw(EDQUOT);
use XML::Simple;
use Data::Dumper;
#
# Export a template.
#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Everything okay.
# $status > 0 - Expected error. User not allowed for some reason.
#
sub usage()
{
print(STDERR
"Usage: template_export [-q] [-s] -i <exptidx> <guid/vers>\n".
"switches and arguments:\n".
"-q - be less chatty\n".
"-s - spew to stdout (as for web page).\n".
"-i <exptidx> - Instance index to export\n".
"<guid/vers> - GUID and version to swapin\n");
exit(-1);
}
my $optlist = "qi:ds";
my %options = ();
my $spew = 0;
my $quiet = 0;
my $debug = 0;
my $eid;
my $exptidx;
my $guid;
my $version;
my %bindings;
#
# Configure variables
#
my $TB = "@prefix@";
my $EVENTSYS = @EVENTSYS@;
my $TBOPS = "@TBOPSEMAIL@";
my $TBLOGS = "@TBLOGSEMAIL@";
my $TBDOCBASE = "@TBDOCBASE@";
my $TBBASE = "@TBBASE@";
my $CONTROL = "@USERNODE@";
my $checkquota = "$TB/sbin/checkquota";
my $TAR = "/usr/bin/tar";
# Locals
my $dbuid;
my $template;
my $instance;
my $archive_tag;
# Protos
sub ParseArgs();
sub fatal($$);
sub cleanup();
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
use Template;
# Be careful not to exit on transient error
$libdb::DBQUERY_MAXTRIES = 0;
# In libdb
my $projroot = PROJROOT();
#
# Turn off line buffering on output
#
$| = 1;
#
# Set umask for start/swap. We want other members in the project to be
# able to swap/end experiments, so the log and intermediate files need
# to be 664 since some are opened for append.
#
umask(0002);
#
# Untaint the path
#
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
#
# Verify user and get his DB uid.
#
if (! UNIX2DBUID($UID, \$dbuid)) {
tbdie("You do not exist in the Emulab Database!");
}
# Now parse arguments.
ParseArgs();
# Temporary
libArchive::setdebug($debug);
#
# Grab template info and do access check.
#
$template = Template->Lookup($guid, $version);
if (!defined($template)) {
tbdie("Experiment template $guid/$version does not exist!");
}
if (! TBProjAccessCheck($dbuid,
$template->pid(), $template->gid(),
TB_PROJECT_READINFO)) {
tberror("You do not have permission to export template $guid/$version");
exit(1);
}
my $pid = $template->pid();
$instance = Template::Instance->LookupByExptidx($exptidx);
if (!defined($instance)) {
fatal(-1, "Could not get instance record for experiment $exptidx!");
}
if ($instance->ArchiveTag(\$archive_tag) < 0) {
fatal(-1, "Could not get current archive tag for instance $exptidx!");
}
if (system("$checkquota $dbuid") != 0) {
tberror("You are over your disk quota on $CONTROL; please cleanup!");
exit(1);
}
#
# Checkout to the project tree.
#
my $checkout = "$projroot/$pid/export/$guid/$version/$exptidx";
# Cleanup existing goo.
if (-e $checkout) {
system("/bin/rm -rf $checkout");
}
system("mkdir -p $checkout") == 0
or fatal(-1, "Could not mkdir $checkout");
if (libArchive::TBCheckoutExperimentArchivebyExptIDX($exptidx,
$checkout,
$archive_tag) < 0) {
fatal(-1, "Could not checkout $archive_tag to $pid/.checkouts/$$");
}
#
# Since that worked, lets create the XML representation of it and stash
# to a file.
#
my $root = {};
$root->{"guid"} = $instance->guid() . "/" . $instance->vers();
$root->{"pid"} = $instance->pid();
$root->{"eid"} = $instance->eid();
$root->{"uid"} = $instance->uid();
# Fill in the bindings
$instance->BindingList(\%bindings) == 0
or fatal(-1, "Could not get bindings for $instance");
if (keys(%bindings)) {
$root->{"bindings"} = {};
foreach my $name (keys(%bindings)) {
my $value = $bindings{$name};
my $description;
$template->FormalParameterDescription($name, \$description);
$root->{"bindings"}->{$name} = {'value' => $value,
'description' => $description};
}
}
# Fill in the runs.
my %runlist;
$instance->RunList(\%runlist) == 0
or fatal(-1, "Could not get runlist for $instance");
if (keys(%runlist)) {
$root->{"runs"} = {};
foreach my $name (keys(%runlist)) {
my $value = $runlist{$name};
$root->{"runs"}->{$name} = $value;
}
}
# Fill in the metadata
my %mlist;
$template->MetadataList(\%mlist) == 0
or fatal(-1, "Could not get Metadata list for $instance");
if (keys(%mlist)) {
$root->{"metadata"} = {};
foreach my $name (keys(%mlist)) {
my $value = $mlist{$name};
$root->{"metadata"}->{$name} = $value;
}
}
if ($debug) {
print Dumper($root);
print "\n";
}
my %xmloptions = ('RootName' => 'instance',
'SuppressEmpty' => undef,
'NoAttr' => 1);
my $xml = XMLout($root, %xmloptions);
if ($debug) {
print "$xml\n";
$root = XMLin($xml, KeyAttr => "name",
"ForceArray" => ["metadata", "runs", "bindings"]);
print Dumper($root);
print "\n";
}
#
# Stash this into the checkout directory we created above.
#
open(DUMP, ">$checkout/export.xml")
or fatal(-1, "Could not open $checkout/export.xml for writing");
print DUMP "$xml\n";
close(DUMP);
if ($spew) {
system("$TAR zcf - -C $checkout .");
cleanup();
}
exit(0);
#
# Parse command arguments. Once we return from getopts, all that are
# left are the required arguments.
#
sub ParseArgs()
{
if (! getopts($optlist, \%options)) {
usage();
}
if (@ARGV != 1) {
usage();
}
#
# Pick up guid/version first and untaint.
#
my $tmp = shift(@ARGV);
if ($tmp =~ /^([\w]*)\/([\d]*)$/) {
$guid = $1;
$version = $2;
}
else {
tbdie("Bad data in argument: $tmp");
}
if (defined($options{"i"})) {
$exptidx = $options{"i"};
if (! TBcheck_dbslot($exptidx, "default", "int",
TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
tbdie("Improper experiment index!");
}
# real check.
if ($exptidx =~ /^([\w]*)$/) {
$exptidx = $1;
}
else {
tbdie("Bad data in argument: $exptidx");
}
}
else {
tbdie("You must supply the -i option!");
}
if (defined($options{"q"})) {
$quiet = 1;
}
if (defined($options{"s"})) {
$spew = 1;
}
if (defined($options{"d"})) {
$debug = 2;
}
}
#
# Cleanup the mess.
#
sub cleanup()
{
if (defined($checkout) && -e $checkout) {
system("/bin/rm -rf $checkout");
}
}
sub fatal($$)
{
my ($errorstat, $msg) = @_;
tberror $msg;
tbinfo "Cleaning up and exiting with status $errorstat ...";
exit($errorstat);
}
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
#
# This gets invoked from the Web interface. Simply a wrapper ...
#
#
# Configure variables
#
my $TB = "@prefix@";
#
# Run the real thing, and never return.
#
exec "$TB/bin/template_export", @ARGV;
die("webtemplate_exprun: Could not exec template_export: $!");
......@@ -491,6 +491,7 @@ class Template
<th>Stop Time</th>
<th align=center>Show</th>
<th align=center>Archive</th>
<th align=center>Export</th>
</tr>\n";
$idlemark = "<b>*</b>";
......@@ -535,12 +536,17 @@ class Template
MakeLink("instance",
"guid=$guid&version=$vers&exptidx=$exptidx",
"<img border=0 alt='Show' src='greenball.gif'>");
echo " </td>\n";
echo " <td align=center>
echo " <td align=center>
<a href=archive_view.php3/$exptidx/history/$tag/".
"?exptidx=$exptidx>
<img border=0 alt='i' src='greenball.gif'></a></td>";
echo " <td align=center>".
MakeLink("export",
"guid=$guid&version=$vers&exptidx=$exptidx",
"<img border=0 alt='Show' src='greenball.gif'>");
echo " </td>
</tr>\n";
......@@ -1390,6 +1396,9 @@ function MakeLink($which, $args, $text)
elseif ($which == "experiment") {
$page = "showexp.php3";
}
elseif ($which == "export") {
$page = "template_export.php";
}
return "<a href=${page}?${args}>$text</a>";
}
......
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");
include_once("template_defs.php");
#
# Only known and logged in users can end experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
#
# Verify page arguments.
#
if (!isset($guid) ||
strcmp($guid, "") == 0) {
USERERROR("You must provide a template GUID.", 1);
}
if (!isset($version) ||
strcmp($version, "") == 0) {
USERERROR("You must provide a template version number", 1);
}
if (!isset($exptidx) ||
strcmp($exptidx, "") == 0) {
USERERROR("You must provide a template instance ID", 1);
}
if (!TBvalid_guid($guid)) {
PAGEARGERROR("Invalid characters in GUID!");
}
if (!TBvalid_integer($version)) {
PAGEARGERROR("Invalid characters in version!");
}
if (!TBvalid_integer($exptidx)) {
PAGEARGERROR("Invalid characters in instance ID!");
}
# Canceled operation redirects back to template page.
if ($canceled) {
header("Location: template_show.php?guid=$guid&version=$version");
return;
}
#
# Check to make sure this is a valid template and user has permission.
#
$template = Template::Lookup($guid, $version);
if (!$template) {
USERERROR("The experiment template $guid/$version is not a valid ".
"experiment template!", 1);
}
if (! $template->AccessCheck($uid, $TB_EXPT_MODIFY)) {
USERERROR("You do not have permission to modify experiment template ".
"$guid/$version!", 1);
}
# Need these below.
$pid = $template->pid();
$gid = $template->gid();
#
# Check to make sure and a valid instance that is actually swapped in.
#
$instance = TemplateInstance::LookupByExptidx($exptidx);
if (!$instance) {
TBERROR("Template Instance $guid/$version/$exptidx is not ".
"a valid experiment template instance!", 1);
}
if (!$confirmed) {
PAGEHEADER("Template Export");
echo "<center><br><font size=+1>
Export instance $exptidx in Template
in Template $guid/$version?</font>\n";
$template->Show();
echo "<form action='template_export.php?guid=$guid&version=$version".
"&exptidx=$exptidx' method=post>\n";
echo "<br>\n";
echo "<input type=checkbox name=spew value=yep>
Save to local disk? [<b>1</b>]\n";
echo "<br>\n";
echo "<br>\n";
echo "<b><input type=submit name=confirmed value=Confirm></b>\n";
echo "<b><input type=submit name=canceled value=Cancel></b>\n";
echo "</form>\n";
echo "</center>\n";
echo "<blockquote><blockquote>
<ol>
<li> By default, your instance will be exported to
<tt>/proj/$pid/export/$guid/$version/$exptidx</tt>,
available to
other experiments in your project. If you want to export
to a local file, click the <em>local disk</em> option
and a .tgz file will be generated and sent to your browser.
</ol>
</blockquote></blockquote>\n";
PAGEFOOTER();
return;
}
# Check spew option.
if (isset($spew) && $spew == "yep") {
$spew = 1;
}
else {
$spew = 0;
}
#
# We need the unix gid for the project for running the scripts below.