-
Notifications
You must be signed in to change notification settings - Fork 26
/
test-instance-connectivity.php
77 lines (67 loc) · 2.33 KB
/
test-instance-connectivity.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
<?php
// Find info for all the instances in this stack.
$tf_instances = explode("\n", `terraform state list | grep aws_instance`);
$instances = [];
foreach (array_filter($tf_instances) as $instance) {
$command = "terraform state show ${instance}";
$retval = `$command`;
$rows = explode("\n", $retval);
foreach (array_filter($rows) as $row) {
list($key, $value) = explode('=', $row);
$instances[$instance][trim($key)] = trim($value);
}
if (strpos($instance, 'primary') !== FALSE) {
$instances[$instance]['vpc'] = 'primary';
}
if (strpos($instance, 'secondary') !== FALSE) {
$instances[$instance]['vpc'] = 'secondary';
}
}
$stack = [];
foreach ($instances as $info) {
$stack[$info['vpc']][$info['availability_zone']] = [
'public' => $info['public_ip'],
'private' => $info['private_ip'],
];
}
// Run through each permutation and ensure machines can all connect to each other.
foreach ($stack as $source_vpc => $source_instances) {
foreach ($source_instances as $source_az => $source_ip) {
foreach ($stack as $dest_vpc => $dest_instances) {
foreach ($dest_instances as $dest_az => $dest_ip) {
if ($source_az == $dest_az && $source_ip == $dest_ip) continue;
$pid = pcntl_fork();
if ($pid == -1) {
exit("Error forking...\n");
}
else if ($pid == 0) {
//echo "- Testing connection between ${source_vpc}.${source_az} ${dest_vpc}.${dest_az}\n";
$response = test_connection($source_ip, $dest_ip);
print_table_row([
'source_vpc' => $source_vpc,
'source_az' => $source_az,
'dest_vpc' => $dest_vpc,
'dest_az' => $dest_az,
'source_ip' => $source_ip['private'],
'dest_ip' => $dest_ip['private'],
'response' => trim($response),
]);
exit();
}
}
}
}
}
while(pcntl_waitpid(0, $status) != -1);
function test_connection($source_ip, $dest_ip) {
$remote_command = "curl -s --connect-timeout 3 -I -XGET ${dest_ip['private']} | grep HTTP";
$local_command = "ssh -q ubuntu@${source_ip['public']} '${remote_command}'";
$response = `$local_command`;
return $response;
}
function print_table_row($row) {
foreach ($row as $i => & $col) {
$col = str_pad($col, 16, " ", STR_PAD_BOTH);
}
print "| " . implode(" | ", $row) . " |\n";
}