template_checkout.in 6.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006, 2007 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;

#
# Checkout a copy of a template. Do not be confused by the name; this will
# make a copy of a template in the specified directory and drop a little
# cookie so that we can tell later what template it refers to. It only works
# when called from ops (the directory specified resides on ops, there is
# no remote checkout at this time). Later, you can commit the template, which
# creates a new template (template_modify) and changes the cookie so that the
# current directory refers to the new template.
#
# Eventually support remote checkout via the web interface.
#
# There is no "update" operation, as with "cvs update" so you can easily
# muck up things by having two directories that refer to the same template!
#
# $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
37
	  "Usage: template_checkout [-q] [-u] -f <path> <guid/vers>\n".
38 39
	  "switches and arguments:\n".
	  "-q          - be less chatty\n".
40
	  "-u          - update existing checkout\n".
41 42 43 44
	  "-f          - path to checkout directory on ops (via NFS).\n".
	  "<guid/vers> - GUID and version to swapin\n");
    exit(-1);
}
45
my $optlist	 = "qdf:u";
46 47
my %options      = ();
my $quiet        = 0;
48
my $update       = 0;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
my $debug        = 0;
my $directory;
my $guid;
my $version;

#
# 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";

# Protos
sub ParseArgs();
sub fatal($);
sub cleanup();

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libtblog;
use Template;
use Experiment;
use Archive;

#
# 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.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
}
my $user_name  = $this_user->name();
my $user_email = $this_user->email();
my $user_uid   = $this_user->uid();

#
# Before doing anything else, check for overquota ... lets not waste
# our time. Make sure user sees the error by exiting with 1.
#
if (system("$checkquota $user_uid") != 0) {
    tberror("You are over your disk quota on $CONTROL; ".
	    "please login there and cleanup!");
    exit(1);
}

# Now parse arguments.
ParseArgs();

# A little cookie file in that indicates what template it refers to. 
my $cookie = "$directory/.template";

129
if (-e $cookie && !$update) {
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    print STDERR "$directory already contains a checkout!\n";
    print STDERR "There is no 'update' operation yet; delete or move it.\n";
    exit(1);
}

my $template = Template->Lookup($guid, $version);
if (!defined($template)) {
    tbdie("Experiment template $guid/$version does not exist!");
}

# Grab the archive tag from the template itself, for an archive checkout.
my $archive_tag = $template->ArchiveTag();
if (!defined($archive_tag) || $archive_tag eq "") {
    tbdie("Could not determine archive_tag for $template!");
}

# And do the checkout to the directory specified.
my $archive = $template->GetArchive();
if (!defined($archive)) {
    tbdie("Could not get archive object for $template!");
}

# This toplevel "exp" directory has turned out to be a pain in the ass.
# What was I thinking?
if (-e "$directory/exp" && ! -l "$directory/exp") {
    print STDERR "Please remove '$directory/exp' first!\n";
    exit(1);
}
if (! -e "$directory/exp") {
    system("cd $directory; ln -s . exp") == 0 or
	tbdie("Could not create exp symlink in $directory!");
}

163 164 165 166 167 168 169 170
#
# No update in place (yet) since we use simple zip files. Move the current
# datastore directory out of the way. 
#
if ($update && -d "$directory/datastore") {
    
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
#
# There is a plain experiment underlying the template, which has a lot
# of stuff we do not want to show the user in the checkout cause its
# pointless stuff. So rather then a toplevel checkout, grab the two
# things we really care about (datastore and the nsfile).
#
if ($archive->Checkout($directory, $template->exptidx(),
		       $archive_tag, "exp/datastore/") != 0) {
    fatal("Could not checkout datastore from $archive_tag in ".
	  "$archive to $directory!");
}
if ($archive->Checkout($directory, $template->exptidx(),
		       $archive_tag, "exp/tbdata/nsfile.ns") != 0) {
    fatal("Could not checkout tbdata/nsfile.ns from $archive_tag in ".
	  "$archive to $directory!");
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}
unlink("$directory/exp");

#
# If the checkout succeeds then drop a little cookie file in that indicates
# what template it refers to. 
#
unlink($cookie)
    if (-e $cookie);

open(COOKIE, "> $cookie") or
    fatal("Could not create $cookie\n");
print COOKIE "# Do not remove this file!\n";
print COOKIE "GUID: $guid/$version\n";
print COOKIE "TIME: " . time() . "\n";
close(COOKIE);
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();
    }

    #
    # Parse out guid/vers argument.
    #
    usage()
	if (@ARGV != 1);
    
    my $tmp = shift(@ARGV);

    if ($tmp =~ /^([\w]*)\/([\d]*)$/) {
	$guid = $1;
	$version = $2;
    }
    else {
	tbdie("Bad data in argument: $tmp");
    }

    if (defined($options{"f"})) {
	$directory = $options{"f"};

        # The Archive library has a nice routine to validate this path.
	if (Archive::ValidatePath(\$directory) != 0) {
	    tberror("Invalid path for checkout $directory");
	    exit(1);
	}
    }
    else {
	tbdie("Must supply a path argument with the -f option!");
    }

    if (defined($options{"q"})) {
	$quiet = 1;
    }
    if (defined($options{"d"})) {
	$debug = 2;
    }
249 250 251
    if (defined($options{"u"})) {
	$update = 1;
    }
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
}

#
# Cleanup the mess.
#
sub cleanup()
{
}

sub fatal($)
{
    my ($msg) = @_;
    
    tberror $msg;
    cleanup();
    exit(-1);
}