@platforma-sdk/package-builder-lib 1.1.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 (134) hide show
  1. package/assets/conda-dockerfile.template +45 -0
  2. package/assets/python-dockerfile.template +22 -0
  3. package/dist/_virtual/_rolldown/runtime.cjs +33 -0
  4. package/dist/_virtual/_rolldown/runtime.js +13 -0
  5. package/dist/core/archive.cjs +52 -0
  6. package/dist/core/archive.cjs.map +1 -0
  7. package/dist/core/archive.js +47 -0
  8. package/dist/core/archive.js.map +1 -0
  9. package/dist/core/conda/builder.cjs +226 -0
  10. package/dist/core/conda/builder.cjs.map +1 -0
  11. package/dist/core/conda/builder.js +220 -0
  12. package/dist/core/conda/builder.js.map +1 -0
  13. package/dist/core/conda/validate-spec.cjs +76 -0
  14. package/dist/core/conda/validate-spec.cjs.map +1 -0
  15. package/dist/core/conda/validate-spec.js +73 -0
  16. package/dist/core/conda/validate-spec.js.map +1 -0
  17. package/dist/core/core.cjs +403 -0
  18. package/dist/core/core.cjs.map +1 -0
  19. package/dist/core/core.d.ts +82 -0
  20. package/dist/core/core.d.ts.map +1 -0
  21. package/dist/core/core.js +397 -0
  22. package/dist/core/core.js.map +1 -0
  23. package/dist/core/docker-conda.cjs +75 -0
  24. package/dist/core/docker-conda.cjs.map +1 -0
  25. package/dist/core/docker-conda.js +71 -0
  26. package/dist/core/docker-conda.js.map +1 -0
  27. package/dist/core/docker-python.cjs +147 -0
  28. package/dist/core/docker-python.cjs.map +1 -0
  29. package/dist/core/docker-python.js +144 -0
  30. package/dist/core/docker-python.js.map +1 -0
  31. package/dist/core/docker.cjs +223 -0
  32. package/dist/core/docker.cjs.map +1 -0
  33. package/dist/core/docker.js +207 -0
  34. package/dist/core/docker.js.map +1 -0
  35. package/dist/core/envs.cjs +58 -0
  36. package/dist/core/envs.cjs.map +1 -0
  37. package/dist/core/envs.d.ts +26 -0
  38. package/dist/core/envs.d.ts.map +1 -0
  39. package/dist/core/envs.js +51 -0
  40. package/dist/core/envs.js.map +1 -0
  41. package/dist/core/package-info.cjs +450 -0
  42. package/dist/core/package-info.cjs.map +1 -0
  43. package/dist/core/package-info.d.ts +57 -0
  44. package/dist/core/package-info.d.ts.map +1 -0
  45. package/dist/core/package-info.js +447 -0
  46. package/dist/core/package-info.js.map +1 -0
  47. package/dist/core/paths.cjs +15 -0
  48. package/dist/core/paths.cjs.map +1 -0
  49. package/dist/core/paths.js +14 -0
  50. package/dist/core/paths.js.map +1 -0
  51. package/dist/core/resolver.cjs +99 -0
  52. package/dist/core/resolver.cjs.map +1 -0
  53. package/dist/core/resolver.js +95 -0
  54. package/dist/core/resolver.js.map +1 -0
  55. package/dist/core/schemas/artifacts.cjs +136 -0
  56. package/dist/core/schemas/artifacts.cjs.map +1 -0
  57. package/dist/core/schemas/artifacts.d.ts +280 -0
  58. package/dist/core/schemas/artifacts.d.ts.map +1 -0
  59. package/dist/core/schemas/artifacts.js +122 -0
  60. package/dist/core/schemas/artifacts.js.map +1 -0
  61. package/dist/core/schemas/entrypoints.cjs +43 -0
  62. package/dist/core/schemas/entrypoints.cjs.map +1 -0
  63. package/dist/core/schemas/entrypoints.d.ts +32 -0
  64. package/dist/core/schemas/entrypoints.d.ts.map +1 -0
  65. package/dist/core/schemas/entrypoints.js +41 -0
  66. package/dist/core/schemas/entrypoints.js.map +1 -0
  67. package/dist/core/schemas/sw-json.cjs +111 -0
  68. package/dist/core/schemas/sw-json.cjs.map +1 -0
  69. package/dist/core/schemas/sw-json.js +110 -0
  70. package/dist/core/schemas/sw-json.js.map +1 -0
  71. package/dist/core/storage.cjs +86 -0
  72. package/dist/core/storage.cjs.map +1 -0
  73. package/dist/core/storage.js +83 -0
  74. package/dist/core/storage.js.map +1 -0
  75. package/dist/core/sw-json-render.cjs +327 -0
  76. package/dist/core/sw-json-render.cjs.map +1 -0
  77. package/dist/core/sw-json-render.js +322 -0
  78. package/dist/core/sw-json-render.js.map +1 -0
  79. package/dist/core/util.cjs +313 -0
  80. package/dist/core/util.cjs.map +1 -0
  81. package/dist/core/util.d.ts +66 -0
  82. package/dist/core/util.d.ts.map +1 -0
  83. package/dist/core/util.js +281 -0
  84. package/dist/core/util.js.map +1 -0
  85. package/dist/defaults.cjs +54 -0
  86. package/dist/defaults.cjs.map +1 -0
  87. package/dist/defaults.d.ts +21 -0
  88. package/dist/defaults.d.ts.map +1 -0
  89. package/dist/defaults.js +37 -0
  90. package/dist/defaults.js.map +1 -0
  91. package/dist/index.cjs +31 -0
  92. package/dist/index.cjs.map +1 -0
  93. package/dist/index.d.ts +20 -0
  94. package/dist/index.d.ts.map +1 -0
  95. package/dist/index.js +12 -0
  96. package/dist/index.js.map +1 -0
  97. package/package.json +56 -0
  98. package/src/__test__/Dockerfile +1 -0
  99. package/src/__test__/docker.test.ts +67 -0
  100. package/src/__test__/package.json +39 -0
  101. package/src/__test__/python-example/main.py +1 -0
  102. package/src/__test__/python-example/requirements.txt +0 -0
  103. package/src/__test__/vite.config.mts +7 -0
  104. package/src/core/__tests__/docker-conda.test.ts +145 -0
  105. package/src/core/__tests__/docker-entrypoint-generation.test.ts +119 -0
  106. package/src/core/__tests__/docker-python.test.ts +374 -0
  107. package/src/core/__tests__/publish-docker-images.test.ts +309 -0
  108. package/src/core/archive.ts +102 -0
  109. package/src/core/conda/builder.test.ts +71 -0
  110. package/src/core/conda/builder.ts +384 -0
  111. package/src/core/conda/validate-spec.test.ts +213 -0
  112. package/src/core/conda/validate-spec.ts +107 -0
  113. package/src/core/core.ts +835 -0
  114. package/src/core/docker-conda.ts +149 -0
  115. package/src/core/docker-python.ts +283 -0
  116. package/src/core/docker.ts +300 -0
  117. package/src/core/envs.ts +31 -0
  118. package/src/core/package-info.test.ts +82 -0
  119. package/src/core/package-info.ts +843 -0
  120. package/src/core/paths.ts +16 -0
  121. package/src/core/resolver.ts +195 -0
  122. package/src/core/schemas/artifacts.test.ts +72 -0
  123. package/src/core/schemas/artifacts.ts +279 -0
  124. package/src/core/schemas/entrypoints.test.ts +171 -0
  125. package/src/core/schemas/entrypoints.ts +150 -0
  126. package/src/core/schemas/sw-json.ts +216 -0
  127. package/src/core/schemas/test-artifacts.ts +362 -0
  128. package/src/core/storage.ts +125 -0
  129. package/src/core/sw-json-render.test.ts +215 -0
  130. package/src/core/sw-json-render.ts +682 -0
  131. package/src/core/util.ts +390 -0
  132. package/src/defaults.ts +38 -0
  133. package/src/index.ts +31 -0
  134. package/src/requirements.txt +1 -0
