2 |
## $Id$ |
## $Id$ |
3 |
## ---------------------------------------------------------------------- |
## ---------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.9 2003/04/04 17:23:11 joko |
6 |
|
## minor update: debugging output |
7 |
|
## |
8 |
|
## Revision 1.8 2003/03/29 07:11:54 joko |
9 |
|
## modified: sub run_executable |
10 |
|
## new: sub run_script |
11 |
|
## |
12 |
|
## Revision 1.7 2003/03/28 07:02:56 joko |
13 |
|
## modified structure around '$wrapper_program' |
14 |
|
## |
15 |
|
## Revision 1.6 2003/03/27 15:31:05 joko |
16 |
|
## fixes to modules regarding new namespace(s) below Data::Mungle::* |
17 |
|
## |
18 |
|
## Revision 1.5 2003/03/27 15:03:03 joko |
19 |
|
## enhanced 'sub run_executable' |
20 |
|
## |
21 |
## Revision 1.4 2003/02/22 16:51:21 joko |
## Revision 1.4 2003/02/22 16:51:21 joko |
22 |
## + enhanced run_executable |
## + enhanced run_executable |
23 |
## modified logging output |
## modified logging output |
47 |
use Iterate; |
use Iterate; |
48 |
|
|
49 |
use shortcuts qw( run_cmd ); |
use shortcuts qw( run_cmd ); |
50 |
use Data::Code::Ref qw( ref_slot ); |
use Data::Mungle::Code::Ref qw( ref_slot ); |
51 |
use Data::Transform::Deep qw( expand ); |
use Data::Mungle::Transform::Deep qw( expand deep_copy ); |
52 |
|
use File::Temp qw/ tempfile tempdir /; |
53 |
|
|
54 |
|
|
55 |
sub performTarget { |
sub performTarget { |
64 |
|
|
65 |
my $header = ("- " x 12) . " " . $targetname . " " . ("- " x 6); |
my $header = ("- " x 12) . " " . $targetname . " " . ("- " x 6); |
66 |
|
|
67 |
|
# V1 |
68 |
#$self->log("- " x 35, 'notice'); |
#$self->log("- " x 35, 'notice'); |
69 |
#$self->log("Performing Target '$targetname'.", 'notice'); |
#$self->log("Performing Target '$targetname'.", 'notice'); |
70 |
|
|
71 |
$self->log($header, 'notice'); |
# V2 |
72 |
|
#$self->log($header, 'notice'); |
73 |
|
|
74 |
|
# V3 |
75 |
|
$self->log("- " x 20, 'info'); |
76 |
|
$self->log("Performing Target '$targetname'.", 'notice'); |
77 |
|
|
78 |
#exit; |
#exit; |
79 |
|
|
116 |
foreach my $entry (@{$target->{content}}) { |
foreach my $entry (@{$target->{content}}) { |
117 |
my $command = $entry->{name}; |
my $command = $entry->{name}; |
118 |
my $args = $entry->{attrib}; |
my $args = $entry->{attrib}; |
119 |
$self->perform_command($command, $args); |
my $content = $entry->{content}; |
120 |
|
$self->perform_command($command, $args, $content); |
121 |
# check recursiveness |
# check recursiveness |
122 |
if ($entry->{content} && ref $entry->{content}) { |
if ($entry->{content} && ref $entry->{content}) { |
123 |
$self->perform_details($entry); |
$self->perform_details($entry); |
133 |
sub perform_command { |
sub perform_command { |
134 |
my $self = shift; |
my $self = shift; |
135 |
my $command = shift; |
my $command = shift; |
136 |
|
my $args_list = shift; |
137 |
|
my $content = shift; |
138 |
|
|
139 |
if (!$command) { |
if (!$command) { |
140 |
$self->log("Command was empty!", 'debug'); |
$self->log("Command was empty!", 'debug'); |
144 |
# FIXME: make '__PACKAGE__' go one level deeper properly! |
# FIXME: make '__PACKAGE__' go one level deeper properly! |
145 |
$self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug'); |
$self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug'); |
146 |
|
|
|
my $args_list = shift; |
|
147 |
my $args = {}; |
my $args = {}; |
148 |
#print Dumper($args_list); |
#print Dumper($args_list); |
149 |
if ($args_list) { |
if ($args_list) { |
163 |
$container ||= $self; |
$container ||= $self; |
164 |
|
|
165 |
if ($container->can($command)) { |
if ($container->can($command)) { |
166 |
$container->$command($args); |
$container->$command($args, $content); |
167 |
} else { |
} else { |
168 |
$self->log("Command '$command' not implemented.", "warning"); |
$self->log("Command '$command' not implemented.", "warning"); |
169 |
} |
} |
316 |
my $self = shift; |
my $self = shift; |
317 |
my $opts = shift; |
my $opts = shift; |
318 |
|
|
319 |
|
my $meta = deep_copy($opts); |
320 |
|
|
321 |
|
delete $opts->{caption}; |
322 |
|
delete $opts->{async}; |
323 |
|
|
324 |
|
#print Dumper($meta); |
325 |
|
|
326 |
if ($opts->{executable}) { |
if ($opts->{executable}) { |
327 |
|
|
328 |
my $program = $opts->{executable}; |
my $program = $opts->{executable}; |
329 |
delete $opts->{executable}; |
delete $opts->{executable}; |
330 |
|
|
331 |
|
# determine execution method |
332 |
|
my $wrapper_program = ''; |
333 |
|
|
334 |
|
# check if program is a namespace-string (contains '::') - use 'do' in this case! |
335 |
|
if ($program =~ /::/) { |
336 |
|
#$wrapper_program = 'rap.pl'; |
337 |
|
$wrapper_program = $0; |
338 |
|
} |
339 |
|
|
340 |
|
# prepare arguments |
341 |
my @buf; |
my @buf; |
342 |
foreach (keys %$opts) { |
foreach (keys %$opts) { |
343 |
my $value = $opts->{$_}; |
my $value = $opts->{$_}; |
356 |
} |
} |
357 |
push @buf, "--$_=$value"; |
push @buf, "--$_=$value"; |
358 |
} |
} |
359 |
|
|
360 |
my $cmd = $program . ' ' . join(' ', @buf); |
# build {program} & {arguments} |
361 |
|
my $cmd = ($wrapper_program ? $wrapper_program . ' ' : '') . $program . ' ' . join(' ', @buf); |
362 |
|
|
363 |
# trace |
# trace |
364 |
#print "command: $cmd", "\n"; |
#print "command: $cmd", "\n"; |
371 |
$ENV{PERL5LIB} = $INC[0]; |
$ENV{PERL5LIB} = $INC[0]; |
372 |
|
|
373 |
#print Dumper(%ENV); |
#print Dumper(%ENV); |
374 |
run_cmd($cmd); |
|
375 |
|
#print "command: '$cmd'", "\n"; |
376 |
|
|
377 |
|
# V1 - basic |
378 |
|
#run_cmd($cmd); |
379 |
|
|
380 |
|
# V1.b - enhanced: variable local method |
381 |
|
$meta->{caption} ||= ''; |
382 |
|
$meta->{async} ||= 0; |
383 |
|
my $evalstr = "run_cmd('$cmd', '$meta->{caption}', { async => $meta->{async} });"; |
384 |
|
eval($evalstr); |
385 |
|
#my $res = do "$cmd"; |
386 |
|
#print $res, "\n" if $res; |
387 |
|
|
388 |
|
#$self->log("run_executable: $evalstr", 'info'); |
389 |
|
$self->raiseException("run_executable: $evalstr\n$@") if $@; |
390 |
|
|
391 |
# V2: via IPC::Run |
# V2: via IPC::Run |
392 |
# .... (TODO) |
# .... (TODO) |
393 |
|
|
394 |
|
|
395 |
} |
} |
396 |
|
|
397 |
} |
} |
398 |
|
|
399 |
|
|
400 |
|
sub run_script { |
401 |
|
|
402 |
|
my $self = shift; |
403 |
|
my $args = shift; |
404 |
|
my $code = shift; |
405 |
|
|
406 |
|
if ($args->{language} eq 'msdos/bat') { |
407 |
|
|
408 |
|
#print "code: $code", "\n"; |
409 |
|
|
410 |
|
# reporting |
411 |
|
$self->log("Executing some arbitrary unsigned code (probably unsafe). [language=$args->{language}]", 'info'); |
412 |
|
$self->log("\n<code>\n$code\n</code>", 'info'); |
413 |
|
|
414 |
|
# create temporary intermediate file to execute code |
415 |
|
my $tmpdir = '/tmp/rap'; |
416 |
|
mkdir $tmpdir; |
417 |
|
(my $fh, my $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.bat' ); |
418 |
|
print $fh $code, "\n"; |
419 |
|
run_cmd( $filename, '', { async => 1 } ); |
420 |
|
|
421 |
|
# FIXME: DELETE code inside temp-files as soon as possible! |
422 |
|
#print $fh ''; |
423 |
|
|
424 |
|
# TODO: delete FILE completely! |
425 |
|
# required for this: wait until execution has finished, then unlink.... |
426 |
|
# but: "how to wait until execution is finished"? |
427 |
|
# i believe the best is to spawn *another* process directly from here, |
428 |
|
# let's call it 'watcher-agent'. |
429 |
|
# This one should monitor a certain running process and delete its |
430 |
|
# executable file after it has finished execution. |
431 |
|
# Possible extensions could be: |
432 |
|
# keep track of all stuff sent to STDOUT or STDERR and |
433 |
|
# send that stuff to the task-owner indirectly (not via shell/console) |
434 |
|
# (e.g. via email, by posting report to a newsgroup or publishing on a specified web-page: use mod-dav!) |
435 |
|
|
436 |
|
} elsif ($args->{language} eq 'bash') { |
437 |
|
$self->log("FIXME: - - - - - -- - - -- BASH - - - - - - - -- - ", 'error'); |
438 |
|
|
439 |
|
} else { |
440 |
|
$self->log("FIXME: Script language '$args->{language}' not implemented.", 'error'); |
441 |
|
|
442 |
|
} |
443 |
|
|
444 |
|
} |
445 |
|
|
446 |
1; |
1; |
447 |
__END__ |
__END__ |