template_export.php 6.59 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6 7 8 9 10
# All rights reserved.
#
include("defs.php3");
include_once("template_defs.php");

#
11
# Only known and logged in users.
12
#
13 14 15
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
16 17 18

#
# Verify page arguments.
19
#
20 21 22
$optargs = OptionalPageArguments("instance",  PAGEARG_INSTANCE,
				 "template",  PAGEARG_TEMPLATE,
				 "canceled",  PAGEARG_BOOLEAN,
23
				 "confirmed", PAGEARG_BOOLEAN,
24
				 "referrer",  PAGEARG_STRING,
25
				 "runidx",    PAGEARG_INTEGER,
26 27
				 "tag",       PAGEARG_STRING,
				 "overwrite", PAGEARG_BOOLEAN,
28
				 "spew",      PAGEARG_BOOLEAN);
29 30 31 32 33 34 35 36 37 38 39

#
# 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");
}
40

41
if (isset($canceled) && $canceled) {
42 43 44 45
    if (isset($instance))
	header("Location: ". CreateURL("template_show", $template));
    else
	header("Location: ". CreateURL("template_show", $instance));	
46 47 48 49
    return;
}

# Need these below.
50 51 52
$guid     = $template->guid();
$vers     = $template->vers();
$pid      = $template->pid();
53
$unix_gid = $template->UnixGID();
54

55 56
if (! $template->AccessCheck($this_user, $TB_EXPT_READINFO)) {
    USERERROR("You do not have permission to export from template ".
57
	      "$guid/$vers!", 1);
58
}
59 60 61 62 63 64 65
if (isset($instance)) {
    $exptidx  = $instance->exptidx();
    
    if (isset($runidx) && !$instance->ValidRun($runidx)) {
	USERERROR("Run '$runidx' is not a valid run in instance '$exptidx'",
		  1);
    }
66
}
67

68 69
function SPITFORM($error)
{
70
    global $template, $instance, $runidx, $TBPROJ_DIR, $referrer;
71 72
    global $tag, $pid, $guid, $vers;

73 74
    $iid   = $instance->id();

75 76 77 78 79 80 81 82 83
    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";
84
    
85 86 87 88
	echo "<center>\n";
	echo "<font size=+1>";

	if (isset($instance)) {
89 90 91 92 93
	    echo "Export Instance $iid ";
	    if (isset ($runidx)) {
		$runid = $instance->GetRunID($runidx);
		echo "(Run $runid)";
	    }
94 95 96 97 98 99
	}
	else {
	    echo "Export Template Datastore";
	}
	echo "</font>";
    }
100 101 102
    
    $template->Show();

103 104 105 106 107 108 109 110 111 112 113
    if (isset($instance)) {
	$url = CreateURL("template_export", $instance);
	if (isset($runidx)) {
	    $url .= "&runidx=$runidx";
	}
    }
    else {
	$url = CreateURL("template_export", $template);
    }
    if (isset($tag)) {
	$url .= "&tag=$tag";
114
    }
115 116
    
    echo "<form action='$url' method=post>\n";
117
    echo "<br>\n";
118
    echo "<input type=checkbox name=spew value=1>
119 120
                Save to local disk? [<b>1</b>]\n";
    echo "<br>\n";
121 122 123
    echo "<input type=checkbox name=overwrite value=1>
                Overwrite existing export on server? [<b>2</b>]\n";
    echo "<br>\n";
124 125 126
    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";
127 128 129
    if (isset($referrer)) {
	echo "<input type=hidden name=referrer value=$referrer>\n";
    }
130 131 132 133
    echo "</form>\n";
    echo "</center>\n";
    echo "<blockquote><blockquote>
          <ol>
134 135
            <li> By default, your export will be saved to
                 <tt>$TBPROJ_DIR/$pid/export/$guid/$vers</tt>,
136 137 138 139
                 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.
140 141 142

            <li> When exporting to the above mentioned directory, overwrite
                 any existing export. Otherwise an error will be reported.
143 144
          </ol>
          </blockquote></blockquote>\n";
145 146 147
    return;
}
if (!isset($confirmed)) {
148 149 150
    if (!isset($referrer))
	$referrer = $_SERVER['HTTP_REFERER'];
    
151 152
    PAGEHEADER("Template Export");
    SPITFORM(null);
153 154 155 156 157
    PAGEFOOTER();
    return;
}

# Check spew option.
158
if (isset($spew) && $spew) {
159 160 161 162 163
    $spew = 1;
}
else {
    $spew = 0;
}
164 165 166 167 168 169
if (isset($overwrite) && $overwrite) {
    $overwrite = 1;
}
else {
    $overwrite = 0;
}
170 171 172 173 174 175 176 177 178

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

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

179
    echo $template->PageHeader();
180 181 182 183 184 185
    echo "<br><br>\n";

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

186
    STARTBUSY("Starting export");
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    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.
#
210 211 212 213 214
$export_args = (isset($tag) ? "-t " . escapeshellarg($tag) . " " : "");

if (isset($instance)) {
    $export_args .= "-i $exptidx";
    if (isset($runidx))
215
	$export_args .=  " -r " . escapeshellarg($runidx);
216 217 218 219 220 221 222
}
else {
    $export_args .= "$guid/$vers";
}
if ($overwrite) {
     $export_args = "-o " . $export_args;
}
223

224 225 226 227
if ($spew) {
    ignore_user_abort(1);
    register_shutdown_function("SPEWCLEANUP");

228
    if (($fp = popen("$TBSUEXEC_PATH $uid $pid,$unix_gid ".
229
		     "  webtemplate_export -s $export_args",
230
		     "r"))) {
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	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 ...
#
$retval = SUEXEC($uid, "$pid,$unix_gid",
257
		 "webtemplate_export $export_args",
258 259
		 SUEXEC_ACTION_IGNORE);

260 261 262 263 264 265
/* Clear the 'loading' indicators above */
if ($retval) {
    CLEARBUSY();
}
else {
    STOPBUSY();
266 267 268 269 270 271 272 273 274 275 276 277
}

#
# 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) {
278 279 280 281 282
    if ($retval == 2) {
	SPITFORM("Export directory already exists! Use the overwrite option.");
	PAGEFOOTER();
	return;
    }
283 284 285 286
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
}

287 288 289 290
if (!isset($referrer)) {
    $referrer = CreateURL("template_show", $template);
}

291
# Zap back to template page.
292
PAGEREPLACE($referrer);
293 294 295 296 297 298

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