@kapeta/local-cluster-service 0.0.73 → 0.0.75

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapeta/local-cluster-service",
3
- "version": "0.0.73",
3
+ "version": "0.0.75",
4
4
  "description": "Manages configuration, ports and service discovery for locally running Kapeta systems",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -143,12 +143,12 @@ router.get('/consumes/resource/:resourceType/:portType/:name', async (req, res)
143
143
  * to handle clients for services that hasn't started yet.
144
144
  */
145
145
  router.get('/consumes/:resourceName/:type', (req, res) => {
146
-
147
146
  res.send(serviceManager.getConsumerAddress(
148
147
  req.kapeta.systemId,
149
148
  req.kapeta.instanceId,
150
149
  req.params.resourceName,
151
- req.params.type
150
+ req.params.type,
151
+ req.kapeta.environment,
152
152
  ));
153
153
  });
154
154
 
@@ -8,6 +8,7 @@ const socketManager = require('./socketManager');
8
8
  const serviceManager = require('./serviceManager');
9
9
  const assetManager = require('./assetManager');
10
10
  const containerManager = require('./containerManager');
11
+ const configManager = require("./configManager");
11
12
 
12
13
  const CHECK_INTERVAL = 10000;
13
14
  const DEFAULT_HEALTH_PORT_TYPE = 'rest';
@@ -314,6 +315,7 @@ class InstanceManager {
314
315
  const blockRef = blockInstance.block.ref;
315
316
 
316
317
  const blockAsset = await assetManager.getAsset(blockRef);
318
+ const instanceConfig = await configManager.getConfigForSection(planRef, instanceId);
317
319
 
318
320
  if (!blockAsset) {
319
321
  throw new Error('Block not found: ' + blockRef);
@@ -324,47 +326,85 @@ class InstanceManager {
324
326
  }
325
327
 
326
328
  await this.stopProcess(planRef, instanceId);
329
+ const type = blockAsset.version === 'local' ? 'local' : 'docker';
327
330
 
328
331
  const runner = new BlockInstanceRunner(planRef);
332
+
329
333
  const startTime = Date.now();
330
- const process = await runner.start(blockRef, instanceId);
331
- //emit stdout/stderr via sockets
332
- process.output.on("data", (data) => {
333
- const payload = {source: "stdout", level: "INFO", message: data.toString(), time: Date.now()};
334
- this._emit(instanceId, EVENT_INSTANCE_LOG, payload);
335
- });
334
+ try {
335
+ const process = await runner.start(blockRef, instanceId, instanceConfig);
336
+ //emit stdout/stderr via sockets
337
+ process.output.on("data", (data) => {
338
+ const payload = {
339
+ source: "stdout",
340
+ level: "INFO",
341
+ message: data.toString(),
342
+ time: Date.now()
343
+ };
344
+ this._emit(instanceId, EVENT_INSTANCE_LOG, payload);
345
+ });
336
346
 
337
- process.output.on('exit', (exitCode) => {
338
- const timeRunning = Date.now() - startTime;
339
- const instance = this.getInstance(planRef, instanceId);
340
- if (instance.status === STATUS_READY) {
341
- //It's already been running
342
- return;
343
- }
347
+ process.output.on('exit', (exitCode) => {
348
+ const timeRunning = Date.now() - startTime;
349
+ const instance = this.getInstance(planRef, instanceId);
350
+ if (instance.status === STATUS_READY) {
351
+ //It's already been running
352
+ return;
353
+ }
344
354
 
345
- if (exitCode === 143 ||
346
- exitCode === 137) {
347
- //Process got SIGTERM (143) or SIGKILL (137)
348
- //TODO: Windows?
349
- return;
350
- }
355
+ if (exitCode === 143 ||
356
+ exitCode === 137) {
357
+ //Process got SIGTERM (143) or SIGKILL (137)
358
+ //TODO: Windows?
359
+ return;
360
+ }
351
361
 
352
- if (exitCode !== 0 || timeRunning < MIN_TIME_RUNNING) {
353
- this._emit(blockInstance.id, EVENT_INSTANCE_EXITED, {
354
- error: "Failed to start instance",
355
- status: EVENT_INSTANCE_EXITED,
356
- instanceId: blockInstance.id
357
- })
358
- }
359
- });
362
+ if (exitCode !== 0 || timeRunning < MIN_TIME_RUNNING) {
363
+ this._emit(blockInstance.id, EVENT_INSTANCE_EXITED, {
364
+ error: "Failed to start instance",
365
+ status: EVENT_INSTANCE_EXITED,
366
+ instanceId: blockInstance.id
367
+ });
368
+ }
369
+ });
360
370
 
361
- await this.registerInstance(planRef, instanceId, {
362
- type: process.type,
363
- pid: process.pid,
364
- health: null
365
- });
371
+ await this.registerInstance(planRef, instanceId, {
372
+ type: process.type,
373
+ pid: process.pid,
374
+ health: null
375
+ });
376
+
377
+ return this._processes[planRef][instanceId] = process;
378
+ } catch (e) {
379
+ const logs = [
380
+ {
381
+ source: "stdout",
382
+ level: "ERROR",
383
+ message: e.message,
384
+ time: Date.now()
385
+ }
386
+ ]
387
+ await this.registerInstance(planRef, instanceId, {
388
+ type: 'local',
389
+ pid: null,
390
+ health: null
391
+ });
392
+
393
+ this._emit(instanceId, EVENT_INSTANCE_LOG, logs[0]);
394
+
395
+ this._emit(blockInstance.id, EVENT_INSTANCE_EXITED, {
396
+ error: `Failed to start instance: ${e.message}`,
397
+ status: EVENT_INSTANCE_EXITED,
398
+ instanceId: blockInstance.id
399
+ });
400
+
401
+ return this._processes[planRef][instanceId] = {
402
+ pid: -1,
403
+ type,
404
+ logs: () => logs
405
+ };
406
+ }
366
407
 
367
- return this._processes[planRef][instanceId] = process;
368
408
  }
369
409
 
370
410
  /**
@@ -4,11 +4,13 @@ module.exports = function kapeta(req, res, next) {
4
4
  let blockRef = req.headers['x-kapeta-block'];
5
5
  let systemId = req.headers['x-kapeta-system'];
6
6
  let instanceId = req.headers['x-kapeta-instance'];
7
+ let environment = req.headers['x-kapeta-environment'];
7
8
 
8
9
  req.kapeta = {
9
10
  blockRef,
10
11
  instanceId,
11
- systemId
12
+ systemId,
13
+ environment
12
14
  };
13
15
 
14
16
  next();
@@ -105,7 +105,7 @@ router.all('/:systemId/:consumerInstanceId/:consumerResourceName/:type/*', async
105
105
  );
106
106
 
107
107
  while(address.endsWith('/')) {
108
- address = address.substr(0, address.length - 1);
108
+ address = address.substring(0, address.length - 1);
109
109
  }
110
110
 
111
111
  typeHandler(req, res, {
@@ -21,14 +21,20 @@ class ServiceManager {
21
21
  });
22
22
  }
23
23
 
24
- _forLocal(port, path) {
24
+ _forLocal(port, path, environmentType) {
25
25
  if (!path) {
26
26
  path = '';
27
27
  }
28
- const host = clusterService.getClusterServiceHost();
28
+ let host;
29
+ if (environmentType === 'docker') {
30
+ //We're inside a docker container, so we can use this special host name to access the host machine
31
+ host = 'host.docker.internal';
32
+ } else {
33
+ host = clusterService.getClusterServiceHost();
34
+ }
29
35
 
30
36
  if (path.startsWith('/')) {
31
- path = path.substr(1);
37
+ path = path.substring(1);
32
38
  }
33
39
  return `http://${host}:${port}/${path}`;
34
40
  }
@@ -84,12 +90,13 @@ class ServiceManager {
84
90
  * @param {string} consumerInstanceId
85
91
  * @param {string} consumerResourceName
86
92
  * @param {string} portType
93
+ * @param {'docker'|'process'} environmentType
87
94
  * @return {string}
88
95
  */
89
- getConsumerAddress(systemId, consumerInstanceId, consumerResourceName, portType) {
96
+ getConsumerAddress(systemId, consumerInstanceId, consumerResourceName, portType, environmentType) {
90
97
  const port = clusterService.getClusterServicePort();
91
98
  const path = clusterService.getProxyPath(systemId, consumerInstanceId, consumerResourceName, portType);
92
- return this._forLocal(port, path);
99
+ return this._forLocal(port, path, environmentType);
93
100
  }
94
101
 
95
102
  /**