-
Notifications
You must be signed in to change notification settings - Fork 1
/
gethosts.php
executable file
·255 lines (240 loc) · 8.8 KB
/
gethosts.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
#!/usr/bin/php
<?php
require 'gethosts.cfg.php';
function run($argv) {
if (empty($argv[1])) {
echo "No command found.\n\n";
help();
exit;
}
switch ($argv[1]) {
case '--list':
case '--host':
$headers = loginHeaders();
// Build a group hierarchy.
$groupResource = 'taxonomy_term';
$groups = get($headers, $groupResource, NULL);
$groupHierarchy = buildGroupHierarchy($groups);
// Pull the view that shows the list of Ansible-enabled servers from Drupal.
$resource = 'views/server_list?display_id=services_1';
$titleResource = $urlResource = $queryParam = NULL;
if ($argv[1] == '--host' && $argv[2]) {
$resource .= "&title=$argv[2]";
}
$servers = get($headers, $resource, NULL);
// Get the websites too.
// Ugh - wish I could use "Views Combined Filter" but it uses WS_CONCAT which makes it impossible to do exact "rquals" searches.
if ($argv[1] == '--host' && $argv[2]) {
$queryParam = $argv[2];
}
$titleResource = "views/website_list?display_id=services_1&title=$queryParam";
$websites = get($headers, $titleResource, NULL);
// Don't need a second query of the website list if we just got all websites.
if ($queryParam) {
$urlResource = "views/website_list?display_id=services_1&url=$queryParam";
$websites = $websites + get($headers, $urlResource, NULL);
}
$inventory = buildServerList($servers, $websites);
$inventory = json_encode(array_merge_recursive($inventory, $groupHierarchy));
echo $inventory;
break;
case '--site-mappings':
generateSiteMapping();
break;
default:
echo "Unknown command.\n\n";
case '--help':
help();
}
}
run($argv);
/**
* Generates site mapping for bookmarklets to change between environments. Not strictly Ansible but uses the inventory.
*/
function generateSiteMapping() {
$headers = loginHeaders();
$mapping = [];
$titleResource = 'views/website_list?display_id=services_1';
$websites = get($headers, $titleResource, NULL);
// Because relationships are bidirectional, we only look at dev/test sites.
foreach ($websites as $website) {
// if (!str_contains($website['primary_url'], 'agbu')) {
// continue;
// }
// Don't get any localhosts besides your own.
if ($website['env'] == 'Dev' && $website['server'] !== gethostname()) {
continue;
}
if ($website['canonical_primary_url']) {
$mapping[strtolower($website['env'])][$website['canonical_primary_url']] = $website['primary_url'];
$mapping['live'][$website['primary_url']] = $website['canonical_primary_url'];
}
}
// Dev and test sites aren't connected to each other directly, so we can use the mapping to generate them.
foreach ($mapping['dev'] as $liveSite => $devSite) {
if (!isset($mapping['test'][$liveSite])) {
continue;
}
$mapping['test'][$devSite] = $mapping['test'][$liveSite];
$mapping['dev'][$mapping['test'][$liveSite]] = $devSite;
}
$mappingJson = json_encode($mapping);
echo $mappingJson;
}
/**
* Creates the login headers necessary to access the Drupal data.
*/
function loginHeaders(): array {
# Get password from the "pass" utility.
exec('/usr/bin/pass ls megaphone/crm/restpassword', $password);
$password = $password[0];
return login($password);
}
/**
* Accepts a taxonomy term list from Drupal Services.
* Outputs a hierarchical array of children, suitable for merging into the taxonomy.
*/
function buildGroupHierarchy($groups) {
$hierarchicalList = [];
$groupTids = array_combine(array_column($groups, 'tid'), array_column($groups, 'name'));
foreach ($groups as $group) {
if ($groupTids[$group['parent']] ?? FALSE) {
$hierarchicalList[$groupTids[$group['parent']]]['children'][] = $group['name'];
}
}
// Websites are in their own hierarchy.
$hierarchicalList['websites']['children'] = ['websites_dev', 'websites_test', 'websites_live'];
return $hierarchicalList;
}
/**
* Accepts an array from Drupal Services Views, outputs an Ansible-compatible host list.
*
* @param array $servers
* The array that holds all the server data.
* @param array $websites
* The array that holds all the website data.
*
* @return string $inventory This is a JSON-encoded inventory file in the format Ansible expects.
*/
function buildServerList($servers, $websites) {
$inventory = [];
foreach ($servers as $server) {
// Pull in all field values as Ansible variables.
foreach ($server as $key => $value) {
if (!is_null($value)) {
$key = str_replace(' ', '_', $key);
$inventory['_meta']['hostvars'][$server['fqdn']][$key] = $value;
}
// Handle non-standard SSH ports.
if ($server['security_ssh_port'] != 22) {
$inventory['_meta']['hostvars'][$server['fqdn']]['ansible_port'] = $server['security_ssh_port'];
}
}
// Put servers in groups.
$groups = explode(', ', $server['group']);
foreach ($groups as $group) {
// Don't create a "blank" group.
if (!$group) {
continue;
}
// Don't get any localhosts besides your own.
if ($group == 'localhosts' && $server['hostname'] !== gethostname()) {
$ignoredServers[$server['hostname']] = TRUE;
continue 2;
}
$inventory[$group]['hosts'][] = $server['fqdn'];
}
}
// Websites go in their own group.
if ($websites) {
foreach ($websites as $website) {
// Ignore localhost sites not on this localhost.
if ($ignoredServers[$website['server']] ?? FALSE) {
continue;
}
$inventory['_meta']['hostvars'][$website['bare_url']] = $website;
// Put sites in groups per the "website_groups" field.
$websiteGroups = explode(', ', $website['website_groups']) ?? NULL;
foreach ($websiteGroups as $websiteGroup) {
if ($websiteGroup) {
$inventory[$websiteGroup][] = $website['bare_url'];
}
}
// Put sites in groups based on environment.
$envGroup = 'websites_' . strtolower($website['env']);
$inventory[$envGroup][] = $website['bare_url'];
// Also create maintenance groups.
$maintenanceArray = [
'maintenance_drupal' => 'Drupal',
'maintenance_backdrop' => 'Backdrop',
'maintenance_wp' => 'WordPress',
];
foreach ($maintenanceArray as $group => $descriptor) {
if (strpos($website['contract_type'], $descriptor . ' Maintenance') !== FALSE && $website['cms'] == $descriptor) {
$inventory[$group][] = $website['bare_url'];
}
}
// Drupal8 hack, sigh. Fix when D7 is no more.
if (strpos($website['contract_type'], 'Drupal Maintenance') !== FALSE && $website['cms'] === 'Drupal8') {
$inventory['maintenance_drupal8'][] = $website['bare_url'];
}
if (strpos($website['contract_type'], 'Civi Maintenance') !== false && $website['civicrm'] === 'Yes') {
$inventory['maintenance_civi'][] = $website['bare_url'];
}
// Also put website data in the metadata of their respective server for building Icinga templates.}
$parentServer = $inventory['_meta']['hostvars'][$website['server']] ?? NULL;
if ($parentServer) {
$inventory['_meta']['hostvars'][$website['server']]['sites'][$website['bare_url']] = $website;
}
}
}
return $inventory;
}
function post($curlHeaders, $operation, $postFields = NULL) {
$curlHeaders[] = 'Content-Type: application/json';
$curlHeaders[] = 'Accept: application/json';
$curlOptions = [
CURLOPT_HTTPHEADER => $curlHeaders,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POST => 1,
CURLOPT_URL => ENDPOINT . $operation,
CURLOPT_POSTFIELDS => $postFields ? json_encode($postFields) : NULL,
];
$curl = curl_init();
curl_setopt_array($curl, $curlOptions);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result);
}
function get($curlHeaders, $operation, $body = NULL) {
$curlHeaders[] = 'Accept: application/json';
$curlOptions = [
CURLOPT_HTTPHEADER => $curlHeaders,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => ENDPOINT . $operation,
];
$curl = curl_init();
curl_setopt_array($curl, $curlOptions);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, TRUE);
}
/**
* Handle login (including getting a CSRF header)
* @return array $headers The necessary headers to perform further POST/GET actions.
*/
function login($password) {
$creds = [
'username' => USERNAME,
'password' => $password,
];
$result = post(NULL, 'user/login.json', $creds);
$headers[] = "Cookie: $result->session_name=$result->sessid";
return $headers;
}
function help() {
echo "Usage:\n";
echo " --help This message\n";
echo " --list Show all hosts\n";
echo " --host <hostname> Show a single host specified by <hostname>";
}