diff --git a/apps/docs/content/docs/cli/cli-reference.mdx b/apps/docs/content/docs/cli/cli-reference.mdx index 24e8bd06e11..34254dce1bc 100644 --- a/apps/docs/content/docs/cli/cli-reference.mdx +++ b/apps/docs/content/docs/cli/cli-reference.mdx @@ -671,10 +671,13 @@ superset hosts list }`} > Set up a machine you can SSH into as a host, with nothing typed on it: -installs this CLI's version there, signs it in with a new API key sent over +installs the CLI there, signs it in with a new API key sent over ssh stdin, runs it as a user service, and waits for it to come online. Running it again on the same target repairs only what is wrong. +It prefers this CLI's version. If that release's archive is unavailable, +it warns and installs the latest published CLI instead. + The remote must be macOS or glibc Linux on x64 or arm64 (not Alpine or Windows) and have `curl`. On Linux it also needs a valid `/etc/machine-id`, and lingering (`loginctl enable-linger`) to keep running after you log out. diff --git a/apps/marketing/public/cli/install.sh b/apps/marketing/public/cli/install.sh index 43c80acdc7f..66a5e280e67 100755 --- a/apps/marketing/public/cli/install.sh +++ b/apps/marketing/public/cli/install.sh @@ -60,9 +60,20 @@ download_tarball() { info "Downloading $url" tmp="$(mktemp -t superset-install.XXXXXX)" - if ! curl -fsSL -o "$tmp" "$url"; then - rm -f "$tmp" - error "Failed to download $url" + if ! http_status=$(curl -fsSL -w '%{http_code}' -o "$tmp" "$url"); then + if [ "$fallback_latest" = true ] && [ "$http_status" = "404" ] \ + && [ "$TAG" != "latest" ] && [ "$TAG" != "cli-latest" ]; then + warn "Release $TAG has no CLI archive for $target; falling back to cli-latest" + url="https://github.com/${REPO}/releases/download/cli-latest/${tarball}" + info "Downloading $url" + if ! curl -fsSL -o "$tmp" "$url"; then + rm -f "$tmp" + error "Failed to download $url" + fi + else + rm -f "$tmp" + error "Failed to download $url" + fi fi echo "$tmp" } @@ -127,6 +138,15 @@ update_path() { } main() { + fallback_latest=false + no_next_steps=false + for arg in "$@"; do + case "$arg" in + --fallback-latest) fallback_latest=true ;; + --no-next-steps) no_next_steps=true ;; + esac + done + printf "${BOLD}Installing Paradev CLI${RESET}\n\n" target="$(detect_target)" @@ -150,7 +170,7 @@ main() { printf "\n${GREEN}${BOLD}Installed!${RESET}\n" # `superset hosts add` signs the machine in itself, so it skips this advice. - if [ "${1:-}" = "--no-next-steps" ]; then + if [ "$no_next_steps" = true ]; then return fi printf "Run ${BOLD}exec \$SHELL${RESET} (or open a new terminal) to load the updated PATH.\n" diff --git a/packages/cli/src/commands/hosts/add/command.ts b/packages/cli/src/commands/hosts/add/command.ts index 608c8d5f16c..46178b2d059 100644 --- a/packages/cli/src/commands/hosts/add/command.ts +++ b/packages/cli/src/commands/hosts/add/command.ts @@ -17,6 +17,7 @@ import { hostKeyName, revokeKeysNamed, } from "../../../lib/ssh-host-setup/host-key"; +import { installCliScript } from "../../../lib/ssh-host-setup/install"; import { log } from "../../../lib/ssh-host-setup/log"; import { MACHINE_ID_FIX, @@ -74,12 +75,13 @@ async function install( "Install curl there, then run this again.", ); } - const tag = wanted ? `cli-v${wanted}` : "latest"; - const exitCode = await ssh.stream(`set -e -script=$(mktemp) -trap 'rm -f "$script"' EXIT -curl -fsSL ${INSTALL_URL} -o "$script" -SUPERSET_HOME="$HOME/superset" SUPERSET_VERSION=${tag} sh "$script" --no-next-steps`); + const exitCode = await ssh.stream( + installCliScript({ + installerUrl: INSTALL_URL, + installDir: `${facts.home}/superset`, + version: wanted, + }), + ); if (exitCode !== 0) { throw new CLIError(`Installing the Paradev CLI on ${ssh.target} failed`); } diff --git a/packages/cli/src/lib/ssh-host-setup/install.test.ts b/packages/cli/src/lib/ssh-host-setup/install.test.ts new file mode 100644 index 00000000000..a965dea2ed1 --- /dev/null +++ b/packages/cli/src/lib/ssh-host-setup/install.test.ts @@ -0,0 +1,155 @@ +import { afterEach, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { shellQuote } from "../shell-quote"; +import { installCliScript } from "./install"; + +const installer = resolve( + import.meta.dir, + "../../../../../apps/marketing/public/cli/install.sh", +); +const roots: string[] = []; +afterEach(() => { + for (const root of roots.splice(0)) + rmSync(root, { recursive: true, force: true }); +}); + +async function runInstaller({ + status, + fallback = true, + latestStatus = 200, + version = "9.9.9", +}: { + status: number; + fallback?: boolean; + latestStatus?: number; + version?: string | null; +}) { + const root = mkdtempSync(join(tmpdir(), "cli-installer-")); + roots.push(root); + const bin = join(root, "bin"); + const payload = join(root, "payload"); + const installDir = join(root, "installed CLI"); + mkdirSync(bin); + mkdirSync(join(payload, "bin"), { recursive: true }); + for (const name of ["superset", "superset-host"]) { + writeFileSync( + join(payload, "bin", name), + "#!/bin/sh\nprintf '2.0.5\\n'\n", + { mode: 0o755 }, + ); + } + const archivePath = join(root, "release.tar.gz"); + const tar = Bun.spawnSync(["tar", "-czf", archivePath, "-C", payload, "."]); + if (tar.exitCode !== 0) throw new Error(tar.stderr.toString()); + const archive = await Bun.file(archivePath).arrayBuffer(); + const requested: string[] = []; + const server = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + fetch(request) { + const path = new URL(request.url).pathname; + if (path === "/install.sh") return new Response(Bun.file(installer)); + requested.push(path); + const code = path.includes("/cli-latest/") ? latestStatus : status; + return new Response(code === 200 ? archive : "release unavailable", { + status: code, + }); + }, + }); + const curl = Bun.which("curl"); + if (!curl) throw new Error("curl is required for the installer test"); + writeFileSync( + join(bin, "curl"), + [ + "#!/bin/bash", + "args=()", + 'for arg in "$@"; do', + ' case "$arg" in', + ` https://github.com/*) arg="$FIXTURE_ORIGIN/\${arg#https://github.com/}" ;;`, + " esac", + ' args+=("$arg")', + "done", + `exec ${shellQuote(curl)} -q "\${args[@]}"`, + "", + ].join("\n"), + { mode: 0o755 }, + ); + try { + const command = fallback + ? installCliScript({ + installerUrl: `${server.url}install.sh`, + installDir, + version, + }) + : `sh ${shellQuote(installer)} --no-next-steps`; + const child = Bun.spawn(["sh", "-c", command], { + env: { + PATH: `${bin}:${join(installDir, "bin")}:${process.env.PATH ?? "/usr/bin:/bin"}`, + SUPERSET_HOME: installDir, + SUPERSET_VERSION: "cli-v9.9.9", + FIXTURE_ORIGIN: server.url.toString().replace(/\/$/, ""), + }, + stdout: "pipe", + stderr: "pipe", + }); + const [exitCode, stdout, stderr] = await Promise.all([ + child.exited, + new Response(child.stdout).text(), + new Response(child.stderr).text(), + ]); + return { exitCode, stdout, stderr, requested, installDir }; + } finally { + server.stop(true); + } +} + +test("hosts add installs cli-latest when its pinned release is missing", async () => { + const result = await runInstaller({ status: 404 }); + expect(result.exitCode).toBe(0); + expect(result.requested.map((path) => path.split("/").at(-2))).toEqual([ + "cli-v9.9.9", + "cli-latest", + ]); + expect(result.stderr).toContain("cli-latest"); + const installed = Bun.spawnSync([ + join(result.installDir, "bin", "superset"), + "--version", + ]); + expect(installed.exitCode).toBe(0); + expect(installed.stdout.toString().trim()).toBe("2.0.5"); + expect(result.stdout).not.toContain("auth login"); +}); + +test("keeps a published pinned release without a fallback request", async () => { + const result = await runInstaller({ status: 200 }); + expect(result.exitCode).toBe(0); + expect(result.requested).toHaveLength(1); +}); + +test.each([ + { name: "an explicit pin", status: 404, fallback: false, latestStatus: 200 }, + { name: "a server error", status: 500, fallback: true, latestStatus: 200 }, +])("does not retry $name as cli-latest", async (options) => { + const result = await runInstaller(options); + expect(result.exitCode).not.toBe(0); + expect(result.requested).toHaveLength(1); +}); + +test("reports failure when the latest release is unavailable too", async () => { + const result = await runInstaller({ status: 404, latestStatus: 404 }); + expect(result.exitCode).not.toBe(0); + expect(result.requested).toHaveLength(2); + expect(result.stderr).toContain("Failed to download"); +}); + +test("does not retry the latest release URL when it is already selected", async () => { + const result = await runInstaller({ + status: 404, + latestStatus: 404, + version: null, + }); + expect(result.exitCode).not.toBe(0); + expect(result.requested).toHaveLength(1); +}); diff --git a/packages/cli/src/lib/ssh-host-setup/install.ts b/packages/cli/src/lib/ssh-host-setup/install.ts new file mode 100644 index 00000000000..9cbf1d2df35 --- /dev/null +++ b/packages/cli/src/lib/ssh-host-setup/install.ts @@ -0,0 +1,18 @@ +import { shellQuote } from "../shell-quote"; + +export function installCliScript({ + installerUrl, + installDir, + version, +}: { + installerUrl: string; + installDir: string; + version: string | null; +}): string { + const tag = version ? `cli-v${version}` : "latest"; + return `set -e +script=$(mktemp) +trap 'rm -f "$script"' EXIT +curl -fsSL ${shellQuote(installerUrl)} -o "$script" +SUPERSET_HOME=${shellQuote(installDir)} SUPERSET_VERSION=${shellQuote(tag)} sh "$script" --no-next-steps --fallback-latest`; +}