-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
177 lines (148 loc) · 4.53 KB
/
index.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
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
#!/usr/bin/env node
/* jshint -W098 */
/*!
* Totem Discover
* Copyright(c) 2013 Meltmedia <[email protected]>
* MIT Licensed
*/
'use strict';
var winston = require('winston'),
os = require('os'),
path = require('path'),
request = require('request'),
async = require('async'),
Discover = require('./lib/discover');
// Remove console transport until we know we are run from the CLI
winston.remove(winston.transports.Console);
/**
* Discover CLI
*/
function main() {
var nconf = require('nconf');
//
// Setup nconf to use (in-order):
// 1. Command-line arguments
// 2. Environment variables
// 2. Custom config file
// 2. Defaults
//
nconf
.argv()
.env('_');
// Load custom config file
if (nconf.get('config')) {
nconf.file('config', nconf.get('config'));
}
// Last case scenerio, we set sane defaults
nconf.file('defaults', path.join(__dirname, 'config/defaults.json'));
var logOptions = {
label: process.pid,
timestamp: true,
prettyPrint: true,
colorize: true
};
if (nconf.get('silly')) {
logOptions.level = 'silly';
} else if (nconf.get('debug')) {
logOptions.level = 'debug';
} else if (nconf.get('verbose')) {
logOptions.level = 'verbose';
}
if (nconf.get('log')) {
logOptions.filename = nconf.get('log');
winston.add(winston.transports.File, logOptions);
}
// Configure the console logger
winston.add(winston.transports.Console, logOptions);
console.log(' ___ _ '.cyan);
console.log(' / _ \\(_)__ _______ _ _____ ____'.cyan);
console.log(' / // / (_-</ __/ _ \\ |/ / -_) __/'.cyan);
console.log('/____/_/___/\\__/\\___/___/\\__/_/ '.cyan);
try {
configure(function (err, config) {
if (err) {
throw err;
}
winston.debug('Starting discover...');
var discover = new Discover(config);
discover.once('connect', function () {
discover.start();
});
});
} catch (err) {
winston.error('Failed to start due to: ' + err);
process.exit(1);
}
// Build application configuration from explicit and implicit sources
function configure(cb) {
winston.debug('Configuring discover...');
async.parallel(
{
discover: function configureDiscover(config) {
config(null, {
serviceVariable: nconf.get('discover:serviceVariable') || 'DISCOVER'
});
},
docker: function configureDocker(config) {
config(null, {
socketPath: nconf.get('docker:socketPath'),
host: nconf.get('docker:host'),
port: nconf.get('docker:port'),
version: nconf.get('docker:version')
});
},
etcd: function configureEtcd(config) {
config(null, {
host: nconf.get('etcd:host'),
port: nconf.get('etcd:port'),
prefix: nconf.get('etcd:prefix')
});
},
host: function configureHost(config) {
var hostIp = '127.0.0.1', // Default host IP if we can't find one in network interfaces
hostId = os.hostname(); // Default host name if not explicitly configured
async.parallel([
function (done) {
// Get instance ID from AWS for host ID
request({ url: 'http://169.254.169.254/latest/meta-data/instance-id', timeout: 2000 }, function (err, res, body) {
if (!err && body) {
hostId = body;
}
done();
});
},
function (done) {
// Get the host IP address from the public interface
var interfaces = os.networkInterfaces()['eth0'];
if (interfaces) {
interfaces.forEach(function (item) {
if (item.family === 'IPv4') {
hostIp = item.address;
}
});
}
done();
}
], function () {
config(null, {
ip: nconf.get('host:ip') || hostIp,
realm: nconf.get('host:realm') || 'default',
id: nconf.get('host:id') || hostId
});
});
}
},
function (err, config) {
if (err) {
winston.error('Unable to configure Discover due to: ' + err);
return cb(err);
}
winston.debug('Configuring discover complete: ' + JSON.stringify(config, null, ' '));
cb(null, config);
}
);
}
}
if (require.main === module) {
main();
}