line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FusionInventory::Agent::Task::Deploy::ActionProcessor::Action::Cmd; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
134471706
|
use strict; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
106
|
|
4
|
4
|
|
|
4
|
|
16
|
use warnings; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
132
|
|
5
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
15
|
use Fcntl qw(SEEK_SET); |
|
4
|
|
|
|
|
35
|
|
|
4
|
|
|
|
|
241
|
|
7
|
4
|
|
|
4
|
|
17
|
use UNIVERSAL::require; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
42
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
120
|
use English qw(-no_match_vars); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
35
|
|
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
|
|
|
my ($params, $logger) = @_; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
0
|
|
|
|
my $buf = `$params->{exec} 2>&1` || ''; |
55
|
0
|
|
|
|
|
|
my $errMsg = "$ERRNO"; |
56
|
0
|
|
|
|
|
|
$logger->debug2("Run: ".$buf); |
57
|
0
|
|
|
|
|
|
my $exitStatus = $CHILD_ERROR >> 8; |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return ($buf, $errMsg, $exitStatus); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _runOnWindows { |
63
|
0
|
|
|
0
|
|
|
my ($params, $logger) = @_; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
FusionInventory::Agent::Tools::Win32->require; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my ($exitcode, $fd) = FusionInventory::Agent::Tools::Win32::runCommand( |
68
|
|
|
|
|
|
|
command => $params->{exec} |
69
|
0
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$fd->seek(0, SEEK_SET); |
72
|
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $buf; |
74
|
0
|
|
|
|
|
|
while(my $line = readline($fd)) { |
75
|
0
|
|
|
|
|
|
$buf .= $line; |
76
|
|
|
|
|
|
|
} |
77
|
0
|
|
|
|
|
|
$logger->debug2("Run: ".$buf); |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
my $errMsg = ''; |
80
|
0
|
0
|
|
|
|
|
if ($exitcode eq '293') { |
81
|
0
|
|
|
|
|
|
$errMsg = "timeout"; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
return ($buf, $errMsg, $exitcode); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub do { |
89
|
0
|
|
|
0
|
0
|
|
my ($params, $logger) = @_; |
90
|
0
|
0
|
|
|
|
|
return { 0, ["Internal agent error"]} unless $params->{exec}; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my %envsSaved; |
93
|
|
|
|
|
|
|
|
94
|
0
|
0
|
|
|
|
|
if ($params->{envs}) { |
95
|
0
|
|
|
|
|
|
foreach my $key (keys %{$params->{envs}}) { |
|
0
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
$envsSaved{$key} = $ENV{$key}; |
97
|
0
|
|
|
|
|
|
$ENV{$key} = $params->{envs}{$key}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $buf; |
102
|
|
|
|
|
|
|
my $errMsg; |
103
|
0
|
|
|
|
|
|
my $exitStatus; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
0
|
0
|
|
|
|
|
if ($OSNAME eq 'MSWin32') { |
107
|
0
|
|
|
|
|
|
($buf, $errMsg, $exitStatus) = _runOnWindows(@_); |
108
|
|
|
|
|
|
|
} else { |
109
|
0
|
|
|
|
|
|
($buf, $errMsg, $exitStatus) = _runOnUnix(@_); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
0
|
|
|
|
my $logLineLimit = $params->{logLineLimit} || 10; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my @msg; |
115
|
0
|
0
|
|
|
|
|
if($buf) { |
116
|
0
|
|
|
|
|
|
my @lines = split('\n', $buf); |
117
|
0
|
|
|
|
|
|
foreach my $line (reverse @lines) { |
118
|
0
|
|
|
|
|
|
chomp($line); |
119
|
0
|
0
|
|
|
|
|
shift @msg if @msg > $logLineLimit; |
120
|
0
|
|
|
|
|
|
unshift @msg, $line; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
0
|
0
|
|
|
|
|
shift @msg if @msg > $logLineLimit; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Use the retChecks key to know if the command exec is successful |
126
|
0
|
|
|
|
|
|
my $t = _evaluateRet ($params->{retChecks}, \$buf, $exitStatus); |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
my $status = $t->[0]; |
129
|
0
|
|
|
|
|
|
push @msg, "--------------------------------"; |
130
|
0
|
0
|
|
|
|
|
push @msg, "error msg: `$errMsg'" if $errMsg; |
131
|
0
|
|
|
|
|
|
push @msg, "exit status: `$exitStatus'"; |
132
|
0
|
|
|
|
|
|
push @msg, $t->[1]; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
foreach (@msg) { |
135
|
0
|
|
|
|
|
|
$logger->debug($_); |
136
|
|
|
|
|
|
|
} |
137
|
0
|
|
|
|
|
|
$logger->debug("final status: ".$status); |
138
|
|
|
|
|
|
|
|
139
|
0
|
0
|
|
|
|
|
if ($params->{envs}) { |
140
|
0
|
|
|
|
|
|
foreach my $key (keys %envsSaved) { |
141
|
0
|
|
|
|
|
|
$ENV{$key} = $envsSaved{$key}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
return { |
146
|
0
|
|
|
|
|
|
status => $status, |
147
|
|
|
|
|
|
|
msg => \@msg, |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
1; |