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   141434 use v5.12.5;
  91         332  
8 91     91   625 use warnings;
  91         168  
  91         4566  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12             require Exporter;
13 91     91   690 use base qw(Exporter);
  91         214  
  91         7128  
14 91     91   557 use vars qw(@EXPORT);
  91         201  
  91         4360  
15              
16 91     91   3460 use Net::OpenSSH::ShellQuoter;
  91         33427  
  91         945  
17 91     91   4830 use Rex::Interface::File;
  91         215  
  91         898  
18 91     91   2940 use Rex::Interface::Fs;
  91         202  
  91         869  
19 91     91   2846 use Rex::Helper::Path;
  91         215  
  91         5931  
20 91     91   546 use Carp;
  91         204  
  91         83814  
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 909     909 0 16475 my $cmd = shift;
56 909         1739 my ( $code, $option );
57 909         1869 $option = {};
58 909 50       2650 if ( ref $_[0] eq "CODE" ) {
59 0         0 $code = shift;
60             }
61 909 100       2649 if ( scalar @_ > 0 ) {
62 822         4245 $option = {@_};
63             }
64              
65 909   50     11602 $option->{valid_retval} ||= [0];
66 909   100     3598 $option->{fail_ok} //= 0;
67              
68 909 50       2480 if ( $option->{no_stderr} ) {
69 0         0 $cmd = "$cmd 2>/dev/null";
70             }
71              
72 909 50       2270 if ( $option->{stderr_to_stdout} ) {
73 0         0 $cmd = "$cmd 2>&1";
74             }
75              
76 909 50       5550 if ( ref $option->{valid_retval} ne "ARRAY" ) {
77 0         0 $option->{valid_retval} = [ $option->{valid_retval} ];
78             }
79              
80 909         2288 my $is_no_hup = 0;
81 909         7476 my $tmp_output_file = get_tmp_file();
82 909 0 33     6681 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 909         1820 my $path;
89              
90 909 50       10025 if ( !Rex::Config->get_no_path_cleanup() ) {
91 909         3935 $path = join( ":", Rex::Config->get_path() );
92             }
93              
94 909         8487 my $exec = Rex::Interface::Exec->create;
95 909         4350 my ( $out, $err ) = $exec->exec( $cmd, $path, $option );
96 909         6448 my $ret_val = $?;
97              
98 909 100       3561 chomp $out if $out;
99 909 50       2747 chomp $err if $err;
100              
101 909         12769 $Rex::Commands::Run::LAST_OUTPUT = [ $out, $err ];
102              
103 909   100     7982 $out ||= "";
104 909   50     12835 $err ||= "";
105              
106 909 100       2868 if ( scalar( grep { $_ == $ret_val } @{ $option->{valid_retval} } ) == 0 ) {
  909         11267  
  909         11853  
107 344 50       1285 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 909 50       3359 if ($code) {
125 0         0 return &$code( $out, $err );
126             }
127              
128 909 100       2476 if (wantarray) {
129 123         7265 return split( /\r?\n/, $out );
130             }
131              
132 786 50       2021 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 786         31468 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;