line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Deploy::ActionProcessor::Action::Cmd; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
160296672
|
use strict; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
187
|
|
4
|
4
|
|
|
4
|
|
25
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
242
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
39
|
use Fcntl qw(SEEK_END); |
|
4
|
|
|
|
|
121
|
|
|
4
|
|
|
|
|
497
|
|
7
|
4
|
|
|
4
|
|
22
|
use UNIVERSAL::require; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
37
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
107
|
use English qw(-no_match_vars); |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
34
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub _evaluateRet { |
12
|
0
|
|
|
0
|
|
|
my ($retChecks, $buf, $exitStatus) = @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
0
|
|
|
|
|
if (ref($retChecks) ne 'ARRAY') { |
15
|
0
|
|
|
|
|
|
return [ 1, 'ok, no check to evaluate.' ]; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
|
foreach my $retCheck (@$retChecks) { |
19
|
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
if ($retCheck->{type} eq 'okCode') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
21
|
0
|
|
|
|
|
|
foreach (@{$retCheck->{values}}) { |
|
0
|
|
|
|
|
|
|
22
|
0
|
0
|
|
|
|
|
if ($exitStatus == $_) { |
23
|
0
|
|
|
|
|
|
return [ 1, "exit status is ok: $_" ]; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} elsif ($retCheck->{type} eq 'okPattern') { |
27
|
0
|
|
|
|
|
|
foreach (@{$retCheck->{values}}) { |
|
0
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
next unless length($_); |
29
|
0
|
0
|
|
|
|
|
if ($$buf =~ /$_/) { |
30
|
0
|
|
|
|
|
|
return [ 1, "ok pattern found in log: /$_/" ]; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} elsif ($retCheck->{type} eq 'errorCode') { |
34
|
0
|
|
|
|
|
|
foreach (@{$retCheck->{values}}) { |
|
0
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
if ($exitStatus == $_) { |
36
|
0
|
|
|
|
|
|
return [ 0, "exit status is not ok: `$_'" ]; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} elsif ($retCheck->{type} eq 'errorPattern') { |
40
|
0
|
|
|
|
|
|
foreach (@{$retCheck->{values}}) { |
|
0
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
next unless length($_); |
42
|
0
|
0
|
|
|
|
|
if ($$buf =~ /$_/) { |
43
|
0
|
|
|
|
|
|
return [ 0, "error pattern found in log: /$_/" ]; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
0
|
|
|
|
|
|
return [ 0, '' ]; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub runOnUnix { |
52
|
0
|
|
|
0
|
0
|
|
my ($params, $logger) = @_; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
0
|
|
|
|
my $buf = `$params->{exec} 2>&1` || ''; |
55
|
0
|
|
|
|
|
|
my $errMsg = $ERRNO; |
56
|
0
|
|
|
|
|
|
$logger->debug("Run: ".$buf); |
57
|
0
|
|
|
|
|
|
my $exitStatus = $CHILD_ERROR >> 8; |
58
|
0
|
|
|
|
|
|
$logger->debug("exitStatus: ".$exitStatus); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
return ($buf, $errMsg, $exitStatus); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub runOnWindows { |
64
|
0
|
|
|
0
|
0
|
|
my ($params) = @_; |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
FusionInventory::Agent::Tools::Win32->require; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my ($exitcode, $fd) = FusionInventory::Agent::Tools::Win32::runCommand( |
69
|
|
|
|
|
|
|
command => $params->{exec} |
70
|
0
|
|
|
|
|
|
); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
$fd->seek(-2000, SEEK_END); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $buf; |
76
|
0
|
|
|
|
|
|
while(my $line = readline($fd)) { |
77
|
0
|
|
|
|
|
|
$buf .= $line; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $errMsg; |
81
|
0
|
0
|
|
|
|
|
if ($exitcode eq '293') { |
82
|
0
|
|
|
|
|
|
$errMsg = "timeout"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
return ($buf, $errMsg, $exitcode); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub do { |
91
|
0
|
|
|
0
|
0
|
|
my ($params, $logger) = @_; |
92
|
0
|
0
|
|
|
|
|
return { 0, ["Internal agent error"]} unless $params->{exec}; |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
my %envsSaved; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
0
|
0
|
|
|
|
|
if ($params->{envs}) { |
98
|
0
|
|
|
|
|
|
foreach my $key (keys %{$params->{envs}}) { |
|
0
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$envsSaved{$key} = $ENV{$key}; |
100
|
0
|
|
|
|
|
|
$ENV{$key} = $params->{envs}{$key}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $buf; |
105
|
|
|
|
|
|
|
my $errMsg; |
106
|
0
|
|
|
|
|
|
my $exitStatus; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
0
|
0
|
|
|
|
|
if ($OSNAME eq 'MSWin32') { |
110
|
0
|
|
|
|
|
|
($buf, $errMsg, $exitStatus) = runOnWindows(@_); |
111
|
|
|
|
|
|
|
} else { |
112
|
0
|
|
|
|
|
|
($buf, $errMsg, $exitStatus) = runOnUnix(@_); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
0
|
|
|
|
my $logLineLimit = $params->{logLineLimit} || 10; |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
my @msg; |
118
|
0
|
0
|
|
|
|
|
if($buf) { |
119
|
0
|
|
|
|
|
|
my @lines = split('\n', $buf); |
120
|
0
|
|
|
|
|
|
foreach my $line (reverse @lines) { |
121
|
0
|
|
|
|
|
|
chomp($line); |
122
|
0
|
0
|
|
|
|
|
shift @msg if @msg > $logLineLimit; |
123
|
0
|
|
|
|
|
|
unshift @msg, $line; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
0
|
0
|
|
|
|
|
shift @msg if @msg > $logLineLimit; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Use the retChecks key to know if the command exec is successful |
129
|
0
|
|
|
|
|
|
my $t = _evaluateRet ($params->{retChecks}, \$buf, $exitStatus); |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
my $status = $t->[0]; |
132
|
0
|
|
|
|
|
|
push @msg, "--------------------------------"; |
133
|
0
|
0
|
|
|
|
|
push @msg, "error msg: `$errMsg'" if $errMsg; |
134
|
0
|
|
|
|
|
|
push @msg, "exit status: `$exitStatus'"; |
135
|
0
|
|
|
|
|
|
push @msg, $t->[1]; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
foreach (@msg) { |
138
|
0
|
|
|
|
|
|
$logger->debug($_); |
139
|
|
|
|
|
|
|
} |
140
|
0
|
|
|
|
|
|
$logger->debug("exitStatus: ".$exitStatus);; |
141
|
|
|
|
|
|
|
|
142
|
0
|
0
|
|
|
|
|
if ($params->{envs}) { |
143
|
0
|
|
|
|
|
|
foreach my $key (keys %envsSaved) { |
144
|
0
|
|
|
|
|
|
$ENV{$key} = $envsSaved{$key}; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
return { |
149
|
0
|
|
|
|
|
|
status => $status, |
150
|
|
|
|
|
|
|
msg => \@msg, |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |