1 |
#!/usr/bin/env perl
|
2 |
|
3 |
use strict;
|
4 |
use Mysql;
|
5 |
|
6 |
sub getWorkingDir;
|
7 |
sub dbUpdate;
|
8 |
sub dbStatus;
|
9 |
sub dbReport;
|
10 |
sub dbInsert;
|
11 |
sub addSlashes;
|
12 |
sub getOption;
|
13 |
sub deleteEntry;
|
14 |
|
15 |
our $db = Mysql->connect ("cmshead.mps.ohio-state.edu", "ntuple", "osuT3User") or die "Failed to connect to Tier 3, stopped";
|
16 |
|
17 |
my $arg = join (" ", @ARGV);
|
18 |
if ($arg =~ m/-create/)
|
19 |
{
|
20 |
my $crabCfgName = $arg;
|
21 |
$crabCfgName =~ s/.*-cfg *([^ \t\n]*).*/$1/;
|
22 |
open (CRAB_CFG, "<$crabCfgName");
|
23 |
my @crabCfg = <CRAB_CFG>;
|
24 |
close (CRAB_CFG);
|
25 |
my $dataset = getOption (\@crabCfg, "datasetpath");
|
26 |
my $psetName = getOption (\@crabCfg, "pset");
|
27 |
my $jsonFileName = getOption (\@crabCfg, "lumi_mask");
|
28 |
my $workingDir = getOption (\@crabCfg, "ui_working_dir");
|
29 |
my $publishName = "";
|
30 |
$publishName = getOption (\@crabCfg, "publish_data_name") if getOption (\@crabCfg, "publish_data") eq "1";
|
31 |
my $stageOutLocation = getOption (\@crabCfg, "storage_element");
|
32 |
|
33 |
print "Ntuple format (BEAN): ";
|
34 |
my $format = <STDIN>;
|
35 |
$format =~ s/\n//g;
|
36 |
$format = "BEAN" if !$format;
|
37 |
print "Comment for the ntuple database: ";
|
38 |
my $comment = <STDIN>;
|
39 |
$comment =~ s/\n//g;
|
40 |
|
41 |
system ("rm -f .crabSubmit.log ; crab $arg 2>&1 | tee .crabSubmit.log");
|
42 |
|
43 |
my $status = "created";
|
44 |
$status = "submitted" if $arg =~ m/-submit/;
|
45 |
my $hostname = `/bin/hostname`;
|
46 |
$hostname =~ s/[\f\n\r]//g;
|
47 |
my $results = dbInsert ($dataset, "$ENV{'USER'}\@$hostname", $format, "", 0, 0, $status, $comment, $psetName, $crabCfgName, $jsonFileName, $workingDir, $publishName, $stageOutLocation);
|
48 |
}
|
49 |
elsif ($arg =~ m/-submit/)
|
50 |
{
|
51 |
my $workingDir = getWorkingDir (\@ARGV);
|
52 |
my $id = -1;
|
53 |
if (-e "$workingDir/log/.osuID")
|
54 |
{
|
55 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
56 |
$id = <ID_FILE>;
|
57 |
close (ID_FILE);
|
58 |
$id =~ s/(.*)\n/$1/;
|
59 |
}
|
60 |
my $logFile = $workingDir . "/log/crabSubmit.log";
|
61 |
|
62 |
system ("crab $arg 2>&1 | tee -a .crabSubmit.log ; if [ -e \"$workingDir/log/crabSubmit.log\" ] ; then rm -f .crabSubmit.log ; else mv -f .crabSubmit.log $workingDir/log/crabSubmit.log ; fi");
|
63 |
|
64 |
my $hostname = `/bin/hostname`;
|
65 |
$hostname =~ s/[\f\n\r]//g;
|
66 |
my $results = dbUpdate ($id, "submitted", "$ENV{'USER'}\@$hostname", $logFile) if $id > -1;
|
67 |
}
|
68 |
elsif ($arg =~ m/-report/)
|
69 |
{
|
70 |
my $workingDir = getWorkingDir (\@ARGV);
|
71 |
my $id = -1;
|
72 |
if (-e "$workingDir/log/.osuID")
|
73 |
{
|
74 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
75 |
$id = <ID_FILE>;
|
76 |
close (ID_FILE);
|
77 |
$id =~ s/(.*)\n/$1/;
|
78 |
}
|
79 |
my $lumiSummaryName = $workingDir . "/res/lumiSummary.json";
|
80 |
|
81 |
system ("crab $arg");
|
82 |
|
83 |
my $hostname = `/bin/hostname`;
|
84 |
$hostname =~ s/[\f\n\r]//g;
|
85 |
my $results = dbReport ($id, $lumiSummaryName, "$ENV{'USER'}\@$hostname") if $id > -1;
|
86 |
}
|
87 |
elsif ($arg =~ m/-status/)
|
88 |
{
|
89 |
my $workingDir = getWorkingDir (\@ARGV);
|
90 |
my $id = -1;
|
91 |
if (-e "$workingDir/log/.osuID")
|
92 |
{
|
93 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
94 |
$id = <ID_FILE>;
|
95 |
close (ID_FILE);
|
96 |
$id =~ s/(.*)\n/$1/;
|
97 |
}
|
98 |
my $currentStatusName = "$workingDir/log/.currentStatus";
|
99 |
unlink ($currentStatusName) if -e $currentStatusName;
|
100 |
system ("crab $arg | tee $currentStatusName");
|
101 |
my $hostname = `/bin/hostname`;
|
102 |
$hostname =~ s/[\f\n\r]//g;
|
103 |
my $results = dbStatus ($id, $currentStatusName, "$ENV{'USER'}\@$hostname") if $id > -1;
|
104 |
unlink ($currentStatusName);
|
105 |
}
|
106 |
elsif ($arg =~ m/-kill/)
|
107 |
{
|
108 |
my $workingDir = getWorkingDir (\@ARGV);
|
109 |
my $id = -1;
|
110 |
if (-e "$workingDir/log/.osuID")
|
111 |
{
|
112 |
open (ID_FILE, "<$workingDir/log/.osuID");
|
113 |
$id = <ID_FILE>;
|
114 |
close (ID_FILE);
|
115 |
$id =~ s/(.*)\n/$1/;
|
116 |
}
|
117 |
|
118 |
print "Are you cancelling this task? (y/N): ";
|
119 |
my $response = <STDIN>;
|
120 |
$response =~ s/\n//g;
|
121 |
deleteEntry ($id) if lc ($response) eq "y";
|
122 |
system ("crab $arg");
|
123 |
}
|
124 |
else
|
125 |
{
|
126 |
system ("crab $arg");
|
127 |
}
|
128 |
|
129 |
sub
|
130 |
getWorkingDir
|
131 |
{
|
132 |
my $argv = shift;
|
133 |
|
134 |
my $workingDir;
|
135 |
my $nextToLast = 0;
|
136 |
foreach my $argument (@$argv)
|
137 |
{
|
138 |
if ($nextToLast)
|
139 |
{
|
140 |
$workingDir = $argument;
|
141 |
last;
|
142 |
}
|
143 |
$nextToLast = 1 if ($argument eq "-c" || $argument eq "-continue");
|
144 |
}
|
145 |
|
146 |
return $workingDir;
|
147 |
}
|
148 |
|
149 |
sub
|
150 |
getOption
|
151 |
{
|
152 |
my $file = shift;
|
153 |
my $option = shift;
|
154 |
|
155 |
my $optionValue;
|
156 |
foreach my $line (@$file)
|
157 |
{
|
158 |
next if ($line =~ m/^[ \t\n]*#/);
|
159 |
next if (!($line =~ m/$option *= *[^ \t\n]*/));
|
160 |
$optionValue = $line;
|
161 |
$optionValue =~ s/.*$option *= *([^ \t\n]*).*\n/$1/;
|
162 |
}
|
163 |
|
164 |
return $optionValue;
|
165 |
}
|
166 |
|
167 |
sub
|
168 |
addSlashes
|
169 |
{
|
170 |
my $string = shift;
|
171 |
|
172 |
$string =~ s/\\/\\\\/g;
|
173 |
$string =~ s/'/\\'/g;
|
174 |
$string =~ s/"/\\"/g;
|
175 |
$string =~ s/\\0/\\\\0/g;
|
176 |
|
177 |
return $string;
|
178 |
}
|
179 |
|
180 |
sub
|
181 |
dbInsert
|
182 |
{
|
183 |
my $dataset = shift;
|
184 |
my $userAndHost = shift;
|
185 |
my $format = shift;
|
186 |
my $location = shift;
|
187 |
my $nFiles = shift;
|
188 |
my $sizeInGB = shift;
|
189 |
my $status = shift;
|
190 |
my $comment = shift;
|
191 |
my $psetName = shift;
|
192 |
my $crabCfgName = shift;
|
193 |
my $jsonFileName = shift;
|
194 |
my $workingDir = shift;
|
195 |
my $publishName = shift;
|
196 |
my $stageOutLocation = shift;
|
197 |
|
198 |
my $user = $userAndHost;
|
199 |
$user =~ s/@.*$//;
|
200 |
|
201 |
open (PY_CONFIG, "<$psetName");
|
202 |
my @pset = <PY_CONFIG>;
|
203 |
close (PY_CONFIG);
|
204 |
my $pset = join ("", @pset);
|
205 |
open (CRAB_CONFIG, "<$crabCfgName");
|
206 |
my @crabCfg = <CRAB_CONFIG>;
|
207 |
close (CRAB_CONFIG);
|
208 |
my $crabCfg = join ("", @crabCfg);
|
209 |
open (JSON_FILE, "<$jsonFileName");
|
210 |
my @jsonFile = <JSON_FILE>;
|
211 |
close (JSON_FILE);
|
212 |
my $jsonFile = join ("", @jsonFile);
|
213 |
|
214 |
$dataset = addSlashes ($dataset);
|
215 |
$user = addSlashes ($user);
|
216 |
$format = addSlashes ($format);
|
217 |
$pset = addSlashes ($pset);
|
218 |
$crabCfg = addSlashes ($crabCfg);
|
219 |
$jsonFile = addSlashes ($jsonFile);
|
220 |
$location = addSlashes ($location);
|
221 |
$status = addSlashes ($status);
|
222 |
$comment = addSlashes ($comment);
|
223 |
$publishName = addSlashes ($publishName);
|
224 |
$stageOutLocation = addSlashes ($stageOutLocation);
|
225 |
|
226 |
my $results;
|
227 |
my $query = "select max(id) from ntuple";
|
228 |
$db->selectdb ("ntuple");
|
229 |
$results = $db->query ($query);
|
230 |
my @row = $results->fetchrow ();
|
231 |
my $id = 1;
|
232 |
$id = $row[0] + 1 if $results->numrows ();
|
233 |
|
234 |
open (ID_FILE, ">$workingDir/log/.osuID");
|
235 |
print ID_FILE "$id\n";
|
236 |
close (ID_FILE);
|
237 |
|
238 |
$query = "insert into ntuple (id, dataset, creationTime, lastUpdateTime, lastUpdateUser, user, format, location, nFiles, sizeInGB, status, comment, pset, crabCfg, jsonFile, publishName, stageOutLocation, published) values ($id, '$dataset', now(), now(), '$userAndHost', '$user', '$format', '$location', $nFiles, $sizeInGB, '$status', '$comment', '$pset', '$crabCfg', '$jsonFile', '$publishName', '$stageOutLocation', 0)";
|
239 |
$results = $db->query ($query);
|
240 |
|
241 |
return $results;
|
242 |
}
|
243 |
|
244 |
sub
|
245 |
dbUpdate
|
246 |
{
|
247 |
my $id = shift;
|
248 |
my $status = shift;
|
249 |
my $user = shift;
|
250 |
my $logName = shift;
|
251 |
|
252 |
open (LOG, "<$logName");
|
253 |
my @log = <LOG>;
|
254 |
close (LOG);
|
255 |
my $log = join ("", @log);
|
256 |
|
257 |
$status = addSlashes ($status);
|
258 |
$user = addSlashes ($user);
|
259 |
$log = addSlashes ($log);
|
260 |
|
261 |
my $results;
|
262 |
my $query = "update ntuple set lastUpdateTime=now(), lastUpdateUser='$user', status='$status', submissionLog='$log' where id=$id";
|
263 |
$db->selectdb ("ntuple");
|
264 |
$results = $db->query ($query);
|
265 |
|
266 |
return $results;
|
267 |
}
|
268 |
|
269 |
sub
|
270 |
dbStatus
|
271 |
{
|
272 |
my $id = shift;
|
273 |
my $currentStatusName = shift;
|
274 |
my $user = shift;
|
275 |
|
276 |
open (CURRENT_STATUS, "<$currentStatusName");
|
277 |
my @currentStatus = <CURRENT_STATUS>;
|
278 |
close (CURRENT_STATUS);
|
279 |
my $currentStatus = join ("", @currentStatus);
|
280 |
|
281 |
$currentStatus = addSlashes ($currentStatus);
|
282 |
$user = addSlashes ($user);
|
283 |
|
284 |
my $results;
|
285 |
my $query = "update ntuple set lastUpdateTime=now(), lastUpdateUser='$user', currentStatus='$currentStatus' where id=$id";
|
286 |
$db->selectdb ("ntuple");
|
287 |
$results = $db->query ($query);
|
288 |
|
289 |
return $results;
|
290 |
}
|
291 |
|
292 |
sub
|
293 |
dbReport
|
294 |
{
|
295 |
my $id = shift;
|
296 |
my $lumiSummaryName = shift;
|
297 |
my $user = shift;
|
298 |
|
299 |
open (LUMI_SUMMARY, "<$lumiSummaryName");
|
300 |
my @lumiSummary = <LUMI_SUMMARY>;
|
301 |
close (LUMI_SUMMARY);
|
302 |
my $lumiSummary = join ("", @lumiSummary);
|
303 |
|
304 |
$lumiSummary = addSlashes ($lumiSummary);
|
305 |
$user = addSlashes ($user);
|
306 |
|
307 |
my $results;
|
308 |
my $query = "update ntuple set lastUpdateTime=now(), lastUpdateUser='$user', lumiSummary='$lumiSummary' where id=$id";
|
309 |
$db->selectdb ("ntuple");
|
310 |
$results = $db->query ($query);
|
311 |
|
312 |
return $results;
|
313 |
}
|
314 |
|
315 |
sub
|
316 |
deleteEntry
|
317 |
{
|
318 |
my $id = shift;
|
319 |
|
320 |
my $results;
|
321 |
my $query = "delete from ntuple where id=$id";
|
322 |
$db->selectdb ("ntuple");
|
323 |
$results = $db->query ($query);
|
324 |
|
325 |
return $results;
|
326 |
}
|