2 # Batch EXecutor 3.0 -- Insert to Queue
3 # (c) 2011-2013 Martin Mares <mj@ucw.cz>
24 Usage: bex add [<options>] [!]<machine-or-class> ...
27 -a, --attach=<path> Attach a file or directory to the job
28 -b, --body=<file> Load job body from the given file
29 -e, --execute=<command> Set job body to the given command
30 -g, --go Do not run editor, go enqueue the job immediately
31 -i, --id=<id> Set job ID of the new job
32 -q, --queue=<name> Insert new jobs to the given queue
33 -r, --requeue=<id> Re-queue an existing job instead of creating a new one
34 (and possibly add/replace its attachments)
35 -s, --subject=<subj> Set subject of the new job
36 -t, --template=<file> Load job template (headers and body) from the given file
42 "a|attach=s" => \@attach,
43 "b|body=s" => \$given_body,
44 "e|execute=s" => \$given_execute,
45 "g|go!" => \$given_go,
46 "i|id=s" => \$given_id,
47 "q|queue=s" => \$queue_name,
48 "r|requeue=s" => \$requeue_id,
49 "s|subject=s" => \$given_subject,
50 "t|template=s" => \$given_template,
52 ) or die "Try `bex add --help' for more information.\n";
55 @ARGV or die "No machines specified\n";
56 my @machines = BEX::Config::parse_machine_list(@ARGV);
57 @machines or die "No machines match\n";
59 # Verify that all attachments exist
61 -f $a || -d $a or die "Attachment $a does not exist\n";
64 my $queue = BEX::Queue->new($queue_name);
68 if (defined $requeue_id) {
69 # When requeueing, just fetch the existing job
70 if (defined($given_body) || defined($given_execute) || defined($given_id) || defined($given_subject) || defined($given_template)) {
71 die "Parameters of a requeued job cannot be changed\n";
73 my $fn = $queue->job_file($requeue_id);
74 -f $fn or die "Job $requeue_id not known\n";
75 $job = BEX::Job->new_from_file($fn);
78 if (defined $given_template) {
79 $job = BEX::Job->new_from_file($given_template);
83 $job->attr('ID', $given_id) if defined $given_id;
84 $job->attr('Subject', $given_subject) if defined $given_subject;
85 if (defined $given_execute) {
86 $job->attr('body', $given_execute. "\n");
87 } elsif (defined $given_body) {
88 open B, '<', $given_body or die "Cannot open $given_body: $!\n";
90 $job->attr('body', <B>);
94 # Let the user edit the template
97 my $orig_stat = stat($tmp_fn) or die;
98 system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
99 my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
100 if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
104 $job = BEX::Job->new_from_file($tmp_fn);
108 # Put the job to the queue
109 print "New job ", $job->id, "\n";
110 $queue->save_job($job);
114 my $adir = $queue->attachment_dir($job->id);
115 -d $adir || mkdir $adir or die "Cannot create $adir: $!\n";
116 for my $asrc (@attach) {
118 my ($vol, $dir, $base) = File::Spec->splitpath($asrc);
119 my $adest = File::Spec->catfile($adir, $base);
120 my $msg = "Attached";
122 system "/bin/rm", "-rf", $adest;
123 $? and die "Cannot delete old attachment $adest\n";
124 $msg = "Updated attachment";
126 system "/bin/cp", "-a", $asrc, $adest;
127 $? and die "Cannot copy attachment $asrc to $adest\n";
129 print "$msg $base/...\n";
131 print "$msg $base\n";
136 # Enqueue the job on all machines
137 for my $m (@machines) {
138 if ($queue->enqueue($m, $job)) {
139 $queue->update_job_status($m, $job->id, 'NEW');
142 $queue->log($m, $job->id, 'REQUEUE');
143 print "\t$m (already queued)\n";
147 # Remove the temporary file if there's any
148 unlink $tmp_fn if defined $tmp_fn;