@@ -0,0 +1,843 @@
1
+ import * as path from "node:path";
2
+ import * as fs from "node:fs";
3
+ import type winston from "winston";
4
+ import { z } from "zod/v4";
5
+
6
+ import * as defaults from "../defaults";
7
+ import * as util from "./util";
8
+ import * as envs from "./envs";
9
+ import * as artifacts from "./schemas/artifacts";
10
+ import * as entrypoints from "./schemas/entrypoints";
11
+ import { tryResolve } from "@milaboratories/resolve-helper";
12
+ import * as docker from "./docker";
13
+ import { prepareDockerOptions as pythonDockerOptions } from "./docker-python";
14
+ import { prepareDockerOptions as condaDockerOptions } from "./docker-conda";
15
+
16
+ const storagePresetSchema = z.object({
17
+ downloadURL: z.string().optional(),
18
+ storageURL: z.string().optional(),
19
+ });
20
+ type storagePreset = z.infer<typeof storagePresetSchema>;
21
+
22
+ const binaryRegistryPresetsSchema = z.record(z.string(), storagePresetSchema);
23
+ type binaryRegistryPresets = z.infer<typeof binaryRegistryPresetsSchema>;
24
+
25
+ const packageJsonSchema = z.object({
26
+ name: z.string(),
27
+ version: z.string(),
28
+ private: z.boolean().optional(),
29
+
30
+ "block-software": z.object({
31
+ registries: z
32
+ .object({
33
+ binary: binaryRegistryPresetsSchema.optional(),
34
+ })
35
+ .optional(),
36
+
37
+ artifacts: artifacts.artifactIndexSchema.optional(),
38
+ entrypoints: entrypoints.entrypointListSchema,
39
+ }),
40
+ });
41
+ type packageJson = z.infer<typeof packageJsonSchema>;
42
+
43
+ const wellKnownRegistries: Record<string, storagePreset> = {
44
+ [defaults.BIN_REGISTRY_NAME]: {
45
+ downloadURL: defaults.BIN_REGISTRY_DOWNLOAD_URL,
46
+ },
47
+ };
48
+
49
+ /*
50
+ * package.json -> block-software structure example:
51
+ * {
52
+ * "block-software": {
53
+ * "registries": {
54
+ * "binary": {
55
+ * "default": { "uploadURL": "s3://<bucket>/<some-prefix>?region=<region-name>" }
56
+ * }
57
+ * },
58
+ *
59
+ * "artifacts": {
60
+ * "pkg-1": {
61
+ * "type": "binary",
62
+ * "roots": {
63
+ * "linux-x64": "./linux-x64/src",
64
+ * "linux-aarch64": "./linux-aarch/src",
65
+ * "...and so on...": "platform-dependant roots",
66
+ * }
67
+ * }
68
+ * }
69
+ *
70
+ * "entrypoints": {
71
+ * "script1": {
72
+ * "software": {
73
+ * "artifact": "pkg-1",
74
+ * "cmd": [ "{pkg}/script1" ]
75
+ * }
76
+ * }
77
+ * }
78
+ * }
79
+ * }
80
+ */
81
+ export class PackageInfo {
82
+ public readonly packageRoot: string;
83
+ public readonly packageName: string;
84
+
85
+ private readonly pkgJson: packageJson;
86
+ private _versionOverride: string | undefined;
87
+
88
+ constructor(
89
+ private logger: winston.Logger,
90
+ options?: {
91
+ packageRoot?: string;
92
+ pkgJsonData?: string;
93
+ },
94
+ ) {
95
+ this.logger.debug("Reading package information...");
96
+
97
+ this.packageRoot = options?.packageRoot ?? util.findPackageRoot(logger);
98
+
99
+ let pkgJsonData: string;
100
+ if (options?.pkgJsonData) {
101
+ pkgJsonData = options.pkgJsonData;
102
+ } else {
103
+ const pkgJsonPath = path.resolve(this.packageRoot, util.packageJsonName);
104
+
105
+ this.logger.debug(` - loading '${pkgJsonPath}'`);
106
+ if (!fs.existsSync(pkgJsonPath)) {
107
+ this.logger.error(`no '${util.packageJsonName}' file found at '${this.packageRoot}'`);
108
+ throw util.CLIError("not a platform software package directory");
109
+ }
110
+
111
+ pkgJsonData = fs.readFileSync(pkgJsonPath, "utf8");
112
+ }
113
+
114
+ const result = parsePackageJson(pkgJsonData);
115
+ if (!result.success) {
116
+ throw util.CLIError(`\n` + util.formatZodIssues(result.error.issues));
117
+ }
118
+ this.pkgJson = result.data;
119
+ this.logger.debug(" " + JSON.stringify(this.pkgJson));
120
+
121
+ this.validateConfig();
122
+
123
+ this.packageName = this.pkgJson.name;
124
+
125
+ logger.debug(" package information loaded successfully.");
126
+ }
127
+
128
+ get isPrivate(): boolean {
129
+ return this.pkgJson.private ?? false;
130
+ }
131
+
132
+ get binaryRegistries(): binaryRegistryPresets {
133
+ return this.pkgJson["block-software"].registries?.binary ?? {};
134
+ }
135
+
136
+ get entrypoints(): Map<string, entrypoints.Entrypoint> {
137
+ const list = new Map<string, entrypoints.Entrypoint>();
138
+
139
+ for (const [epName, ep] of Object.entries(this.pkgJson["block-software"].entrypoints)) {
140
+ if (ep.docker) {
141
+ const artifactID = typeof ep.docker.artifact === "string" ? ep.docker.artifact : epName;
142
+ const artifact = this.getArtifact(artifactID, "docker");
143
+
144
+ // will mix docker to separate entrypoint
145
+ // render function have to merge
146
+ list.set(docker.entrypointName(epName), {
147
+ type: "software",
148
+ name: epName,
149
+ artifact: artifact,
150
+ cmd: ep.docker.cmd ?? [],
151
+ env: ep.docker.envVars ?? [],
152
+ });
153
+ }
154
+
155
+ if (ep.reference) {
156
+ list.set(epName, {
157
+ type: "reference",
158
+ name: epName,
159
+ reference: ep.reference,
160
+ });
161
+ continue;
162
+ }
163
+
164
+ if (ep.binary || ep.conda) {
165
+ const swOptions: entrypoints.softwareOptionsType = ep.binary ? ep.binary : ep.conda!;
166
+ const artifactOrRef = ep.binary ? ep.binary.artifact : ep.conda!.artifact;
167
+ const artifactID = typeof artifactOrRef === "string" ? artifactOrRef : epName;
168
+ const artifact = this.getArtifact(artifactID, "any");
169
+ if (artifact.type === "asset") {
170
+ throw util.CLIError(`binary entrypoint cannot point to asset artifact: ${epName}`);
171
+ }
172
+ if (artifact.type === "environment") {
173
+ throw util.CLIError(`binary entrypoint cannot point to environment artifact: ${epName}`);
174
+ }
175
+
176
+ list.set(epName, {
177
+ type: "software",
178
+ name: epName,
179
+ artifact: artifact,
180
+ cmd: swOptions.cmd,
181
+ env: swOptions.envVars ?? [],
182
+ });
183
+
184
+ if (!ep.docker) {
185
+ if (artifacts.dockerAutogenTypes.includes(artifact.type)) {
186
+ switch (artifact.type) {
187
+ case "conda":
188
+ case "python":
189
+ list.set(docker.entrypointName(epName), {
190
+ type: "software",
191
+ name: epName,
192
+ artifact: this.generateDockerPackage(artifact),
193
+ cmd: swOptions.cmd,
194
+ env: swOptions.envVars ?? [],
195
+ });
196
+ continue;
197
+ default: {
198
+ throw new Error(`docker autogeneration is not defined for type: ${artifact.type}`);
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }
204
+
205
+ if (ep.environment) {
206
+ const packageID =
207
+ typeof ep.environment.artifact === "string" ? ep.environment.artifact : epName;
208
+ list.set(epName, {
209
+ type: "environment",
210
+ name: epName,
211
+ artifact: this.getArtifact(packageID, "environment"),
212
+ env: ep.environment.envVars ?? [],
213
+ });
214
+ continue;
215
+ }
216
+
217
+ if (ep.asset) {
218
+ const packageID = typeof ep.asset === "string" ? ep.asset : epName;
219
+ list.set(epName, {
220
+ type: "asset",
221
+ name: epName,
222
+ artifact: this.getArtifact(packageID, "asset"),
223
+ });
224
+ continue;
225
+ }
226
+
227
+ if (list.size === 0) {
228
+ throw util.CLIError(
229
+ `entrypoint '${epName}' type is not supported by current platforma package builder`,
230
+ );
231
+ }
232
+ }
233
+
234
+ return list;
235
+ }
236
+
237
+ // Get not docker entrypoint if exists.
238
+ // If only docker entrypoint exists, return it.
239
+ public getMainEntrypoint(name: string): entrypoints.Entrypoint {
240
+ const ep = this.entrypoints.get(name);
241
+ if (ep) {
242
+ return ep;
243
+ }
244
+
245
+ return this.entrypoints.get(docker.entrypointName(name))!;
246
+ }
247
+
248
+ /**
249
+ * Resolves entrypoint reference to full entrypoint file path and type
250
+ */
251
+ public resolveReference(epName: string, ep: entrypoints.ReferenceEntrypoint): string {
252
+ this.logger.debug(
253
+ `resolving entrypoint '${epName}' reference '${ep.reference}'. packageRoot='${this.packageRoot}'`,
254
+ );
255
+
256
+ const refInfo = ep.reference.match(entrypoints.EnyrypointReferencePattern)?.groups;
257
+
258
+ if (!refInfo) {
259
+ this.logger.error(
260
+ `entrypoint reference '${epName}' has incorrect reference format. <full package name>/<path to entrypoint> is expected`,
261
+ );
262
+ throw util.CLIError(`invalid entrypoint '${epName}' reference format`);
263
+ }
264
+
265
+ const refPath = tryResolve(this.packageRoot, ep.reference);
266
+ if (!refPath) {
267
+ this.logger.error(`entrypoint reference '${epName}' cannot be resolved into file path`);
268
+ throw util.CLIError(`invalid entrypoint '${epName}' reference`);
269
+ }
270
+
271
+ return refPath;
272
+ }
273
+
274
+ // Packages are buildable artifacts with entrypoints
275
+ get packages(): Map<string, artifacts.withId<artifacts.anyArtifactType>> {
276
+ const result = new Map<string, artifacts.withId<artifacts.anyArtifactType>>();
277
+
278
+ for (const ep of this.entrypoints.values()) {
279
+ if (ep.type === "reference") {
280
+ // Entrypoint references are not buildable
281
+ continue;
282
+ }
283
+
284
+ if (!result.has(ep.artifact.id)) {
285
+ result.set(ep.artifact.id, ep.artifact);
286
+ }
287
+ }
288
+
289
+ return result;
290
+ }
291
+
292
+ public artifactInfoLocation(
293
+ pkgID: string,
294
+ artifactType: "docker",
295
+ platform: util.ArchType,
296
+ ): string;
297
+ public artifactInfoLocation(
298
+ pkgID: string,
299
+ artifactType: "archive",
300
+ platform?: util.PlatformType,
301
+ ): string;
302
+ public artifactInfoLocation(
303
+ pkgID: string,
304
+ artifactType: "archive" | "docker",
305
+ platform?: util.PlatformType | util.ArchType,
306
+ ): string {
307
+ const platformPart = platform ? `_${platform}` : "";
308
+ return path.resolve(
309
+ this.packageRoot,
310
+ "dist",
311
+ "artifacts",
312
+ pkgID,
313
+ `${artifactType}${platformPart}.json`,
314
+ );
315
+ }
316
+
317
+ private generateDockerPackage(
318
+ artifact: artifacts.withId<artifacts.pythonType | artifacts.condaType>,
319
+ ): artifacts.withId<artifacts.dockerType> {
320
+ switch (artifact.type) {
321
+ case "python": {
322
+ const options = pythonDockerOptions(
323
+ this.logger,
324
+ this.packageRoot,
325
+ this.packageName,
326
+ artifact.id,
327
+ artifact,
328
+ );
329
+
330
+ return {
331
+ id: artifact.id,
332
+ type: "docker",
333
+ registry: artifact["docker-registry"],
334
+ ...options,
335
+ };
336
+ }
337
+ case "conda": {
338
+ const options = condaDockerOptions(
339
+ this.logger,
340
+ this.packageRoot,
341
+ artifact.id,
342
+ artifact,
343
+ util.currentArch(),
344
+ );
345
+
346
+ return {
347
+ id: artifact.id,
348
+ type: "docker",
349
+ registry: artifact["docker-registry"],
350
+ ...options,
351
+ };
352
+ }
353
+ default: {
354
+ util.assertNever(artifact);
355
+ throw new Error("calm down the linter");
356
+ }
357
+ }
358
+ }
359
+
360
+ public getArtifact(
361
+ id: string,
362
+ type: "asset",
363
+ ): artifacts.withId<artifacts.withType<"asset", artifacts.assetType>>;
364
+ public getArtifact(id: string, type: "environment"): artifacts.withId<artifacts.environmentType>;
365
+ public getArtifact(id: string, type: "binary"): artifacts.withId<artifacts.binaryType>;
366
+ public getArtifact(id: string, type: "java"): artifacts.withId<artifacts.javaType>;
367
+ public getArtifact(id: string, type: "python"): artifacts.withId<artifacts.pythonType>;
368
+ public getArtifact(id: string, type: "R"): artifacts.withId<artifacts.rType>;
369
+ public getArtifact(id: string, type: "docker"): artifacts.withId<artifacts.dockerType>;
370
+ public getArtifact(id: string, type: "conda"): artifacts.withId<artifacts.condaType>;
371
+ public getArtifact(id: string, type: "any"): artifacts.withId<artifacts.anyArtifactType>;
372
+ public getArtifact(
373
+ id: string,
374
+ type: artifacts.artifactType | "any",
375
+ ): artifacts.withId<artifacts.anyArtifactType> {
376
+ const artifacts = this.pkgJson["block-software"].artifacts ?? {};
377
+ const entrypoints = this.pkgJson["block-software"].entrypoints;
378
+
379
+ let artifact: artifacts.anyArtifactType | undefined;
380
+ let errMsg = "";
381
+ if (artifacts[id]) {
382
+ artifact = artifacts[id];
383
+ } else {
384
+ const ep = entrypoints[id];
385
+ if (!ep) {
386
+ throw util.CLIError(
387
+ `artifact with id '${id}' not found neither in 'entrypoints', nor in 'artifacts'`,
388
+ );
389
+ }
390
+
391
+ errMsg = `incorrect artifact reference in entrypoint '${id}'`;
392
+
393
+ if (ep.asset) {
394
+ artifact =
395
+ typeof ep.asset === "string"
396
+ ? artifacts[ep.asset]
397
+ : ({ type: "asset", ...ep.asset } as artifacts.assetType);
398
+ } else if (ep.binary && type !== "docker") {
399
+ // single entrypoint can keep both binary and docker
400
+ const a = ep.binary.artifact;
401
+ artifact = typeof a === "string" ? artifacts[a] : a;
402
+ } else if (ep.conda && type !== "docker") {
403
+ // single entrypoint can keep both conda and docker
404
+ const a = ep.conda.artifact;
405
+ artifact =
406
+ typeof a === "string" ? artifacts[a] : ({ type: "conda", ...a } as artifacts.condaType);
407
+ } else if (ep.environment) {
408
+ const a = ep.environment.artifact;
409
+ artifact =
410
+ typeof a === "string"
411
+ ? artifacts[a]
412
+ : ({ type: "environment", ...a } as artifacts.environmentType);
413
+ } else if (ep.docker) {
414
+ const a = ep.docker.artifact;
415
+ artifact =
416
+ typeof a === "string" ? artifacts[a] : ({ type: "docker", ...a } as artifacts.dockerType);
417
+ }
418
+ }
419
+
420
+ if (!artifact) {
421
+ throw util.CLIError(
422
+ `artifact '${id}' not found neither in 'entrypoints', nor in 'artifacts'`,
423
+ );
424
+ }
425
+
426
+ switch (type) {
427
+ case "docker": {
428
+ return {
429
+ id: id,
430
+ ...requireArtifactType(artifact, "docker", errMsg),
431
+ };
432
+ }
433
+ case "asset": {
434
+ return {
435
+ id: id,
436
+ ...requireArtifactType(artifact, "asset", errMsg),
437
+ };
438
+ }
439
+ case "environment": {
440
+ return {
441
+ id: id,
442
+ ...requireArtifactType(artifact, "environment", errMsg),
443
+ };
444
+ }
445
+ case "binary": {
446
+ return {
447
+ id: id,
448
+ ...requireArtifactType(artifact, "binary", errMsg),
449
+ };
450
+ }
451
+ case "java": {
452
+ return {
453
+ id: id,
454
+ ...requireArtifactType(artifact, "java", errMsg),
455
+ };
456
+ }
457
+ case "python": {
458
+ return {
459
+ id: id,
460
+ ...requireArtifactType(artifact, "python", errMsg),
461
+ };
462
+ }
463
+ case "R": {
464
+ return {
465
+ id: id,
466
+ ...requireArtifactType(artifact, "R", errMsg),
467
+ };
468
+ }
469
+ case "conda": {
470
+ return {
471
+ id: id,
472
+ ...requireArtifactType(artifact, "conda", errMsg),
473
+ };
474
+ }
475
+ case "any": {
476
+ return {
477
+ id: id,
478
+ ...artifact,
479
+ };
480
+ }
481
+ default: {
482
+ util.assertNever(type);
483
+ throw util.CLIError(`invalid artifact type`); // calm down the linter
484
+ }
485
+ }
486
+ }
487
+
488
+ public set version(v: string | undefined) {
489
+ this._versionOverride = v;
490
+ }
491
+
492
+ public artifactName(artifact: artifacts.withId<artifacts.anyArtifactType>): string {
493
+ if (artifact.type !== "docker") {
494
+ if (artifact.name) {
495
+ return artifact.name;
496
+ }
497
+ }
498
+
499
+ return util.trimPrefix(this.pkgJson.name, "@") + "/" + artifact.id;
500
+ }
501
+
502
+ public artifactVersion(artifact: artifacts.anyArtifactType): string {
503
+ if (this._versionOverride) {
504
+ return this._versionOverride;
505
+ }
506
+
507
+ if (process.env[envs.PL_PKG_VERSION]) {
508
+ return process.env[envs.PL_PKG_VERSION];
509
+ }
510
+
511
+ if (artifact.type !== "docker" && artifact.version) {
512
+ return artifact.version;
513
+ }
514
+
515
+ return this.pkgJson.version;
516
+ }
517
+
518
+ public artifactRegistrySettings(artifact: artifacts.anyArtifactType): artifacts.registry {
519
+ if (artifact.type === "docker") {
520
+ return {
521
+ name: artifact.registry,
522
+ };
523
+ }
524
+
525
+ const registry = artifact.registry;
526
+ const registries = this.binaryRegistries;
527
+
528
+ const result: artifacts.registry = {
529
+ name: "default",
530
+ downloadURL: registries.default?.downloadURL,
531
+ storageURL: registries.default?.storageURL,
532
+ };
533
+
534
+ if (registry) {
535
+ if (typeof registry === "string") {
536
+ result.name = registry;
537
+ const regDefault = wellKnownRegistries[result.name];
538
+ result.downloadURL = registries[result.name]?.downloadURL ?? regDefault?.downloadURL;
539
+ result.storageURL = registries[result.name]?.storageURL ?? regDefault?.storageURL;
540
+ } else {
541
+ result.name = registry.name;
542
+ const regDefault = wellKnownRegistries[result.name];
543
+ result.downloadURL =
544
+ registry.downloadURL ?? registries[result.name]?.downloadURL ?? regDefault?.downloadURL;
545
+ result.storageURL =
546
+ registry.storageURL ?? registries[result.name]?.storageURL ?? regDefault?.storageURL;
547
+ }
548
+ }
549
+
550
+ const regNameUpper = result.name.toUpperCase().replaceAll(/[^A-Z0-9_]/g, "_");
551
+
552
+ const uploadTo = process.env[`PL_REGISTRY_${regNameUpper}_UPLOAD_URL`];
553
+ if (uploadTo) {
554
+ result.storageURL = uploadTo;
555
+ }
556
+
557
+ const downloadFrom = process.env[`PL_REGISTRY_${regNameUpper}_DOWNLOAD_URL`];
558
+ if (downloadFrom) {
559
+ result.downloadURL = downloadFrom;
560
+ }
561
+
562
+ if (result.downloadURL) {
563
+ const u = new URL(result.downloadURL, "file:/nonexistent"); // check download URL is valid URL
564
+ if (!["https:", "http:"].includes(u.protocol)) {
565
+ throw util.CLIError(
566
+ `registry ${result.name} download URL is not valid. Only 'https://' and 'http://' schemes are supported for now`,
567
+ );
568
+ }
569
+ }
570
+
571
+ return result;
572
+ }
573
+
574
+ public artifactContentRoot(
575
+ artifact: artifacts.withId<artifacts.anyArtifactType>,
576
+ platform: util.PlatformType,
577
+ ): string {
578
+ if ("root" in artifact && artifact.root) {
579
+ return path.resolve(this.packageRoot, artifact.root);
580
+ }
581
+ if ("roots" in artifact && artifact.roots[platform]) {
582
+ return path.resolve(this.packageRoot, artifact.roots[platform]);
583
+ }
584
+
585
+ throw util.CLIError(
586
+ `root path of software archive on ${platform} is undefined for binary package`,
587
+ );
588
+ }
589
+
590
+ public artifactPlatforms(
591
+ artifact: artifacts.withId<artifacts.anyArtifactType>,
592
+ ): util.PlatformType[] {
593
+ if (artifact.type === "docker") {
594
+ return [util.currentPlatform()];
595
+ }
596
+
597
+ if ("root" in artifact && artifact.root) {
598
+ return [util.currentPlatform()];
599
+ }
600
+
601
+ if ("roots" in artifact) {
602
+ return Object.keys(artifact.roots) as util.PlatformType[];
603
+ }
604
+
605
+ throw util.CLIError(
606
+ `no platforms are defined as supported for artifact '${artifact.id}' in binary mode ` +
607
+ `(no 'roots' are defined)`,
608
+ );
609
+ }
610
+
611
+ public artifactArchiveFullName(
612
+ artifact: artifacts.withId<artifacts.anyArtifactType>,
613
+ platform: util.PlatformType,
614
+ ): string {
615
+ const group = artifact.type === "asset" ? "assets" : "software";
616
+ const extension = artifact.type === "asset" ? "zip" : "tgz";
617
+ const name = this.artifactName(artifact);
618
+ const version = this.artifactVersion(artifact);
619
+
620
+ if (artifacts.isCrossPlatform(artifact.type)) {
621
+ return `${group}/${name}/${version}.${extension}`;
622
+ }
623
+
624
+ const { os, arch } = util.splitPlatform(platform);
625
+ return `${group}/${name}/${version}-${os}-${arch}.${extension}`;
626
+ }
627
+
628
+ public artifactArchiveAddressPattern(
629
+ artifact: artifacts.withId<artifacts.anyArtifactType>,
630
+ ): string {
631
+ const group = artifact.type === "asset" ? "assets" : "software";
632
+ const extension = artifact.type === "asset" ? "zip" : "tgz";
633
+ const name = this.artifactName(artifact);
634
+ const version = this.artifactVersion(artifact);
635
+
636
+ if (artifacts.isCrossPlatform(artifact.type)) {
637
+ return `${group}/${name}/${version}.${extension}`;
638
+ }
639
+
640
+ return `${group}/${name}/${version}-{os}-{arch}.${extension}`;
641
+ }
642
+
643
+ private validateConfig() {
644
+ let hasErrors: boolean = false;
645
+
646
+ const blockSoftware = this.pkgJson["block-software"];
647
+
648
+ const as = blockSoftware.artifacts ?? {};
649
+ const entrypoints = blockSoftware.entrypoints ?? {};
650
+
651
+ for (const [epName, ep] of Object.entries(entrypoints)) {
652
+ if (ep.binary) {
653
+ const artifactID = typeof ep.binary.artifact === "string" ? ep.binary.artifact : epName;
654
+ const artifact = this.getArtifact(artifactID, "any");
655
+
656
+ if (!artifact) {
657
+ this.logger.error(
658
+ `entrypoint '${epName}' refers to artifact '${artifactID}' which is not defined in '${util.softwareConfigName}'`,
659
+ );
660
+ hasErrors = true;
661
+ }
662
+
663
+ if (!this.validateArtifact(artifactID, artifact)) {
664
+ hasErrors = true;
665
+ }
666
+
667
+ if (!artifacts.isBuildable(artifact.type)) {
668
+ this.logger.error(
669
+ `entrypoint '${epName}' artifact type '${artifact.type}' is not buildable to binary package`,
670
+ );
671
+ hasErrors = true;
672
+ }
673
+
674
+ if (artifact.type === "environment") {
675
+ this.logger.error(
676
+ `entrypoint '${epName}' artifact type '${artifact.type}' cannot be build into software pacakge. Use 'environment' entrypoint`,
677
+ );
678
+ hasErrors = true;
679
+ }
680
+ }
681
+
682
+ if (ep.environment) {
683
+ const artifactID =
684
+ typeof ep.environment.artifact === "string" ? ep.environment.artifact : epName;
685
+ const artifact = this.getArtifact(artifactID, "any");
686
+
687
+ if (!artifact) {
688
+ this.logger.error(
689
+ `entrypoint '${epName}' refers to artifact '${artifactID}' which is not defined in '${util.softwareConfigName}'`,
690
+ );
691
+ hasErrors = true;
692
+ }
693
+
694
+ if (!this.validateArtifact(artifactID, artifact)) {
695
+ hasErrors = true;
696
+ }
697
+
698
+ if (artifact.type !== "environment") {
699
+ this.logger.error(
700
+ `entrypoint '${epName}' with 'environment' settings should refer to 'environment' artifact type`,
701
+ );
702
+ hasErrors = true;
703
+ }
704
+ }
705
+
706
+ // TODO: add docker validation here
707
+ // TODO(rfiskov)[MILAB-3163]: Carefully handle docker artifacts, as some (e.g., for Python) may be autogenerated dynamically rather than explicitly defined.
708
+ }
709
+
710
+ const uniquePackageNames = new Set<string>();
711
+
712
+ for (const [artifactID, artifact] of Object.entries(as)) {
713
+ if (!artifacts.isBuildable(artifact.type)) {
714
+ continue;
715
+ }
716
+
717
+ if (!this.validateArtifact(artifactID, artifact)) {
718
+ hasErrors = true;
719
+ }
720
+
721
+ const name = this.artifactName({ id: artifactID, ...artifact });
722
+ const version = this.artifactVersion(artifact);
723
+ const uniqueName = `${name}-${version}`;
724
+ if (uniquePackageNames.has(uniqueName)) {
725
+ this.logger.error(
726
+ `found two packages with the same name '${name}' and version '${version}'`,
727
+ );
728
+ hasErrors = true;
729
+ }
730
+
731
+ uniquePackageNames.add(uniqueName);
732
+ }
733
+
734
+ if (hasErrors) {
735
+ throw util.CLIError(
736
+ `${util.softwareConfigName} has xconfiguration errors in 'block-software' section. See error log messages above for details`,
737
+ );
738
+ }
739
+ }
740
+
741
+ private validateArtifact(artifactName: string, artifact: artifacts.anyArtifactType): boolean {
742
+ if (artifacts.isBuildable(artifact.type)) {
743
+ // Validate that root is not equal to package root
744
+ const resolvedRoots: string[] = [];
745
+ const aType = artifact.type;
746
+ switch (aType) {
747
+ case "asset":
748
+ case "java":
749
+ case "R":
750
+ case "python": {
751
+ resolvedRoots.push(path.resolve(this.packageRoot, artifact.root));
752
+ break;
753
+ }
754
+ case "docker": {
755
+ resolvedRoots.push(path.resolve(this.packageRoot, artifact.context));
756
+ break;
757
+ }
758
+ case "environment":
759
+ case "conda":
760
+ case "binary": {
761
+ resolvedRoots.push(
762
+ ...Object.values(artifact.roots).map((root) => path.resolve(this.packageRoot, root)),
763
+ );
764
+ break;
765
+ }
766
+ default:
767
+ util.assertNever(aType);
768
+ }
769
+
770
+ for (const root of resolvedRoots) {
771
+ if (root === this.packageRoot) {
772
+ this.logger.error(
773
+ `Invalid configuration: '${artifact.type}' artifact '${artifactName}' has 'root' set to the package root, which is not allowed`,
774
+ );
775
+ return false;
776
+ }
777
+ }
778
+ }
779
+
780
+ return true;
781
+ }
782
+ }
783
+
784
+ function parsePackageJson(data: string): z.ZodSafeParseResult<packageJson> {
785
+ const parsedData: unknown = JSON.parse(data);
786
+ return packageJsonSchema.safeParse(parsedData);
787
+ }
788
+
789
+ function requireArtifactType(
790
+ artifact: artifacts.anyArtifactType,
791
+ type: "asset",
792
+ errMsg: string,
793
+ ): artifacts.withType<"asset", artifacts.assetType>;
794
+ function requireArtifactType(
795
+ artifact: artifacts.anyArtifactType,
796
+ type: "environment",
797
+ errMsg: string,
798
+ ): artifacts.environmentType;
799
+ function requireArtifactType(
800
+ artifact: artifacts.anyArtifactType,
801
+ type: "java",
802
+ errMsg: string,
803
+ ): artifacts.javaType;
804
+ function requireArtifactType(
805
+ artifact: artifacts.anyArtifactType,
806
+ type: "python",
807
+ errMsg: string,
808
+ ): artifacts.pythonType;
809
+ function requireArtifactType(
810
+ artifact: artifacts.anyArtifactType,
811
+ type: "R",
812
+ errMsg: string,
813
+ ): artifacts.rType;
814
+ function requireArtifactType(
815
+ artifact: artifacts.anyArtifactType,
816
+ type: "binary",
817
+ errMsg: string,
818
+ ): artifacts.binaryType;
819
+ function requireArtifactType(
820
+ artifact: artifacts.anyArtifactType,
821
+ type: "docker",
822
+ errMsg: string,
823
+ ): artifacts.dockerType;
824
+ function requireArtifactType(
825
+ artifact: artifacts.anyArtifactType,
826
+ type: "conda",
827
+ errMsg: string,
828
+ ): artifacts.condaType;
829
+ function requireArtifactType(
830
+ artifact: artifacts.anyArtifactType,
831
+ type: artifacts.artifactType,
832
+ errMsg: string,
833
+ ): artifacts.anyArtifactType {
834
+ if (artifact.type === type) {
835
+ return artifact;
836
+ }
837
+
838
+ if (errMsg) {
839
+ errMsg = `${errMsg}: `;
840
+ }
841
+
842
+ throw util.CLIError(`${errMsg}wrong artifact type: expected '${type}', got '${artifact.type}'`);
843
+ }