template_export.php 7.4 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2012 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
# 
# {{{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/>.
# 
# }}}
23 24 25 26 27
#
include("defs.php3");
include_once("template_defs.php");

#
28
# Only known and logged in users.
29
#
30 31 32
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
33 34 35

#
# Verify page arguments.
36
#
37 38 39
$optargs = OptionalPageArguments("instance",  PAGEARG_INSTANCE,
				 "template",  PAGEARG_TEMPLATE,
				 "canceled",  PAGEARG_BOOLEAN,
40
				 "confirmed", PAGEARG_BOOLEAN,
41
				 "referrer",  PAGEARG_STRING,
42
				 "runidx",    PAGEARG_INTEGER,
43 44
				 "tag",       PAGEARG_STRING,
				 "overwrite", PAGEARG_BOOLEAN,
45
				 "spew",      PAGEARG_BOOLEAN);
46 47 48 49 50 51 52 53 54 55 56

#
# An instance might be a current or historical. If its a template, use
# the underlying experiment of the template.
#
if (isset($instance)) {
    $template = $instance->GetTemplate();
}
elseif (! isset($template)) {
    PAGEARGERROR("Must provide a template or an instance to export");
}
57

58
if (isset($canceled) && $canceled) {
59 60 61 62
    if (isset($instance))
	header("Location: ". CreateURL("template_show", $template));
    else
	header("Location: ". CreateURL("template_show", $instance));	
63 64 65 66
    return;
}

# Need these below.
67 68 69
$guid     = $template->guid();
$vers     = $template->vers();
$pid      = $template->pid();
70
$unix_gid = $template->UnixGID();
71 72
$project  = $template->GetProject();
$unix_pid = $project->unix_gid();
73

74 75
if (! $template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
    USERERROR("You do not have permission to export from template ".
76
	      "$guid/$vers!", 1);
77
}
78 79 80 81 82 83 84
if (isset($instance)) {
    $exptidx  = $instance->exptidx();
    
    if (isset($runidx) && !$instance->ValidRun($runidx)) {
	USERERROR("Run '$runidx' is not a valid run in instance '$exptidx'",
		  1);
    }
85
}
86

87 88
function SPITFORM($error)
{
89
    global $template, $instance, $runidx, $TBPROJ_DIR, $referrer;
90 91
    global $tag, $pid, $guid, $vers;

92 93
    $iid   = $instance->id();

94 95 96 97 98 99 100 101 102
    if ($error) {
	echo "<center>\n";
	echo "<font size=+1 color=red>";
	echo $error;
	echo "</font><br>\n";
    }
    else {
	echo $template->PageHeader();
	echo "<br><br>\n";
103
    
104 105 106 107
	echo "<center>\n";
	echo "<font size=+1>";

	if (isset($instance)) {
108 109 110 111 112
	    echo "Export Instance $iid ";
	    if (isset ($runidx)) {
		$runid = $instance->GetRunID($runidx);
		echo "(Run $runid)";
	    }
113 114 115 116 117 118
	}
	else {
	    echo "Export Template Datastore";
	}
	echo "</font>";
    }
119 120 121
    
    $template->Show();

122 123 124 125 126 127 128 129 130 131
    if (isset($instance)) {
	$url = CreateURL("template_export", $instance);
	if (isset($runidx)) {
	    $url .= "&runidx=$runidx";
	}
    }
    else {
	$url = CreateURL("template_export", $template);
    }
    if (isset($tag)) {
132
	$url .= "&tag=" . urlencode($tag);
133
    }
134 135
    
    echo "<form action='$url' method=post>\n";
136
    echo "<br>\n";
137
    echo "<input type=checkbox name=spew value=1>
138 139
                Save to local disk? [<b>1</b>]\n";
    echo "<br>\n";
140 141 142
    echo "<input type=checkbox name=overwrite value=1>
                Overwrite existing export on server? [<b>2</b>]\n";
    echo "<br>\n";
143 144 145
    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";
146
    if (isset($referrer)) {
147
	$referrer = urlencode($referrer);
148 149
	echo "<input type=hidden name=referrer value=$referrer>\n";
    }
150 151 152 153
    echo "</form>\n";
    echo "</center>\n";
    echo "<blockquote><blockquote>
          <ol>
154 155
            <li> By default, your export will be saved to
                 <tt>$TBPROJ_DIR/$pid/export/$guid/$vers</tt>,
156 157 158 159
                 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.
160 161 162

            <li> When exporting to the above mentioned directory, overwrite
                 any existing export. Otherwise an error will be reported.
163 164
          </ol>
          </blockquote></blockquote>\n";
165 166 167
    return;
}
if (!isset($confirmed)) {
168 169 170
    if (!isset($referrer))
	$referrer = $_SERVER['HTTP_REFERER'];
    
171 172
    PAGEHEADER("Template Export");
    SPITFORM(null);
173 174 175 176 177
    PAGEFOOTER();
    return;
}

