line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Framework::Feature::Run ; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
App::Framework::Feature::Run - Execute external commands |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use App::Framework '+Run' ; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
$app->run("perl t/test/runtest.pl"); |
12
|
|
|
|
|
|
|
$app->run('cmd' => "perl t/test/runtest.pl"); |
13
|
|
|
|
|
|
|
$app->run('cmd' => "perl t/test/runtest.pl", 'progress' => \&progress); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $results_aref = $app->run('cmd' => "perl t/test/runtest.pl"); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $run = $app->run() ; |
18
|
|
|
|
|
|
|
$run->run("perl t/test/runtest.pl"); |
19
|
|
|
|
|
|
|
$run->run('cmd' => "perl t/test/runtest.pl", 'timeout' => $sleep); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Provides for external command running from within an application. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
An external conmmand may be run using this feature, and the output from the command may be returned for additional processing. The feature |
27
|
|
|
|
|
|
|
also provides timed execution (aborting after a certain time), exit code status, and callbacks that can be defined to be called during execution |
28
|
|
|
|
|
|
|
and/or after program completion. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head2 Arguments |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
The access method for this feature (called as B<$app-Erun()>) allows the complete run settings to be specified as a HASH. The call sets |
33
|
|
|
|
|
|
|
the object L from the values in this HASH, for example: |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$app->run( |
36
|
|
|
|
|
|
|
'cmd' => "perl t/test/runtest.pl", |
37
|
|
|
|
|
|
|
'progress' => \&progress, |
38
|
|
|
|
|
|
|
) ; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
which specifies the command to run along with the L field (a callback). |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
A simpler alternative is allowed: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$app->run("perl t/test/runtest.pl", "some args") ; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
or: |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
$app->run("perl t/test/runtest.pl some args") ; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
The command arguments can be specified either as part of the L field definition, or separately in the L field. One benefit of using |
51
|
|
|
|
|
|
|
the L field is that the command need only be specified once - subsequent calls will use the same setting, for example: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$app->run('cmd' => "perl t/test/runtest.pl"); |
54
|
|
|
|
|
|
|
$app->run('progress' => \&progress); |
55
|
|
|
|
|
|
|
$app->run('progress' => \&progress); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head2 Return code |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
When the external command completes, it's return code can be accessed by reading the L field: |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$app->run()->status ; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
This value is set in the feature object to the result of the last run (i.e. you must save status values between runs if you want to |
64
|
|
|
|
|
|
|
keep track of the values). |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
The status value is entirely defined by the external command and the operating system. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Also, if you want your script to automatically abort on error (rather than write your own program error handler) then you can set the |
69
|
|
|
|
|
|
|
B field to 'fatal'. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head2 Required Programs Check |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
It's a good idea to start your script with a check for all the external programs you're about to use. You can do this by specifying them |
74
|
|
|
|
|
|
|
in a HASH ref using the L method. This does the checking for you, returning the path of all the executables. You can also |
75
|
|
|
|
|
|
|
tell the object to abort the script if some programs are not found, for example: |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
$app->run->set( |
78
|
|
|
|
|
|
|
'on_error' => 'fatal', |
79
|
|
|
|
|
|
|
'required' => { |
80
|
|
|
|
|
|
|
'lsvob' => 1, |
81
|
|
|
|
|
|
|
'ffmpeg' => 1, |
82
|
|
|
|
|
|
|
'transcode' => 1, |
83
|
|
|
|
|
|
|
'vlc' => 1, |
84
|
|
|
|
|
|
|
}, |
85
|
|
|
|
|
|
|
) ; |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
NOTE: The values you specify along with the program names are not important when you set the required list - these values get updated |
88
|
|
|
|
|
|
|
with the actual executable path. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 Command output |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
All output (both STDOUT and STDERR) is captured from the external command and can be accessed by reading the L field. This returns |
93
|
|
|
|
|
|
|
an ARRAY reference, where the ARRAY contains the lines of text output (one array entry per line). |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
NOTE: the lines have the original trailing newline B. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $results_aref = $app->run()->results ; |
98
|
|
|
|
|
|
|
foreach my $line (@$results_aref) |
99
|
|
|
|
|
|
|
{ |
100
|
|
|
|
|
|
|
print "$line\n"; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 Timeout |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
If you specify a L then the command is executed as normal but will be aborted if it runs for longer than the specified time. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This can be useful, for example, for running commands that don't normally terminate (or run on much longer than is necessary). |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 Callbacks |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
There are 2 optional callback routines that may be specified: |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=over 4 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=item B |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This subroutine is called for every line of output from the external command. This can be used in an application for monitoring |
119
|
|
|
|
|
|
|
progress, checking for errors etc. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item B |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
This subroutine is called at the end of external command completion. It allows the application to process the results to determine whether |
124
|
|
|
|
|
|
|
the command passed or failed some additional criteria. The L field is then set to the results of this subroutine. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=back |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 Examples |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Run a command: |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$app->run( |
134
|
|
|
|
|
|
|
'cmd' => "perl t/test/runtest.pl", |
135
|
|
|
|
|
|
|
) ; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Run a command and get a callback for each line of output: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$app->run( |
140
|
|
|
|
|
|
|
'cmd' => "perl t/test/runtest.pl", |
141
|
|
|
|
|
|
|
'progress' => \&progress, |
142
|
|
|
|
|
|
|
) ; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Ping a machine for 10 seconds and use a callback routine to check the replies: |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
my $run_for = 10 ; |
147
|
|
|
|
|
|
|
my $host = '192.168.0.1' ; |
148
|
|
|
|
|
|
|
my $run = $app->run() ; |
149
|
|
|
|
|
|
|
$run->run_cmd("ping", |
150
|
|
|
|
|
|
|
'progress' => \&progress, |
151
|
|
|
|
|
|
|
'args' => "$host", |
152
|
|
|
|
|
|
|
'timeout' => $run_for, |
153
|
|
|
|
|
|
|
) ; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Note the above example uses the B feature object to access it's methods directly. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
|
159
|
18
|
|
|
18
|
|
11288
|
use strict ; |
|
18
|
|
|
|
|
112
|
|
|
18
|
|
|
|
|
1096
|
|
160
|
18
|
|
|
18
|
|
109
|
use Carp ; |
|
18
|
|
|
|
|
44
|
|
|
18
|
|
|
|
|
2506
|
|
161
|
|
|
|
|
|
|
|
162
|
18
|
|
|
18
|
|
573608
|
use File::Which ; |
|
18
|
|
|
|
|
23509
|
|
|
18
|
|
|
|
|
2697
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
our $VERSION = "1.008" ; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
#============================================================================================ |
167
|
|
|
|
|
|
|
# USES |
168
|
|
|
|
|
|
|
#============================================================================================ |
169
|
18
|
|
|
18
|
|
148
|
use App::Framework::Feature ; |
|
18
|
|
|
|
|
42
|
|
|
18
|
|
|
|
|
2804323
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
#============================================================================================ |
172
|
|
|
|
|
|
|
# OBJECT HIERARCHY |
173
|
|
|
|
|
|
|
#============================================================================================ |
174
|
|
|
|
|
|
|
our @ISA = qw(App::Framework::Feature) ; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
#============================================================================================ |
177
|
|
|
|
|
|
|
# GLOBALS |
178
|
|
|
|
|
|
|
#============================================================================================ |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
our $ON_ERROR_DEFAULT = 'fatal' ; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 Fields |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=over 4 |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item B - command string (program name) |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
The program to run |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item B - any optional program arguments |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
String containing program arguments (may be specified as part of the 'cmd' string instead) |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item B - optional timeout time in secs. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
When specified causes the program to be run as a forked child |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=item B - optional nice level |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
On operating systems that allow this, runs the external command at the specified "nice" level |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=item B - what to do when a program fails |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
When this field is set to something other than 'status' it causes an error to be thrown. The default 'status' |
205
|
|
|
|
|
|
|
just returns with the error information stored in the object fields (i.e. 'status', 'results' etc). This field may be set to: |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=over 4 |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=item I - error information returned in fields |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item I - throw a warning with the message string indicating the error |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=item I - [default] throw a fatal error (and abort the script) with the message string indicating the error |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=back |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item B - required programs check |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
This is a HASH ref where the keys are the names of the required programs. When reading the field, the values |
220
|
|
|
|
|
|
|
are set to the path for that program. Where a program is not found then it's path is set to undef. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
See L method. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item B - optional results check subroutine |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
results check subroutine which should be of the form: |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
check_results($results_aref) |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Where: |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=over 4 |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item I<$results_aref> = ARRAY ref to all lines of text |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=back |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Subroutine should return 0 = results ok; non-zero for program failed. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=item B |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
progress subroutine which should be in the form: |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
progress($line, $linenum, $state_href) |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Where: |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=over 4 |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item I<$line> = line of text |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item I<$linenum> = line number (starting at 1) |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item I<$state_href> = An empty HASH ref (allows progress routine to store variables between calls) |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=back |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item B - Program exit status |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
Reads as the program exit status |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=item B - Program results |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
ARRAY ref of program output text lines |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=item B - Flag used for debug |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
Evaluates all parameters and prints out the command that would have been executed |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=back |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=cut |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
my %FIELDS = ( |
277
|
|
|
|
|
|
|
# Object Data |
278
|
|
|
|
|
|
|
'cmd' => undef, |
279
|
|
|
|
|
|
|
'args' => undef, |
280
|
|
|
|
|
|
|
'timeout' => undef, |
281
|
|
|
|
|
|
|
'nice' => undef, |
282
|
|
|
|
|
|
|
'dryrun' => 0, |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
'on_error' => $ON_ERROR_DEFAULT, |
285
|
|
|
|
|
|
|
'error_str' => "", |
286
|
|
|
|
|
|
|
'required' => {}, |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
'check_results' => undef, |
289
|
|
|
|
|
|
|
'progress' => undef, |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
'status' => 0, |
292
|
|
|
|
|
|
|
'results' => [], |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
# Options/flags |
295
|
|
|
|
|
|
|
'norun' => 0, |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
'log' => { |
298
|
|
|
|
|
|
|
'all' => 0, |
299
|
|
|
|
|
|
|
'cmd' => 0, |
300
|
|
|
|
|
|
|
'results' => 0, |
301
|
|
|
|
|
|
|
'status' => 0, |
302
|
|
|
|
|
|
|
}, |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
## Private |
305
|
|
|
|
|
|
|
'_logobj' => undef, |
306
|
|
|
|
|
|
|
) ; |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
#============================================================================================ |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=head2 CONSTRUCTOR |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=over 4 |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=cut |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
#============================================================================================ |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
=item B |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
Create a new Run. |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
The %args are specified as they would be in the B method (see L). |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=cut |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
sub new |
327
|
|
|
|
|
|
|
{ |
328
|
17
|
|
|
17
|
1
|
727
|
my ($obj, %args) = @_ ; |
329
|
|
|
|
|
|
|
|
330
|
17
|
|
33
|
|
|
622
|
my $class = ref($obj) || $obj ; |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
# Create object |
333
|
17
|
|
|
|
|
1396
|
my $this = $class->SUPER::new(%args) ; |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
|
336
|
17
|
|
|
|
|
231
|
return($this) ; |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
#============================================================================================ |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=back |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=head2 CLASS METHODS |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
=over 4 |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=cut |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
#============================================================================================ |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
354
|
|
|
|
|
|
|
|
355
|
|
|
|
|
|
|
=item B |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
Initialises the Run object class variables. |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=cut |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
sub init_class |
362
|
|
|
|
|
|
|
{ |
363
|
17
|
|
|
17
|
1
|
148
|
my $class = shift ; |
364
|
17
|
|
|
|
|
246
|
my (%args) = @_ ; |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
# Add extra fields |
367
|
17
|
|
|
|
|
850
|
$class->add_fields(\%FIELDS, \%args) ; |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
# init class |
370
|
17
|
|
|
|
|
384
|
$class->SUPER::init_class(%args) ; |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
#============================================================================================ |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
=back |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=head2 OBJECT DATA METHODS |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
=over 4 |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=cut |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
#============================================================================================ |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
#----------------------------------------------------------------------------- |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
=item B |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
Get/set the required programs list. If specified, B<$required_href> is a HASH ref where the |
391
|
|
|
|
|
|
|
keys are the names of the required programs (the values are unimportant). |
392
|
|
|
|
|
|
|
|
393
|
|
|
|
|
|
|
This method returns the B<$required_href> HASH ref having set the values associated with the |
394
|
|
|
|
|
|
|
program name keys to the path for that program. Where a program is not found then |
395
|
|
|
|
|
|
|
it's path is set to undef. |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
Also, if the L field is set to 'warning' or 'fatal' then this method throws a warning |
398
|
|
|
|
|
|
|
or fatal error if one or more required programs are not found. Sets the message string to indicate |
399
|
|
|
|
|
|
|
which programs were not found. |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
=cut |
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
sub required |
404
|
|
|
|
|
|
|
{ |
405
|
34
|
|
|
34
|
1
|
117
|
my $this = shift ; |
406
|
34
|
|
|
|
|
131
|
my ($new_required_href) = @_ ; |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
## my $required_href = $this->SUPER::required($new_required_href) ; |
409
|
34
|
|
|
|
|
404
|
my $required_href = $this->field_access('required', $new_required_href) ; |
410
|
34
|
100
|
|
|
|
161
|
if ($new_required_href) |
411
|
|
|
|
|
|
|
{ |
412
|
|
|
|
|
|
|
## Test for available executables |
413
|
19
|
|
|
|
|
135
|
foreach my $exe (keys %$new_required_href) |
414
|
|
|
|
|
|
|
{ |
415
|
6
|
|
|
|
|
823
|
$required_href->{$exe} = which($exe) ; |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
## check for errors |
419
|
19
|
|
|
|
|
1003
|
my $throw = $this->_throw_on_error($this->on_error) ; |
420
|
19
|
100
|
|
|
|
106
|
if ($throw) |
421
|
|
|
|
|
|
|
{ |
422
|
18
|
|
|
|
|
57
|
my $error = "" ; |
423
|
18
|
|
|
|
|
82
|
foreach my $exe (keys %$new_required_href) |
424
|
|
|
|
|
|
|
{ |
425
|
3
|
100
|
|
|
|
11
|
if (!$required_href->{$exe}) |
426
|
|
|
|
|
|
|
{ |
427
|
1
|
|
|
|
|
4
|
$error .= " $exe\n" ; |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
18
|
100
|
|
|
|
120
|
if ($error) |
432
|
|
|
|
|
|
|
{ |
433
|
1
|
|
|
|
|
21
|
$this->$throw("The following programs are required but not available:\n$error\n") ; |
434
|
|
|
|
|
|
|
} |
435
|
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
|
} |
437
|
|
|
|
|
|
|
|
438
|
33
|
|
|
|
|
150
|
return $required_href ; |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
#============================================================================================ |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
=back |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
=head2 OBJECT METHODS |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
=over 4 |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
=cut |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
#============================================================================================ |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------------- |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
=item B |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
Execute a command if B are specified. Whether B are specified or not, always returns the run object. |
459
|
|
|
|
|
|
|
|
460
|
|
|
|
|
|
|
This method has reasonably flexible arguments which can be one of: |
461
|
|
|
|
|
|
|
|
462
|
|
|
|
|
|
|
=item (%args) |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
The args HASH contains the information needed to set the L and then run teh command for example: |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
('cmd' => 'ping', 'args' => $host) |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=item ($cmd) |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
You can specify just the command string. This will be treated as if you had called the function with: |
471
|
|
|
|
|
|
|
|
472
|
|
|
|
|
|
|
('cmd' => $cmd) |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
=item ($cmd, $args) |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
You can specify the command string and the arguments string. This will be treated as if you had called the function with: |
477
|
|
|
|
|
|
|
|
478
|
|
|
|
|
|
|
('cmd' => $cmd, 'args' => $args) |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
NOTE: Need to get B object from application to access this method. This can be done as one of: |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
$app->run()->run(.....); |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
or |
485
|
|
|
|
|
|
|
|
486
|
|
|
|
|
|
|
my $run = $app->run() ; |
487
|
|
|
|
|
|
|
$run->run(....) ; |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
=cut |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
sub run |
492
|
|
|
|
|
|
|
{ |
493
|
22
|
|
|
22
|
1
|
5582
|
my $this = shift ; |
494
|
22
|
|
|
|
|
75
|
my (@args) = @_ ; |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
# See if this is a class call |
497
|
22
|
|
|
|
|
198
|
$this = $this->check_instance() ; |
498
|
|
|
|
|
|
|
|
499
|
22
|
|
|
|
|
217
|
$this->_dbg_prt(["run() this=", $this], 2) ; |
500
|
22
|
|
|
|
|
160
|
$this->_dbg_prt(["run() args=", \@args]) ; |
501
|
|
|
|
|
|
|
|
502
|
22
|
|
|
|
|
49
|
my %args ; |
503
|
22
|
100
|
|
|
|
95
|
if (@args == 1) |
|
|
100
|
|
|
|
|
|
504
|
|
|
|
|
|
|
{ |
505
|
7
|
|
|
|
|
38
|
$args{'cmd'} = $args[0] ; |
506
|
|
|
|
|
|
|
} |
507
|
|
|
|
|
|
|
elsif (@args == 2) |
508
|
|
|
|
|
|
|
{ |
509
|
2
|
50
|
|
|
|
19
|
if ($args[0] ne 'cmd') |
510
|
|
|
|
|
|
|
{ |
511
|
|
|
|
|
|
|
# not 'cmd' => '....' so treat as ($cmd, $args) |
512
|
2
|
|
|
|
|
12
|
$args{'cmd'} = $args[0] ; |
513
|
2
|
|
|
|
|
7
|
$args{'args'} = $args[1] ; |
514
|
|
|
|
|
|
|
} |
515
|
|
|
|
|
|
|
else |
516
|
|
|
|
|
|
|
{ |
517
|
0
|
|
|
|
|
0
|
%args = (@args) ; |
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
} |
520
|
|
|
|
|
|
|
else |
521
|
|
|
|
|
|
|
{ |
522
|
13
|
|
|
|
|
41
|
%args = (@args) ; |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
|
525
|
|
|
|
|
|
|
## return immediately if no args |
526
|
22
|
100
|
|
|
|
256
|
return $this unless %args ; |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
## If associated with an app, then see if Logging is enabled |
529
|
15
|
|
|
|
|
621
|
my $app = $this->app ; |
530
|
15
|
50
|
|
|
|
56
|
if ($app) |
531
|
|
|
|
|
|
|
{ |
532
|
15
|
|
|
|
|
328
|
my $logging = $app->feature_installed('Logging') ; |
533
|
15
|
|
|
|
|
489
|
$this->_logobj($logging) ; |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
## create local copy of variables |
537
|
15
|
|
|
|
|
164
|
my %local = $this->vars() ; |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
# Set any specified args |
540
|
15
|
|
|
|
|
131
|
foreach my $key (keys %local) |
541
|
|
|
|
|
|
|
{ |
542
|
420
|
100
|
|
|
|
900
|
$local{$key} = $args{$key} if exists($args{$key}) ; |
543
|
|
|
|
|
|
|
} |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
## set any 'special' vars |
546
|
15
|
|
|
|
|
127
|
my %set ; |
547
|
15
|
|
|
|
|
36
|
foreach my $key (qw/debug/) |
548
|
|
|
|
|
|
|
{ |
549
|
15
|
50
|
|
|
|
60
|
$set{$key} = $args{$key} if exists($args{$key}) ; |
550
|
|
|
|
|
|
|
} |
551
|
15
|
50
|
|
|
|
49
|
$this->set(%set) if keys %set ; |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
# Get command |
555
|
|
|
|
|
|
|
# my $cmd = $this->cmd() ; |
556
|
15
|
|
|
|
|
49
|
my $cmd = $local{'cmd'} ; |
557
|
15
|
50
|
|
|
|
77
|
$this->throw_fatal("command not specified") unless $cmd ; |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
# Add niceness |
560
|
|
|
|
|
|
|
# my $nice = $this->nice() ; |
561
|
15
|
|
|
|
|
29
|
my $nice = $local{'nice'} ; |
562
|
15
|
50
|
|
|
|
43
|
if (defined($nice)) |
563
|
|
|
|
|
|
|
{ |
564
|
0
|
|
|
|
|
0
|
$cmd = "nice -n $nice $cmd" ; |
565
|
|
|
|
|
|
|
} |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
|
568
|
|
|
|
|
|
|
# clear vars |
569
|
|
|
|
|
|
|
$this->set( |
570
|
15
|
|
|
|
|
144
|
'status' => 0, |
571
|
|
|
|
|
|
|
'results' => [], |
572
|
|
|
|
|
|
|
'error_str' => "", |
573
|
|
|
|
|
|
|
) ; |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
# Check arguments |
577
|
15
|
|
|
|
|
70
|
my $args = $this->_check_args($local{'args'}) ; |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
# Run command and save results |
580
|
15
|
|
|
|
|
27
|
my @results ; |
581
|
|
|
|
|
|
|
my $rc ; |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
## Logging |
584
|
15
|
|
|
|
|
424
|
my $logopts_href = $this->log ; |
585
|
15
|
|
|
|
|
491
|
my $logging = $this->_logobj ; |
586
|
|
|
|
|
|
|
|
587
|
15
|
100
|
100
|
|
|
316
|
$logging->logging("RUN: $cmd $args\n") if $logging && ($logopts_href->{all} || $logopts_href->{cmd}) ; |
|
|
|
66
|
|
|
|
|
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
# my $timeout = $this->timeout() ; |
591
|
15
|
|
|
|
|
34
|
my $timeout = $local{'timeout'} ; |
592
|
15
|
50
|
|
|
|
74
|
if ($local{'dryrun'}) |
593
|
|
|
|
|
|
|
{ |
594
|
|
|
|
|
|
|
## Print |
595
|
0
|
0
|
|
|
|
0
|
my $timeout_str = $timeout ? "[timeout after $timeout secs]" : "" ; |
596
|
0
|
|
|
|
|
0
|
print "RUN: $cmd $args $timeout_str\n" ; |
597
|
|
|
|
|
|
|
} |
598
|
|
|
|
|
|
|
else |
599
|
|
|
|
|
|
|
{ |
600
|
|
|
|
|
|
|
## Run |
601
|
|
|
|
|
|
|
|
602
|
15
|
100
|
|
|
|
43
|
if (defined($timeout)) |
603
|
|
|
|
|
|
|
{ |
604
|
|
|
|
|
|
|
# Run command with timeout |
605
|
4
|
|
|
|
|
35
|
($rc, @results) = $this->_run_timeout($cmd, $args, $timeout, $local{'progress'}, $local{'check_results'}) ; |
606
|
|
|
|
|
|
|
} |
607
|
|
|
|
|
|
|
else |
608
|
|
|
|
|
|
|
{ |
609
|
|
|
|
|
|
|
# run command |
610
|
11
|
|
|
|
|
46
|
($rc, @results) = $this->_run_cmd($cmd, $args, $local{'progress'}, $local{'check_results'}) ; |
611
|
|
|
|
|
|
|
} |
612
|
|
|
|
|
|
|
} |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
# Update vars |
615
|
15
|
|
|
|
|
1781
|
$this->status($rc) ; |
616
|
15
|
|
|
|
|
148
|
chomp foreach (@results) ; |
617
|
15
|
|
|
|
|
7819
|
$this->results(\@results) ; |
618
|
|
|
|
|
|
|
|
619
|
15
|
100
|
100
|
|
|
200
|
$logging->logging(\@results) if $logging && ($logopts_href->{all} || $logopts_href->{results}) ; |
|
|
|
66
|
|
|
|
|
620
|
15
|
100
|
100
|
|
|
167
|
$logging->logging("Status: $rc\n") if $logging && ($logopts_href->{all} || $logopts_href->{status}) ; |
|
|
|
66
|
|
|
|
|
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
## Handle non-zero exit status |
623
|
15
|
|
|
|
|
146
|
my $throw = $this->_throw_on_error($local{'on_error'}) ; |
624
|
15
|
50
|
33
|
|
|
70
|
if ($throw && $rc) |
625
|
|
|
|
|
|
|
{ |
626
|
0
|
|
|
|
|
0
|
my $results = join("\n", @results) ; |
627
|
0
|
|
|
|
|
0
|
my $error_str = $local{'error_str'} ; |
628
|
0
|
|
|
|
|
0
|
$this->$throw("Command \"$cmd $args\" exited with non-zero error status $rc : \"$error_str\"\n$results\n") ; |
629
|
|
|
|
|
|
|
} |
630
|
|
|
|
|
|
|
|
631
|
15
|
|
|
|
|
1680
|
return($this) ; |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
|
634
|
|
|
|
|
|
|
#---------------------------------------------------------------------------- |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
=item B< Run([%args]) > |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
Alias to L |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
=cut |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
*Run = \&run ; |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
##-------------------------------------------------------------------------------------------- |
645
|
|
|
|
|
|
|
# |
646
|
|
|
|
|
|
|
#=item B |
647
|
|
|
|
|
|
|
# |
648
|
|
|
|
|
|
|
#DEBUG: Display the full command line as if it was going to be run |
649
|
|
|
|
|
|
|
# |
650
|
|
|
|
|
|
|
#NOTE: Need to get B object from application to access this method. |
651
|
|
|
|
|
|
|
# |
652
|
|
|
|
|
|
|
#=cut |
653
|
|
|
|
|
|
|
# |
654
|
|
|
|
|
|
|
#sub print_run |
655
|
|
|
|
|
|
|
#{ |
656
|
|
|
|
|
|
|
# my $this = shift ; |
657
|
|
|
|
|
|
|
# my (@args) = @_ ; |
658
|
|
|
|
|
|
|
# |
659
|
|
|
|
|
|
|
# # See if this is a class call |
660
|
|
|
|
|
|
|
# $this = $this->check_instance() ; |
661
|
|
|
|
|
|
|
# |
662
|
|
|
|
|
|
|
# my %args ; |
663
|
|
|
|
|
|
|
# if (@args == 1) |
664
|
|
|
|
|
|
|
# { |
665
|
|
|
|
|
|
|
# $args{'cmd'} = $args[0] ; |
666
|
|
|
|
|
|
|
# } |
667
|
|
|
|
|
|
|
# elsif (@args == 2) |
668
|
|
|
|
|
|
|
# { |
669
|
|
|
|
|
|
|
# if ($args[0] ne 'cmd') |
670
|
|
|
|
|
|
|
# { |
671
|
|
|
|
|
|
|
# # not 'cmd' => '....' so treat as ($cmd, $args) |
672
|
|
|
|
|
|
|
# $args{'cmd'} = $args[0] ; |
673
|
|
|
|
|
|
|
# $args{'args'} = $args[1] ; |
674
|
|
|
|
|
|
|
# } |
675
|
|
|
|
|
|
|
# else |
676
|
|
|
|
|
|
|
# { |
677
|
|
|
|
|
|
|
# %args = (@args) ; |
678
|
|
|
|
|
|
|
# } |
679
|
|
|
|
|
|
|
# } |
680
|
|
|
|
|
|
|
# else |
681
|
|
|
|
|
|
|
# { |
682
|
|
|
|
|
|
|
# %args = (@args) ; |
683
|
|
|
|
|
|
|
# } |
684
|
|
|
|
|
|
|
# |
685
|
|
|
|
|
|
|
# # Set any specified args |
686
|
|
|
|
|
|
|
# $this->set(%args) if %args ; |
687
|
|
|
|
|
|
|
# |
688
|
|
|
|
|
|
|
# # Get command |
689
|
|
|
|
|
|
|
# my $cmd = $this->cmd() ; |
690
|
|
|
|
|
|
|
# $this->throw_fatal("command not specified") unless $cmd ; |
691
|
|
|
|
|
|
|
# |
692
|
|
|
|
|
|
|
# # Check arguments |
693
|
|
|
|
|
|
|
# my $args = $this->_check_args() ; |
694
|
|
|
|
|
|
|
# |
695
|
|
|
|
|
|
|
# print "$cmd $args\n" ; |
696
|
|
|
|
|
|
|
#} |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
# ============================================================================================ |
700
|
|
|
|
|
|
|
# PRIVATE METHODS |
701
|
|
|
|
|
|
|
# ============================================================================================ |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
#-------------------------------------------------------------------------------------------- |
704
|
|
|
|
|
|
|
# |
705
|
|
|
|
|
|
|
# Ensure arguments are correct |
706
|
|
|
|
|
|
|
# |
707
|
|
|
|
|
|
|
sub _check_args |
708
|
|
|
|
|
|
|
{ |
709
|
15
|
|
|
15
|
|
36
|
my $this = shift ; |
710
|
|
|
|
|
|
|
# my $args = $this->args() || "" ; |
711
|
15
|
|
|
|
|
27
|
my ($args) = @_ ; |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
# If there is no redirection, just add redirect 2>1 |
714
|
15
|
50
|
66
|
|
|
87
|
if (!$args || ($args !~ /\>/) ) |
715
|
|
|
|
|
|
|
{ |
716
|
15
|
|
|
|
|
37
|
$args .= " 2>&1" ; |
717
|
|
|
|
|
|
|
} |
718
|
|
|
|
|
|
|
|
719
|
15
|
|
|
|
|
50
|
return $args ; |
720
|
|
|
|
|
|
|
} |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
724
|
|
|
|
|
|
|
# Run command with no timeout |
725
|
|
|
|
|
|
|
# |
726
|
|
|
|
|
|
|
sub _run_cmd |
727
|
|
|
|
|
|
|
{ |
728
|
11
|
|
|
11
|
|
21
|
my $this = shift ; |
729
|
11
|
|
|
|
|
20
|
my ($cmd, $args, $progress, $check_results) = @_ ; |
730
|
|
|
|
|
|
|
|
731
|
11
|
|
|
|
|
87
|
$this->_dbg_prt(["_run_cmd($cmd) args=$args\n"]) ; |
732
|
|
|
|
|
|
|
|
733
|
11
|
|
|
|
|
28
|
my @results ; |
734
|
|
|
|
|
|
|
# @results = `$cmd $args` unless $this->option('norun') ; |
735
|
11
|
|
|
|
|
374257
|
@results = `$cmd $args` ; |
736
|
11
|
|
|
|
|
265
|
my $rc = $? ; |
737
|
|
|
|
|
|
|
|
738
|
11
|
|
|
|
|
329
|
foreach (@results) |
739
|
|
|
|
|
|
|
{ |
740
|
11
|
|
|
|
|
118
|
chomp $_ ; |
741
|
|
|
|
|
|
|
} |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
# if it's defined, call the progress checker for each line |
744
|
|
|
|
|
|
|
# my $progress = $this->progress() ; |
745
|
11
|
100
|
|
|
|
92
|
if (defined($progress)) |
746
|
|
|
|
|
|
|
{ |
747
|
2
|
|
|
|
|
9
|
my $linenum = 0 ; |
748
|
2
|
|
|
|
|
15
|
my $state_href = {} ; |
749
|
2
|
|
|
|
|
4
|
foreach (@results) |
750
|
|
|
|
|
|
|
{ |
751
|
2
|
|
|
|
|
58
|
&$progress($_, ++$linenum, $state_href) ; |
752
|
|
|
|
|
|
|
} |
753
|
|
|
|
|
|
|
} |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
# if it's defined, call the results checker for each line |
757
|
11
|
|
33
|
|
|
2334
|
$rc ||= $this->_check_results(\@results, $check_results) ; |
758
|
|
|
|
|
|
|
|
759
|
11
|
|
|
|
|
317
|
return ($rc, @results) ; |
760
|
|
|
|
|
|
|
} |
761
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
763
|
|
|
|
|
|
|
#Execute a command in the background, gather output, return status. |
764
|
|
|
|
|
|
|
#If timeout is specified (in seconds), process is killed after the timeout period. |
765
|
|
|
|
|
|
|
# |
766
|
|
|
|
|
|
|
sub _run_timeout |
767
|
|
|
|
|
|
|
{ |
768
|
4
|
|
|
4
|
|
15
|
my $this = shift ; |
769
|
4
|
|
|
|
|
15
|
my ($cmd, $args, $timeout, $progress, $check_results) = @_ ; |
770
|
|
|
|
|
|
|
|
771
|
4
|
|
|
|
|
39
|
$this->_dbg_prt(["_run_timeout($cmd) timeout=$timeout args=$args\n"]) ; |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
## Timesout must be set |
774
|
4
|
|
50
|
|
|
21
|
$timeout ||= 60 ; |
775
|
|
|
|
|
|
|
|
776
|
|
|
|
|
|
|
# Run command and save results |
777
|
4
|
|
|
|
|
13
|
my @results ; |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
# Run command but time it and kill it when timed out |
780
|
|
|
|
|
|
|
local $SIG{ALRM} = sub { |
781
|
|
|
|
|
|
|
# normal execution |
782
|
1
|
|
|
1
|
|
11417179
|
die "timeout\n" ; |
783
|
4
|
|
|
|
|
110
|
}; |
784
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
# if it's defined, call the progress checker for each line |
786
|
|
|
|
|
|
|
# my $progress = $this->progress() ; |
787
|
4
|
|
|
|
|
14
|
my $state_href = {} ; |
788
|
4
|
|
|
|
|
8
|
my $linenum = 0 ; |
789
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
# Run inside eval to catch timeout |
791
|
4
|
|
|
|
|
7
|
my $pid ; |
792
|
4
|
|
|
|
|
9
|
my $rc = 0 ; |
793
|
4
|
|
|
|
|
10
|
my $endtime = (time + $timeout) ; |
794
|
|
|
|
|
|
|
eval |
795
|
4
|
|
|
|
|
13
|
{ |
796
|
4
|
|
|
|
|
38
|
alarm($timeout); |
797
|
4
|
50
|
|
|
|
5022382
|
$pid = open my $proc, "$cmd $args |" or $this->throw_fatal("Unable to fork $cmd : $!") ; |
798
|
|
|
|
|
|
|
|
799
|
3
|
|
|
|
|
13502
|
while(<$proc>) |
800
|
|
|
|
|
|
|
{ |
801
|
6
|
|
|
|
|
48
|
chomp $_ ; |
802
|
6
|
|
|
|
|
88
|
push @results, $_ ; |
803
|
|
|
|
|
|
|
|
804
|
6
|
|
|
|
|
20
|
++$linenum ; |
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
# if it's defined, call the progress checker for each line |
807
|
6
|
50
|
|
|
|
50
|
if (defined($progress)) |
808
|
|
|
|
|
|
|
{ |
809
|
6
|
|
|
|
|
257
|
&$progress($_, $linenum, $state_href) ; |
810
|
|
|
|
|
|
|
} |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
# if it's defined, check timeout |
813
|
6
|
50
|
|
|
|
4464329
|
if (time > $endtime) |
814
|
|
|
|
|
|
|
{ |
815
|
0
|
|
|
|
|
0
|
$endtime=0; |
816
|
0
|
|
|
|
|
0
|
last ; |
817
|
|
|
|
|
|
|
} |
818
|
|
|
|
|
|
|
} |
819
|
3
|
|
|
|
|
29
|
alarm(0) ; |
820
|
3
|
|
|
|
|
4679
|
$rc = $? ; |
821
|
3
|
50
|
|
|
|
72
|
print "end of program : rc=$rc\n" if $this->debug ; |
822
|
|
|
|
|
|
|
}; |
823
|
4
|
100
|
|
|
|
40
|
if ($@) |
824
|
|
|
|
|
|
|
{ |
825
|
1
|
|
50
|
|
|
52
|
$rc ||= 1 ; |
826
|
1
|
50
|
|
|
|
19
|
if ($@ eq "timeout\n") |
827
|
|
|
|
|
|
|
{ |
828
|
1
|
50
|
|
|
|
67
|
print "timed out - stopping command pid=$pid...\n" if $this->debug ; |
829
|
|
|
|
|
|
|
# timed out - stop command |
830
|
1
|
|
|
|
|
47
|
kill('INT', $pid) ; |
831
|
|
|
|
|
|
|
} |
832
|
|
|
|
|
|
|
else |
833
|
|
|
|
|
|
|
{ |
834
|
0
|
0
|
|
|
|
0
|
print "unexpected end of program : $@\n" if $this->debug ; |
835
|
|
|
|
|
|
|
# Failed |
836
|
0
|
|
|
|
|
0
|
alarm(0) ; |
837
|
0
|
|
|
|
|
0
|
$this->throw_fatal( "Unexpected error while timing out command \"$cmd $args\": $@" ) ; |
838
|
|
|
|
|
|
|
} |
839
|
|
|
|
|
|
|
} |
840
|
4
|
|
|
|
|
37
|
alarm(0) ; |
841
|
|
|
|
|
|
|
|
842
|
4
|
50
|
|
|
|
29
|
print "exit program\n" if $this->debug ; |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
# if it's defined, call the results checker for each line |
845
|
4
|
|
66
|
|
|
78
|
$rc ||= $this->_check_results(\@results, $check_results) ; |
846
|
|
|
|
|
|
|
|
847
|
4
|
|
|
|
|
205
|
return($rc, @results) ; |
848
|
|
|
|
|
|
|
} |
849
|
|
|
|
|
|
|
|
850
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
851
|
|
|
|
|
|
|
# Check the results calling the check_results() hook if defined |
852
|
|
|
|
|
|
|
# |
853
|
|
|
|
|
|
|
sub _check_results |
854
|
|
|
|
|
|
|
{ |
855
|
14
|
|
|
14
|
|
86
|
my $this = shift ; |
856
|
14
|
|
|
|
|
157
|
my ($results_aref, $check_results) = @_ ; |
857
|
|
|
|
|
|
|
|
858
|
14
|
|
|
|
|
40
|
my $rc = 0 ; |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
# If it's defined, run the check results hook |
861
|
|
|
|
|
|
|
# my $check_results = $this->check_results() ; |
862
|
14
|
50
|
|
|
|
67
|
if (defined($check_results)) |
863
|
|
|
|
|
|
|
{ |
864
|
0
|
|
|
|
|
0
|
$rc = &$check_results($results_aref) ; |
865
|
|
|
|
|
|
|
} |
866
|
|
|
|
|
|
|
|
867
|
14
|
|
|
|
|
91
|
return $rc ; |
868
|
|
|
|
|
|
|
} |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
#---------------------------------------------------------------------- |
872
|
|
|
|
|
|
|
# If the 'on_error' setting is not 'status' then return the "throw" type |
873
|
|
|
|
|
|
|
# |
874
|
|
|
|
|
|
|
sub _throw_on_error |
875
|
|
|
|
|
|
|
{ |
876
|
34
|
|
|
34
|
|
129
|
my $this = shift ; |
877
|
34
|
|
|
|
|
166
|
my ($on_error) = @_ ; |
878
|
34
|
|
66
|
|
|
296
|
$on_error ||= $ON_ERROR_DEFAULT ; |
879
|
|
|
|
|
|
|
|
880
|
34
|
|
|
|
|
216
|
my $throw = ""; |
881
|
|
|
|
|
|
|
# my $on_error = $this->on_error() || $ON_ERROR_DEFAULT ; |
882
|
34
|
100
|
|
|
|
195
|
if ($on_error ne 'status') |
883
|
|
|
|
|
|
|
{ |
884
|
18
|
|
|
|
|
60
|
$throw = 'throw_fatal' ; |
885
|
18
|
50
|
|
|
|
181
|
if ($on_error =~ m/warn/i) |
886
|
|
|
|
|
|
|
{ |
887
|
0
|
|
|
|
|
0
|
$throw = 'throw_warning' ; |
888
|
|
|
|
|
|
|
} |
889
|
|
|
|
|
|
|
} |
890
|
|
|
|
|
|
|
|
891
|
34
|
|
|
|
|
213
|
return $throw ; |
892
|
|
|
|
|
|
|
} |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
# ============================================================================================ |
895
|
|
|
|
|
|
|
# END OF PACKAGE |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
=back |
898
|
|
|
|
|
|
|
|
899
|
|
|
|
|
|
|
=head1 DIAGNOSTICS |
900
|
|
|
|
|
|
|
|
901
|
|
|
|
|
|
|
Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages. |
902
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
=head1 AUTHOR |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
Steve Price C<< >> |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
=head1 BUGS |
908
|
|
|
|
|
|
|
|
909
|
|
|
|
|
|
|
None that I know of! |
910
|
|
|
|
|
|
|
|
911
|
|
|
|
|
|
|
=cut |
912
|
|
|
|
|
|
|
|
913
|
|
|
|
|
|
|
1; |
914
|
|
|
|
|
|
|
|
915
|
|
|
|
|
|
|
__END__ |