-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.js
197 lines (154 loc) · 4.88 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2020 Joyent, Inc.
*/
/*
* Main CloudAPI Server file.
*
* ./build/node/bin/node main.js \
* -f ./etc/cloudapi.cfg 2>&1 | ./node_dules/.bin/bunyan
*/
var assert = require('assert-plus');
var path = require('path');
var bunyan = require('bunyan');
var nopt = require('nopt');
var restify = require('restify');
var auditLogger = require('./lib/audit_logger');
var RequestCaptureStream = restify.bunyan.RequestCaptureStream;
var app = require('./lib').app;
var modConfig = require('./lib/config.js');
// --- Globals
var LOG;
var PARSED;
var opts = {
'debug': Boolean,
'file': String,
'port': Number,
'help': Boolean
};
var shortOpts = {
'd': ['--debug'],
'f': ['--file'],
'p': ['--port'],
'h': ['--help']
};
// --- Helpers
function setupLogger(config) {
assert.object(config.bunyan, 'config.bunyan');
assert.optionalString(config.bunyan.level, 'config.bunyan.level');
var cfg_b = config.bunyan;
var level = cfg_b.level || LOG.level();
LOG = bunyan.createLogger({
name: 'cloudapi',
serializers: restify.bunyan.serializers,
src: Boolean(bunyan.resolveLevel(level) <= bunyan.TRACE),
streams: [
{
level: level,
stream: process.stderr
},
{
type: 'raw',
stream: new RequestCaptureStream({
level: bunyan.WARN,
maxRecords: 100,
maxRequestIds: 100,
streams: [ {
level: level,
stream: process.stderr
} ]
})
}
]
});
}
function usage(code, message) {
var _opts = '';
Object.keys(shortOpts).forEach(function (k) {
var longOpt = shortOpts[k][0].replace('--', '');
var type = opts[longOpt].name || 'string';
if (type && type === 'boolean') {
type = '';
}
type = type.toLowerCase();
_opts += ' [--' + longOpt + ' ' + type + ']';
});
var msg = (message ? message + '\n' : '') +
'usage: ' + path.basename(process.argv[1]) + _opts;
process.stderr.write(msg + '\n');
process.exit(code);
}
// Create a temporary server which simply returns 500 to all requests
function createBootstrapServer(port, log, cb) {
var bootstrapServer = restify.createServer({ log: log });
bootstrapServer.use(restify.fullResponse());
bootstrapServer.on('after', auditLogger({
log: log.child({ component: 'bootstrap' })
}));
function bootstrapHandler(_req, res, next) {
var msg = 'Attempting to connect to internal services during bootstrap';
log.warn(msg);
res.send(new restify.InternalError(msg));
next();
}
function bootstrapMethodNotAllowed(req, res, _err, next) {
bootstrapHandler(req, res, next);
}
['get', 'post', 'put', 'del', 'head'].forEach(function (method) {
bootstrapServer[method]('/.*/', bootstrapHandler);
});
bootstrapServer.on('MethodNotAllowed', bootstrapMethodNotAllowed);
bootstrapServer.listen(port, function () {
cb(null, bootstrapServer);
});
}
function run() {
LOG = bunyan.createLogger({
level: (PARSED.debug ? 'trace' : 'info'),
name: 'cloudapi',
stream: process.stderr,
serializers: restify.bunyan.serializers
});
var config = modConfig.configure({
configFilePath: PARSED.file,
overrides: PARSED,
log: LOG
});
setupLogger(config);
config.log = LOG;
// We create a temporary server which will reply to all requests with 503
// until the proper cloudapi server can begin listening. We have the
// boostrap since it may not be possible for the proper server to being
// listening due to dependencies not being available (e.g. Moray is
// offline, so plugin configs cannot be loaded)
createBootstrapServer(config.port, LOG, function (err, bootstrapServer) {
if (err) {
throw err;
}
LOG.info('bootstrap listening at %s', bootstrapServer.url);
app.createServer(config, function (err2, server) {
if (err2) {
throw err2;
}
// close bootstrap...
bootstrapServer.close();
LOG.info('bootstrap shut down');
// and listen() on proper server
server.start(function () {
LOG.info('cloudapi listening at %s', server.url);
});
return server;
});
});
}
// --- Mainline
PARSED = nopt(opts, shortOpts, process.argv, 2);
if (PARSED.help) {
usage(0);
}
// There we go!:
run();