batchexp.in 4.65 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
#!/usr/bin/perl -wT
use English;
use Getopt::Std;

#
# Create a batch experiment.
#
# usage: batchexp <batchfile>
#
sub usage()
{
    print STDOUT "Usage: batchexp <batchfile>\n";
    exit(-1);
}
my  $optlist = "";

#
# Configure variables
#
my $TB       = "@prefix@";
my $DBNAME   = "@TBDBNAME@";

23
24
25
26
27
28
29
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

30
31
my $tbbindir = "$TB/bin/";
my $batchdir = "$TB/batch";
32
my $parser   = "/usr/testbed/libexec/ns2ir/parse.tcl";
33
34
35
36
37
38
39
40
41
42
43
my $projroot = "/proj";
my $dirname;

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

#
# Untaint the path
# 
44
45
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
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
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
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 1) {
    usage();
}
my $tempfile = $ARGV[0];

#
# Untaint the arguments.
#
# Note different taint check (allow /).
if ($tempfile =~ /^([-\@\w.\/]+)$/) {
    $tempfile = $1;
}
else {
    fatal("Tainted argument $tempfile");
}

#
# Parse the batchfile.
#
my $eid;
my $pid;
my $longname;
my $expires;
my $webnsfile;

parse_batchfile($tempfile) or
    fatal("Could not parse batchfile $tempfile");

#
# Sanity check a few things.
#
if (!defined($eid) || !defined($pid) || !defined($longname) ||
    !defined($expires) || !defined($webnsfile)) {
    fatal("Batchfile is incomplete!");
}
$nsfile = "$eid.ns";

#
# Create a subdir in the batch directory to work in.
#
$dirname = "$batchdir/$pid-$eid";

mkdir($dirname, 0775) or
99
    fatal("Could not mkdir $dirname: $!");
100
101

chdir($dirname) or
102
    fatal("Could not chdir to $dirname: $!");
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#
# Copy in the batch file. Web script is responsible for removing the
# original.
#
if (system("/bin/cp", "$tempfile", "batchfile")) {
    fatal("Could not copy $tempfile to $dirname");
}

#
# Now a bunch of DB checks.
#
# First off, get some user information. 
#
$query_result =
118
    DBQuery("SELECT uid from users WHERE unix_uid='$EUID'");
119
120
121
122
123
124
125
126
127
128
129
130

if ($query_result->numrows < 1) {
    fatal("Go Away! You do not exist in the Emulab Database.");
}

@row = $query_result->fetchrow_array();
$uid        = $row[0];

#
# Make sure UID is allowed to create experiments in this project.
#
$query_result =
131
    DBQuery("SELECT trust from proj_memb WHERE uid='$uid' and pid='$pid'");
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

if ($query_result->numrows == 0) {
    fatal("Go Away! You are not a member of project $pid!");
}

@row = $query_result->fetchrow_array();
$trust = $row[0];

if ($trust ne "local_root" &&
    $trust ne "group_root") {
    fatal("Go Away! You are not a trusted member of project $pid!");
}

#
# The pid/eid pair has to be unique. LOCKING!
# 
$query_result =
149
150
    DBQueryFatal("SELECT * FROM experiments ".
		 "WHERE eid='$eid' and pid='$pid'");
151
152
153
154
155
156

if ($query_result->numrows) {
    fatal("Experiment $eid in project $pid already exists!");
}

$query_result =
157
158
    DBQueryFatal("SELECT * FROM batch_experiments ".
		 "WHERE eid='$eid' and pid='$pid'");
159
160
161
162
163
164
165
166
167
168
169
170

if ($query_result->numrows) {
    fatal("Batch experiment $eid in project $pid already exists!");
}

#
# Now we can get the NS file! 
#
if (system("/bin/cp", "$webnsfile", "$nsfile")) {
    fatal("Could not copy $webnsfile to $dirname/$nsfile");
}

171
172
# Run parse in impotent mode on the NS file.  This has no effect but
# will display any errors.
173

174
if (system("$parser -n -a $nsfile") != 0) {
175
176
177
178
179
180
181
    fatal("NS Parse failed!");
}

#
# Gen up the creation time.
#
$created = `date '+%Y:%m:%d %H:%M:%S'`;
182
183
184
if ($created =~ /^([-\@\w: ]+)$/) {
    $created = $1;
}
185
186
187
188
189

#
# Insert the record. We leave this to very last cause the batch daemon
# is looking for batch experiments to run. Easy race avoidance.
#
190
DBQueryFatal("INSERT INTO batch_experiments ".
191
	     "(eid, pid, created, expires, ".
192
	     " name, creator_uid, status) ".
193
	     "VALUES ('$eid', '$pid', '$created', '$expires', ".
194
	     "'$longname', '$uid', 'new')");
195
196
197
198
199
200
201
202
203
204

exit 0;

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

    print STDOUT "$mesg\n";
    print STDOUT "Cleaning up ...\n";

205
    system("/bin/rm", "-rf", "$dirname");
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    exit(-1);
}

#
# Open up the batch file and parse it.
#
sub parse_batchfile()
{
    my($batchfile) = $_[0];

    if (! open(BATCH, "$batchfile")) {
	print STDERR "Could not open $batchfile\n";
	return 0;
    }

    while (<BATCH>) {
	if ($_ =~ /^EID:\s+([-\@\w.]*)/) {
	    $eid = $1;
	    next;
	}
	if ($_ =~ /^PID:\s+([-\@\w.]*)/) {
	    $pid = $1;
	    next;
	}
230
	if ($_ =~ /^name:\s+([-\@\w. ]*)/) {
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
	    $longname = $1;
	    next;
	}
	if ($_ =~ /^expires:\s+([-\@\w.: ]*)$/) {
	    $expires = $1;
	    next;
	}
	if ($_ =~ /^nsfile:\s+([-\@\w.\/]*)/) {
	    $webnsfile = $1;
	    next;
	}
    }
    close(BATCH);
    return 1;
}