@kapeta/local-cluster-service 0.10.1 → 0.11.0

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.
Files changed (38) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cjs/src/containerManager.d.ts +6 -4
  3. package/dist/cjs/src/containerManager.js +100 -45
  4. package/dist/cjs/src/definitionsManager.d.ts +1 -0
  5. package/dist/cjs/src/definitionsManager.js +7 -0
  6. package/dist/cjs/src/instanceManager.d.ts +2 -1
  7. package/dist/cjs/src/instanceManager.js +29 -46
  8. package/dist/cjs/src/instances/routes.js +10 -4
  9. package/dist/cjs/src/operatorManager.js +8 -6
  10. package/dist/cjs/src/repositoryManager.js +4 -4
  11. package/dist/cjs/src/types.d.ts +0 -9
  12. package/dist/cjs/src/utils/BlockInstanceRunner.js +9 -64
  13. package/dist/cjs/src/utils/utils.d.ts +1 -1
  14. package/dist/cjs/src/utils/utils.js +3 -2
  15. package/dist/esm/src/containerManager.d.ts +6 -4
  16. package/dist/esm/src/containerManager.js +100 -45
  17. package/dist/esm/src/definitionsManager.d.ts +1 -0
  18. package/dist/esm/src/definitionsManager.js +7 -0
  19. package/dist/esm/src/instanceManager.d.ts +2 -1
  20. package/dist/esm/src/instanceManager.js +29 -46
  21. package/dist/esm/src/instances/routes.js +10 -4
  22. package/dist/esm/src/operatorManager.js +8 -6
  23. package/dist/esm/src/repositoryManager.js +4 -4
  24. package/dist/esm/src/types.d.ts +0 -9
  25. package/dist/esm/src/utils/BlockInstanceRunner.js +9 -64
  26. package/dist/esm/src/utils/utils.d.ts +1 -1
  27. package/dist/esm/src/utils/utils.js +3 -2
  28. package/package.json +1 -1
  29. package/src/containerManager.ts +126 -49
  30. package/src/definitionsManager.ts +8 -0
  31. package/src/instanceManager.ts +35 -50
  32. package/src/instances/routes.ts +9 -4
  33. package/src/operatorManager.ts +9 -8
  34. package/src/repositoryManager.ts +5 -5
  35. package/src/types.ts +0 -7
  36. package/src/utils/BlockInstanceRunner.ts +10 -66
  37. package/src/utils/LogData.ts +1 -0
  38. package/src/utils/utils.ts +3 -2
@@ -2,7 +2,7 @@ import FS from 'node:fs';
2
2
  import os from 'node:os';
3
3
  import Path from 'node:path';
4
4
  import watch from 'recursive-watch';
5
- import FSExtra, { FSWatcher } from 'fs-extra';
5
+ import FSExtra from 'fs-extra';
6
6
  import ClusterConfiguration from '@kapeta/local-cluster-config';
7
7
  import { parseKapetaUri } from '@kapeta/nodejs-utils';
8
8
  import { socketManager } from './socketManager';
