agentmux_srv_core/backend/container/
cli_release.rs1use crate::backend::cli_download::{self, Archive, Asset, DownloadSpec};
15use crate::backend::container::cli::InstallLimits;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19pub enum Source {
20 Npm,
22 Download,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct Upload {
29 pub name: String,
30 pub bytes: Vec<u8>,
31}
32
33impl Source {
34 pub fn limits(self) -> InstallLimits {
36 match self {
37 Source::Npm | Source::Download => InstallLimits::default(),
38 }
39 }
40}
41
42pub fn source_for(provider_id: &str) -> Option<(Source, &'static str)> {
44 let spec = cli_download::download_spec(provider_id)?;
45 has_linux(spec.assets).then_some((Source::Download, spec.version))
46}
47
48fn has_linux(assets: &[Asset]) -> bool {
49 ["x86_64", "aarch64"].iter().all(|arch| assets.iter().any(|a| a.os == "linux" && a.arch == *arch))
50}
51
52fn kind_word(a: Archive) -> &'static str {
53 match a {
54 Archive::TarGz => "tar.gz",
55 Archive::Raw => "raw",
56 Archive::Zip => "zip",
58 }
59}
60
61fn asset_params(assets: &[Asset]) -> Vec<String> {
63 let mut out = Vec::new();
64 for a in assets.iter().filter(|a| a.os == "linux") {
65 out.extend([
66 a.arch.to_string(),
67 a.url.to_string(),
68 a.sha256.to_string(),
69 a.size.to_string(),
70 kind_word(a.archive).to_string(),
71 a.exe_in_archive.to_string(),
72 ]);
73 }
74 out
75}
76
77fn head(script: &str, dir: &str, marker: &str, command: &str, limits: InstallLimits) -> Vec<String> {
78 vec![
79 "sh".into(),
80 "-c".into(),
81 script.into(),
82 "agentmux-cli-install".into(),
83 dir.into(),
84 marker.into(),
85 command.into(),
86 limits.script.as_secs().max(1).to_string(),
87 ]
88}
89
90pub fn download_argv(dir: &str, marker: &str, command: &str, provider_id: &str, limits: InstallLimits) -> Option<Vec<String>> {
92 let spec: &DownloadSpec = cli_download::download_spec(provider_id)?;
93 let mut argv = head(DOWNLOAD_SCRIPT.as_str(), dir, marker, command, limits);
94 argv.extend(asset_params(spec.assets));
95 Some(argv)
96}
97
98const COMMON: &str = r#"set -eu
101DIR="$1"; MARKER="$2"; BIN="$3"; LIMIT="$4"; shift 4
102case "$(uname -m)" in
103 x86_64|amd64) ARCH=x86_64 ;;
104 aarch64|arm64) ARCH=aarch64 ;;
105 *) echo "there is no build of $BIN for $(uname -m)" >&2; exit 3 ;;
106esac
107# pick MACHINE URL SHA SIZE KIND EXE ...: this machine's group of six, as URL SHA SIZE KIND EXE
108pick() {
109 URL=""
110 while [ "$#" -ge 6 ]; do
111 if [ "$1" = "$ARCH" ]; then URL="$2"; SHA="$3"; SIZE="$4"; KIND="$5"; EXE="$6"; fi
112 shift 6
113 done
114 [ -n "$URL" ] || { echo "there is no build of $BIN for $ARCH Linux" >&2; exit 3; }
115}
116# fetch URL SHA SIZE OUT: https only, no more than the pinned size, and exactly the pinned hash
117fetch() {
118 MAX=$(( $3 + 1048576 ))
119 timeout -k 15 "$LIMIT" curl -fsSL --proto '=https' --tlsv1.2 --connect-timeout 15 --max-filesize "$MAX" -o "$4" "$1"
120 [ "$(wc -c < "$4")" -le "$MAX" ] || { echo "the download is larger than the pinned file" >&2; exit 4; }
121 GOT=$(sha256sum "$4" | cut -d' ' -f1)
122 [ "$GOT" = "$2" ] || { echo "the download does not match the pinned hash (expected $2, got $GOT)" >&2; exit 4; }
123}
124# unpack KIND FILE EXE OUT: the one executable, made 0755. An archive is unpacked into a throwaway folder; tar
125# refuses a member that names a parent folder, and only a plain file is taken from it.
126unpack() {
127 case "$1" in
128 raw) cp "$2" "$4" ;;
129 tar.gz)
130 X="$STAGE/unpacked"; mkdir -p "$X"
131 tar -xzf "$2" -C "$X" --no-same-owner
132 [ -f "$X/$3" ] && [ ! -L "$X/$3" ] || { echo "the archive has no file $3" >&2; exit 4; }
133 cp "$X/$3" "$4" ;;
134 *) echo "unknown archive kind $1" >&2; exit 4 ;;
135 esac
136 chmod 0755 "$4"
137}
138STAGE="$DIR/.staging"
139mkdir -p "$DIR"
140"#;
141
142const DOWNLOAD_BODY: &str = r#"pick "$@"
145rm -rf "$STAGE"
146mkdir -p "$STAGE/bin"
147trap 'rm -rf "$STAGE"' EXIT
148fetch "$URL" "$SHA" "$SIZE" "$STAGE/download"
149unpack "$KIND" "$STAGE/download" "$EXE" "$STAGE/bin/$BIN"
150rm -f "$DIR"/.installed-*
151rm -rf "$DIR/bin"
152mv "$STAGE/bin" "$DIR/bin"
153: > "$DIR/$MARKER""#;
154
155pub static DOWNLOAD_SCRIPT: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| format!("{COMMON}{DOWNLOAD_BODY}"));
157
158#[cfg(test)]
159#[path = "cli_release_tests.rs"]
160mod tests;