archive_control.in 9.18 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005-2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14
# All rights reserved.
#
use English;
use Getopt::Std;

#
# Command line interface to experiment archive module.
#
sub usage()
{
15
    print STDERR
16
	"Usage: archive_control [-f] [-t tag [-u]] [-m file] commit <pid> <eid>\n".
17
	"       archive_control [-f] [-a] addfile <pid> <eid> [files ...]\n".
18
	"       archive_control tag [-u] [-s subdir] <pid> <eid> <tag>\n".
19
	"       archive_control checktag <pid> <eid> <tag>\n";
20 21
    exit(-1);
}
22
my $optlist = "dfat:m:us:l";
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
my $debug   = 0;
my $force   = 0;
my $dbuid;

#
# Configure variables
#
my $TB		    = "@prefix@";
my $TBOPS           = "@TBOPSEMAIL@";
my $NFSTRACESUPPORT = @NFSTRACESUPPORT@;
my $NFSTRACE	    = "$TB/sbin/nfstrace";

# Protos
sub fatal($);

#
# Turn off line buffering on output
#
$| = 1;

# un-taint path
$ENV{'PATH'} = "/bin:/usr/bin:/usr/local/bin:$TB/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use libaudit;
use libArchive;
55 56
use User;
use Experiment;
57

58 59
my $SHAREROOT = SHAREROOT();

60
#
61
# Map invoking user to object.
62
#
63 64 65
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
66 67 68 69 70 71 72 73 74 75
}

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
76
if (@ARGV < 3) {
77 78 79
    usage();
}
if (defined($options{"d"})) {
80
    $debug = 2;
81 82 83 84
}
if (defined($options{"f"})) {
    $force = 1;
}
85 86 87
my $action = shift(@ARGV);
my $pid    = shift(@ARGV);
my $eid    = shift(@ARGV);
88 89 90 91

#
# Untaint args.
#
92
if ($action =~ /^(commit|addfile|checktag|tag|list|missing|addtoarchive)$/) {
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    $action = $1;
}
else {
    usage();
}
if ($pid =~ /^([-\w]+)$/) {
    $pid = $1;
}
else {
    die("Bad data in pid: $pid.");
}
if ($eid =~ /^([-\w]+)$/) {
    $eid = $1;
}
else {
    die("Bad data in eid: $eid.");
}

111 112
my $experiment = Experiment->Lookup($pid, $eid);
if (! $experiment) {
113 114
    fatal("No such experiment $pid/$eid!");
}
115
$expstate = $experiment->state();
116 117

