| 1 | |
| 2 | |
| 3 | import fs from "node:fs"; |
| 4 | import os from "node:os"; |
| 5 | import path from "node:path"; |
| 6 | import { createHash } from "node:crypto"; |
| 7 | import { execFileSync } from "node:child_process"; |
| 8 | import { createGzip } from "node:zlib"; |
| 9 | import { pipeline } from "node:stream/promises"; |
| 10 | |
| 11 | const target = path.resolve(process.argv[2], "cloud-linux-amd64.tar"); |
| 12 | const digest = async file => { |
| 13 | const hash = createHash("sha256"); |
| 14 | for await (const chunk of fs.createReadStream(file)) hash.update(chunk); |
| 15 | return hash.digest("hex"); |
| 16 | }; |
| 17 | const offline = process.env.CAGE_CLOUD_KIT; |
| 18 | if (offline) { |
| 19 | if (await digest(offline) !== fs.readFileSync(offline + ".sha256", "utf8").trim()) |
| 20 | throw Error("Cloud deployment kit checksum mismatch"); |
| 21 | fs.copyFileSync(offline, target); |
| 22 | } else { |
| 23 | const stage = fs.mkdtempSync(path.join(os.tmpdir(), "cage-bootstrap-")); |
| 24 | try { |
| 25 | const version = fs.readFileSync("VERSION", "utf8").trim(); |
| 26 | const binary = path.join(stage, "cage"); |
| 27 | for (const arch of ["amd64", "arm64"]) { |
| 28 | if (!fs.existsSync(path.resolve("internal/tailnetassets/assets/cage-tailnet-linux-" + arch + ".gz"))) throw Error("Build Tailscale guest assets before packaging the cloud kit"); |
| 29 | } |
| 30 | execFileSync("go", ["build", "-trimpath", "-ldflags", "-s -w -X github.com/sylvinus/agent-vm/internal/version.Version=" + version, "-o", binary, "./cmd/cage"], { |
| 31 | env: {...process.env, CGO_ENABLED:"0", GOOS:"linux", GOARCH:"amd64"}, stdio:"inherit" |
| 32 | }); |
| 33 | await pipeline(fs.createReadStream(binary), createGzip(), fs.createWriteStream(binary + ".gz")); |
| 34 | fs.unlinkSync(binary); |
| 35 | const {mihomo} = JSON.parse(fs.readFileSync("packaging/linux/dependencies.json")); |
| 36 | const cache = path.resolve("_output/linux-deps", mihomo.file); |
| 37 | const core = path.join(stage, "mihomo.gz"); |
| 38 | if (fs.existsSync(cache)) fs.copyFileSync(cache, core); |
| 39 | else execFileSync("curl", ["--fail","--location","--retry","3","--max-time","300","--output",core,mihomo.url],{stdio:"inherit"}); |
| 40 | if (await digest(core) !== mihomo.sha256) throw Error("Linux Mihomo checksum mismatch"); |
| 41 | const recipe = path.join(stage, "deploy/cloud"); |
| 42 | fs.mkdirSync(recipe, {recursive:true}); |
| 43 | for (const name of ["Dockerfile.guest","restore-apt-sources.sh","build-guest.sh"]) |
| 44 | fs.copyFileSync(path.join("deploy/cloud",name),path.join(recipe,name)); |
| 45 | fs.mkdirSync(path.join(stage,"internal/cage"),{recursive:true}); |
| 46 | fs.copyFileSync("internal/cage/apt-bootstrap.sh",path.join(stage,"internal/cage/apt-bootstrap.sh")); |
| 47 | for (const name of ["cage-server.service","cage-container-engine.service"]) |
| 48 | fs.copyFileSync(path.join("deploy/cloud",name),path.join(stage,name)); |
| 49 | const runtime = JSON.parse(fs.readFileSync("deploy/cloud/runtime-artifacts.json")); |
| 50 | fs.writeFileSync(path.join(stage,"engine-downloads"),runtime.artifacts.map(a=>[a.sha256,a.file,a.url].join(" ")).join("\n")+"\n"); |
| 51 | execFileSync("tar", ["-cf",target,"-C",stage,"."],{env:{...process.env,COPYFILE_DISABLE:"1"},stdio:"inherit"}); |
| 52 | } finally { fs.rmSync(stage,{recursive:true,force:true}); } |
| 53 | } |
| 54 | fs.writeFileSync(target+".sha256",await digest(target)+"\n"); |