helios_job_submit.pl untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 127 CC 1 SCAR 6.9
CC
1
Cov
0%
validateParamsXML
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 use 5.008;
4 use strict;
5 use warnings;
6  
7 use Helios::Job;
8 use Helios::Config;
9  
10 our $VERSION = '2.82';
11  
12 # CHANGES:
13 # 2012-01-22: Minor changes to comments.
14 # [LH] 2013-08-04: Changed service instantiation to use new Helios::Config API.
15 # [LH] [2013-10-28]: Updated to use Helios::Config and new Helios::Job API.
16 # Replaced all try {} with eval {}. Added --verbose option to return jobid.
17 # [LH] [2013-11-01]: changed cmd line arg parsing so -v and -n work as well as
18 # --verbose and --no-validate
19 # [LH] [2013-11-24]: Added new --verbose and jobtype documentation to POD.
20 # Removed old code already commented out.
21 # [LH] [2014-02-28]: Changed shebang line to use env instead of /usr/bin/perl.
22 # [LH] [2014-09-19]: Changed validateParamsXML() to return undef if the job
23 # arguments do not validate. [RT98845]
24  
25 =head1 NAME
26  
27 helios_job_submit.pl - Submit a job to the Helios job processing system from the cmd line
28  
29 =head1 SYNOPSIS
30  
31 helios_job_submit.pl [--verbose] [--no-validate] I<jobclass> [I<jobargs>]
32  
33 helios_job_submit.pl MyService "<job>params><myarg1>myvalue1</myarg1></params></job>"
34  
35 helios_job_submit.pl --help
36  
37 =head1 DESCRIPTION
38  
39 Use helios_job_submit.pl to submit a job to the Helios job processing system
40 from the command line or a shell script. In the simplest form, 2 options are
41 required. The first parameter is the jobtype (usually the name of your
42 service), and the second is the XML string representing the job's arguments.
43 If the second parameter is not given, helios_job_submit.pl will read the job
44 argument string from STDIN. If the job submission is successful, the command
45 will exit with an exitstatus of 0. If unsuccessful, the command will exit
46 with a non-zero status and will print the error to STDERR.
47  
48 In addition to the normal command options, there are 2 optional parameters
49 that can be specified. These must be specified I<before> the jobtype and
50 argument string:
51  
52 =over 4
53  
54 =item --verbose
55  
56 If --verbose is specified, the jobid of the new job is returned via STDOUT.
57 Without --verbose, the jobid is not returned; this is a backward compatibility
58 measure because Helios pre-2.80 did not return a jobid.
59  
60 =item --no-validate
61  
62 By default, Helios job argument strings are specified with an XML-like syntax.
63 The helios_job_submit.pl command attempts to insure your job argument string
64 is well-formed XML by default, but this can slow down the job submission
65 process. Specifying the --no-validate option will turn off the validation step
66 and will speed up the submission process. Keep in mind your job argument
67 XML will not be validated before submission.
68  
69 =back
70  
71 =cut
72  
73 our $DEBUG_MODE = 0;
74 if (defined($ENV{HELIOS_DEBUG}) && $ENV{HELIOS_DEBUG} == 1) {
75 $DEBUG_MODE = 1;
76 }
77  
78 our $VALIDATE = 1;
79 # BEGIN CODE Copyright (C) 2013 by Logical Helion, LLC.
80 # [LH] [2013-10-28]: Added --verbose option.
81 our $OPT_VERBOSE = 0;
82 our @OPTS = @ARGV;
83 foreach (@OPTS) {
84 last if !/^-/;
85 # END CODE Copyright (C) 2013 by Logical Helion, LLC.
86 if ( lc($_) eq '--no-validate' || lc($_) eq '-n') {
87 shift @ARGV;
88 $VALIDATE = 0;
89 }
90 if ( lc($_) eq '--verbose' || lc($_ eq '-v')) {
91 shift @ARGV;
92 $OPT_VERBOSE = 1;
93 }
94 }
95  
96 our $JOB_CLASS = shift @ARGV;
97 our $PARAMS = shift @ARGV;
98  
99 our $DATABASES_INFO;
100 our $VERBOSE = 0;
101  
102 # print help if asked
103 if ( !defined($JOB_CLASS) || ($JOB_CLASS eq '--help') || ($JOB_CLASS eq '-h') ) {
104 require Pod::Usage;
105 Pod::Usage::pod2usage(-verbose => 2, -exitstatus => 0);
106 }
107  
108 # BEGIN CODE Copyright (C) 2013 by Logical Helion, LLC.
109 my $config = Helios::Config->parseConfig();
110 # END CODE Copyright (C) 2013 by Logical Helion, LLC.
111  
112 # if we were passed a <params> wodge of XML on the command line,
113 # try to validate it
114 # if we DIDN'T get a <params> wodge,
115 # then we have to assume it's coming from STDIN.
116 # which probably means it's a metajob
117 if ( !defined($PARAMS) ) {
118 # read them in from STDIN
119 while (<>) {
120 chomp;
121 $PARAMS .= $_;
122 }
123 }
124 # test the args before we submit
125 if ($VALIDATE) { validateParamsXML($PARAMS) or exit(1); }
126  
127 my $j = Helios::Job->new();
128 $j->setConfig($config);
129 $j->setJobType($JOB_CLASS);
130 $j->setArgString($PARAMS);
131 my $jobid = $j->submit();
132  
133 if ($DEBUG_MODE) {
134 print "Job submit successful. JOBID: ",$jobid,"\n";
135 }
136  
137 # BEGIN CODE Copyright (C) 2013 by Logical Helion, LLC.
138 if ($OPT_VERBOSE && !$DEBUG_MODE) {
139 print "Jobid: $jobid\n";
140 }
141 # END CODE Copyright (C) 2013 by Logical Helion, LLC.
142  
143  
144 =head1 SUBROUTINES
145  
146 =head2 validateParamsXML($xml)
147  
148 Given a wodge of parameter XML (wrapped by <job><params></params></job> tags),
149 validateParamsXML returns a true value if the XML is valid, and a false value
150 if it isn't.
151  
152 =cut
153  
154 sub validateParamsXML {
155 my $arg = shift;
156
157 # [LH] [2013-10-28]: Replaced all try {} with eval {}
158 my $valid = 0;
159 eval {
160 $arg = Helios::Job->parseArgXML($arg);
161 $valid = 1;
162 1;
163 } or do {
164 my $E = $@;
165 warn("Invalid job arguments: $arg (", $E->text(),")\n");
166 return undef;
167 };
168 return 1;
169 }
170  
171  
172 =head1 SEE ALSO
173  
174 L<Helios>, L<Helios::Job>
175  
176 =head1 AUTHOR
177  
178 Andrew Johnson, E<lt>lajandy at cpan dotorgE<gt>
179  
180 =head1 COPYRIGHT AND LICENSE
181  
182 Copyright (C) 2008-9 by CEB Toolbox, Inc.
183  
184 Portions of this software, where noted, are Copyright (C) 2013 by
185 Logical Helion, LLC.
186  
187 This program is free software; you can redistribute it and/or modify
188 it under the same terms as Perl itself, either Perl version 5.8.0 or,
189 at your option, any later version of Perl 5 you may have available.
190  
191 =head1 WARRANTY
192  
193 This software comes with no warranty of any kind.
194  
195 =cut
196