-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathec2dnshelper
executable file
·94 lines (75 loc) · 2.13 KB
/
ec2dnshelper
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
#!/usr/bin/env php
<?php
/**
* ec2dnshelper
*
* @copyright Copyright (c) fruux GmbH. All rights reserved.
* @author Dominik Tobschall (http://fruux.com/)
*/
/*
* Find the Composer autoloader.
* Credit: https://github.com/evert/sabre-vobject/blob/master/bin/vobjectvalidate.php
*/
$paths = [
__DIR__ . '/../vendor/autoload.php', // In case the project is cloned directly
__DIR__ . '/../../../autoload.php', // In case the project is a composer dependency.
];
foreach ($paths as $path) {
if (file_exists($path)) {
include($path);
break;
}
}
/*
* Die if parameter is not set
*/
if (!isset($argv[1])) {
fwrite(STDERR, "Nothing to do here!\n");
die();
}
/* Setting a default timezone so PHP doesn't emit any warnings */
date_default_timezone_set('UTC');
/*
* Import namespaces
*/
use ec2dns\ec2;
use ec2dns\ec2host;
/*
* Instantiate main class.
*/
try {
$ec2 = new ec2(getenv('AWS_ACCESS_KEY_ID'), getenv('AWS_SECRET_ACCESS_KEY'), getenv('EC2_URL'));
} catch (Exception $e) {
fwrite(STDERR, $e->getMessage() . "\n");
die();
}
$matchedHost = null;
/*
* Replace known tags with hostnames
* (This needs optimization!)
*/
$ec2host = new ec2host($ec2);
if ($ec2host->instances) {
$arguments = array_slice($argv, 1);
foreach ($arguments as $argumentKey => $argumentValue) {
if (strpos($argumentValue, '-', 0) === 0 || strpos($argumentValue, '--', 0) === 0) {
continue;
}
foreach ($ec2host->instances as $instance) {
if ($instance['tag'] == $ec2host->emptyTag) {
continue;
}
$newArgumentValue = preg_replace('/(^.*@?)(' . preg_quote($instance['tag'], '/') . ')(:?.*$)/i', '$1' . $instance['dnsName'] . '$3', $argumentValue);
if ($newArgumentValue != $argumentValue) {
$arguments[$argumentKey] = $newArgumentValue;
$matchedHost = $instance['tag'];
break;
}
}
}
}
/*
* Return processed arguments
*/
echo "export ec2args=" . escapeshellarg(implode(' ', $arguments)) . ";\n";
echo "export ec2host=" . $matchedHost . ";\n";