hammerkit 1.27.0-next.5 → 1.27.0-next.6

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.
@@ -8,6 +8,8 @@ const execute_docker_1 = require("./execute-docker");
8
8
  const plan_work_tasks_1 = require("../planner/utils/plan-work-tasks");
9
9
  const get_cache_directory_1 = require("../optimizer/get-cache-directory");
10
10
  const path_1 = require("path");
11
+ const crypto_1 = require("crypto");
12
+ const claim_layout_1 = require("../kubernetes/claim-layout");
11
13
  const move_files_1 = require("../file/move-files");
12
14
  const work_instance_id_1 = require("../planner/work-instance-id");
13
15
  async function restoreCache(environment, path, workTree) {
@@ -43,8 +45,24 @@ function* getArchivePaths(task, path) {
43
45
  }
44
46
  }
45
47
  else {
46
- for (const generate of task.generates.filter((g) => !g.inherited)) {
47
- const generateId = (0, path_1.relative)(task.cwd, generate.path).split(path_1.sep).join('-');
48
+ // `a-b` and `a/b` flatten alike: of those, the least deep (then the first in sorted
49
+ // order) keeps the name, the others' names carry a hash of their relative path, so a
50
+ // store and a restore in another directory or order find the same archive
51
+ const generates = task.generates
52
+ .filter((g) => !g.inherited)
53
+ .map((generate) => ({ generate, rel: (0, path_1.relative)(task.cwd, generate.path).split(path_1.sep).join(path_1.posix.sep) }));
54
+ const holders = new Map();
55
+ for (const { rel } of [...generates].sort((a, b) => (0, claim_layout_1.byDepth)(a.rel, b.rel))) {
56
+ const id = rel.split(path_1.posix.sep).join('-');
57
+ if (!holders.has(id)) {
58
+ holders.set(id, rel);
59
+ }
60
+ }
61
+ for (const { generate, rel } of generates) {
62
+ let generateId = rel.split(path_1.posix.sep).join('-');
63
+ if (holders.get(generateId) !== rel) {
64
+ generateId += '-' + (0, crypto_1.createHash)('sha256').update(rel).digest('hex').slice(0, 12);
65
+ }
48
66
  yield {
49
67
  filename: (0, path_1.join)(path, generateId + '-generates.tgz'),
50
68
  path: generate.path,
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hasOutputsFlatteningAlike = exports.hasPathsEndingAlike = exports.byDepth = exports.getClaimSubPath = void 0;
4
+ const crypto_1 = require("crypto");
5
+ const path_1 = require("path");
6
+ const portable_path_1 = require("../planner/utils/portable-path");
7
+ // The paths a work item keeps in its claim. Its consumers mount its outputs by the same
8
+ // paths, so they find the same folders.
9
+ function getClaimPaths(work) {
10
+ const data = work.data;
11
+ const paths = [];
12
+ if ('mounts' in data) {
13
+ paths.push(...data.mounts.map((mount) => mount.containerPath));
14
+ }
15
+ if ('src' in data) {
16
+ paths.push(...data.src.filter((src) => !src.inherited).map((src) => src.absolutePath));
17
+ }
18
+ if ('generates' in data) {
19
+ paths.push(...data.generates.filter((generate) => !generate.inherited).map((generate) => generate.path));
20
+ }
21
+ if ('volumes' in data) {
22
+ paths.push(...data.volumes.filter((volume) => !volume.inherited).map((volume) => volume.containerPath));
23
+ }
24
+ return paths;
25
+ }
26
+ // Where `path` is in the claim of `work`, relative to its root. A path is named after its
27
+ // last segment, so claims without two such paths are laid out as they always were (a
28
+ // service's data among them). Of the paths with the same last segment, the least deep one
29
+ // (then the first in sorted order) keeps the name, and the others also carry a hash of
30
+ // their path. Both go by the path
31
+ // relative to the project, as the task's id does, so a run in another checkout directory
32
+ // (each run of a server has its own) or with its `generates` in another order finds the
33
+ // same folders in a kept claim.
34
+ function getClaimSubPath(work, path) {
35
+ const name = (0, path_1.basename)(path) || 'root';
36
+ const alike = getClaimPaths(work).filter((other) => other !== path && (0, path_1.basename)(other) === (0, path_1.basename)(path));
37
+ if (alike.length === 0) {
38
+ return name;
39
+ }
40
+ const own = (0, portable_path_1.portablePath)(work.data.projectRoot, path);
41
+ const first = [own, ...alike.map((other) => (0, portable_path_1.portablePath)(work.data.projectRoot, other))].sort(byDepth)[0];
42
+ if (first === own) {
43
+ return name;
44
+ }
45
+ return `${name}-${(0, crypto_1.createHash)('sha256').update(own).digest('hex').slice(0, 12)}`;
46
+ }
47
+ exports.getClaimSubPath = getClaimSubPath;
48
+ function byDepth(a, b) {
49
+ return a.split('/').length - b.split('/').length || (a < b ? -1 : a > b ? 1 : 0);
50
+ }
51
+ exports.byDepth = byDepth;
52
+ // Whether two different paths of the claim of `work` end alike. Before getClaimSubPath
53
+ // named them apart, they shared one folder, and outputs made then are mixed.
54
+ function hasPathsEndingAlike(work) {
55
+ const paths = new Set(getClaimPaths(work));
56
+ return new Set([...paths].map((path) => (0, path_1.basename)(path))).size < paths.size;
57
+ }
58
+ exports.hasPathsEndingAlike = hasPathsEndingAlike;
59
+ // Whether two outputs of `work` flatten to the same name of a cache archive (`a-b` and
60
+ // `a/b`, see getArchivePaths). Before they were named apart, one archive held both.
61
+ function hasOutputsFlatteningAlike(work) {
62
+ const data = work.data;
63
+ if (!('generates' in data) || !('cwd' in data)) {
64
+ return false;
65
+ }
66
+ const paths = new Set(data.generates.filter((generate) => !generate.inherited).map((generate) => generate.path));
67
+ const names = new Set([...paths].map((path) => (0, path_1.relative)(data.cwd, path).split(path_1.sep).join('-')));
68
+ return names.size < paths.size;
69
+ }
70
+ exports.hasOutputsFlatteningAlike = hasOutputsFlatteningAlike;
71
+ //# sourceMappingURL=claim-layout.js.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ensurePersistentData = exports.clearClaim = exports.getPodForPersistence = void 0;
3
+ exports.ensurePersistentData = exports.packSource = exports.clearClaim = exports.getPodForPersistence = void 0;
4
4
  const apply_1 = require("./apply");
5
5
  const path_1 = require("path");
6
6
  const tar_1 = require("tar");
@@ -137,6 +137,25 @@ async function clearClaim(instance, env, work, pod) {
137
137
  });
138
138
  }
139
139
  exports.clearClaim = clearClaim;
140
+ // The archive that uploads a source into the claim: its file or directory, at its place in
141
+ // the claim (see getClaimSubPath) instead of under its own name
142
+ function packSource(source) {
143
+ const fileItem = (0, path_1.basename)(source.localPath);
144
+ return stream_1.Readable.from((0, tar_1.create)({
145
+ cwd: (0, path_1.dirname)(source.localPath),
146
+ filter(path) {
147
+ return source.matcher(path, source.localPath);
148
+ },
149
+ onWriteEntry(entry) {
150
+ entry.path = source.subPath + entry.path.slice(fileItem.length);
151
+ // the second of two hard-linked files links to the first by its path in the archive
152
+ if (entry.type === 'Link' && entry.linkpath?.startsWith(fileItem)) {
153
+ entry.linkpath = source.subPath + entry.linkpath.slice(fileItem.length);
154
+ }
155
+ },
156
+ }, [fileItem]));
157
+ }
158
+ exports.packSource = packSource;
140
159
  async function ensurePersistentData(instance, env, environment, service, persistence,
141
160
  // the kept claim of the work holds what an earlier run left, which goes first
142
161
  options = {}) {
@@ -163,13 +182,7 @@ options = {}) {
163
182
  if (!(await environment.file.exists(source.localPath))) {
164
183
  continue;
165
184
  }
166
- const fileItem = (0, path_1.basename)(source.localPath);
167
- const data = stream_1.Readable.from((0, tar_1.create)({
168
- cwd: (0, path_1.dirname)(source.localPath),
169
- filter(path) {
170
- return source.matcher(path, source.localPath);
171
- },
172
- }, [fileItem]));
185
+ const data = packSource(source);
173
186
  service.status.console('stdout', `upload ${source.localPath}`);
174
187
  const targetPath = `/dev/hammerkit/${service.id()}`;
175
188
  const res = await instance.exec.exec(env.namespace, name, 'volume', ['tar', 'xf', '-', '-C', targetPath], process.stdout, process.stderr, data, false, (status) => {
@@ -1,11 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.restoreKubernetesData = void 0;
3
+ exports.restoreKubernetesData = exports.getRestoreTarget = void 0;
4
4
  const event_cache_1 = require("../executer/event-cache");
5
5
  const log_1 = require("../log");
6
- const path_1 = require("path");
7
6
  const volumes_1 = require("./volumes");
8
7
  const ensure_persistent_data_1 = require("./ensure-persistent-data");
8
+ const claim_layout_1 = require("./claim-layout");
9
+ // Where the upload pod (which mounts the work's claim at /dev/hammerkit/<id>) unpacks the
10
+ // archive of an output: the output's place in the claim, which its mount reads
11
+ function getRestoreTarget(work, path) {
12
+ return `/dev/hammerkit/${work.id()}/${(0, claim_layout_1.getClaimSubPath)(work, path)}`;
13
+ }
14
+ exports.getRestoreTarget = getRestoreTarget;
9
15
  async function restoreKubernetesData(work, kubernetes, instance, environment, path,
10
16
  // the kept claim of the work holds what an earlier run left, which goes first
11
17
  options = {}) {
@@ -18,7 +24,7 @@ options = {}) {
18
24
  if (!(await environment.file.exists(generatedArchive.filename))) {
19
25
  continue;
20
26
  }
21
- const targetPath = `/dev/hammerkit/${work.id()}/${(0, path_1.basename)(generatedArchive.path)}`;
27
+ const targetPath = getRestoreTarget(work, generatedArchive.path);
22
28
  // a claim made for this run (a server removes the claims of a run when it
23
29
  // ends) does not have the directory of the output yet
24
30
  let failure = null;
@@ -4,6 +4,7 @@ exports.getSandboxAffinity = exports.SANDBOX_LABEL = exports.getWorkspacePodSpec
4
4
  const path_1 = require("path");
5
5
  const fs_1 = require("fs");
6
6
  const kept_claims_1 = require("./kept-claims");
7
+ const claim_layout_1 = require("./claim-layout");
7
8
  function getVolumeName(service, kubernetes) {
8
9
  return (0, kept_claims_1.getClaimName)(service, kubernetes);
9
10
  }
@@ -83,6 +84,7 @@ async function getKubernetesPersistence(work, kubernetes) {
83
84
  exports.getKubernetesPersistence = getKubernetesPersistence;
84
85
  function appendVolume(sourceWork, persistence, containerPath, localPath, isFile, readOnly, kubernetes) {
85
86
  const volumeName = getVolumeName(sourceWork, kubernetes);
87
+ const subPath = (0, claim_layout_1.getClaimSubPath)(sourceWork, containerPath);
86
88
  const existVolume = persistence.volumes.some((v) => v.name === volumeName);
87
89
  if (!existVolume) {
88
90
  persistence.volumes.push({
@@ -101,7 +103,7 @@ function appendVolume(sourceWork, persistence, containerPath, localPath, isFile,
101
103
  readOnly,
102
104
  mountPath: containerPath,
103
105
  name: volumeName,
104
- subPath: (0, path_1.basename)(containerPath),
106
+ subPath,
105
107
  },
106
108
  uploadPath: `/dev/hammerkit/${sourceWork.id()}`,
107
109
  cwd: isFile ? (0, path_1.dirname)(containerPath) : containerPath,
@@ -111,6 +113,7 @@ function appendVolume(sourceWork, persistence, containerPath, localPath, isFile,
111
113
  localPath: localPath,
112
114
  matcher: () => true, // TODO
113
115
  containerPath,
116
+ subPath,
114
117
  stateKey: '', // TODO
115
118
  });
116
119
  }
@@ -6,6 +6,7 @@ const crypto_1 = require("crypto");
6
6
  const replace_env_variables_1 = require("../environment/replace-env-variables");
7
7
  const portable_path_1 = require("../planner/utils/portable-path");
8
8
  const work_secret_1 = require("../planner/work-secret");
9
+ const claim_layout_1 = require("../kubernetes/claim-layout");
9
10
  // Every path is made project-relative (portablePath) so the description, and the
10
11
  // task id hashed from it, is identical wherever the project is checked out.
11
12
  //
@@ -40,6 +41,7 @@ function getWorkTaskCacheDescription(item) {
40
41
  cwd: portable(task.cwd),
41
42
  deps: item.deps.length > 0 ? item.deps.map((dep) => dep.id()).sort() : undefined,
42
43
  secrets: cachedSecrets.length > 0 ? cachedSecrets : undefined,
44
+ claimLayout: (0, claim_layout_1.hasPathsEndingAlike)(item) || (0, claim_layout_1.hasOutputsFlatteningAlike)(item) ? 2 : undefined,
43
45
  };
44
46
  }
45
47
  exports.getWorkTaskCacheDescription = getWorkTaskCacheDescription;
@@ -19,7 +19,10 @@ function appendWorkDependencies(workTree, referenced, item, environment, context
19
19
  }
20
20
  if (((0, work_task_1.isContainerWorkTask)(depNode.data) || (0, work_service_1.isContainerWorkService)(depNode.data)) &&
21
21
  ((0, work_task_1.isContainerWorkTask)(item.data) || (0, work_service_1.isContainerWorkService)(item.data))) {
22
+ // the outputs of the dep's own deps too, which stay those of the task that made them
23
+ // (on Kubernetes, they are in its claim)
22
24
  for (const generate of depNode.data.generates) {
25
+ const inherited = generate.inherited ?? depNode;
23
26
  if (generate.isFile) {
24
27
  // TODO check if thats correct
25
28
  continue;
@@ -31,7 +34,7 @@ function appendWorkDependencies(workTree, referenced, item, environment, context
31
34
  export: false,
32
35
  resetOnChange: false,
33
36
  containerPath: generate.path,
34
- inherited: depNode,
37
+ inherited,
35
38
  readOnly: false,
36
39
  });
37
40
  }
@@ -44,7 +47,7 @@ function appendWorkDependencies(workTree, referenced, item, environment, context
44
47
  exportAlways: false,
45
48
  resetOnChange: false,
46
49
  path: generate.path,
47
- inherited: depNode,
50
+ inherited,
48
51
  isFile: false,
49
52
  });
50
53
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hammerkit",
3
3
  "description": "Containerized build tool with incremental caching that runs the same build file locally, in Docker and on Kubernetes.",
4
- "version": "1.27.0-next.5",
4
+ "version": "1.27.0-next.6",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",