archive_control.in 7.98 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 17 18
	"Usage: archive_control [-f] [-t tag] [-m file] commit <pid> <eid>\n".
	"       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:";
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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;

#
# 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();
}
71
if (@ARGV < 3) {
72 73 74 75 76 77 78 79
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"f"})) {
    $force = 1;
}
80 81 82
my $action = shift(@ARGV);
my $pid    = shift(@ARGV);
my $eid    = shift(@ARGV);
83 84 85 86

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

#
# Do not allow an archive system to be controlled if the experiment is not
# active or swapped. Will probably be changed later.
#
if ($expstate ne EXPTSTATE_ACTIVE && 
    $expstate ne EXPTSTATE_SWAPPED) {
    fatal("Experiment $pid/$eid must be active or swapped!");
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# Temporary
libArchive::setdebug(2);

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

149 150 151 152
#
# Allow the user to force a commit of the archive.
#
if ($action eq "commit") {
153 154
    my $tag     = "user_commit";
    my $mfile   = undef;
155 156 157 158 159 160 161 162 163 164

    #
    # Audit this operation for now.
    #
    if (AuditStart(0)) {
	#
	# Parent exits normally
	#
	exit(0);
    }
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    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 (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 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
    if ($NFSTRACESUPPORT) {
	#
	# 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 224
    if (libArchive::TBCommitExperimentArchive($pid, $eid, $tag,
					      1, $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 300 301 302 303
elsif ($action eq "addtoarchive") {
    my $exact   = 1;
    my $userdir = TBExptUserDir($pid, $eid);
    my $errors  = 0;
	
    # This is special by convention;
    my $subdir = "$userdir/archive";

    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") {
	#
304
	# Figure out what files were accessed by NFS, but are not in
305 306 307
	# archive directory. 
	#
	# This is special by convention;
308
	$subdir = "/archive";
309 310 311 312 313 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 365 366 367 368
    }
    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()) {
	if ($fn =~ /^\/share/ ||
	    $fn =~ /\/tbdata\// ||
	    $fn =~ /^<u:/) {
	    next;
	}
	print "$fn\n"
	    if (! exists($filehash{$fn}));
    }
}
369 370 371 372 373 374 375 376 377 378
exit(0);

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

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