-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapi.js
More file actions
383 lines (333 loc) · 17 KB
/
api.js
File metadata and controls
383 lines (333 loc) · 17 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/**
* @license
* Copyright 2019-2020 CERN and copyright holders of ALICE O2.
* See http://alice-o2.web.cern.ch/copyright for details of the copyright holders.
* All rights not expressly granted are reserved.
*
* This software is distributed under the terms of the GNU General Public
* License v3 (GPL Version 3), copied verbatim in the file "COPYING".
*
* In applying this license CERN does not waive the privileges and immunities
* granted to it by virtue of its status as an Intergovernmental Organization
* or submit itself to any jurisdiction.
*/
const { EventEmitter } = require('events');
const { Kafka, logLevel } = require('kafkajs');
const logger = (require('@aliceo2/web-ui').LogManager)
.getLogger(`${process.env.npm_config_log_label ?? 'cog'}/api`);
const config = require('./config/configProvider.js');
const { DetectorId } = require('./common/detectorId.enum.js');
// middleware
const {addDetectorIdMiddleware} = require('./middleware/addDetectorId.middleware.js');
const {logDeploymentRequestMiddleware} = require('./middleware/logDeploymentRequest.middleware.js');
const {minimumRoleMiddleware} = require('./middleware/minimumRole.middleware.js');
const {requireDetectorOrGlobalRoleMiddleware} = require('./middleware/requireDetectorOrGlobalRole.middleware.js');
const {validateConsulServiceMiddlewareFactory} = require('./middleware/validateConsulServiceMiddlewareFactory.js');
const {
setDetectorsFromEnvironmentMiddlewareFactory
} = require('./middleware/setDetectorsFromEnvironmentMiddlewareFactory.js');
const {
getDetectorsLockOwnershipMiddlewareFactory
} = require('./middleware/getDetectorsLockOwnershipMiddlewareFactory.js');
// controllers
const {QCConfigurationController} = require('./controllers/QCConfiguration.controller.js');
const {ConsulController} = require('./controllers/Consul.controller.js');
const {DeploymentController} = require('./controllers/Deployment.controller.js');
const {EnvironmentController} = require('./controllers/Environment.controller.js');
const {LockController} = require('./controllers/Lock.controller.js');
const {RunController} = require('./controllers/Run.controller.js');
const {StatusController} = require('./controllers/Status.controller.js');
const {TaskController} = require('./controllers/Task.controller.js');
const {WebSocketService} = require('./services/WebSocket.service.js');
const {WorkflowTemplateController} = require('./controllers/WorkflowTemplate.controller.js');
// local services
const {BookkeepingService} = require('./services/Bookkeeping.service.js');
const {BroadcastService} = require('./services/Broadcast.service.js');
const {CacheService} = require('./services/Cache.service.js');
const {DeploymentService} = require('./services/Deployment.service.js');
const {DetectorService} = require('./services/Detector.service.js');
const {EnvironmentCacheService} = require('./services/environment/EnvironmentCache.service.js');
const {EnvironmentService} = require('./services/Environment.service.js');
const {Intervals} = require('./services/Intervals.service.js');
const {LockService} = require('./services/Lock.service.js');
const {RunService} = require('./services/Run.service.js');
const {StatusService} = require('./services/Status.service.js');
const {TaskService} = require('./services/Task.service.js');
const {WorkflowTemplateService} = require('./services/WorkflowTemplate.service.js');
const {QCConfigurationService} = require('./services/QCConfiguration.service.js');
// web-ui services
const {NotificationService, ConsulService} = require('@aliceo2/web-ui');
// AliECS Core
const { AliEcsSynchronizer } = require('./kafka/AliEcsSynchronizer.js');
const ApricotService = require('./control-core/ApricotService.js');
const ControlService = require('./control-core/ControlService.js');
const GrpcServiceClient = require('./control-core/GrpcServiceClient.js');
const path = require('path');
const O2_CONTROL_PROTO_PATH = path.join(__dirname, './../protobuf/o2control.proto');
const O2_APRICOT_PROTO_PATH = path.join(__dirname, './../protobuf/o2apricot.proto');
const {Role} = require('./common/role.enum.js');
if (!config.grpc) {
throw new Error('Control gRPC Configuration is missing');
}
if (!config.apricot) {
throw new Error('Apricot gRPC Configuration is missing');
}
if (!config.grafana) {
logger.error('Grafana Configuration is missing');
}
module.exports.setup = (http, ws) => {
const eventEmitter = new EventEmitter();
/**
* Services are initialized with the configuration they need and in order of their dependencies.
* The services are then used by the controllers to perform actions.
*/
let consulService;
if (config.consul) {
consulService = new ConsulService(config.consul);
}
const wsService = new WebSocketService(ws);
const broadcastService = new BroadcastService(ws);
const cacheService = new CacheService(broadcastService);
const environmentCacheService = new EnvironmentCacheService(broadcastService, eventEmitter);
const qcConfigurationService = new QCConfigurationService(consulService);
const qcConfigurationController = new QCConfigurationController(qcConfigurationService, config.consul);
const consulController = new ConsulController(consulService, config.consul);
const ctrlProxy = new GrpcServiceClient(config.grpc, O2_CONTROL_PROTO_PATH);
const ctrlService = new ControlService(ctrlProxy, consulController, config.grpc, O2_CONTROL_PROTO_PATH);
ctrlService.setWS(ws);
const apricotProxy = new GrpcServiceClient(config.apricot, O2_APRICOT_PROTO_PATH);
const apricotService = new ApricotService(apricotProxy);
const lockService = new LockService(broadcastService);
const lockController = new LockController(lockService);
const detectorService = new DetectorService(ctrlProxy);
const environmentService = new EnvironmentService(
ctrlProxy, apricotService, cacheService, broadcastService, environmentCacheService
);
const workflowService = new WorkflowTemplateService(ctrlProxy, apricotService);
const deploymentService = new DeploymentService(environmentService, workflowService, environmentCacheService);
const taskService = new TaskService(ctrlProxy);
/**
* Controllers are initialized with the services they depend on.
*/
const envCtrl = new EnvironmentController(environmentService, workflowService, lockService, detectorService);
const workflowController = new WorkflowTemplateController(workflowService);
const deploymentController = new DeploymentController(deploymentService, workflowService);
const taskController = new TaskController(taskService);
const bkpService = new BookkeepingService(config.bookkeeping ?? {});
const runService = new RunService(bkpService, apricotService, cacheService);
runService.retrieveStaticConfigurations();
const runController = new RunController(runService, cacheService);
const notificationService = new NotificationService();
if (notificationService.isConfigured()) {
notificationService.proxyWebNotificationToWs(ws);
}
let aliEcsSynchronizer = undefined;
if (config.kafka && config.kafka?.enable) {
try {
const kafkaClient = new Kafka({
clientId: 'control-gui',
brokers: config.kafka.brokers,
retry: { retries: Infinity },
logLevel: logLevel.NOTHING,
});
aliEcsSynchronizer = new AliEcsSynchronizer(kafkaClient, cacheService, eventEmitter);
aliEcsSynchronizer.start();
} catch (error) {
logger.errorMessage(`Kafka initialization failed: ${error.message}`);
}
}
const statusService = new StatusService(
config, ctrlService, consulService, apricotService, notificationService, wsService,
);
const statusController = new StatusController(statusService);
const intervals = new Intervals();
initializeData(apricotService, lockService, consulService);
initializeIntervals(intervals, statusService, runService, bkpService, environmentService);
const coreMiddleware = [
ctrlService.isConnectionReady.bind(ctrlService),
];
const setDetectorsFromEnvironmentMiddleware = setDetectorsFromEnvironmentMiddlewareFactory(environmentService);
const verifyLockOwnershipMiddleware = getDetectorsLockOwnershipMiddlewareFactory(lockService);
const validateConsulServiceMiddleware = validateConsulServiceMiddlewareFactory(consulService);
ctrlProxy.methods.forEach(
(method) => http.post(`/${method}`, coreMiddleware, (req, res) => ctrlService.executeCommand(req, res)),
);
http.get('/workflow/template/default/source', workflowController.getDefaultTemplateSource.bind(workflowController));
http.get('/workflow/template/mappings', workflowController.getWorkflowMapping.bind(workflowController));
http.get('/workflow/configuration', workflowController.getWorkflowConfiguration.bind(workflowController));
http.get('/runs/calibration/config', [
minimumRoleMiddleware(Role.GLOBAL),
], runController.refreshCalibrationRunsConfigurationHandler.bind(runController));
http.get('/runs/calibration', runController.getCalibrationRunsHandler.bind(runController));
http.get('/environments', coreMiddleware, envCtrl.getEnvironmentsHandler.bind(envCtrl), {public: true});
http.get('/environment/:id/:source?', coreMiddleware, envCtrl.getEnvironmentHandler.bind(envCtrl), {public: true});
http.post('/environment/auto', coreMiddleware, envCtrl.newAutoEnvironmentHandler.bind(envCtrl));
http.put('/environment/:id', coreMiddleware, envCtrl.transitionEnvironmentHandler.bind(envCtrl));
http.delete('/environment/:id',
coreMiddleware,
minimumRoleMiddleware(Role.DETECTOR),
setDetectorsFromEnvironmentMiddleware,
verifyLockOwnershipMiddleware,
envCtrl.destroyEnvironmentHandler.bind(envCtrl),
);
http.post('/deploy',
coreMiddleware,
logDeploymentRequestMiddleware,
minimumRoleMiddleware(Role.DETECTOR),
verifyLockOwnershipMiddleware,
deploymentController.newAsyncDeploymentHandler.bind(deploymentController)
);
http.delete('/deploy/:id',
minimumRoleMiddleware(Role.DETECTOR),
deploymentController.acknowledgeDeploymentFailureHandler.bind(deploymentController)
);
http.post('/core/environments/configuration/save', (req, res) => apricotService.saveCoreEnvConfig(req, res));
http.post('/core/environments/configuration/update', (req, res) => apricotService.updateCoreEnvConfig(req, res));
/**
* Tasks Routes
*/
http.get('/tasks/:id',
coreMiddleware,
minimumRoleMiddleware(Role.DETECTOR),
taskController.getTaskHandler.bind(taskController)
);
http.get('/tasks',
coreMiddleware,
minimumRoleMiddleware(Role.DETECTOR),
taskController.getTaskListHandler.bind(taskController));
http.delete('/tasks',
coreMiddleware,
minimumRoleMiddleware(Role.ADMIN),
verifyLockOwnershipMiddleware,
taskController.cleanUpTasksHandler.bind(taskController)
);
apricotProxy.methods.forEach(
(method) => http.post(`/${method}`, (req, res) => apricotService.executeCommand(req, res)),
);
http.get('/core/detectors', (req, res) => apricotService.getDetectorList(req, res));
http.get('/core/hostsByDetectors', (req, res) => apricotService.getHostsByDetectorList(req, res));
http.post('/execute/o2-roc-config', coreMiddleware, (req, res) => ctrlService.createAutoEnvironment(req, res));
// Lock Service
http.get('/locks',
minimumRoleMiddleware(Role.DETECTOR),
lockController.getLocksStateHandler.bind(lockController)
);
http.put(`/locks/:action/${DetectorId.ALL}`,
minimumRoleMiddleware(Role.GLOBAL),
addDetectorIdMiddleware(DetectorId.ALL),
lockController.actionLockHandler.bind(lockController)
);
http.put('/locks/:action/:detectorId',
minimumRoleMiddleware(Role.DETECTOR),
requireDetectorOrGlobalRoleMiddleware,
lockController.actionLockHandler.bind(lockController)
);
http.put(`/locks/force/:action/${DetectorId.ALL}`,
minimumRoleMiddleware(Role.ADMIN),
addDetectorIdMiddleware(DetectorId.ALL),
lockController.actionForceLockHandler.bind(lockController)
);
http.put('/locks/force/:action/:detectorId',
minimumRoleMiddleware(Role.GLOBAL),
lockController.actionForceLockHandler.bind(lockController)
);
// Status Service
http.get('/status/consul', statusController.getConsulStatus.bind(statusController));
http.get('/status/grafana', statusController.getGrafanaStatus.bind(statusController));
http.get('/status/notification', statusController.getNotificationSystemStatus.bind(statusController));
http.get('/status/gui', statusController.getGuiStatus.bind(statusController), {public: true});
http.get('/status/apricot', statusController.getApricotStatus.bind(statusController));
http.get('/status/core', coreMiddleware[0], statusController.getAliECSStatus.bind(statusController));
http.get('/status/system', statusController.getSystemCompatibility.bind(statusController));
http.get('/status/core/services', coreMiddleware[0],
statusController.getAliECSIntegratedServicesStatus.bind(statusController),
);
// Configuration
// this order of registering endpoints is necessary
http.get(
'/configurations/restrictions/:key(*)', validateConsulServiceMiddleware,
qcConfigurationController.getConfigurationRestrictionsByKeyHandler.bind(qcConfigurationController)
);
http.get(
'/configurations', validateConsulServiceMiddleware,
qcConfigurationController.getConfigurationsKeysHandler.bind(qcConfigurationController)
);
http.get(
'/configurations/:key(*)', validateConsulServiceMiddleware,
qcConfigurationController.getConfigurationByKeyHandler.bind(qcConfigurationController)
);
http.put(
'/configurations/:key(*)', validateConsulServiceMiddleware,
qcConfigurationController.putConfigurationByKeyHandler.bind(qcConfigurationController)
);
// Consul
http.get('/consul/flps', validateConsulServiceMiddleware, consulController.getFLPs.bind(consulController));
http.get('/consul/crus', validateConsulServiceMiddleware, consulController.getCRUs.bind(consulController));
http.get(
'/consul/crus/config', validateConsulServiceMiddleware,
consulController.getCRUsWithConfiguration.bind(consulController)
);
http.get(
'/consul/crus/aliases', validateConsulServiceMiddleware,
consulController.getCRUsAlias.bind(consulController)
);
http.post(
'/consul/crus/config/save', validateConsulServiceMiddleware,
consulController.saveCRUsConfiguration.bind(consulController)
);
};
/**
* Method to register services at the start of the server
* @param {Intervals} intervalsService - wrapper for storing intervals
* @param {StatusService} statusService - service used for retrieving status on dependent services
* @param {RunService} runService - service for retrieving and building information on runs
* @param {BookkeepingService} bkpService - service for retrieving information on runs from Bookkeeping
* @param {EnvironmentService} environmentService - service for retrieving information on environments
* @return {void}
*/
function initializeIntervals(intervalsService, statusService, runService, bkpService, environmentService) {
const SERVICES_REFRESH_RATE = 10000;
const ENVIRONMENT_REFRESH_RATE = 5000;
const CALIBRATION_RUNS_REFRESH_RATE = bkpService.refreshRate;
intervalsService.register(statusService.retrieveConsulStatus.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(statusService.retrieveAliEcsCoreInfo.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(statusService.retrieveApricotStatus.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(statusService.retrieveGrafanaStatus.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(statusService.retrieveSystemCompatibility.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(statusService.retrieveNotificationSystemStatus.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(statusService.retrieveAliECSIntegratedInfo.bind(statusService), SERVICES_REFRESH_RATE);
intervalsService.register(async () => {
try {
await environmentService.getEnvironments(false, true)
} catch (error) {
logger.errorMessage(`Error retrieving environments: ${error.message}`);
}
}, ENVIRONMENT_REFRESH_RATE);
if (config.bookkeeping) {
intervalsService.register(
runService.retrieveCalibrationRunsGroupedByDetector.bind(runService),
CALIBRATION_RUNS_REFRESH_RATE,
);
}
}
/**
* Function to initialize in order dependent services
* @param {ApricotService} apricotService - request initial set of data from AliECS/Apricot
* @param {LockService} lockService - initialize service with data from Apricot
* @param {ConsulService} consulService - service for communicating with Consul
*/
async function initializeData(apricotService, lockService, consulService) {
testConsulStatus(consulService);
await apricotService.init();
lockService.setLockStatesForDetectors(apricotService.detectors);
}
/**
* Method to check if consul service can be used
* @param {ConsulService} consulService
*/
function testConsulStatus(consulService) {
consulService
.getConsulLeaderStatus()
.then((data) => logger.info(`Service is up and running on: ${data}`))
.catch((error) => logger.error(`Connection failed due to ${error}`));
}