# Check spew option.
178
if (isset($spew) && $spew) {
179 180 181 182 183
    $spew = 1;
}
else {
    $spew = 0;
}
184 185 186 187 188 189
if (isset($overwrite) && $overwrite) {
    $overwrite = 1;
}
else {
    $overwrite = 0;
}
190 191 192 193 194 195 196 197 198

#
# Avoid SIGPROF in child.
#
set_time_limit(0);

if (! $spew) {
    PAGEHEADER("Template Export");

199
    echo $template->PageHeader();
200 201 202 203 204 205
    echo "<br><br>\n";

    echo "<script type='text/javascript' language='javascript' ".
	"        src='template_sup.js'>\n";
    echo "</script>\n";

206
    STARTBUSY("Starting export");
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    sleep(1);
}

#
# A cleanup function to keep the child from becoming a zombie, since
# the script is terminated, but the children are left to roam.
#
$fp = 0;

function SPEWCLEANUP()
{
    global $fp;

    if (!$fp || !connection_aborted()) {
	exit();
    }
    pclose($fp);
    exit();
}

#
# Run the backend script.
#
230 231 232 233 234
$export_args = (isset($tag) ? "-t " . escapeshellarg($tag) . " " : "");

if (isset($instance)) {
    $export_args .= "-i $exptidx";
    if (isset($runidx))
235
	$export_args .=  " -r " . escapeshellarg($runidx);
236 237 238 239 240 241 242
}
else {
    $export_args .= "$guid/$vers";
}
if ($overwrite) {
     $export_args = "-o " . $export_args;
}
243

244 245 246 247
if ($spew) {
    ignore_user_abort(1);
    register_shutdown_function("SPEWCLEANUP");

248
    if (($fp = popen("$TBSUEXEC_PATH $uid $unix_pid,$unix_gid ".
249
		     "  webtemplate_export -s $export_args",
250
		     "r"))) {
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
	header("Content-Type: application/x-tar");
	header("Content-Encoding: x-gzip");
	header("Content-Disposition: attachment; filename=$exptidx.tgz");
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Cache-Control: no-cache, must-revalidate");
	header("Pragma: no-cache");
	flush();

	while (!feof($fp)) {
	    $string = fgets($fp, 1024);
	    echo "$string";
	    flush();
	}
	pclose($fp);
	$fp = 0;
    }
    else {
	USERERROR("Could not export", 1);
    }
    return;
}

#
# Standard mode ...
#
276
$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
277
		 "webtemplate_export $export_args",
278 279
		 SUEXEC_ACTION_IGNORE);

280 281 282 283 284 285
/* Clear the 'loading' indicators above */
if ($retval) {
    CLEARBUSY();
}
else {
    STOPBUSY();
286 287 288 289 290 291 292 293 294 295 296 297
}

#
# Fatal Error. Report to the user, even though there is not much he can
# do with the error. Also reports to tbops.
# 
if ($retval < 0) {
    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
}

# User error. Tell user and exit.
if ($retval) {
298 299 300 301 302
    if ($retval == 2) {
	SPITFORM("Export directory already exists! Use the overwrite option.");
	PAGEFOOTER();
	return;
    }
303 304 305 306
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
}

307 308 309 310
if (!isset($referrer)) {
    $referrer = CreateURL("template_show", $template);
}

311
# Zap back to template page.
312
PAGEREPLACE($referrer);
313 314 315 316 317 318

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>