#!/usr/bin/perl
-# Batch EXecutor 3.0 -- Insert to Queue
-# (c) 2011-2012 Martin Mares <mj@ucw.cz>
+# Batch EXecutor -- Insert to Queue
+# (c) 2011-2013 Martin Mares <mj@ucw.cz>
use strict;
use warnings;
use Getopt::Long;
use File::stat;
+use File::Spec;
use BEX;
my $given_body;
my $requeue_id;
my $given_subject;
my $given_template;
+my @attach = ();
sub usage() {
print <<AMEN ;
-Usage: bex add [<options>] [!]<machine-or-class> ...
+Usage: bex add [<options>] [!]<machine-or-group> ...
Options:
+-a, --attach=<path> Attach a file or directory to the job
-b, --body=<file> Load job body from the given file
-e, --execute=<command> Set job body to the given command
-g, --go Do not run editor, go enqueue the job immediately
-i, --id=<id> Set job ID of the new job
-q, --queue=<name> Insert new jobs to the given queue
-r, --requeue=<id> Re-queue an existing job instead of creating a new one
+ (and possibly add/replace its attachments)
-s, --subject=<subj> Set subject of the new job
-t, --template=<file> Load job template (headers and body) from the given file
AMEN
}
GetOptions(
+ "a|attach=s" => \@attach,
"b|body=s" => \$given_body,
"e|execute=s" => \$given_execute,
"g|go!" => \$given_go,
my @machines = BEX::Config::parse_machine_list(@ARGV);
@machines or die "No machines match\n";
+# Verify that all attachments exist
+for my $a (@attach) {
+ -f $a || -d $a or die "Attachment $a does not exist\n";
+}
+
my $queue = BEX::Queue->new($queue_name);
my $job;
my $tmp_fn;
if (defined($given_body) || defined($given_execute) || defined($given_id) || defined($given_subject) || defined($given_template)) {
die "Parameters of a requeued job cannot be changed\n";
}
- my $fn = $queue->job_file($requeue_id);
- -f $fn or die "Job $requeue_id not known\n";
+ my $id = $queue->resolve_job_id($requeue_id);
+ my $fn = $queue->job_file($id);
$job = BEX::Job->new_from_file($fn);
} else {
# Create job template
if (!$given_go) {
$tmp_fn = $job->save;
my $orig_stat = stat($tmp_fn) or die;
- system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
+ system $BEX::Config::editor_command, $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
unlink $tmp_fn;
# Put the job to the queue
print "New job ", $job->id, "\n";
$queue->save_job($job);
+
+# Create attachments
+if (@attach) {
+ my $adir = $queue->attachment_dir($job->id);
+ -d $adir || mkdir $adir or die "Cannot create $adir: $!\n";
+ for my $asrc (@attach) {
+ $asrc =~ s{/+$}{};
+ my ($vol, $dir, $base) = File::Spec->splitpath($asrc);
+ my $adest = File::Spec->catfile($adir, $base);
+ my $msg = "Attached";
+ if (-e $adest) {
+ system "/bin/rm", "-rf", $adest;
+ $? and die "Cannot delete old attachment $adest\n";
+ $msg = "Updated attachment";
+ }
+ system "/bin/cp", "-aL", $asrc, $adest;
+ $? and die "Cannot copy attachment $asrc to $adest\n";
+ if (-d $asrc) {
+ print "$msg $base/...\n";
+ } else {
+ print "$msg $base\n";
+ }
+ }
+}
+
+# Enqueue the job on all machines
for my $m (@machines) {
if ($queue->enqueue($m, $job)) {
$queue->update_job_status($m, $job->id, 'NEW');