-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathakamai-staging.js
executable file
·146 lines (109 loc) · 5.3 KB
/
akamai-staging.js
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
#!/usr/bin/env node
// verbose logging
const debug = require('debug')('staging');
// Platform independent end-of-line character
const endOfLine = require('os').EOL;
// akamai-staging's own DNS library
const dnsLib = require('./dns-core');
// node DNS library
const dns = require('dns');
// Initialise wildcard string parser
const matcher = require('multimatch');
// Array of Akamai edge domains
const edgeDomains = [
'*.edgekey.net',
'*.edgesuite.net',
'*.akamaiedge.net'
];
function getStagingIPAddress(hostname){
debug('getStagingIPAddress() Entry');
// Resolve the hostname to its aliases
debug('Calling resolveCname(%s)', hostname);
dnsLib.resolveCname(hostname, (response) => {
if (response.success === false) {
debug('An error [%s] occurred in resolveCname(%s): %O', response.message, hostname, response.error);
debug('No aliases returned. The record "%s" might not be a CNAME.', hostname);
console.error('# [%s] did not resolve to a CNAME record', hostname);
} else {
// Check if any alias matches an Akamai edge domain
debug('Processing returned aliases: %O', response.aliases);
let matchingAliases = matcher(response.aliases, edgeDomains);
if (matchingAliases.length > 0) {
// There's at least one alias that matches an Akamai edge domain pattern
debug('These look like Akamai domains: %O', matchingAliases);
// Use the first alias as it's the one the customer has created a CNAME over
let alias = matchingAliases[0];
debug('Using the alias %O', alias);
// Break down the alias FQDN into its constituent parts/segments
let segments = alias.split('.');
debug('[%s] was split into these segments: %O', alias, segments);
// Append '-staging' to the penultimate segment
segments[segments.length - 2] += '-staging';
debug('Edited segment %d so it is now: %O', segments.length - 2, segments);
// Convert the array into a string storing the fully qualified Staging variant
let stagingFQDN = segments.join('.');
debug('The Staging alias for %s is %s', hostname, stagingFQDN);
debug('Calling dns.lookup(%s)', stagingFQDN);
// Set DNS lookup options
let options = {
family: 0,
verbatim: false
};
// Perform lookup of staging FQDN
dns.lookup(stagingFQDN, options, (err, address, family) => {
if (err) {
// An error occurred
debug('Error: dns.lookup() returned: %O', err);
console.error('# %s failed to resolve to an IP address', stagingFQDN);
} else {
debug('dns.lookup() returned - address: %j family: IPv%s', address, family);
// Grab the IP Address
let stagingIPAddress = address;
// Define the comment we'll annotate the hosts file entry with
let comment = `#Akamai Staging variant of [${alias}]`;
// Workout a string buffer length (space between host file entry and comment) to tidy up comments' alignment
let entryLength = stagingIPAddress.length + hostname.length;
const offset = 50;
let bufferLength = 5;
if (offset > entryLength + 1) {
bufferLength = offset - entryLength;
}
// output Staging IP entry in hosts file format
console.log('%s %s %s %s', stagingIPAddress, hostname, ' '.repeat(bufferLength), comment);
}
});
} else {
// The DNS answer didn't contain a nested domain that matched a known Akamai domain
console.error('# %s does not resolve to a known Akamai domain', hostname);
}
}
});
}
try {
debug('[%s] started: %O', __filename, process.argv);
// command line options parser
const argv = require('yargs')
.help(false)
.argv;
if ((process.argv.length === 2) || (argv.help)) {
// Show help screen
const help = require('./help');
help.helpScreen(argv.verbose);
} else if ((argv.platform) || (argv.info)) {
// Show platform information
const help = require('./help');
help.platformSpecific();
} else {
// Print a platform agnostic newline character first. This particularly helps when output is appended to a text file
console.log(endOfLine);
// Loop through command line parameters. Expecting 'akamai-staging.js fqdn [fqdn [fqdn] ... ]'
for (let i = 2; i < process.argv.length; i++) {
debug('Extracted "%s" from the command line', process.argv[i]);
// Get the Staging IP address of each fully qualified domain name specified and print to the console
getStagingIPAddress(process.argv[i]);
}
}
}
catch (e) {
console.error('An unexpected error occurred: %s', e);
}