savelogs.in 2.95 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2002, 2004, 2007 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5 6
# All rights reserved.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
7
use English;
8 9 10 11 12 13 14 15 16 17 18 19

# Save the tiplogs from an experiment

sub usage {
	print "Usage: $0 pid eid\n";
	exit(-1);
}

#
# Configure variables
#
my $TB          = "@prefix@";
20 21
my $CONTROL	= "@USERNODE@";
my $sshtb	= "$TB/bin/sshtb";
22 23 24 25 26 27 28

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
29 30
use Experiment;
use User;
31

32 33 34
# Locals
my $SAVEUID = $UID;

Robert Ricci's avatar
Robert Ricci committed
35 36 37 38 39
#
# Turn off line buffering on output
#
$| = 1;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#
# We don't want to run this script unless its the real version.
#
if ($EUID != 0) {
    die("*** $0:\n".
        "    Must be root! Maybe its a development version?\n");
}

#
# Please run as yourself
# 
if ($UID == 0) {
    die("*** $0:\n".
        "    Please do not run this script as root!\n");
}

56 57 58 59 60 61
#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

62
if (@ARGV != 2) {
63
    usage();
64 65 66 67
}

my ($pid,$eid) = @ARGV;

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#
# Untaint args.
#
if ($pid =~ /^([-\w]+)$/) {
    $pid = $1;
}
else {
    die("*** Bad data in pid: $pid.\n");
}
if ($eid =~ /^([-\w]+)$/) {
    $eid = $1;
}
else {
    die("*** Bad data in eid: $eid.\n");
}
83 84 85 86 87
my $experiment = Experiment->Lookup($pid, $eid);
if (!defined($experiment)) {
    die("*** $0:\n".
	"    Could not lookup experiment object!\n");
}
88

89 90 91 92 93
#
# Verify user and get his DB uid and other info for later.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
94 95
    die("*** $0:\n".
	"    You do not exist in the Emulab Database!\n");
96
}
97
my $user_uid   = $this_user->uid();
98

99 100 101 102 103
#
# Verify permission to muck with this experiment. This is to head off
# permission problems early; the nodes are indvidually checked later
# in the library.
#
104 105
if (!$this_user->IsAdmin() &&
    !$experiment->AccessCheck($this_user, TB_EXPT_MODIFY)) {
106 107
    die("*** $0:\n".
	"    You do not have permission to save log files in $pid/$eid!\n");
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#
# Find the names of all physical nodes with tip lines. 
#
my $query_result =
    DBQueryFatal("select r.node_id,r.vname,t.server from reserved as r ".
		 "left join nodes as n on n.node_id=r.node_id ".
		 "left join node_types as nt on nt.type=n.type ".
		 "left join tiplines as t on t.node_id=r.node_id ".
		 "where r.pid='$pid' and r.eid='$eid' and ".
		 "      nt.isvirtnode=0 and nt.isremotenode=0 and ".
		 "      t.server is not null");

if ($query_result->numrows == 0) {
    print "No console lines found for experiment $eid in project $pid!\n";
    exit(0);
}
126

127
my $savedir = PROJROOT() . "/$pid/tiplogs/$eid";
128
my $cmdargs = "$TB/sbin/savelogs.proxy -u $user_uid -s $savedir ";
129

130 131
while (my ($nodeid, $vname) = $query_result->fetchrow_array()) {
    $cmdargs .= " $nodeid $vname";
132 133
}

134 135 136 137 138 139
#
# Now we call over to the ops node to deal with the rest of this. 
#
$EUID = $UID = 0;
system("$sshtb -host $CONTROL $cmdargs");
$EUID = $UID = $SAVEUID;
140

141 142 143 144
#
# Nothing else to do ... output from proxy went to stdout/stderr.
# 
exit($? >> 8);
145