-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-vpc.php
executable file
·963 lines (811 loc) · 23.6 KB
/
setup-vpc.php
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
#!/usr/bin/env php
<?php
include 'config.php';
$options = parseOptions();
$config = loadConfig();
/**
* Subnet mapping for those rare cases we need to link VPCs
*/
$subnet_map = array(
"DEFAULT" => "10.11.0.0/16",
"us-west-1" => "10.12.0.0/16",
"us-east-1" => "10.13.0.0/16"
);
/* This is the EC2 API Client object */
$ec2 = Aws\Ec2\Ec2Client::factory(array(
'key' => $aws_key,
'secret' => $aws_secret,
'region' => $config['Region'],
'version'=> 'latest'));
switch($options['action'])
{
case 'ADD':
addNewVpc();
break;
case 'STOP':
case 'DROP':
removeVpc();
break;
case 'REBUILD':
rebuildConfigFile();
break;
}
function rebuildConfigFile()
{
global $ec2, $config, $options;
// We need to discover everything about the VPC and recreate the config file.
// All we have is the intended VPC name. Percona-Training-SUFFIX
$vpcName = sprintf("Percona-Training-%s", $options['suffix']);
try
{
$vpcExists = false;
printf("-- Attempting to discover if VPC ('%s') exists in AWS...\n", $vpcName);
$res = $ec2->describeVpcs(array(
'Filters' => array(
array(
'Name' => 'tag:Name',
'Values' => array($vpcName)
)
)
));
$vpcs = $res->get('Vpcs');
if (isset($vpcs[0]['VpcId']))
{
$config['Vpc'] = $vpcs[0];
saveConfig();
printf("-- VPC ('%s') exists in AWS!\n", $config['Vpc']['VpcId']);
}
else
throw new Exception("-- Not found. Maybe under a different name/suffix?");
// Find the subnet.
$subnetName = sprintf("Percona-Training-%s-SN", $options['suffix']);
$res = $ec2->describeSubnets(array(
'Filters' => array(
array(
'Name' => 'tag:Name',
'Values' => array($subnetName)
)
)
));
$subnets = $res->get('Subnets');
if (isset($subnets[0]['SubnetId'])
&& $subnets[0]['VpcId'] == $config['Vpc']['VpcId'])
{
$config['Subnet'] = $subnets[0];
saveConfig();
printf("-- Subnet ('%s') exists.\n", $config['Subnet']['SubnetId']);
}
else
throw new Exception("-- Couldn't find subnet.");
// The keypair/keyname
$config['KeyPair'] = array('KeyName' => 'Percona-Training');
saveConfig();
}
catch(Exception $e)
{
printf("\n** Unable to discover VPC: %s\n%s**\n", $e->getMessage(), $e->getTraceAsString());
dry_exit();
}
}
function removeVpc()
{
global $ec2, $config, $options;
// You must detach or delete all gateways and resources that are associated with
// the VPC before you can delete it. For example, you must terminate all instances
// running in the VPC, delete all security groups associated with the VPC (except
// the default one), delete all route tables associated with the VPC (except the
// default one), and so on.
printf("Not implemented - Remove VPC's manually in web gui console.\n");
}
function addNewVpc()
{
global $config;
// So much happens for creating a VPC. This is our wrapper function.
createVpc();
createSubnet();
createGateway();
createRouteTable();
assignSubnet();
attachGateway();
updateNetworkAcl();
createInboundRoute();
createKeypair();
createSecurityGroups();
printf("\n- VPC Sucessfully created. Please use this VPC ('%s') when launching new instances.\n\n",
$config['Vpc']['VpcId']);
}
function createVpc()
{
global $ec2, $config, $options;
/* Create new VPC to house all the instances.
* We do this so replication is kept within the private network
*/
try
{
$vpcExists = false;
if (isset($config['Vpc']) && isset($config['Vpc']['VpcId']))
{
printf("- Found existing VPC ('%s') in cache config for '%s'.\n",
$config['Vpc']['VpcId'], $options['region']);
printf("-- Attempting to discover if VPC exists in AWS...\n");
$res = $ec2->describeVpcs(array(
'Filters' => array(
array(
'Name' => 'vpc-id',
'Values' => array($config['Vpc']['VpcId'])
)
)
));
$vpcs = $res->get('Vpcs');
if (isset($vpcs[0]['VpcId']) && $vpcs[0]['VpcId'] == $config['Vpc']['VpcId'])
{
printf("-- VPC exists in AWS!\n");
$vpcExists = true;
}
else
printf("-- Not found. Creating new VPC.\n");
}
/* VPC doesn't exist. Create it.
*/
if (!$vpcExists)
{
$res = $ec2->createVpc(array(
'DryRun' => DRY_RUN,
'CidrBlock' => getSubnetCidrBlock($options['region'])
));
$config['Vpc'] = $res->get('Vpc');
$config['Region'] = $options['region'];
saveConfig();
printf("- VPC Created: %s\n", $config['Vpc']['VpcId']);
}
// Update the name in any case
addName($config['Vpc']['VpcId'],
sprintf("Percona-Training-%s", $options['suffix']));
}
catch(Exception $e)
{
printf("\n** Unable to create VPC: %s**\n", $e->getMessage());
dry_exit();
}
}
function createSubnet()
{
global $ec2, $config, $options;
// Create the subnet for our VPC.
// Instances will get DHCP from this pool.
try
{
$subnetExists = false;
if (isset($config['Subnet']) && isset($config['Subnet']['SubnetId']))
{
printf("- Found existing subnet ('%s') in cache config.\n", $config['Subnet']['SubnetId']);
printf("-- Attempting to discover if subnet exists in AWS...\n");
$res = $ec2->describeSubnets(array(
'Filters' => array(
array(
'Name' => 'subnet-id',
'Values' => array($config['Subnet']['SubnetId'])
)
)
));
$subnets = $res->get('Subnets');
if (count($subnets) == 1
&& isset($subnets[0]['SubnetId'])
&& $subnets[0]['SubnetId'] == $config['Subnet']['SubnetId'])
{
printf("-- Subnet exists in AWS!\n");
$subnetExists = true;
/* Update config */
$config['Subnet'] = $subnets[0];
saveConfig();
}
if (!$subnetExists)
printf("-- Couldn't find previously configured subnet. ");
}
if (!$subnetExists)
{
printf("- Creating new Subnet for VPC.\n");
$res = $ec2->createSubnet(array(
'DryRun' => DRY_RUN,
'VpcId' => $config['Vpc']['VpcId'],
'CidrBlock' => getSubnetCidrBlock($options['region']),
));
$config['Subnet'] = $res->get('Subnet');
saveConfig();
printf("- Created subnet ('%s') on VPC ('%s')\n", $config['Subnet']['SubnetId'], $config['Vpc']['VpcId']);
}
// Update the name in any case.
addName($config['Subnet']['SubnetId'],
sprintf("Percona-Training-%s-SN", $options['suffix']));
}
catch(Exception $e)
{
printf("\n** Unable to create subnet: %s **\n", $e->getMessage());
dry_exit();
}
}
function createGateway()
{
global $ec2, $config, $options;
/* Create an internet gateway. We will attach this gateway to the VPC so that
* students and instructors can access the instances within this VPC via each
* instances' public IP.
*/
try
{
$gatewayExists = false;
if (isset($config['InternetGateway']) && isset($config['InternetGateway']['InternetGatewayId']))
{
printf("- Found existing gateway ('%s') in cache config.\n", $config['InternetGateway']['InternetGatewayId']);
printf("-- Attempting to discover if gateway exists in AWS...\n");
$res = $ec2->describeInternetGateways(array(
'Filters' => array(
array(
'Name' => 'internet-gateway-id',
'Values' => array($config['InternetGateway']['InternetGatewayId'])
)
)
));
$gateways = $res->get('InternetGateways');
if (count($gateways) == 1
&& isset($gateways[0]['InternetGatewayId'])
&& $gateways[0]['InternetGatewayId'] == $config['InternetGateway']['InternetGatewayId'])
{
printf("-- Gateway exists in AWS!\n");
$gatewayExists = true;
/* Update config */
$config['InternetGateway'] = $gateways[0];
saveConfig();
}
if (!$gatewayExists)
printf("-- Could not find previously configured gateway.\n");
}
if (!$gatewayExists)
{
$res = $ec2->createInternetGateway(array(
'DryRun' => DRY_RUN
));
$config['InternetGateway'] = $res->get('InternetGateway');
saveConfig();
printf("-- Created gateway ('%s') on VPC ('%s')\n",
$config['InternetGateway']['InternetGatewayId'], $config['Vpc']['VpcId']);
addName($config['InternetGateway']['InternetGatewayId'],
sprintf("Percona-Training-%s-GW", $options['suffix']));
}
}
catch(Exception $e)
{
printf("\n** Unable to create internet gateway: %s **\n", $e->getMessage());
dry_exit();
}
}
function createRouteTable()
{
global $ec2, $config, $options;
// Create route table for this VPC
try
{
$routeTableExists = false;
if (isset($config['RouteTable']) && isset($config['RouteTable']['RouteTableId']))
{
printf("- Found existing route table ('%s') in cache config.\n", $config['RouteTable']['RouteTableId']);
printf("-- Attempting to discover if route table exists in AWS...\n");
$res = $ec2->describeRouteTables(array(
'Filters' => array(
array(
'Name' => 'route-table-id',
'Values' => array($config['RouteTable']['RouteTableId'])
)
)
));
$routeTables = $res->get('RouteTables');
if (count($routeTables) == 1
&& isset($routeTables[0]['RouteTableId'])
&& $routeTables[0]['RouteTableId'] == $config['RouteTable']['RouteTableId'])
{
printf("-- Route table exists in AWS!\n");
$routeTableExists = true;
/* Update config to match information retreived from AWS */
$config['RouteTable'] = $routeTables[0];
saveConfig();
}
if (!$routeTableExists)
printf("-- Could not find previously configured route table.\n");
}
if (!$routeTableExists)
{
$res = $ec2->createRouteTable(array(
'DryRun' => DRY_RUN,
'VpcId' => $config['Vpc']['VpcId']
));
$config['RouteTable'] = $res->get('RouteTable');
saveConfig();
printf("-- Created route table ('%s') on VPC ('%s')\n",
$config['RouteTable']['RouteTableId'], $config['Vpc']['VpcId']);
addName($config['RouteTable']['RouteTableId'],
sprintf("Percona-Training-%s-RT", $options['suffix']));
}
}
catch(Exception $e)
{
printf("\n** Unable to create route table: %s **\n", $e->getMessage());
dry_exit();
}
}
function assignSubnet()
{
global $ec2, $config, $options;
// Assign our subnet to the route table
try
{
$subnetAssociated = false;
printf("- Verifying subnet ('%s') is associated with route table ('%s')...\n",
$config['Subnet']['SubnetId'], $config['RouteTable']['RouteTableId']);
foreach($config['RouteTable']['Associations'] as $assoc)
{
if (isset($assoc['SubnetId']) && $assoc['SubnetId'] == $config['Subnet']['SubnetId'])
{
printf("-- Subnet properly associated with route table.\n");
$subnetAssociated = true;
}
}
if (!$subnetAssociated)
{
printf("-- Subnet not associated. Attempting association...\n");
$res = $ec2->associateRouteTable(array(
'DryRun' => DRY_RUN,
'SubnetId' => $config['Subnet']['SubnetId'],
'RouteTableId' => $config['RouteTable']['RouteTableId']
));
printf("-- Associated subnet with route table.\n");
}
}
catch(Exception $e)
{
printf("\n** Unable to create or associate subnet for VPC: %s **\n", $e->getMessage());
dry_exit();
}
}
function attachGateway()
{
global $ec2, $config, $options;
// Attach our gateway to the VPC
try
{
$gatewayAttached = false;
printf("- Verifying gateway ('%s') is attached to VPC ('%s')...\n",
$config['InternetGateway']['InternetGatewayId'], $config['Vpc']['VpcId']);
foreach($config['InternetGateway']['Attachments'] as $gwattach)
{
if (isset($gwattach['VpcId']) && $gwattach['VpcId'] == $config['Vpc']['VpcId'])
{
printf("-- Gateway attached to VPC.\n");
$gatewayAttached = true;
}
}
if (!$gatewayAttached)
{
printf("-- Gateway not attached to VPC. Attempting to attach...\n");
$res = $ec2->attachInternetGateway(array(
'DryRun' => DRY_RUN,
'InternetGatewayId' => $config['InternetGateway']['InternetGatewayId'],
'VpcId' => $config['Vpc']['VpcId']
));
printf("-- Attached gateway to VPC.\n");
}
}
catch(Exception $e)
{
printf("\n** Unable to attach gateway to VPC: %s **\n\n", $e->getMessage());
dry_exit();
}
}
function createInboundRoute()
{
global $ec2, $config, $options;
// Create route to allow inbound traffic from the gateway
try
{
$publicRouteExists = false;
printf("- Verifying public inbound route exists on route table ('%s') and gateway ('%s')...\n",
$config['RouteTable']['RouteTableId'], $config['InternetGateway']['InternetGatewayId']);
foreach($config['RouteTable']['Routes'] as $routes)
{
if ((isset($routes['DestinationCidrBlock']) && $routes['DestinationCidrBlock'] == "0.0.0.0/0")
&& (isset($routes['GatewayId']) && $routes['GatewayId'] == $config['InternetGateway']['InternetGatewayId']))
{
printf("-- Public route exists. Route attached to gateway.\n");
$publicRouteExists = true;
}
}
if (!$publicRouteExists)
{
printf("-- Route not found. Adding inbound route to table and gateway.\n");
$res = $ec2->createRoute(array(
'DryRun' => DRY_RUN,
'RouteTableId' => $config['RouteTable']['RouteTableId'],
'DestinationCidrBlock' => '0.0.0.0/0',
'GatewayId' => $config['InternetGateway']['InternetGatewayId']
));
printf("-- Created inboud route for route table on gateway.\n");
}
}
catch(Exception $e)
{
printf("\n** Unable to create inbound route for gateway : %s **\n\n", $e->getMessage());
dry_exit();
}
}
function updateNetworkAcl()
{
global $ec2, $config, $options;
// A default network ACL is created allowing every in. This is just to
// update the display name so if you look in AWS console.
try
{
$res = $ec2->describeNetworkAcls(array(
'Filters' => array(
array(
'Name' => 'vpc-id',
'Values' => array($config['Vpc']['VpcId'])
)
)
));
$networkacl = $res->get('NetworkAcls');
if (count($networkacl) != 1)
throw new Exception("Got more than 1 Network ACL. Investigate.");
addName($networkacl[0]['NetworkAclId'],
sprintf("Percona-Training-%s-ACL", $options['suffix']));
}
catch(Exception $e)
{
printf("\n** Unable to get network ACL : %s **\n\n", $e->getMessage());
dry_exit();
}
}
function createKeypair()
{
global $ec2, $config, $options;
// KeyPair is already installed on AMI, thus no need to make one here.
// In the future, might support re-creating of key but for now, just copy
// the default key in to config.
$config['KeyPair'] = array('KeyName' => 'Percona-Training');
saveConfig();
return;
/* Create new keypair for instances. All instances will use the same keypair
* so that distribution to attendees is simple.
*/
try
{
$keyFilename = sprintf("Percona-Training-%s", $options['suffix']) . '.key';
$useExisting = false;
$foundKeypair = false;
if (isset($config['KeyPair']) && isset($config['KeyPair']['KeyName']))
{
printf("- Found existing keypair ('%s') in config. Fingerprint: '%s'\n",
$config['KeyPair']['KeyName'], $config['KeyPair']['KeyFingerprint']);
printf("-- Verifying keypair exists in VPC...\n");
$res = $ec2->describeKeyPairs(array(
'Filters' => array(
array(
'Name' => 'fingerprint',
'Values' => array($config['KeyPair']['KeyFingerprint'])
)
)
));
$keys = $res->get('KeyPairs');
if (count($keys) == 1
&& $keys[0]['KeyFingerprint'] == $config['KeyPair']['KeyFingerprint'])
{
printf("-- Found keypair in VPC.\n");
$foundKeypair = true;
}
/* Even if the keypair exists in the VPC, user may no longer have keypair file. */
if (file_exists($keyFilename))
{
printf("- Found existing private key file ('%s').\n", $keyFilename);
printf("-- Do you wish to use this keypair?\n");
printf("-- (NOTE: Selecting 'n' will delete and replace existing keypair file)\n");
}
else
{
printf("- Cannot find private key file ('%s').\n", $keyFilename);
printf("-- Do you have this key somewhere else and/or still want to use it?\n");
printf("-- (NOTE: Selecting 'y' without really having the key will make instances un-login-able)\n");
}
/* Behavior based on response above is the same for either choice. */
printf("-- (y/n) - ");
$useExisting = getYesNoResponse();
}
if (!$useExisting)
{
$trainingName = sprintf("Percona-Training-%s", $options['suffix']);
/* Don't want to use the existing keypair. Delete old one from VPC if present */
if ($foundKeypair)
{
$res = $ec2->deleteKeyPair(array(
'DryRun' => DRY_RUN,
'KeyName' => $trainingName
));
printf("-- Deleted old keypair from VPC.\n");
}
printf("- Creating new keypair ('%s')...\n", $trainingName);
$res = $ec2->createKeyPair(array(
'DryRun' => DRY_RUN,
'KeyName' => $trainingName
));
$config['KeyPair'] = array(
'KeyName' => $res->get('KeyName'),
'KeyFingerprint' => $res->get('KeyFingerprint')
);
saveConfig();
// save to file
if (!DRY_RUN)
file_put_contents($keyFilename, $res->get('KeyMaterial'));
printf("-- Created new keypair:\n\n%s\n\n", file_get_contents($keyFilename));
printf("-- Saved keypair to: '%s'\n", $keyFilename);
}
}
catch(Exception $e)
{
printf("Unable to create new keypair: %s\n", $e->getMessage());
dry_exit();
}
}
function createSecurityGroups()
{
global $ec2, $config, $options;
// Have to have a security group when launching an instance
try
{
/* get the default security group for our VPC and see if it already has the SSH
* ingress rule applied.
*/
printf("- Verifying security group has proper ingress rules...\n");
$haveInboundSSHRule = false;
$haveInboundHTTPRule = false;
$haveInboundAltHTTPRule = false;
$haveInboundHTTPSRule = false;
$res = $ec2->describeSecurityGroups(array(
'Filters' => array(
array(
'Name' => 'vpc-id',
'Values' => array($config['Vpc']['VpcId'])
)
)
));
$sgs = $res->get('SecurityGroups');
if (count($sgs) == 1)
{
printf("-- Found default security group ('%s')\n", $sgs[0]['GroupId']);
$config['SecurityGroup'] = $sgs[0];
saveConfig();
/* Found the default security group. Check if has proper ingress rules. */
foreach($config['SecurityGroup']['IpPermissions'] as $perm)
{
// Inbound SSH
if ($perm['IpProtocol'] == 'tcp'
&& $perm['FromPort'] == 22
&& $perm['ToPort'] == 22
&& count($perm['IpRanges']) == 1
&& $perm['IpRanges'][0]['CidrIp'] == '0.0.0.0/0')
{
printf("-- Found ingress rule for SSH (22).\n");
$haveInboundSSHRule = true;
}
// Inbound HTTP
if ($perm['IpProtocol'] == 'tcp'
&& $perm['FromPort'] == 80
&& $perm['ToPort'] == 80
&& count($perm['IpRanges']) == 1
&& $perm['IpRanges'][0]['CidrIp'] == '0.0.0.0/0')
{
printf("-- Found ingress rule for HTTP (80).\n");
$haveInboundHTTPRule = true;
}
// Inbound Alt-HTTP
if ($perm['IpProtocol'] == 'tcp'
&& $perm['FromPort'] == 8080
&& $perm['ToPort'] == 8080
&& count($perm['IpRanges']) == 1
&& $perm['IpRanges'][0]['CidrIp'] == '0.0.0.0/0')
{
printf("-- Found ingress rule for Alt-HTTP (8080).\n");
$haveInboundAltHTTPRule = true;
}
// Inbound HTTPS
if ($perm['IpProtocol'] == 'tcp'
&& $perm['FromPort'] == 443
&& $perm['ToPort'] == 443
&& count($perm['IpRanges']) == 1
&& $perm['IpRanges'][0]['CidrIp'] == '0.0.0.0/0')
{
printf("-- Found ingress rule for HTTPS (443).\n");
$haveInboundHTTPSRule = true;
}
}
}
else
{
throw new Exception("Found something other than 1 security group. Investigate.");
}
// Tag the Security Group
addName($config['SecurityGroup']['GroupId'],
sprintf("Percona-Training-%s-SG", $options['suffix']));
if (!$haveInboundSSHRule)
{
printf("-- Did not find ingress rule for SSH. Adding rule...\n");
addIngressRule(22, "0.0.0.0/0");
printf("-- Added ingress rule for SSH.\n");
}
if (!$haveInboundHTTPRule)
{
printf("-- Did not find ingres rule for HTTP. Adding rule..\n");
addIngressRule(80, "0.0.0.0/0");
printf("-- Added ingress rule for HTTP.\n");
}
if (!$haveInboundHTTPSRule)
{
printf("-- Did not find ingres rule for HTTPS. Adding rule..\n");
addIngressRule(443, "0.0.0.0/0");
printf("-- Added ingress rule for HTTPS.\n");
}
if (!$haveInboundAltHTTPRule)
{
printf("-- Did not find ingres rule for Alt-HTTP. Adding rule..\n");
addIngressRule(8080, "0.0.0.0/0");
printf("-- Added ingress rule for Alt-HTTP.\n");
}
}
catch(Exception $e)
{
printf("Unable to create security group: %s\n", $e->getMessage());
dry_exit();
}
}
/* Helper Functions
*/
function parseOptions()
{
global $argv, $argc;
$opts = getopt("a:p:r:");
foreach($opts as $k => $v)
{
switch ($k)
{
case 'a': $_opt['action'] = strtoupper($v); break;
case 'p': $_opt['suffix'] = strtoupper($v); break;
case 'r': $_opt['region'] = $v; break;
}
}
// Action is required
$actions = array('ADD', 'DROP', 'STATUS', 'TAG', 'REBUILD');
if (!isset($_opt['action']) || !in_array($_opt['action'], $actions))
{
printf("-a is a required option. Possible values are: %s\n",
implode(', ', $actions));
printHelp();
exit();
}
// AWS region required
if (!isset($_opt['region']))
{
printf("\nError: -r is a required option. Possible values are:\n\n\tus-east-1, us-west-1, us-west-2,\n\tap-south-1, ap-northeast-1, ap-northeast-2, ap-southeast-1, ap-southeast-2\n\teu-central-1, eu-west-1, sa-east-1\n");
printHelp();
exit();
}
// Suffix is required
if (!isset($_opt['suffix']) || strlen($_opt['suffix']) < 3)
{
printf("-p is required. 3 character minimum.\n");
printHelp();
exit();
}
return $_opt;
}
function printHelp()
{
global $argv;
print "\n";
printf("Usage: %s -a <action> -r <region> -p <suffix>\n", $argv[0]);
print "\n";
print " -a Action: ADD, DROP, STATUS, TAG, REBUILD\n";
print " -r Region: us-west-1, us-west-2, us-east-1, eu-west-1\n";
print " -p Suffix: Usually a 3-letter code of the city hosting training.\n";
print "\n";
}
function loadConfig()
{
global $config, $options;
// Retrieve or Save settings to cache file
$config = array();
$config['configfile'] = getConfigFile($options['suffix'], $options['region']);
if (file_exists($config['configfile']))
{
if($config = json_decode(file_get_contents($config['configfile']), true))
{
// printf("- Successfully loaded config '%s'\n", $config['configfile']);
}
else
{
printf("\n** Error loading config file: '%s' **\n", $config['configfile']);
exit();
}
}
else
{
if ($options['action'] != 'ADD' && $options['action'] != 'REBUILD')
{
printf("\n** No config file found. Have you configured the VPC? **\n\n");
exit();
}
$config['Region'] = $options['region'];
$config['Suffix'] = $options['suffix'];
}
return $config;
}
function dry_exit()
{
// if (!DRY_RUN)
// exit(1);
}
function saveConfig()
{
global $config;
file_put_contents($config['configfile'], json_encode($config));
}
function getYesNoResponse()
{
$stdin = fopen('php://stdin', 'r');
$res = fgetc($stdin);
fclose($stdin);
return ($res == 'Y' || $res == 'y');
}
function tagEntity($entity, $key, $value)
{
global $ec2;
try
{
$res = $ec2->createTags(array(
'DryRun' => DRY_RUN,
'Resources' => array($entity),
'Tags' => array(
array('Key' => $key, 'Value' => $value)
)
));
}
catch(Exception $e)
{
printf("** Unable to tag entity ('%s') - '%s' => '%s' **\n",
$entity, $key, $value);
}
}
function addName($entity, $value)
{
tagEntity($entity, 'Name', $value);
}
function addIngressRule($port, $cidr)
{
global $ec2, $config;
$res = $ec2->authorizeSecurityGroupIngress(array(
'DryRun' => DRY_RUN,
'GroupId' => $config['SecurityGroup']['GroupId'],
'IpPermissions' => array(
array(
'IpProtocol' => 'tcp',
'FromPort' => $port,
'ToPort' => $port,
'IpRanges' => array(array('CidrIp' => $cidr))
)
)
));
}
function getSubnetCidrBlock($r)
{
global $subnet_map;
if(!array_key_exists($r, $subnet_map))
{
return $subnet_map["DEFAULT"];
}
return $subnet_map[$r];
}