archive_control.in 8.17 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005, 2006 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 18
	"       archive_control [-f] [-a] addfile <pid> <eid> [files ...]\n".
	"       archive_control checktag <pid> <eid> <tag>\n";
19 20
    exit(-1);
}
21
my $optlist = "dfat:m:u";
22 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
my $SHAREROOT = SHAREROOT();

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#
# Verify user and get his DB uid.
#
if (! UNIX2DBUID($UID, \$dbuid)) {
    die("*** $0:\n".
	"    You do not exist in the Emulab Database!\n");
}

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

#
# Untaint args.
#
89
if ($action =~ /^(commit|addfile|checktag|list|missing|addtoarchive)$/) {
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
    $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.");
}

if (! ($expstate = ExpState($pid, $eid))) {
    fatal("No such experiment $pid/$eid!");
}

#
# Check permission. Only people with permission to destroy the experiment
# can do this.
#
if (! TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_UPDATE)) {
    fatal("You do not have permission to control the archive for $pid/$eid!");
}

120
# Temporary
121
libArchive::setdebug($debug);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

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

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

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

160 161 162 163 164 165 166 167 168 169
    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;
170 171 172 173

	if (defined($options{"u"})) {
	    $usertag = 1;
	}
174
    }
175
    
176 177 178 179 180 181 182 183 184 185 186 187 188 189
    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");
	}
    }
    
190
    if (0 && $NFSTRACESUPPORT) {
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
	#
	# 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!");
    }

221
    # And commit the archive.
222
    print "Doing a commit on the experiment archive ...\n";
223
    if (libArchive::TBCommitExperimentArchive($pid, $eid, $tag,
224
					      $usertag, $mfile) < 0) {
225 226 227
	  fatal("Failed to commit experiment archive!");
    }
}
228
elsif ($action eq "addfile") {
229 230
    my $exact   = 0;

231 232 233
    usage()
	if (! @ARGV);

234 235 236 237
    if (defined($options{"a"})) {
	$exact = 1;
    }

238 239 240 241 242 243 244 245 246 247
    # 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!");
	}
    }
}
248 249 250 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
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;
    
    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") {
300 301 302 303 304 305 306 307
	#
	# 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");
	}
 
308
	#
309
	# Figure out what files were accessed by NFS, but are not in
310 311 312
	# archive directory. 
	#
	# This is special by convention;
313
	$subdir = "/archive";
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    }
    elsif (@ARGV) {
	$subdir = $ARGV[0];

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

    my $rval = 
      libArchive::TBListExperimentArchive($pid, $eid, \@files, $tag, $subdir);
    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()) {
365 366
	#print "Accessed: $fn\n";
	
367
	if ($fn =~ /^$SHAREROOT/ ||
368 369 370 371 372 373 374 375
	    $fn =~ /\/tbdata\// ||
	    $fn =~ /^<u:/) {
	    next;
	}
	print "$fn\n"
	    if (! exists($filehash{$fn}));
    }
}
376 377 378 379 380 381 382 383 384 385
exit(0);

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

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