hammerkit 1.27.0-next.5 → 1.27.0-next.7

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,6 +5,7 @@ const crypto_1 = require("crypto");
5
5
  const os_1 = require("os");
6
6
  const path_1 = require("path");
7
7
  const tar_1 = require("tar");
8
+ const tar_2 = require("../../utils/tar");
8
9
  const registry_reference_1 = require("./registry/registry-reference");
9
10
  const registry_client_1 = require("./registry/registry-client");
10
11
  const MAX_TAG_LENGTH = 128;
@@ -74,8 +75,7 @@ function createRegistryCacheBackend(spec) {
74
75
  const layerFile = (0, path_1.join)(dir, 'layer.tar');
75
76
  const files = (await environment.file.listFiles(from)).sort();
76
77
  // portable + mtime-free so identical entries produce identical digests
77
- // `noMtime` is supported by tar 6 but missing from its type definitions
78
- await (0, tar_1.create)({ file: layerFile, cwd: from, portable: true, noMtime: true }, files);
78
+ await (0, tar_2.packArchive)({ file: layerFile, cwd: from, portable: true, noMtime: true }, files);
79
79
  const layer = await (0, registry_client_1.digestFile)(layerFile);
80
80
  const config = Buffer.from(JSON.stringify({
81
81
  architecture: 'amd64',
@@ -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,9 +1,9 @@
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
- const tar_1 = require("tar");
6
+ const tar_1 = require("../utils/tar");
7
7
  const resources_1 = require("./resources");
8
8
  const await_running_state_1 = require("./await-running-state");
9
9
  const sleep_1 = require("../utils/sleep");
@@ -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.packArchive)({
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
  }
@@ -12,6 +12,7 @@ const event_cache_1 = require("../executer/event-cache");
12
12
  const get_docker_executor_1 = require("../executer/get-docker-executor");
13
13
  const path_1 = require("path");
14
14
  const tar_1 = require("tar");
15
+ const tar_2 = require("../utils/tar");
15
16
  const plan_work_volume_1 = require("./utils/plan-work-volume");
16
17
  const work_instance_id_1 = require("./work-instance-id");
17
18
  const stream_1 = require("stream");
@@ -302,7 +303,7 @@ async function archiveContainer(docker, environment, item, path) {
302
303
  for (const generatedArchive of (0, event_cache_1.getArchivePaths)(item.data, path)) {
303
304
  // a file output is a bind mount of a host file, so archive it from the host
304
305
  const readable = hostFiles.has(generatedArchive.path)
305
- ? stream_1.Readable.from((0, tar_1.create)({ cwd: (0, path_1.dirname)(generatedArchive.path) }, [(0, path_1.basename)(generatedArchive.path)]))
306
+ ? stream_1.Readable.from((0, tar_2.packArchive)({ cwd: (0, path_1.dirname)(generatedArchive.path) }, [(0, path_1.basename)(generatedArchive.path)]))
306
307
  : await container.getArchive({
307
308
  path: generatedArchive.path,
308
309
  });
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.getLocalWorkRuntime = exports.getStateFilename = void 0;
7
7
  const tar_1 = require("tar");
8
+ const tar_2 = require("../utils/tar");
8
9
  const path_1 = require("path");
9
10
  const local_task_1 = require("../executer/local-task");
10
11
  const event_cache_1 = require("../executer/event-cache");
@@ -121,7 +122,7 @@ async function archiveLocal(environment, task, path) {
121
122
  for (const generatedArchive of (0, event_cache_1.getArchivePaths)(task.data, path)) {
122
123
  await environment.file.writeStream(generatedArchive.filename,
123
124
  // tar's stream is a Minipass stream; Readable.from adapts it to a Node one
124
- stream_1.Readable.from((0, tar_1.create)({ cwd: task.data.cwd, gzip: true }, [(0, path_1.relative)(task.data.cwd, generatedArchive.path)])));
125
+ stream_1.Readable.from((0, tar_2.packArchive)({ cwd: task.data.cwd, gzip: true }, [(0, path_1.relative)(task.data.cwd, generatedArchive.path)])));
125
126
  }
126
127
  }
127
128
  async function restoreLocal(environment, task, path) {
@@ -7,6 +7,7 @@ const path_1 = require("path");
7
7
  const stream_1 = require("stream");
8
8
  const promises_2 = require("stream/promises");
9
9
  const tar_1 = require("tar");
10
+ const tar_2 = require("../utils/tar");
10
11
  const apply_1 = require("../kubernetes/apply");
11
12
  const kubernetes_instance_1 = require("../kubernetes/kubernetes-instance");
12
13
  const network_policy_1 = require("../kubernetes/network-policy");
@@ -463,7 +464,7 @@ class KubernetesSandboxStorage {
463
464
  await (0, git_checkout_1.git)([`--git-dir=${repository}`, 'fetch', '--quiet', '--depth', '1', '--', fetch.url, fetch.ref], fetch.env, fetch.signal, what);
464
465
  await (0, git_checkout_1.git)([`--git-dir=${repository}`, 'update-ref', `refs/heads/${sandbox_pod_1.INCOMING_REF}`, 'FETCH_HEAD'], fetch.env, fetch.signal, what, op);
465
466
  const incoming = `${CLAIM_PATH}/${sandbox_pod_1.INCOMING}`;
466
- await this.withPod(key, (pod) => this.exec(pod, ['sh', '-c', 'rm -rf "$1" && mkdir -p "$1" && tar xf - -C "$1"', 'sh', incoming], stream_1.Readable.from((0, tar_1.create)({ cwd: repository, portable: true }, ['.'])), null, fetch.signal));
467
+ await this.withPod(key, (pod) => this.exec(pod, ['sh', '-c', 'rm -rf "$1" && mkdir -p "$1" && tar xf - -C "$1"', 'sh', incoming], stream_1.Readable.from((0, tar_2.packArchive)({ cwd: repository, portable: true }, ['.'])), null, fetch.signal));
467
468
  return (await this.request(key, { op, id: key.id }, fetch.signal)).result;
468
469
  }
469
470
  finally {
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.packArchive = void 0;
4
+ const tar_1 = require("tar");
5
+ function packArchive(options, files) {
6
+ return (0, tar_1.create)({ ...options, jobs: 1 }, files);
7
+ }
8
+ exports.packArchive = packArchive;
9
+ //# sourceMappingURL=tar.js.map
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.7",
5
5
  "keywords": [
6
6
  "build",
7
7
  "build-tool",