line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Helper::Run; |
6
|
|
|
|
|
|
|
|
7
|
91
|
|
|
91
|
|
140061
|
use v5.12.5; |
|
91
|
|
|
|
|
328
|
|
8
|
91
|
|
|
91
|
|
504
|
use warnings; |
|
91
|
|
|
|
|
197
|
|
|
91
|
|
|
|
|
4986
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.14.2.3'; # TRIAL VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Exporter; |
13
|
91
|
|
|
91
|
|
618
|
use base qw(Exporter); |
|
91
|
|
|
|
|
212
|
|
|
91
|
|
|
|
|
7352
|
|
14
|
91
|
|
|
91
|
|
635
|
use vars qw(@EXPORT); |
|
91
|
|
|
|
|
246
|
|
|
91
|
|
|
|
|
4542
|
|
15
|
|
|
|
|
|
|
|
16
|
91
|
|
|
91
|
|
3554
|
use Net::OpenSSH::ShellQuoter; |
|
91
|
|
|
|
|
34883
|
|
|
91
|
|
|
|
|
1005
|
|
17
|
91
|
|
|
91
|
|
5129
|
use Rex::Interface::File; |
|
91
|
|
|
|
|
220
|
|
|
91
|
|
|
|
|
967
|
|
18
|
91
|
|
|
91
|
|
2990
|
use Rex::Interface::Fs; |
|
91
|
|
|
|
|
244
|
|
|
91
|
|
|
|
|
926
|
|
19
|
91
|
|
|
91
|
|
2916
|
use Rex::Helper::Path; |
|
91
|
|
|
|
|
229
|
|
|
91
|
|
|
|
|
6135
|
|
20
|
91
|
|
|
91
|
|
598
|
use Carp; |
|
91
|
|
|
|
|
169
|
|
|
91
|
|
|
|
|
87682
|
|
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
|
16850
|
my $cmd = shift; |
56
|
909
|
|
|
|
|
1933
|
my ( $code, $option ); |
57
|
909
|
|
|
|
|
2227
|
$option = {}; |
58
|
909
|
50
|
|
|
|
3150
|
if ( ref $_[0] eq "CODE" ) { |
59
|
0
|
|
|
|
|
0
|
$code = shift; |
60
|
|
|
|
|
|
|
} |
61
|
909
|
100
|
|
|
|
2809
|
if ( scalar @_ > 0 ) { |
62
|
822
|
|
|
|
|
4686
|
$option = {@_}; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
909
|
|
50
|
|
|
12335
|
$option->{valid_retval} ||= [0]; |
66
|
909
|
|
100
|
|
|
3786
|
$option->{fail_ok} //= 0; |
67
|
|
|
|
|
|
|
|
68
|
909
|
50
|
|
|
|
2673
|
if ( $option->{no_stderr} ) { |
69
|
0
|
|
|
|
|
0
|
$cmd = "$cmd 2>/dev/null"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
909
|
50
|
|
|
|
2429
|
if ( $option->{stderr_to_stdout} ) { |
73
|
0
|
|
|
|
|
0
|
$cmd = "$cmd 2>&1"; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
909
|
50
|
|
|
|
5528
|
if ( ref $option->{valid_retval} ne "ARRAY" ) { |
77
|
0
|
|
|
|
|
0
|
$option->{valid_retval} = [ $option->{valid_retval} ]; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
909
|
|
|
|
|
2132
|
my $is_no_hup = 0; |
81
|
909
|
|
|
|
|
7562
|
my $tmp_output_file = get_tmp_file(); |
82
|
909
|
0
|
33
|
|
|
6382
|
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
|
|
|
|
|
2005
|
my $path; |
89
|
|
|
|
|
|
|
|
90
|
909
|
50
|
|
|
|
10379
|
if ( !Rex::Config->get_no_path_cleanup() ) { |
91
|
909
|
|
|
|
|
5882
|
$path = join( ":", Rex::Config->get_path() ); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
909
|
|
|
|
|
10098
|
my $exec = Rex::Interface::Exec->create; |
95
|
909
|
|
|
|
|
4578
|
my ( $out, $err ) = $exec->exec( $cmd, $path, $option ); |
96
|
909
|
|
|
|
|
8079
|
my $ret_val = $?; |
97
|
|
|
|
|
|
|
|
98
|
909
|
100
|
|
|
|
4829
|
chomp $out if $out; |
99
|
909
|
50
|
|
|
|
2694
|
chomp $err if $err; |
100
|
|
|
|
|
|
|
|
101
|
909
|
|
|
|
|
15208
|
$Rex::Commands::Run::LAST_OUTPUT = [ $out, $err ]; |
102
|
|
|
|
|
|
|
|
103
|
909
|
|
100
|
|
|
9179
|
$out ||= ""; |
104
|
909
|
|
50
|
|
|
14971
|
$err ||= ""; |
105
|
|
|
|
|
|
|
|
106
|
909
|
100
|
|
|
|
2511
|
if ( scalar( grep { $_ == $ret_val } @{ $option->{valid_retval} } ) == 0 ) { |
|
909
|
|
|
|
|
11166
|
|
|
909
|
|
|
|
|
13736
|
|
107
|
344
|
50
|
|
|
|
1562
|
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
|
|
|
|
3233
|
if ($code) { |
125
|
0
|
|
|
|
|
0
|
return &$code( $out, $err ); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
909
|
100
|
|
|
|
2775
|
if (wantarray) { |
129
|
123
|
|
|
|
|
8885
|
return split( /\r?\n/, $out ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
786
|
50
|
|
|
|
2510
|
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
|
|
|
|
|
35243
|
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; |