]> mj.ucw.cz Git - bex.git/blob - lib/bin/add
Updated command usage
[bex.git] / lib / bin / add
1 #!/usr/bin/perl
2 # Batch EXecutor 3.0 -- Insert to Queue
3 # (c) 2011-2012 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7 use Getopt::Long;
8 use File::stat;
9 use BEX;
10
11 my $given_body;
12 my $given_go;
13 my $given_id;
14 my $queue_name;
15 my $requeue_id;
16 my $given_subject;
17 my $given_template;
18
19 GetOptions(
20         "b|body=s" => \$given_body,
21         "g|go!" => \$given_go,
22         "i|id=s" => \$given_id,
23         "q|queue=s" => \$queue_name,
24         "r|requeue=s" => \$requeue_id,
25         "s|subject=s" => \$given_subject,
26         "t|template=s" => \$given_template,
27 ) or die <<AMEN ;
28 Usage: bex add [<options>] [!]<machine-or-class> ...
29
30 Options:
31 -b, --body=<file>       Load job body from the given file
32 -g, --go                Do not run editor, go enqueue the job immediately
33 -i, --id=<id>           Set job ID of the new job
34 -q, --queue=<name>      Insert new jobs to the given queue
35 -r, --requeue=<id>      Re-queue an existing job instead of creating a new one
36 -s, --subject=<subj>    Set subject of the new job
37 -t, --template=<file>   Load job template (headers and body) from the given file
38 AMEN
39
40 # Prepare machine set
41 @ARGV or die "No machines specified\n";
42 my @machines = BEX::Config::parse_machine_list(@ARGV);
43 @machines or die "No machines match\n";
44
45 my $queue = BEX::Queue->new($queue_name);
46 my $job;
47 my $tmp_fn;
48
49 if (defined $requeue_id) {
50         # When requeueing, just fetch the existing job
51         if (defined($given_body) || defined($given_id) || defined($given_subject) || defined($given_template)) {
52                 die "Parameters of a requeued job cannot be changed\n";
53         }
54         my $fn = $queue->job_file($requeue_id);
55         -f $fn or die "Job $requeue_id not known\n";
56         $job = BEX::Job->new_from_file($fn);
57 } else {
58         # Create job template
59         if (defined $given_template) {
60                 $job = BEX::Job->new_from_file($given_template);
61         } else {
62                 $job = BEX::Job->new;
63         }
64         $job->attr('ID', $given_id) if defined $given_id;
65         $job->attr('Subject', $given_subject) if defined $given_subject;
66         if (defined $given_body) {
67                 open B, '<', $given_body or die "Cannot open $given_body: $!\n";
68                 local $/;
69                 $job->attr('body', <B>);
70                 close B;
71         }
72
73         # Let the user edit the template
74         if (!$given_go) {
75                 $tmp_fn = $job->save;
76                 my $orig_stat = stat($tmp_fn) or die;
77                 system "editor", $tmp_fn and die "Editor exited with an error, file kept as $tmp_fn\n";
78                 my $new_stat = stat($tmp_fn) or die "File $tmp_fn disappeared under my hands: $!\n";
79                 if ($new_stat->mtime <= $orig_stat->mtime && $new_stat->size == $orig_stat->size) {
80                         unlink $tmp_fn;
81                         die "Cancelled\n";
82                 }
83                 $job = BEX::Job->new_from_file($tmp_fn);
84         }
85 }
86
87 # Put the job to the queue
88 print "New job ", $job->id, "\n";
89 for my $m (@machines) {
90         if ($queue->enqueue($m, $job)) {
91                 $queue->update_job_status($m, $job->id, 'NEW');
92                 print "\t$m\n";
93         } else {
94                 $queue->log($m, $job->id, 'REQUEUE');
95                 print "\t$m (already queued)\n";
96         }
97 }
98
99 # Remove the temporary file if there's any
100 unlink $tmp_fn if defined $tmp_fn;