🌳
pt0/deployF/k8sF/kanikoF/localCtxUploadAI.mts
1import * as _ from 'lodash-es'
15type CtxTarSpec = { tarPath: string, filesA?: string[], type?: string }
17export const genImportTreeCtxTar = async ({tarSpecsA}: {tarSpecsA: CtxTarSpec[]}) => {
18 // bundle tars are esbuilt on the dev pc (no esbuild in the prebuild container) — ship the
19 // prebuilt tar itself in the ctx; prebuildTarsAI skips regenerating it
20 const filesA = _.uniq(_.flatMap(tarSpecsA, ts => ts.filesA || (ts.type === 'bundle' ? [ts.tarPath] : [])))
21 filesA.push(prebuildTarsPtPath)
22 // include gitSha of the files in the cache key — without this, content changes to existing files
23 // (same names, different content) would cache-hit on the stale tar and ship old code.
24 const gitSha = await getHeadGitSha({filterPathList: filesA})
25 const ctxSha = calcShortHash([filesA.sort().join('\n'), gitSha].join('\n'))
26 const ctxTarPath = `tmpdockertars/localctx-${ctxSha}.tar.gz`
27 if (!await fileExists(tsAbsPath(ctxTarPath))) {
28 mkdirParentP(ctxTarPath)
29 await exec1FileThrow([getTarCmd(), '-zcf', `${ptDir}/${ctxTarPath}`, '-C', ptDir, ...filesA], {env: {...process.env, GZIP: '-n'}})
30 }
31 const sizeBytes = await fileSizeBytes(tsAbsPath(ctxTarPath))
32 console.log(`localCtx: ${humanBytes(sizeBytes)} (${filesA.length} files)`)
33 return ctxTarPath
36export const uploadCtxToPod = async ({cluster_name, podName, containerName, ctxTarPath}: {
37 cluster_name: string, podName: string, containerName: string, ctxTarPath: string,
38}) => {
39 const localPath = `${ptDir}/${ctxTarPath}`
40 const localSize = await fileSizeBytes(tsAbsPath(ctxTarPath))
41 let uploadOk = false
42 for (let attempt = 1; attempt <= 3; attempt++) {
43 await eptKubeCli([cluster_name, 'cp', '-c', containerName, localPath, `${podName}:/kanwork/localctx.tar.gz`])
44 const result = await noOutCmdCtx.run({}, () => eptKubeCli([cluster_name, 'exec', podName, '-c', containerName, '--', 'wc', '-c', '/kanwork/localctx.tar.gz'])) as {stdout?: string}
45 const remoteSize = parseInt((result?.stdout || '').trim())
46 if (remoteSize === localSize) { uploadOk = true; break }
47 console.log(`localCtx upload size mismatch attempt ${attempt}: ${remoteSize} != ${localSize}`)
48 if (attempt < 3) await sleep(3000)
49 }
50 if (!uploadOk) throwDebugH({localSize, reason: 'localCtx upload failed'})
51 console.log(`localCtx uploaded ${humanBytes(localSize)}`)
52 await eptKubeCli([cluster_name, 'exec', podName, '-c', containerName, '--', 'touch', '/kanwork/.ctxready'])