@kapeta/local-cluster-service 0.41.0 → 0.42.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.
@@ -5,8 +5,8 @@
5
5
 
6
6
  import FSExtra from 'fs-extra';
7
7
  import ClusterConfig, { DefinitionInfo } from '@kapeta/local-cluster-config';
8
- import { getDockerHostIp, getBlockInstanceContainerName, getOperatorInstancePorts, readYML, toPortInfo } from './utils';
9
- import { KapetaURI, parseKapetaUri, normalizeKapetaUri } from '@kapeta/nodejs-utils';
8
+ import { getBlockInstanceContainerName, getDockerHostIp, getOperatorInstancePorts, readYML } from './utils';
9
+ import { KapetaURI, normalizeKapetaUri, parseKapetaUri } from '@kapeta/nodejs-utils';
10
10
  import { DEFAULT_PORT_TYPE, HTTP_PORT_TYPE, HTTP_PORTS, serviceManager } from '../serviceManager';
11
11
  import {
12
12
  COMPOSE_LABEL_PROJECT,
@@ -32,7 +32,7 @@ import Docker from 'dockerode';
32
32
  import OS from 'node:os';
33
33
  import Path from 'node:path';
34
34
  import { taskManager } from '../taskManager';
35
- import { LocalDevContainer, LocalInstance } from '@kapeta/schemas';
35
+ import { EntityList, EntityType, LocalDevContainer, LocalInstance } from '@kapeta/schemas';
36
36
  import { createInternalConfigProvider, InternalConfigProvider } from './InternalConfigProvider';
37
37
  import {
38
38
  getConfigFilePath,
@@ -46,6 +46,8 @@ import {
46
46
  writeEnvConfigFile,
47
47
  } from '@kapeta/config-mapper';
48
48
  import crypto from 'crypto';
49
+ import { configManager } from '../configManager';
50
+ import _ from 'lodash';
49
51
 
50
52
  const KAPETA_SYSTEM_ID = 'KAPETA_SYSTEM_ID';
51
53
  const KAPETA_BLOCK_REF = 'KAPETA_BLOCK_REF';
@@ -121,11 +123,10 @@ export class BlockInstanceRunner {
121
123
  * Start a block
122
124
  *
123
125
  */
124
- async start(blockRef: string, instanceId: string, configuration: AnyMap): Promise<ProcessInfo> {
126
+ async start(blockRef: string, instanceId: string): Promise<ProcessInfo> {
125
127
  return this._execute({
126
128
  ref: blockRef,
127
129
  id: instanceId,
128
- configuration,
129
130
  });
130
131
  }
131
132
 
@@ -152,10 +153,18 @@ export class BlockInstanceRunner {
152
153
 
153
154
  const baseDir = ClusterConfig.getRepositoryAssetPath(blockUri.handle, blockUri.name, blockUri.version);
154
155
  const realBaseDir = await FSExtra.realpath(baseDir);
156
+
157
+ const config = await configManager.getConfigForBlockInstance(this._systemId, blockInstance.id);
158
+ const configSchema = (
159
+ assetVersion.definition.spec.configuration ? assetVersion.definition.spec.configuration : {}
160
+ ) as EntityList;
161
+ const fileMapping = configManager.rewriteFilePaths(configSchema, config);
162
+
155
163
  const internalConfigProvider = await createInternalConfigProvider(
156
164
  this._systemId,
157
165
  blockInstance.id,
158
- assetVersion
166
+ assetVersion,
167
+ config
159
168
  );
160
169
 
161
170
  // Resolve the environment variables
@@ -189,9 +198,9 @@ export class BlockInstanceRunner {
189
198
  const portTypes = getServiceProviderPorts(assetVersion, providerVersion);
190
199
 
191
200
  if (blockUri.version === 'local') {
192
- processInfo = await this._startLocalProcess(blockInstance, blockUri, env, assetVersion);
201
+ processInfo = await this._startLocalProcess(blockInstance, blockUri, env, assetVersion, fileMapping);
193
202
  } else {
194
- processInfo = await this._startDockerProcess(blockInstance, blockUri, env, assetVersion);
203
+ processInfo = await this._startDockerProcess(blockInstance, blockUri, env, assetVersion, fileMapping);
195
204
  }
196
205
 
197
206
  if (portTypes.length > 0) {
@@ -209,7 +218,8 @@ export class BlockInstanceRunner {
209
218
  blockInstance: BlockProcessParams,
210
219
  blockInfo: KapetaURI,
211
220
  env: StringMap,
212
- assetVersion: DefinitionInfo
221
+ assetVersion: DefinitionInfo,
222
+ fileMapping: StringMap
213
223
  ): Promise<ProcessInfo> {
214
224
  const baseDir = ClusterConfig.getRepositoryAssetPath(blockInfo.handle, blockInfo.name, blockInfo.version);
215
225
 
@@ -299,14 +309,7 @@ export class BlockInstanceRunner {
299
309
  HealthCheck = containerManager.toDockerHealth({ cmd: localContainer.healthcheck });
300
310
  }
301
311
 
302
- if (env[KAPETA_CONFIG_ENV_VAR]) {
303
- // If we have a config file, we need to bind it to the container and adjust the env var
304
- const localConfig = `/${KAPETA_ENV_CONFIG_FILE}`;
305
- Binds.push(`${toLocalBindVolume(env[KAPETA_CONFIG_ENV_VAR])}:${localConfig}:ro`);
306
- // We also provide the hash to detect changes to the config file
307
- env['KAPETA_CONFIG_HASH'] = await this.getFileHash(env[KAPETA_CONFIG_ENV_VAR]);
308
- env[KAPETA_CONFIG_ENV_VAR] = localConfig;
309
- }
312
+ await this.prepareBinds(env, Binds, fileMapping);
310
313
 
311
314
  const Mounts = isDockerImage
312
315
  ? // For docker images we mount the local directory to the working directory
@@ -366,11 +369,29 @@ export class BlockInstanceRunner {
366
369
  });
367
370
  }
368
371
 
372
+ private async prepareBinds(env: StringMap, Binds: string[], fileMapping: StringMap) {
373
+ if (env[KAPETA_CONFIG_ENV_VAR]) {
374
+ // If we have a config file, we need to bind it to the container and adjust the env var
375
+ const localConfig = `/${KAPETA_ENV_CONFIG_FILE}`;
376
+ Binds.push(`${toLocalBindVolume(env[KAPETA_CONFIG_ENV_VAR])}:${localConfig}:ro`);
377
+ // We also provide the hash to detect changes to the config file
378
+ env['KAPETA_CONFIG_HASH'] = await this.getFileHash(env[KAPETA_CONFIG_ENV_VAR]);
379
+ env[KAPETA_CONFIG_ENV_VAR] = localConfig;
380
+ }
381
+
382
+ if (fileMapping) {
383
+ Object.entries(fileMapping).forEach(([src, dst]) => {
384
+ Binds.push(`${toLocalBindVolume(src)}:${dst}:ro`);
385
+ });
386
+ }
387
+ }
388
+
369
389
  private async _startDockerProcess(
370
390
  blockInstance: BlockProcessParams,
371
391
  blockInfo: KapetaURI,
372
392
  env: StringMap,
373
- assetVersion: DefinitionInfo
393
+ assetVersion: DefinitionInfo,
394
+ fileMapping: StringMap
374
395
  ) {
375
396
  const { versionFile } = ClusterConfig.getRepositoryAssetInfoPath(
376
397
  blockInfo.handle,
@@ -414,14 +435,7 @@ export class BlockInstanceRunner {
414
435
  const systemUri = parseKapetaUri(this._systemId);
415
436
 
416
437
  const Binds = [`${toLocalBindVolume(ClusterConfig.getKapetaBasedir())}:${innerHome}`];
417
- if (env[KAPETA_CONFIG_ENV_VAR]) {
418
- // If we have a config file, we need to bind it to the container and adjust the env var
419
- const localConfig = `/${KAPETA_ENV_CONFIG_FILE}`;
420
- Binds.push(`${toLocalBindVolume(env[KAPETA_CONFIG_ENV_VAR])}:${localConfig}:ro`);
421
- // We also provide the hash to detect changes to the config file
422
- env['KAPETA_CONFIG_HASH'] = await this.getFileHash(env[KAPETA_CONFIG_ENV_VAR]);
423
- env[KAPETA_CONFIG_ENV_VAR] = localConfig;
424
- }
438
+ await this.prepareBinds(env, Binds, fileMapping);
425
439
 
426
440
  return this.ensureContainer({
427
441
  Image: dockerImage,
@@ -664,14 +678,20 @@ export class BlockInstanceRunner {
664
678
  const connectedBlockIds = [
665
679
  ...new Set(
666
680
  plan.spec.connections
667
- .filter((connection) => connection.provider.blockId == blockInstance.id || connection.consumer.blockId == blockInstance.id)
681
+ .filter(
682
+ (connection) =>
683
+ connection.provider.blockId == blockInstance.id ||
684
+ connection.consumer.blockId == blockInstance.id
685
+ )
668
686
  .flatMap((connection) => [connection.provider.blockId, connection.consumer.blockId])
669
687
  .filter((blockId) => blockId !== blockInstance.id)
670
688
  ),
671
689
  ];
672
690
 
673
691
  for (const connectedBlockId of connectedBlockIds) {
674
- const connectedBlockInstance = plan.spec.blocks.find((blockInstance) => blockInstance.id == connectedBlockId);
692
+ const connectedBlockInstance = plan.spec.blocks.find(
693
+ (blockInstance) => blockInstance.id == connectedBlockId
694
+ );
675
695
  if (!connectedBlockInstance) {
676
696
  continue;
677
697
  }
@@ -207,8 +207,8 @@ export class InternalConfigProvider implements ConfigProvider {
207
207
  export async function createInternalConfigProvider(
208
208
  systemId: string,
209
209
  instanceId: string,
210
- info: DefinitionInfo
210
+ info: DefinitionInfo,
211
+ config: AnyMap
211
212
  ): Promise<InternalConfigProvider> {
212
- const config = await configManager.getConfigForBlockInstance(systemId, instanceId);
213
213
  return new InternalConfigProvider(systemId, instanceId, info, config);
214
214
  }