-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.js
321 lines (280 loc) · 11 KB
/
server.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* 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 entry-point for the VMs API.
*/
var assert = require('assert-plus');
var bunyan = require('bunyan');
var changefeed = require('changefeed');
var cueball = require('cueball');
var fs = require('fs');
var http = require('http');
var https = require('https');
var jsprim = require('jsprim');
var path = require('path');
var restify = require('restify');
var sigyan = require('sigyan');
var util = require('util');
var vasync = require('vasync');
var sdc = require('sdc-clients');
var CNAPI = require('./lib/apis/cnapi');
var IMGAPI = require('./lib/apis/imgapi');
var PAPI = require('./lib/apis/papi');
var VmapiApp = require('./lib/vmapi');
var VOLAPI = require('sdc-clients').VOLAPI;
var WFAPI = require('./lib/apis/wfapi');
var configLoader = require('./lib/config-loader');
var createMetricsManager = require('triton-metrics').createMetricsManager;
var DataMigrationsController = require('./lib/data-migrations/controller');
var dataMigrationsLoader = require('./lib/data-migrations/loader');
var morayInit = require('./lib/moray/moray-init.js');
var DATA_MIGRATIONS;
var dataMigrationCtrl;
var morayBucketsInitializer;
var morayClient;
var moray;
var METRICS_SERVER_PORT = 8881;
var VERSION = false;
/*
* Returns the current semver version stored in CloudAPI's package.json.
* This is used to set in the API versioning and in the Server header.
*
* @return {String} version.
*/
function version() {
if (!VERSION) {
var pkg = fs.readFileSync(__dirname + '/package.json', 'utf8');
VERSION = JSON.parse(pkg).version;
}
return VERSION;
}
/*
* Creates instances of objects providing abstractions to various Triton APIs
* that are used by VMAPI. It returns an object of the following form:
*
* {
* cnapi: cnapiClientInstance,
* imgapi: imgapiClientInstance,
* ...
* }
*
* with each property named after these APIs, and each value being set to an
* instance of each corresponding abstraction layer for these APIs.
*/
function createApiClients(config, parentLog) {
assert.object(config, 'config');
assert.object(parentLog, 'parentLog');
var agent;
if (config.cueballHttpAgent) {
agent = new cueball.HttpAgent(config.cueballHttpAgent);
}
assert.object(config.cnapi, 'config.cnapi');
var cnapiClientOpts = jsprim.deepCopy(config.cnapi);
cnapiClientOpts.log = parentLog.child({ component: 'cnapi' }, true);
cnapiClientOpts.agent = agent;
var cnapiClient = new CNAPI(cnapiClientOpts);
assert.object(config.imgapi, 'config.imgapi');
var imgapiClientOpts = jsprim.deepCopy(config.imgapi);
imgapiClientOpts.log = parentLog.child({ component: 'imgapi' }, true);
imgapiClientOpts.agent = agent;
var imgapiClient = new IMGAPI(imgapiClientOpts);
assert.object(config.napi, 'config.napi');
var napiLog = parentLog.child({ component: 'napi' }, true);
var napiClient = new sdc.NAPI({
log: napiLog,
url: config.napi.url,
agent: agent
});
assert.object(config.papi, 'config.papi');
var papiClientOpts = jsprim.deepCopy(config.papi);
papiClientOpts.log = parentLog.child({ component: 'papi' }, true);
papiClientOpts.agent = agent;
var papiClient = new PAPI(papiClientOpts);
assert.object(config.volapi, 'config.volapi');
var volapiClientOpts = jsprim.deepCopy(config.volapi);
var volapiClient = new VOLAPI({
agent: agent,
url: volapiClientOpts.url,
userAgent: 'sdc-vmapi'
});
assert.object(config.wfapi, 'config.wfapi');
var wfapiClientOpts = jsprim.deepCopy(config.wfapi);
wfapiClientOpts.log = parentLog.child({ component: 'wfapi' }, true);
wfapiClientOpts.agent = agent;
var wfapiClient = new WFAPI(wfapiClientOpts);
return {
cnapi: cnapiClient,
imgapi: imgapiClient,
napi: napiClient,
papi: papiClient,
volapi: volapiClient,
wfapi: wfapiClient
};
}
function startVmapiService() {
var apiClients;
var changefeedPublisher;
var configFilePath = path.join(__dirname, 'config.json');
var config = configLoader.loadConfig(configFilePath);
var dataMigrations;
var dataMigrationsCtrl;
var metricsManager;
var vmapiLog = bunyan.createLogger({
name: 'vmapi',
level: config.logLevel,
serializers: restify.bunyan.serializers
});
config.version = version() || '7.0.0';
// Increase/decrease loggers levels using SIGUSR2/SIGUSR1:
sigyan.add([vmapiLog]);
http.globalAgent.maxSockets = config.maxSockets || 100;
https.globalAgent.maxSockets = config.maxSockets || 100;
apiClients = createApiClients(config, vmapiLog);
vasync.pipeline({funcs: [
function initChangefeedPublisher(_, next) {
var changefeedOptions = jsprim.deepCopy(config.changefeed);
changefeedOptions.log = vmapiLog.child({ component: 'changefeed' },
true);
changefeedOptions.log.level(bunyan.WARN);
changefeedPublisher =
changefeed.createPublisher(changefeedOptions);
changefeedPublisher.on('moray-ready', function onMorayReady() {
changefeedPublisher.start();
next();
});
},
function loadDataMigrations(_, next) {
vmapiLog.info('Loading data migrations modules');
dataMigrationsLoader.loadMigrations({
log: vmapiLog.child({ component: 'migrations-loader' }, true)
}, function onMigrationsLoaded(migrationsLoadErr, migrations) {
if (migrationsLoadErr) {
vmapiLog.error({err: migrationsLoadErr},
'Error when loading data migrations modules');
} else {
vmapiLog.info({migrations: migrations},
'Loaded data migrations modules successfully!');
}
dataMigrations = migrations;
next(migrationsLoadErr);
});
},
function initMoray(_, next) {
assert.object(changefeedPublisher, 'changefeedPublisher');
var morayConfig = jsprim.deepCopy(config.moray);
morayConfig.changefeedPublisher = changefeedPublisher;
var moraySetup = morayInit.startMorayInit({
morayConfig: morayConfig,
log: vmapiLog.child({ component: 'moray-init' }, true),
changefeedPublisher: changefeedPublisher
});
morayBucketsInitializer = moraySetup.morayBucketsInitializer;
morayClient = moraySetup.morayClient;
moray = moraySetup.moray;
/*
* We don't set an 'error' event listener because we want the
* process to abort when there's a non-transient data migration
* error.
*/
dataMigrationsCtrl = new DataMigrationsController({
log: vmapiLog.child({
component: 'migrations-controller'
}, true),
migrations: dataMigrations,
moray: moray
});
/*
* We purposely start data migrations *only when all buckets are
* updated and reindexed*. Otherwise, if we we migrated records that
* have a value for a field for which a new index was just added,
* moray could discard that field when fetching the object using
* findObjects or getObject requests (See
* http://smartos.org/bugview/MORAY-104 and
* http://smartos.org/bugview/MORAY-428). We could thus migrate
* those records erroneously, and in the end write bogus data.
*/
morayBucketsInitializer.on('done',
function onMorayBucketsInitialized() {
dataMigrationsCtrl.start();
});
/*
* We don't want to wait for the Moray initialization process to be
* done before creating the HTTP server that will provide VMAPI's
* API endpoints, as:
*
* 1. some endpoints can function properly without using the Moray
* storage layer.
*
* 2. some endpoints are needed to provide status information,
* including status information about the storage layer.
*/
next();
},
function connectToWfApi(_, next) {
apiClients.wfapi.connect();
/*
* We intentionally don't need and want to wait for the Workflow API
* client to be connected before continuing the process of standing
* up VMAPI. Individual request handlers will handle the Workflow
* API client's connection status appropriately and differently.
*/
next();
},
function createMetricsCollector(_, next) {
metricsManager = createMetricsManager({
address: config.adminIp,
log: vmapiLog.child({component: 'metrics'}),
port: METRICS_SERVER_PORT,
restify: restify,
staticLabels: {
datacenter: config.datacenterName,
instance: config.instanceUuid,
server: config.serverUuid,
service: config.serviceName
}
});
metricsManager.createRestifyMetrics();
metricsManager.listen(function metricsServerStarted() {
next();
});
}
]}, function dependenciesInitDone(err) {
if (err) {
vmapiLog.error({
error: err
}, 'failed to initialize VMAPI\'s dependencies');
if (changefeedPublisher) {
changefeedPublisher.stop();
}
if (morayClient) {
morayClient.close();
}
process.exitCode = 1;
} else {
var vmapiOptions = jsprim.mergeObjects(config, {
apiClients: apiClients,
changefeedPublisher: changefeedPublisher,
dataMigrationsCtrl: dataMigrationsCtrl,
log: vmapiLog.child({ component: 'http-api' }, true),
metricsManager: metricsManager,
moray: moray,
morayBucketsInitializer: morayBucketsInitializer,
// TODO: These config items should use the same name(s).
userMigrationAllowed: config.user_migration_allowed,
zfs_send_mbps_limit: config.migration_send_mbps_limit,
serverConfig: {
bindPort: config.api.port
}
});
var vmapiApp = new VmapiApp(vmapiOptions);
vmapiApp.listen();
}
});
}
startVmapiService();