-
Notifications
You must be signed in to change notification settings - Fork 0
/
remoteCommand.pl
353 lines (306 loc) · 9.86 KB
/
remoteCommand.pl
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/perl
#
#
# This script call a REST api to get remote commands to execute
# It's part of provisioning script
# The direct ssh call is deprecated.
# Every command or script is stored in database and read from there
# 24.02.2014 A. cristalli
#
#
use Time::Piece;
use integer;
use POSIX qw(strftime);
use Backticks;
use Data::Dumper;
use Sys::Hostname;
use Socket;
use LWP::UserAgent;
use LWP::Simple;
use strict;
use warnings;
use integer;
use Switch;
use JSON ;
use feature qw(switch);
use HTTP::Cookies;
use HTTP::Request;
use LWP;
use File::Path qw{mkpath};
my @so;
my @rack;
my @shelf;
my @client;
my @client_ip;
my @args;
my @exeSeq;
my @scriptID;
my @retCode;
my @exeFlag;
my @logTime;
my @scriptName;
my @scriptContent;
my @remoteCommandID;
my @interpreter;
my @version;
my @execTime;
my @stdOut;
my @stdErr;
my $now_string = strftime "%H", gmtime;
my $file_path = "/tmp/";
my $f;
my @scheduled_time;
my $flag;
my $REST;
my $cmd;
my $ret = 0;
my $list;
sub getIPAddress() {
my $ifconfig = `which ifconfig`
or return "UNDEFINED1";
chomp $ifconfig;
my @ifconfig_output = `$ifconfig -a 2>&1`
or return "UNDEFINED2";
my $interface;
my $STATE;
my $IP;
foreach (@ifconfig_output) {
$interface = $1 if /^(\S+?):?\s/;
next unless defined $interface;
$STATE = uc($1) if /\b(up|down)\b/i;
$IP = $1 if /inet\D+(\d+\.\d+\.\d+\.\d+)/i;
if ( defined $STATE and $STATE eq "UP" ) {
if ( defined $IP and $IP ne "0.0.0.0" and $IP ne "127.0.0.1" ) {
return $IP;
}
}
}
return "UNDEFINED3";
}
#first let know our ip address to before to call REST API
my $address = getIPAddress();
#
#
# Get data in json format, parse and prepare
# the argument is the client address to filter SQL request and the return values are all commands for client not yet executed
#
my $uri_get = 'http://'.$api_url.'/SPOT/provisioning/api/remotecommandses?';
my $uri_delete = 'http://'.$api_url.'/SPOT/provisioning/api/remotecommands/';
my $list_def = $uri_get.'clientaddress='.$address;
my $req_get= HTTP::Request->new( 'GET', $list_def);
#print $req_get->as_string();
my $get = LWP::UserAgent->new(
requests_redirectable => [],
timeout => 10,
);
my $datas = $get->request($req_get);
my $decoded = decode_json($datas->content);
my @recursive = @{ $decoded->{rows} };
if (scalar @recursive == 0) {
print "\n REST interface: no remote commands found.\n";
}
my $uri_complete;
my $req_delete;
#print Dumper @recursive;
foreach $f ( @recursive ) {
if ($f->{executionflag} ne 0 and $f->{executionflag} ne 100) {
print "$f->{exectime}\n";
my $nowTime = localtime;
my $execTime = $f->{exectime};
my $tp1 = Time::Piece->strptime($nowTime, '%a %b %d %H:%M:%S %Y');
my $tp2 = Time::Piece->strptime($execTime, '%a %b %d %H:%M:%S %Y');
print "Now is : $tp1 \n";
my $diff = $tp1 - $tp2;
print "Execution time of the script $tp2 \n";
if ( $diff > 7200 ) {
print "The difference is more than two hours\n";
my $ua = LWP::UserAgent->new;
$uri_complete = $uri_delete . $f->{remotecommandid};
$req_delete = HTTP::Request->new( DELETE => $uri_complete);
my $resp_delete = $ua->request($req_delete);
print Dumper $resp_delete;
print "\n REST interface: About to remove past successfull commands already executed....\n";
}
else
{
print "The difference is less than two hours.\nAborting delete from database.....\n";
}
}
else
{
push(@so, $f->{salesorder});
push(@rack, $f->{rack});
push(@shelf, $f->{shelf});
push(@client, "SO_".$f->{salesorder}."_rack".$f->{rack}."_shelf".$f->{shelf});
push(@client_ip, $f->{clientaddress});
push(@args, $f->{arguments});
push(@exeSeq, $f->{exesequence});
push(@scriptID, $f->{scriptid});
push(@retCode, $f->{returncode});
push(@stdOut, $f->{returnstdout});
push(@stdErr, $f->{returnstderr});
push(@exeFlag, $f->{executionflag});
push(@logTime, $f->{logtime});
push(@execTime, $f->{exectime});
# get script name and script content here
my $uriGetScript = 'http://'.$api_url.'/SPOT/provisioning/api/provisioningscripts/'.$f->{scriptid};
my $req_script_name = HTTP::Request->new( 'GET', $uriGetScript);
my $scriptGet = LWP::UserAgent->new(
requests_redirectable => [],
timeout => 10,
);
my $Rawjson = $scriptGet->request($req_script_name);
my $string = decode_json($Rawjson->content);
my $Sname = $string->{scriptname};
my $Scontent = $string->{scriptcontent};
my $Sinterpreter = $string->{interpreter};
my $Sversion = $string->{version};
push(@scriptName, $file_path.$f->{exesequence}."_SO_".$f->{salesorder}."_rack".$f->{rack}."_shelf".$f->{shelf}."_".$Sname);
push(@scriptContent, $Scontent);
push(@remoteCommandID, $f->{remotecommandid});
push(@interpreter, $Sinterpreter);
push(@version, $Sversion);
}
}
#print Dumper @scriptID;
#print Dumper @scriptName;
#print Dumper @scriptContent;
# Here we are, all data got
# let's get the script associated
# Get the size of records
my $size = scalar @so;
# prepare the script to be executed in /tmp dir
for (my $c = 0; $c < $size; $c++) {
open my $fh, '>', $scriptName[$c];
print $fh $scriptContent[$c];
close($fh);
}
#print "Before sorting: \n";
#print Dumper @rack;
#print Dumper @shelf;
#print Dumper @scriptName;
# Do a permutation on all arrays depending on execution sequence par client
my @permutation = sort { $exeSeq[$a] <=> $exeSeq[$b] } (0..$#exeSeq);
@so = @so[@permutation];
@rack = @rack[@permutation];
@shelf = @shelf[@permutation];
@client = @client[@permutation];
@client_ip = @client_ip[@permutation];
@args = @args[@permutation];
@exeSeq = @exeSeq[@permutation];
@scriptID = @scriptID[@permutation];
@retCode = @retCode[@permutation];
@exeFlag = @exeFlag[@permutation];
@logTime = @logTime[@permutation];
@scriptName = @scriptName[@permutation];
@scriptContent = @scriptContent[@permutation];
@remoteCommandID = @remoteCommandID[@permutation];
@interpreter = @interpreter[@permutation];
@version = @version[@permutation];
@execTime = @execTime[@permutation];
@stdErr = @stdErr[@permutation];
@stdOut = @stdOut[@permutation];
#print "After sorting : \n";
#print Dumper @rack;
#print Dumper @scriptName;
# All values are ready to be executed in the right order
my $result;
#Prepare the response
for ( my $c = 0; $c < $size; $c++) {
my $uri = "http://'.$api_url.'/SPOT/provisioning/api/remotecommands/";
# $REST =
#build the string
my $uri_put = $uri.$remoteCommandID[$c];
my $req = HTTP::Request->new( 'PUT', $uri_put );
$req->header( 'Content-Type' => 'application/json' );
# set custom HTTP request header fields
#This is the directory where linux images reside
$execTime[$c] = localtime;
my $lwp = LWP::UserAgent->new(
timeout => 10,
);
$REST = '{"remotecommandid" : "'.$remoteCommandID[$c].'", "salesorder" : "'.$so[$c]. '", "rack" : "'.$rack[$c].'", "shelf" : "'.$shelf[$c].'", "exectime" : "'.$execTime[$c].'", ';
@scheduled_time = split(':', $execTime[$c]);
if ($exeFlag[$c] == 0 || ($exeFlag[$c] == 100 && $now_string == $scheduled_time[0])) {
system("chmod 775 ".$scriptName[$c]);
$now_string = strftime "%H:%M", localtime;
#converting from json to string
if ( ! $args[$c] eq "")
{
my %arguments = %{ decode_json($args[$c]) };
while (my ($key, $value) = each %arguments) {
print "$key = $value\n";
}
print "arguments sorted by key after decoding:\n";
#Be sure that scalar $list is empty
$list = '';
foreach (sort { $a <=> $b } keys(%arguments) )
{
print "key: $_ value: $arguments{$_}\n";
$list .= " $arguments{$_}";
}
}
#If list is not initialised (no arguments needed) we put a fake value
$list //=" foo";
print "Executing: ".$scriptName[$c].$list."\n";
# Sanitize it before to execute
`dos2unix $scriptName[$c]`;
#$cmd = $scriptName[$c].$args[$c];
$cmd = $scriptName[$c].$list;
#print $remoteCommandID[$c]."\n";
# Decide if expect to not wait for
if ($exeSeq[$c] == 0)
{
# Put the command in execution date on the database to avoid to be executed two times
my $TEMP; # hold the temporary reponse
$flag = 9;
$ret = 9;
$TEMP = $REST.'"executionflag" : "'.$flag.'", "returncode" : "'.$ret.'", "returnstdout" : "The script is going to be executed. The command running is: '.$cmd.'" }';
$req->content($TEMP);
my $resp = $lwp->request($req);
print $req->as_string;
print Dumper $resp;
system($cmd);
$ret = $?/256;
if ($ret == 0 || $ret == 145 ){
$flag = ($exeFlag[$c] == 0) ? 1:101;
$REST = $REST. '"executionflag" : "'.$flag.'", "returncode" : "'.$ret.'", "returnstdout" : "The script output cannot be parsed, only signal error check. The command running is: '.$cmd.'" }';
}
else
{
$ret = $?/256;
$flag = ($exeFlag[$c] == 0) ? 2:102;
$REST = $REST.'"executionflag" : "'.$flag.'", "returncode" : "'.$ret.'", "returnstdout" : "execution FAILED" }'
}
}
else
{
$result = `$cmd`;
$ret = $?/256;
my $stdout = $result->stdout;
my $stderr = $result->stderr;
$stdout =~ s/:/ /g; $stdout =~ s/\"/ /g; $stdout =~ s/{/ /g; $stdout =~ s/}/ /g;
$stderr =~ s/:/ /g; $stderr =~ s/\"/ /g; $stderr =~ s/{/ /g; $stderr =~ s/}/ /g;
# print Dumper $stdout;
# $stdout = to_json $stdout;
if ($ret == 0 || $ret == 145 ){
# if ( $result->success ) {
$flag = ($exeFlag[$c] == 0) ? 1:101;
$REST = $REST.'"executionflag" : "'.$flag.'" , "returncode" : "'.$ret.'" , "returnstdout" : "'.$stdout.'", "returnstderr" : "'.$stderr.'"}';
}
else {
$flag = ($exeFlag[$c] == 0) ? 2:102;
$REST = $REST.'"executionflag" : "'.$flag.'" , "returncode" : "'.$ret.'", "returnstdout" : "'.$stdout.'", "returnstderr" : "'.$stderr.'"}';
}
}
}
elsif ($exeFlag[$c] == 101 && $now_string != $scheduled_time[0]) {
$flag = 100;
$REST = $REST.'"executionflag" : "'.$flag.'"}';
}
$req->content($REST);
my $resp = $lwp->request($req);
print $req->as_string;
print Dumper $resp;
`rm $scriptName[$c]`;
}