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