-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsConn.pl
563 lines (399 loc) · 16.2 KB
/
awsConn.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#====================================================================
#
# Filename: awsConn.pl
# Author: Ozan Akcin
# Created: 20161202
# Purpose: manages AWS server connections
# Last Modified:
#
#====================================================================
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use File::Path;
use File::Basename;
use Time::Local;
use FsData::FsSysVars; #---env-specific key-value pairings (replace/ignore)
use FsData::FsLog; #---env-specific logging utility (replace/ignore)
use Util::CfgAws;
my ( $fsVars, $fsLog, $svcTag, $appName );
my ( $defInst, $defCmd, $defUser, $startSleep, $stopSleep, @chkParam );
#---general settings-------------------------------------------------
#---job definition:
$svcTag = 'AWS';
$appName = 'AWS_CONNECTION_MANAGER';
#---task-specific:
$defInst = 'dev';
$defCmd = 'connect';
$defUser = $ENV{USER};
$startSleep = 20;
$stopSleep = 60;
@chkParam = (
'OwnerId'
, 'PublicIpAddress'
, 'VpcId'
, 'ImageId'
, 'KeyName'
, 'GroupId'
, 'InstanceType'
);
#--------------------------------------------------------------------
#==============================================================================
sub setTaskDef
{
my ( $program, $ymd, $HHMMSS, $dtStampLocal );
my ($logRoot, $logFile, $taskArgs, $fsTaskDef );
$dtStampLocal = strftime("%Y%m%d %H%M%S", localtime);
$program = basename($0);
$program =~ s/\.\w*//;
$ymd = substr($dtStampLocal,0,8);
$HHMMSS = substr($dtStampLocal,-6);
$logRoot = "$program.".$HHMMSS;
$taskArgs = {
appName => $appName
, processName => $appName
, logRoot => $logRoot
};
$fsTaskDef = {
SERVICE_TAG => "${svcTag}"
};
$fsVars = new FsData::FsSysVars($taskArgs, $fsTaskDef);
$logFile = defined($fsVars->getArgv('logFile'))
? $fsVars->getArgv('logFile')
: $fsVars->getFsVar('FSDATA_LOG_DIR')
. '/' . $ymd
. '/' . $fsVars->getFsVar('SERVICE_TAG')
. '/' . $fsVars->getArgv('logRoot')
;
$fsLog = new FsData::FsLog( $fsVars->getFsVars, $logFile );
$fsVars->setArgv('YYYYMMDD',$ymd);
$fsVars->setArgv('HHMMSS',$HHMMSS);
#---print invocation to logFile:
$fsLog->print("***START***");
$fsLog->print(`ps -o args -C perl | grep $0 | tr -d '\n'`)
}
#==============================================================================
#==============================================================================
sub initArgs
{
#---future instances need to be coded for here, including if not 'current'
my ( $fsLog, $fsVars ) = @_;
$fsLog->print("Initializing arguments...");
my ( $workDir, $inst, $cmd, $user, $awsCfg );
$workDir = defined($fsVars->getArgv('workDir'))
? $fsVars->getArgv('workDir')
: $fsVars->getFsVar('FSDATA_OUTPUT_DIR')
. '/' . $svcTag
. '/' . 'tmp_' . $$
;
$inst = defined($fsVars->getArgv('inst'))
? $fsVars->getArgv('inst')
: $defInst
;
$cmd = defined($fsVars->getArgv('cmd'))
? $fsVars->getArgv('cmd')
: $defCmd
;
$user = defined($fsVars->getArgv('user'))
? $fsVars->getArgv('user')
: $defUser
;
$user = 'oakcin' if ( $user eq 'akcinoz' );
$awsCfg = new FsData::CfgAws;
mkpath($workDir) if ( ! -d $workDir && $cmd eq 'start' );
$fsVars->setArgv('workDir',$workDir);
$fsVars->setArgv('inst',$inst);
$fsVars->setArgv('cmd',$cmd);
$fsVars->setArgv('user',$user);
$fsVars->setArgv('awsCfg', $awsCfg);
}
#==============================================================================
#==============================================================================
sub awsConnect
{
my ( $fsLog, $fsVars ) = @_;
my ( %awsCfg, $user, $inst, $instanceId, $keyPairFile, $ipFile, $invFile, $awsIp, $cmdString );
$fsLog->print("Reading configurations for connection to AWS server...");
$user = $fsVars->getArgv('user');
$inst = $fsVars->getArgv('inst');
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$ipFile = $awsCfg{awsGlobalVars}->{ipFile};
$ipFile =~ s/INSTANCE/$inst/g;
($invFile = $ipFile) =~ s/pubDNS/invalid/g;
$fsLog->die("ERROR: no IP file detected - try starting remote AWS server for: $inst first.") if ( ! -f $ipFile );
$keyPairFile = $awsCfg{instances}->{$inst}->{keyPairFile};
$instanceId = $awsCfg{instances}->{$inst}->{instanceId};
open ( my $fh, $ipFile ) or $fsLog->die("Cannot open: $ipFile");
{ $awsIp = <$fh>; }
close($fh);
chomp $awsIp;
my $currentIp = getInstanceIp($fsLog, $instanceId);
if ( $currentIp ne $awsIp ) {
my $alert = "IP Addresses do not match. Instance: $inst currently running on: $currentIp, file gives: $awsIp...";
$fsLog->print($alert);
print $alert."\n";
$fsLog->die("ERROR: invalid IP/log-on control file: $invFile detected. Try removing: $ipFile and (re)-starting AWS server instance.");
}
$cmdString = "ssh -q -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no ";
$cmdString .= "-i KEYPAIRFILE USER\@AWSIP";
$cmdString =~ s/KEYPAIRFILE/$keyPairFile/g;
$cmdString =~ s/USER/$user/g;
$cmdString =~ s/AWSIP/$awsIp/g;
$fsLog->print("Connecting to AWS instance id: $instanceId...bye!");
$fsLog->print("$cmdString");
exec ("$cmdString");
}
#==============================================================================
#==============================================================================
sub getLocParam
{
my ( $fsLog, $fsVars ) = @_;
my ( $inst, %awsCfg, %locParam );
$inst = $fsVars->getArgv('inst');
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$fsLog->print("Collating local connection parameters for server: $inst...");
$locParam{'InstanceType'} = $awsCfg{instances}->{$inst}->{instanceType};
$locParam{'VpcId'} = $awsCfg{instances}->{$inst}->{vpcId};
$locParam{'OwnerId'} = $awsCfg{awsGlobalVars}->{accountNum};
$locParam{'KeyName'} = $awsCfg{instances}->{$inst}->{keyPairName};
$locParam{'ImageId'} = $awsCfg{instances}->{$inst}->{ami};
$locParam{'GroupId'} = $awsCfg{instances}->{$inst}->{secGroupId};
$fsVars->setArgv('locParam',\%locParam);
}
#==============================================================================
#==============================================================================
sub awsAuthenticate
{
my ( $fsLog, $fsVars ) = @_;
getRemParam($fsLog, $fsVars);
getLocParam($fsLog, $fsVars);
my ( %awsCfg, %locParam, %remParam, $valid, $inst, $instanceId, $ipFile, $invFile );
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$ipFile = $awsCfg{awsGlobalVars}->{ipFile};
$inst = $fsVars->getArgv('inst');
$instanceId = $awsCfg{instances}->{$inst}->{instanceId};
$ipFile =~ s/INSTANCE/$inst/g;
($invFile = $ipFile) =~ s/pubDNS/invalid/g;
$valid = 0;
$fsLog->print("Comparing remote and local AWS parameter settings...");
%locParam = %{$fsVars->getArgv('locParam')};
%remParam = %{$fsVars->getArgv('remParam')};
#---compare parameters and log differences
foreach my $skey ( keys %remParam ) {
next if ( $skey eq 'PublicIpAddress' );
if ( $locParam{$skey} ne $remParam{$skey} ) {
$fsLog->print("Parameter mismatch - $skey: $locParam{$skey} (loc) v. $remParam{$skey} (rem)");
$valid++;
} else {
$fsLog->print("Validated $skey: $locParam{$skey} (loc) and $remParam{$skey} (rem)");
}
}
if ( $valid > 0 ) {
$fsLog->print("Generating invalid ip-logon file...");
$fsLog->print("System(bash -c \"touch $invFile\")");
my $sysMsg = `bash -c \"touch $invFile\"`;
$fsLog->die($sysMsg) if ($sysMsg);
$fsLog->die("ERROR: One or more settings do not match..see $fsLog->{_logFile} for details...");
}
$fsLog->print("Expected remote and local settings verified - updating ip file for inst: $inst...");
unlink $ipFile if ( -f $ipFile );
open my $out, '>', $ipFile;
print $out $remParam{PublicIpAddress};
close($out);
}
#==============================================================================
#==============================================================================
sub getInstanceIp
{
my ( $fsLog, $instanceId ) = @_;
$fsLog->print("Fetching instance IP for instance: $instanceId...");
my ( $cmdString, $currentIp );
$cmdString = "aws ec2 describe-instances --instance-ids $instanceId ";
$cmdString .= "--query 'Reservations[0].Instances[0].PublicIpAddress'";
$fsLog->print("System('$cmdString')");
$currentIp = `bash -c \"$cmdString\" | tr -d '\n'`;
$currentIp =~ s/\"//g;
return $currentIp;
}
#==============================================================================
#==============================================================================
sub awsDescribe
{
my ( $fsLog, $fsVars ) = @_;
my ( %awsCfg, $inst, $instanceId );
my ( $cmdStringRoot, $cmdStringCode, $cmdStringState, $cmdStringIP, $remState, $remCode, $awsIp);
$inst = $fsVars->getArgv('inst');
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$instanceId = $awsCfg{instances}->{$inst}->{instanceId};
$fsLog->die("Instance: $inst not found in configurations.") if ( ! $instanceId );
$cmdStringRoot = "aws ec2 describe-instances --instance-ids $instanceId";
$cmdStringCode = $cmdStringRoot." --query 'Reservations[0].Instances[0].State.Code'";
$cmdStringState = $cmdStringRoot." --query 'Reservations[0].Instances[0].State.Name'";
$fsLog->print("Checking AWS status code...");
$fsLog->print("System('$cmdStringCode')");
$remCode = `bash -c \"$cmdStringCode\"`;
$fsLog->print("Checking AWS status name...");
$fsLog->print("System('$cmdStringState')");
$remState = `bash -c \"$cmdStringState\"`;
chomp $remCode;
chomp $remState;
$remState =~ s/\"//g;
$remState =~ s/\'//g;
if ( ! defined($remCode) || ! defined($remState) ){
$fsLog->die("Error in ascertaining one or more state descriptive variables: 'Code', 'Name'");
}
if ( ! defined($awsCfg{awsReturnValues}->{$remCode}) ) {
$fsLog->print("Unable to verify returned status code: $remCode");
$fsLog->die("No such code recorded in module/library.");
}
if ( $awsCfg{awsReturnValues}->{$remCode} ne $remState ) {
$fsLog->print("Unable to verify returned status: $remState for code: $remCode.");
$fsLog->die("Regsitered status for code: $remCode is: ".$awsCfg{awsReturnValues}->{$remCode});
}
my $status = "Verified AWS server instance: $inst ($instanceId) "
." returned status: $remState ($remCode) ";
if ( $remCode == 16 ) {
$cmdStringIP = $cmdStringRoot." --query 'Reservations[0].Instances[0].PublicIpAddress'";
$fsLog->print("Checking AWS IP address...");
$fsLog->print("System('$cmdStringIP')");
$awsIp = `bash -c \"$cmdStringIP\"`;
$status .= "on ip address: $awsIp";
}
$fsLog->print($status);
print $status."\n";
}
#==============================================================================
#==============================================================================
sub getRemParam
{
my ( $fsLog, $fsVars ) = @_;
my ( $workDir, $outFile, %awsCfg, $inst, $instanceId, $cmdString, $response );
my ( %remParam, $remState, $remCode, $awsIp );
$workDir = $fsVars->getArgv('workDir');
$inst = $fsVars->getArgv('inst');
$outFile = $workDir.'/aws_response';
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$instanceId = $awsCfg{instances}->{$inst}->{instanceId};
$fsLog->print("Checking AWS server response...");
$cmdString = "aws ec2 describe-instances --instance-ids $instanceId ";
$response = `bash -c \"$cmdString\"`;
$fsLog->print("$response");
$response =~ s/\,/\n/g;
$response =~ s/\"//g;
open my $out, '>', $outFile;
print $out $response;
close($out);
open (my $fh, $outFile);
while(<$fh>){
chomp;
$_ =~ s/\s//g;
my ( $param, $arg ) = split /\:/, $_;
next if ( ! defined($param) );
if ( $param eq 'PublicIpAddress' ) {
$awsIp = $arg;
$fsVars->setArgv('awsIp',$awsIp);
}
if ( grep { $chkParam[$_] eq $param } 0 ..$#chkParam ) {
$remParam{$param} = $arg;
}
}
$fsVars->setArgv('remParam',\%remParam);
}
#==============================================================================
#==============================================================================
sub awsStart
{
my ( $fsLog, $fsVars ) = @_;
my ( %awsCfg, $inst, $instance, $instanceId, $cmdString, $response );
$inst = $fsVars->getArgv('inst');
$fsLog->print("Reading configurations to start AWS server...");
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$instanceId = $awsCfg{instances}->{$inst}->{instanceId};
$cmdString = "aws ec2 start-instances --instance-ids $instanceId";
$fsLog->print("Sending start command to remote AWS server instance: $instanceId...");
$fsLog->print("$cmdString");
$response = `bash -c \"$cmdString\"`;
$fsLog->print("AWS Response: $response...");
sleep($startSleep);
awsDescribe( $fsLog, $fsVars );
awsAuthenticate( $fsLog, $fsVars );
}
#==============================================================================
#==============================================================================
sub awsStop
{
my ( $fsLog, $fsVars ) = @_;
my ( %awsCfg, $inst, $instanceId, $cmdString, $response, $ipFile );
$inst = $fsVars->getArgv('inst');
$fsLog->print("Reading configurations to stop AWS server...");
%awsCfg = %{$fsVars->getArgv('awsCfg')};
%awsCfg = %{$awsCfg{awsCfgVars}};
$ipFile = $awsCfg{awsGlobalVars}->{ipFile};
$inst = $fsVars->getArgv('inst');
$instanceId = $awsCfg{instances}->{$inst}->{instanceId};
$ipFile =~ s/INSTANCE/$inst/g;
$cmdString = "aws ec2 stop-instances --instance-ids $instanceId";
$fsLog->print("Sending stop command to remote AWS server instance: $instanceId...");
$fsLog->print("$cmdString");
$response = `bash -c \"$cmdString\"`;
$fsLog->print("AWS Response: $response...");
sleep($stopSleep);
awsDescribe( $fsLog, $fsVars );
$fsLog->print("Removing ip file: $ipFile...");
unlink $ipFile if ( -f $ipFile );
}
#==============================================================================
#==============================================================================
sub awsConnManager
{
my ( $fsLog, $fsVars) = @_;
my $cmd = $fsVars->getArgv('cmd');
if ( $cmd eq 'connect' ) {
awsConnect($fsLog, $fsVars);
} elsif ( $cmd eq 'start' ) {
awsStart($fsLog, $fsVars);
} elsif ( $cmd eq 'describe' ) {
awsDescribe( $fsLog , $fsVars);
} elsif ( $cmd eq 'stop' ) {
awsStop($fsLog, $fsVars);
} else {
$fsLog->die("Unrecognized command: $cmd...");
}
}
#==============================================================================
#==============================================================================
sub cleanWork
{
my ($fsLog, $fsVars) = @_;
$fsLog->print("Cleaning up...");
my $workDir = $fsVars->getArgv('workDir');
if ( -d $workDir ) {
$fsLog->print("Removing work directory: $workDir");
$fsLog->print("System('rm -r $workDir')");
my $sysMsg = `bash -c \"rm -r $workDir\"`;
if ( $sysMsg ) {
$fsLog->print("Unexpected return encountered trying to remove: $workDir...");
$fsLog->die("SYSMSG: $sysMsg");
}
}
1;
}
#==============================================================================
exit(main());
#==============================================================================
sub main
{
setTaskDef;
initArgs($fsLog, $fsVars);
awsConnManager($fsLog,$fsVars);
cleanWork($fsLog, $fsVars);
$fsLog->print("***DONE***");
}
#==============================================================================