File Coverage

lib/Rex/Helper/Run.pm
Criterion Covered Total %
statement 60 109 55.0
branch 17 34 50.0
condition 7 11 63.6
subroutine 10 13 76.9
pod 0 4 0.0
total 94 171 54.9


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Helper::Run;
6              
7 91     91   142638 use v5.12.5;
  91         345  
8 91     91   532 use warnings;
  91         181  
  91         5113  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12             require Exporter;
13 91     91   597 use base qw(Exporter);
  91         184  
  91         7510  
14 91     91   634 use vars qw(@EXPORT);
  91         234  
  91         4444  
15              
16 91     91   3494 use Net::OpenSSH::ShellQuoter;
  91         35662  
  91         1029  
17 91     91   5337 use Rex::Interface::File;
  91         236  
  91         955  
18 91     91   2920 use Rex::Interface::Fs;
  91         221  
  91         917  
19 91     91   3268 use Rex::Helper::Path;
  91         230  
  91         5979  
20 91     91   571 use Carp;
  91         165  
  91         88538  
21             require Rex::Commands;
22             require Rex::Config;
23              
24             @EXPORT = qw(upload_and_run i_run i_exec i_exec_nohup);
25              
26             sub upload_and_run {
27 0     0 0 0 my ( $template, %option ) = @_;
28              
29 0         0 my $rnd_file = get_tmp_file;
30              
31 0         0 my $fh = Rex::Interface::File->create;
32 0         0 $fh->open( ">", $rnd_file );
33 0         0 $fh->write($template);
34 0         0 $fh->close;
35              
36 0         0 my $fs = Rex::Interface::Fs->create;
37 0         0 $fs->chmod( 755, $rnd_file );
38              
39 0         0 my @argv;
40 0         0 my $command = $rnd_file;
41              
42 0 0       0 if ( exists $option{with} ) {
43 0         0 $command = Rex::Config->get_executor_for( $option{with} ) . " $command";
44             }
45              
46 0 0       0 if ( exists $option{args} ) {
47 0         0 $command .= join( " ", @{ $option{args} } );
  0         0  
48             }
49              
50 0         0 return i_run("$command 2>&1");
51             }
52              
53             # internal run command, doesn't get reported
54             sub i_run {
55 910     910 0 19437 my $cmd = shift;
56 910         2022 my ( $code, $option );
57 910         2264 $option = {};
58 910 50       3183 if ( ref $_[0] eq "CODE" ) {
59 0         0 $code = shift;
60             }
61 910 100       2999 if ( scalar @_ > 0 ) {
62 823         4843 $option = {@_};
63             }
64              
65 910   50     14203 $option->{valid_retval} ||= [0];
66 910   100     4468 $option->{fail_ok} //= 0;
67              
68 910 50       2785 if ( $option->{no_stderr} ) {
69 0         0 $cmd = "$cmd 2>/dev/null";
70             }
71              
72 910 50       2457 if ( $option->{stderr_to_stdout} ) {
73 0         0 $cmd = "$cmd 2>&1";
74             }
75              
76 910 50       5674 if ( ref $option->{valid_retval} ne "ARRAY" ) {
77 0         0 $option->{valid_retval} = [ $option->{valid_retval} ];
78             }
79              
80 910         2214 my $is_no_hup = 0;
81 910         8346 my $tmp_output_file = get_tmp_file();
82 910 0 33     5889 if ( exists $option->{nohup} && $option->{nohup} ) {
83 0         0 $cmd = "nohup $cmd >$tmp_output_file";
84 0         0 delete $option->{nohup};
85 0         0 $is_no_hup = 1;
86             }
87              
88 910         1676 my $path;
89              
90 910 50       11730 if ( !Rex::Config->get_no_path_cleanup() ) {
91 910         4957 $path = join( ":", Rex::Config->get_path() );
92             }
93              
94 910         10338 my $exec = Rex::Interface::Exec->create;
95 910         4993 my ( $out, $err ) = $exec->exec( $cmd, $path, $option );
96 910         8456 my $ret_val = $?;
97              
98 910 100       4359 chomp $out if $out;
99 910 50       3479 chomp $err if $err;
100              
101 910         16311 $Rex::Commands::Run::LAST_OUTPUT = [ $out, $err ];
102              
103 910   100     9406 $out ||= "";
104 910   50     15322 $err ||= "";
105              
106 910 100       2319 if ( scalar( grep { $_ == $ret_val } @{ $option->{valid_retval} } ) == 0 ) {
  910         12498  
  910         13610  
107 344 50       2112 if ( !$option->{fail_ok} ) {
108 0         0 Rex::Logger::debug("Error executing `$cmd`: ");
109 0         0 Rex::Logger::debug("STDOUT:");
110 0         0 Rex::Logger::debug($out);
111 0         0 Rex::Logger::debug("STDERR:");
112 0         0 Rex::Logger::debug($err);
113              
114 0 0       0 if ($is_no_hup) {
115 0         0 $out = $exec->exec("cat $tmp_output_file ; rm -f $tmp_output_file");
116 0         0 $Rex::Commands::Run::LAST_OUTPUT = [$out];
117 0         0 $? = $ret_val;
118             }
119              
120 0         0 confess("Error during `i_run`");
121             }
122             }
123              
124 910 50       3511 if ($code) {
125 0         0 return &$code( $out, $err );
126             }
127              
128 910 100       5527 if (wantarray) {
129 679         35734 return split( /\r?\n/, $out );
130             }
131              
132 231 50       1618 if ($is_no_hup) {
133 0         0 $out = $exec->exec("cat $tmp_output_file ; rm -f $tmp_output_file");
134 0         0 $Rex::Commands::Run::LAST_OUTPUT = [$out];
135 0         0 $? = $ret_val;
136             }
137              
138 231         11908 return $out;
139             }
140              
141             sub i_exec {
142 0     0 0   my ( $cmd, @args ) = @_;
143              
144 0           my $exec = Rex::Interface::Exec->create;
145 0           my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
146              
147 0           my $_cmd_str = "$cmd " . join( " ", map { $quoter->quote($_) } @args );
  0            
148              
149 0           i_run $_cmd_str;
150             }
151              
152             sub i_exec_nohup {
153 0     0 0   my ( $cmd, @args ) = @_;
154              
155 0           my $exec = Rex::Interface::Exec->create;
156 0           my $quoter = Net::OpenSSH::ShellQuoter->quoter( $exec->shell->name );
157              
158 0           my $_cmd_str = "$cmd " . join( " ", map { $quoter->quote($_) } @args );
  0            
159 0           i_run $_cmd_str, nohup => 1;
160             }
161              
162             1;