-
Notifications
You must be signed in to change notification settings - Fork 223
/
Local.pm
150 lines (109 loc) · 3.18 KB
/
Local.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
package Rex::Interface::Exec::Local;
use v5.12.5;
use warnings;
our $VERSION = '9999.99.99_99'; # VERSION
use Rex::Logger;
use Rex::Commands;
use English qw(-no_match_vars);
use Symbol 'gensym';
use IPC::Open3;
use IO::Select;
use Rex::Interface::Exec::IOReader;
# Use 'parent' is recommended, but from Perl 5.10.1 its in core
use base qw(Rex::Interface::Exec::Base Rex::Interface::Exec::IOReader);
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = {@_};
bless( $self, $proto );
return $self;
}
sub set_env {
my ( $self, $env ) = @_;
my $cmd = undef;
die("Error: env must be a hash")
if ( ref $env ne "HASH" );
while ( my ( $k, $v ) = each(%$env) ) {
$cmd .= "export $k='$v'; ";
}
$self->{env} = $cmd;
}
sub exec {
my ( $self, $cmd, $path, $option ) = @_;
my ( $out, $err, $pid );
if ( exists $option->{cwd} ) {
my $cd_cmd = $OSNAME eq 'MSWin32' ? 'cd /d' : 'cd';
$cmd = "$cd_cmd " . $option->{cwd} . " && $cmd";
}
if ( exists $option->{path} ) {
$path = $option->{path};
}
if ( exists $option->{env} ) {
$self->set_env( $option->{env} );
}
if ( exists $option->{format_cmd} ) {
$option->{format_cmd} =~ s/\{\{CMD\}\}/$cmd/;
$cmd = $option->{format_cmd};
}
Rex::Commands::profiler()->start("exec: $cmd");
if ( $^O !~ m/^MSWin/ ) {
if ($path) { $path = "PATH=$path" }
$path ||= "";
my $new_cmd = "LC_ALL=C $cmd";
if ($path) {
$new_cmd = "export $path ; $new_cmd";
}
if ( $self->{env} ) {
$new_cmd = $self->{env} . " $new_cmd";
}
if ( Rex::Config->get_source_global_profile ) {
$new_cmd = ". /etc/profile >/dev/null 2>&1; $new_cmd";
}
$cmd = $new_cmd;
}
Rex::Logger::debug("Executing: $cmd");
( $out, $err ) = $self->_exec( $cmd, $option );
Rex::Logger::debug($out) if ($out);
if ($err) {
Rex::Logger::debug("========= ERR ============");
Rex::Logger::debug($err);
Rex::Logger::debug("========= ERR ============");
}
Rex::Commands::profiler()->end("exec: $cmd");
if (wantarray) { return ( $out, $err ); }
return $out;
}
sub can_run {
my ( $self, $commands_to_check, $check_with_command ) = @_;
$check_with_command ||= $^O =~ /^MSWin/i ? 'where' : 'which';
return $self->SUPER::can_run( $commands_to_check, $check_with_command );
}
sub _exec {
my ( $self, $cmd, $option ) = @_;
my ( $pid, $writer, $reader, $error, $out, $err );
$error = gensym;
if ( $^O !~ m/^MSWin/ && Rex::Config->get_no_tty ) {
$pid = open3( $writer, $reader, $error, $cmd );
( $out, $err ) = $self->io_read( $reader, $error, $pid, $option );
waitpid( $pid, 0 ) or die($!);
}
else {
$pid = open( my $fh, "-|", "$cmd 2>&1" ) or die($!);
while (<$fh>) {
$out .= $_;
chomp;
$self->execute_line_based_operation( $_, $option )
&& do { kill( 'KILL', $pid ); last };
}
waitpid( $pid, 0 ) or die($!);
}
# we need to bitshift $? so that $? contains the right (and for all
# connection methods the same) exit code after a run()/i_run() call.
# this is for the user, so that he can query $? in his task.
$? >>= 8;
return ( $out, $err );
}
1;