@@ -119,9 +119,11 @@ class RepositoryManager {
119
119
  this._installQueue.push(async () => {
120
120
  try {
121
121
  const normalizedRefs = refs.map((ref) => parseKapetaUri(ref).id);
122
- const filteredRefs = normalizedRefs.filter((ref) => !INSTALL_ATTEMPTED[ref]);
123
- console.log(filteredRefs);
122
+ const filteredRefs = normalizedRefs
123
+ .filter((ref) => !INSTALL_ATTEMPTED[ref])
124
+ .filter((ref) => !definitionsManager.exists(ref));
124
125
  if (filteredRefs.length > 0) {
126
+ console.log(`Auto-installing dependencies: ${filteredRefs.join(', ')}`);
125
127
  filteredRefs.forEach((ref) => (INSTALL_ATTEMPTED[ref] = true));
126
128
  //Auto-install missing asset
127
129
  try {
@@ -218,13 +220,11 @@ class RepositoryManager {
218
220
 
219
221
  this._cache[ref] = true;
220
222
  if (!installedAsset) {
221
- console.log(`Auto-installing missing asset: ${ref}`);
222
223
  await this._install([ref]);
223
224
  } else {
224
225
  //Ensure dependencies are installed
225
226
  const refs = assetVersion.dependencies.map((dep: Dependency) => dep.name);
226
227
  if (refs.length > 0) {
227
- console.log(`Auto-installing dependencies: ${refs.join(', ')}`);
228
228
  await this._install(refs);
229
229
  }
230
230
  }
package/src/types.ts CHANGED
@@ -53,10 +53,7 @@ export enum DesiredInstanceStatus {
53
53
  export type ProcessInfo = {
54
54
  type: InstanceType;
55
55
  pid?: number | string | null;
56
- output: EventEmitter;
57
56
  portType?: string;
58
- logs: () => LogEntry[];
59
- stop: () => Promise<void> | void;
60
57
  };
61
58
 
62
59
  export type InstanceInfo = {
@@ -75,10 +72,6 @@ export type InstanceInfo = {
75
72
  pid?: number | string | null;
76
73
  portType?: string;
77
74
 
78
- internal?: {
79
- output: EventEmitter;
80
- logs: () => LogEntry[];
81
- };
82
75
  };
83
76
 
84
77
  interface ResourceRef {
@@ -169,7 +169,7 @@ export class BlockInstanceRunner {
169
169
  throw new Error(`Missing docker image information: ${JSON.stringify(localContainer)}`);
170
170
  }
171
171
 
172
- const containerName = getBlockInstanceContainerName(blockInstance.id);
172
+ const containerName = getBlockInstanceContainerName(this._systemId, blockInstance.id);
173
173
  const startCmd = localContainer.handlers?.onCreate ? localContainer.handlers.onCreate : '';
174
174
  const dockerOpts = localContainer.options ?? {};
175
175
  const homeDir = localContainer.userHome ? localContainer.userHome : '/root';
@@ -243,7 +243,7 @@ export class BlockInstanceRunner {
243
243
  addonEnv
244
244
  } = await this.getDockerPortBindings(blockInstance, assetVersion);
245
245
 
246
- const containerName = getBlockInstanceContainerName(blockInstance.id);
246
+ const containerName = getBlockInstanceContainerName(this._systemId, blockInstance.id);
247
247
 
248
248
  // For windows we need to default to root
249
249
  const innerHome = process.platform === 'win32' ? '/root/.kapeta' : ClusterConfig.getKapetaBasedir();
@@ -306,7 +306,8 @@ export class BlockInstanceRunner {
306
306
 
307
307
  const dockerImage = spec?.local?.image;
308
308
 
309
- const containerName = getBlockInstanceContainerName(blockInstance.id);
309
+ //We only want 1 operator per operator type - across all local systems
310
+ const containerName = getBlockInstanceContainerName(this._systemId, blockInstance.id);
310
311
  const logs = new LogData();
311
312
 
312
313
  const bindHost = getBindHost();
@@ -340,7 +341,7 @@ export class BlockInstanceRunner {
340
341
  }
341
342
 
342
343
  if (spec.local?.mounts) {
343
- const mounts = containerManager.createMounts(blockUri.id, spec.local.mounts);
344
+ const mounts = await containerManager.createMounts(this._systemId, blockUri.id, spec.local.mounts);
344
345
  Mounts = containerManager.toDockerMounts(mounts);
345
346
  }
346
347
 
@@ -418,78 +419,21 @@ export class BlockInstanceRunner {
418
419
  }
419
420
 
420
421
  private async ensureContainer(opts: any) {
421
- const logs = new LogData();
422
-
423
422
  const container = await containerManager.ensureContainer(opts);
424
423
 
425
- try {
426
- if (opts.HealthCheck) {
427
- await containerManager.waitForHealthy(container);
428
- } else {
429
- await containerManager.waitForReady(container);
430
- }
431
- } catch (e: any) {
432
- logs.addLog(e.message, 'ERROR');
433
- }
424
+ await containerManager.waitForReady(container);
425
+
426
+ return this._handleContainer(container);
434
427
 
435
- return this._handleContainer(container, logs);
436
428
  }
437
429
 
438
430
  private async _handleContainer(
439
- container: Container,
440
- logs: LogData,
441
- deleteOnExit: boolean = false
431
+ container: Container
442
432
  ): Promise<ProcessInfo> {
443
- let localContainer: Container | null = container;
444
- const logStream = (await container.logs({
445
- follow: true,
446
- stdout: true,
447
- stderr: true,
448
- tail: LogData.MAX_LINES,
449
- })) as EventEmitter;
450
-
451
- const outputEvents = new EventEmitter();
452
- logStream.on('data', (data) => {
453
- logs.addLog(data.toString());
454
- outputEvents.emit('data', data);
455
- });
456
-
457
- logStream.on('error', (data) => {
458
- logs.addLog(data.toString());
459
- outputEvents.emit('data', data);
460
- });
461
-
462
- logStream.on('close', async () => {
463
- const status = await container.status();
464
- const data = status.data as any;
465
- if (deleteOnExit) {
466
- try {
467
- await containerManager.remove(container);
468
- } catch (e: any) {}
469
- }
470
- outputEvents.emit('exit', data?.State?.ExitCode ?? 0);
471
- });
472
433
 
473
434
  return {
474
435
  type: InstanceType.DOCKER,
475
- pid: container.id,
476
- output: outputEvents,
477
- stop: async () => {
478
- if (!localContainer) {
479
- return;
480
- }
481
-
482
- try {
483
- await localContainer.stop();
484
- if (deleteOnExit) {
485
- await containerManager.remove(localContainer);
486
- }
487
- } catch (e) {}
488
- localContainer = null;
489
- },
490
- logs: () => {
491
- return logs.getLogs();
492
- },
436
+ pid: container.id
493
437
  };
494
438
  }
495
439
  }
@@ -20,6 +20,7 @@ export class LogData {
20
20
  if (!msg.endsWith('\n')) {
21
21
  msg += '\n';
22
22
  }
23
+
23
24
  this.entries.push({
24
25
  time: Date.now(),
25
26
  message: msg,
@@ -1,9 +1,10 @@
1
1
  import FS from 'node:fs';
2
2
  import YAML from 'yaml';
3
3
  import { parseKapetaUri } from '@kapeta/nodejs-utils';
4
+ import md5 from "md5";
4
5
 
5
- export function getBlockInstanceContainerName(instanceId: string) {
6
- return `kapeta-block-instance-${instanceId}`;
6
+ export function getBlockInstanceContainerName(systemId:string, instanceId: string) {
7
+ return `kapeta-block-instance-${md5(systemId + instanceId)}`;
7
8
  }
8
9
 
9
10
  export function normalizeKapetaUri(uri: string) {