#
118
# Check permission. 
119
#
120
if (! $experiment->AccessCheck($this_user, TB_EXPT_UPDATE)) {
121 122 123
    fatal("You do not have permission to control the archive for $pid/$eid!");
}

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
# Do not audit this operation.
if ($action eq "checktag") {
    usage()
	if (! @ARGV);

    my $tag = $ARGV[0];

    if (! TBcheck_dbslot($tag, "archive_tags", "tag",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	fatal("Illegal characters in tag");
    }
    my $unique;

    fatal("Error checking tag for uniqueness!")
	if (libArchive::TBIsTaqUnique($pid, $eid, $tag, \$unique) < 0);

    exit(($unique ? 0 : 1));
}

143 144 145 146
#
# Do not allow an archive system to be controlled if the experiment is not
# active or swapped. Will probably be changed later.
#
147 148
if ($expstate ne EXPTSTATE_ACTIVE &&
    $expstate ne EXPTSTATE_TERMINATING &&
Leigh Stoller's avatar
Leigh Stoller committed
149
    $expstate ne EXPTSTATE_ACTIVATING &&
150 151 152 153
    $expstate ne EXPTSTATE_SWAPPED) {
    fatal("Experiment $pid/$eid must be active or swapped!");
}

154 155 156 157
#
# Allow the user to force a commit of the archive.
#
if ($action eq "commit") {
158 159
    my $tag     = "commit";
    my $usertag = 0;
160
    my $mfile   = undef;
161

162 163 164 165 166 167 168 169 170 171
    if (defined($options{"t"})) {
	$tag = $options{"t"};

	if (! TBcheck_dbslot($tag, "archive_tags", "tag",
			   TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    fatal("Illegal characters in tag");
	}
	# Force a taint check; the library will escape it for the shell.
	$tag =~ /(.*)/;
	$tag = $1;
172 173 174 175

	if (defined($options{"u"})) {
	    $usertag = 1;
	}
176
    }
177
    
178 179 180 181 182 183 184 185 186 187 188 189 190 191
    if (defined($options{"m"})) {
	#
	# Argument is a pathname to a tempfile.
	#
	$mfile = $options{"m"};

	if ($mfile =~ /^([-\w\/\.]+)$/) {
	    $mfile = $1;
	}
	else {
	    fatal("Illegal characters in $mfile");
	}
    }
    
192
    if (0 && $NFSTRACESUPPORT) {
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
	#
	# This program asks the nodes for the trace results.
	#
	print "Getting files accessed via NFS.\n";
	system("$NFSTRACE transfer $pid $eid");
 
	#
	# Add the files that have been detected by tracing to the archive.
	#
	if (libArchive::TBExperimentArchiveAddTracedFiles($pid, $eid) < 0) {
	    fatal("Failed to add traced files to the experiment archive!");
	}
    }

    #
    # Add the special per-experiment archive directory.
    #
    print "Getting user added files.\n";
    if (libArchive::TBExperimentArchiveAddUserFiles($pid, $eid) < 0) {
	fatal("Failed to add user specified files to the experiment archive!");
    }

    #
    # Do a SavePoint on the experiment files.
    #
    print "Doing a savepoint on the experiment archive ...\n";
    if (libArchive::TBExperimentArchiveSavePoint($pid, $eid) < 0) {
	fatal("Failed to do a savepoint on the experiment archive!");
    }

223
    # And commit the archive.
224
    print "Doing a commit on the experiment archive ...\n";
225
    if (libArchive::TBCommitExperimentArchive($pid, $eid, $tag,
226
					      $usertag, $mfile) < 0) {
227 228 229
	  fatal("Failed to commit experiment archive!");
    }
}
230
elsif ($action eq "tag") {
Leigh Stoller's avatar
Leigh Stoller committed
231 232 233 234
    my $tag = "tag";
    
    $tag = $ARGV[0]
	if (@ARGV);
235 236 237 238 239 240 241 242 243 244

    if (! TBcheck_dbslot($tag, "archive_tags", "tag",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	fatal("Illegal characters in tag");
    }
    # Force a taint check; the library will escape it for the shell.
    $tag =~ /(.*)/;
    $tag = $1;

    my $usertag = 0;
Leigh Stoller's avatar
Leigh Stoller committed
245
    if (scalar(@ARGV) && defined($options{"u"})) {
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	$usertag = 1;
    }

    # Subdir to place the tag into.
    my $subdir = undef;
    if (defined($options{"s"})) {
	$subdir = $options{"s"};

	if ($subdir =~ /^([-\w\/]+)$/) {
	    $subdir = $1;
	}
	else {
	    fatal("Illegal characters in subdir");
	}
    }
    print "Adding tag $tag to the experiment archive ...\n";
    if (libArchive::TBTagExperimentArchive($pid, $eid, $tag,
					   $usertag, $subdir) < 0) {
	  fatal("Failed to tag experiment archive!");
    }
}
267
elsif ($action eq "addfile") {
268 269
    my $exact   = 0;

270 271 272
    usage()
	if (! @ARGV);

273 274 275 276
    if (defined($options{"a"})) {
	$exact = 1;
    }

277 278 279 280 281 282 283 284 285 286
    # The Archive library does a taint check on the pathnames.
    while (@ARGV) {
	my $pathname = shift(@ARGV);

	if (libArchive::TBExperimentArchiveAddFile($pid, $eid,
						   $pathname, $exact) < 0) {
	    fatal("Failed to add '$pathname' to experiment archive!");
	}
    }
}
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
elsif ($action eq "addtoarchive") {
    my $exact   = 1;
    my $userdir = TBExptUserDir($pid, $eid);
    my $errors  = 0;
	
    usage()
	if (! @ARGV);

    while (@ARGV) {
	my $pathname = shift(@ARGV);

	if (! -e $pathname) {
	    print STDERR
		"*** '$pathname' does not exist! Skipping ...\n";
	    $errors++;
	    next;
	}
	if (! -f $pathname) {
	    print STDERR
		"*** '$pathname' is not a plain file! Skipping ...\n";
	    $errors++;
	    next;
	}

	# This returns a taint checked value in $pathname.
	if (libArchive::ValidatePath(\$pathname) != 0) {
	    print STDERR "Could not validate pathname $pathname\n";
	    $errors++;
	    next;
	}
    }
    exit($errors);
}
elsif ($action eq "list" ||
       $action eq "missing") {
    my @files    = ();
    my $tag      = undef;
    my $subdir   = undef;
325
    my $long     = (defined($options{"l"}) ? 1 : 0);
326 327 328 329 330 331 332 333 334 335 336 337 338 339
    
    if (defined($options{"t"})) {
	$tag = $options{"t"};

	if (! TBcheck_dbslot($tag, "archive_tags", "tag",
			   TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    fatal("Illegal characters in tag");
	}
	# Force a taint check; the library will escape it for the shell.
	$tag =~ /(.*)/;
	$tag = $1;
    }

    if ($action eq "missing") {
340 341 342 343 344 345 346 347
	#
	# This program asks the nodes for the trace results.
	#
	if ($expstate ne EXPTSTATE_ACTIVE) {
	    print "Getting files accessed via NFS.\n";
	    system("$NFSTRACE transfer $pid $eid");
	}
 
348
	#
349
	# Figure out what files were accessed by NFS, but are not in
350 351 352
	# archive directory. 
	#
	# This is special by convention;
353
	$subdir = "/archive";
354 355 356 357 358 359 360 361 362 363 364 365 366 367
    }
    elsif (@ARGV) {
	$subdir = $ARGV[0];

	# Taint check path before handing off.
	if ($subdir =~ /^([-\w\/\.\+\@,~]+)$/) {
	    $subdir = $1;
	}
	else {
	    fatal("Illegal characters in subdir");
	}
    }

    my $rval = 
368 369
      libArchive::TBListExperimentArchive($pid, $eid, \@files,
					  $long, $tag, $subdir);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    exit($rval)
	if ($rval);

    if ($action eq "list") {
	foreach my $file (@files) {
	    print "$file\n";
	}
	exit(0);
    }
    #
    # Grab the NFS accessed files.
    #
    my %filehash = ();
    
    foreach my $file (@files) {
	my $path = "$subdir/$file";

	#print "Archived: $path\n";
	$filehash{$path} = 1;
    }
    
    my $query_result =
	DBQueryFatal("SELECT s.rsrcidx FROM experiments as e ".
		     "left join experiment_stats as s on e.idx=s.exptidx ".
		     "where e.pid='$pid' and e.eid='$eid'");
    
    if ($query_result->num_rows() != 1) {
	fatal("Experiment $pid/$eid has no stats record!");
    }
    my ($rsrcidx) = $query_result->fetchrow_array();

    $query_result = 
	DBQueryFatal("SELECT af.fn FROM fs_resources as fr ".
		     "LEFT JOIN accessed_files as af on af.idx=fr.fileidx ".
		     "WHERE fr.rsrcidx=$rsrcidx and fr.type!='l'");
    while (my ($fn) = $query_result->fetchrow_array()) {
406 407
	#print "Accessed: $fn\n";
	
408
	if ($fn =~ /^$SHAREROOT/ ||
409 410 411 412 413 414 415 416
	    $fn =~ /\/tbdata\// ||
	    $fn =~ /^<u:/) {
	    next;
	}
	print "$fn\n"
	    if (! exists($filehash{$fn}));
    }
}
417 418 419 420 421 422 423 424 425 426
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}