diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fa65a69..6b10a68 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -145,7 +145,11 @@ jobs: || gh release create "$GITHUB_REF_NAME" --verify-tag --generate-notes $prerelease gh release upload "$GITHUB_REF_NAME" --clobber berth-*.tar.gz berth-*.tar.gz.sha256 \ berth-*.zip berth-*.zip.sha256 install.sh install.ps1 VERSION + # The image holds the program the release publishes for x86_64, so it is + # built once. Its Dockerfile is the one berth carries to build an image of + # itself on a server. image: + needs: [binary] runs-on: ubuntu-latest permissions: contents: read @@ -164,6 +168,17 @@ jobs: fi echo "BERTH_BUILD_VERSION=$version" >> "$GITHUB_ENV" fi + - uses: actions/download-artifact@v4 + with: + name: berth-x86_64-unknown-linux-musl + - name: The context + run: | + mkdir -p build/image + tar -xzf berth-x86_64-linux.tar.gz -C build/image berth + cp provision/requirements.txt build/image/ + version=${BERTH_BUILD_VERSION:-$(sed -n '/^\[workspace.package\]/,/^\[/s/^version = "\(.*\)"/\1/p' Cargo.toml)} + echo "BERTH_IMAGE_VERSION=$version" >> "$GITHUB_ENV" + echo "BERTH_IMAGE_SHA256=$(sha256sum build/image/berth | cut -d' ' -f1)" >> "$GITHUB_ENV" - uses: docker/setup-buildx-action@v4 - uses: docker/login-action@v4 if: github.event_name != 'pull_request' @@ -181,9 +196,12 @@ jobs: type=edge,branch=main - uses: docker/build-push-action@v6 with: - context: . + context: build/image file: container/berth.Dockerfile - build-args: BERTH_BUILD_VERSION=${{ env.BERTH_BUILD_VERSION }} + build-args: | + BERTH_VERSION=${{ env.BERTH_IMAGE_VERSION }} + BERTH_SHA256=${{ env.BERTH_IMAGE_SHA256 }} + BERTH_SOURCE=published push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/container/berth.Dockerfile b/container/berth.Dockerfile index e7cef49..562bb60 100644 --- a/container/berth.Dockerfile +++ b/container/berth.Dockerfile @@ -3,25 +3,19 @@ # client for the Docker plugin. The engine is another machine's, or this # machine's through its socket. # -# docker build -f container/berth.Dockerfile -t berth . +# The program is not built here. The context holds a static Linux build of +# it as `berth`, with `requirements.txt` from the provisioning source: # -# BERTH_BUILD_VERSION is the version a release gives it, from its tag; without -# it the image says the version in Cargo.toml. +# berth server image builds it with this machine's engine +# berth server deploy HOST builds it on HOST and runs the server from it +# +# CI builds the published image from the same file, with the release's own +# program in the context and BERTH_SOURCE=published. berth carries this file +# and a test checks that the two are the same. # # Settings and host profiles live in /config, the store and builds in /data. -FROM rust:1-bookworm AS build -RUN apt-get update && apt-get install -y --no-install-recommends musl-tools python3 \ - && rm -rf /var/lib/apt/lists/* \ - && rustup target add x86_64-unknown-linux-musl -WORKDIR /src -COPY . . -ARG BERTH_BUILD_VERSION= -RUN cargo build --release --locked --target x86_64-unknown-linux-musl -p berth \ - && said=$(target/x86_64-unknown-linux-musl/release/berth --version) && echo "$said" \ - && { [ -z "$BERTH_BUILD_VERSION" ] || [ "$said" = "berth $BERTH_BUILD_VERSION" ]; } - FROM debian:trixie-slim -COPY provision/requirements.txt /tmp/requirements.txt +COPY requirements.txt /tmp/requirements.txt RUN apt-get update && apt-get install -y --no-install-recommends \ ca-certificates curl docker-buildx docker-cli docker-compose npm openssh-client openssl \ python3 python3-pip tar tini \ @@ -30,7 +24,24 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ && rm -rf /var/lib/apt/lists/* /tmp/requirements.txt \ && useradd --create-home --uid 1000 berth \ && mkdir -p /config /data && chown berth:berth /config /data -COPY --from=build /src/target/x86_64-unknown-linux-musl/release/berth /usr/local/bin/berth +# The context gives it mode 0755, which COPY keeps. +COPY berth /usr/local/bin/berth +# The version the program has to say, and the SHA-256 it has to have. The +# build stops when either is not so, which also stops a program for another +# CPU, as it does not run here. +ARG BERTH_VERSION= +ARG BERTH_SHA256= +RUN said=$(berth --version) && echo "$said" \ + && case "$said" in "berth $BERTH_VERSION" | "berth $BERTH_VERSION "*) ;; *) [ -z "$BERTH_VERSION" ] ;; esac \ + && { [ -z "$BERTH_SHA256" ] || echo "$BERTH_SHA256 /usr/local/bin/berth" | sha256sum -c -; } +# `self-built` for an image built from a berth, `published` for CI's. The +# baseline is the repository a self-built server goes back to for a release. +ARG BERTH_SOURCE=self-built +ARG BERTH_BASELINE=ghcr.io/helightdev/berth +LABEL org.opencontainers.image.version=$BERTH_VERSION \ + dev.berth.source=$BERTH_SOURCE \ + dev.berth.baseline=$BERTH_BASELINE \ + dev.berth.binary-sha256=$BERTH_SHA256 USER berth ENV XDG_CONFIG_HOME=/config XDG_DATA_HOME=/data TERM=xterm-256color VOLUME ["/config", "/data"] diff --git a/crates/berth-api/src/types.rs b/crates/berth-api/src/types.rs index 31acab0..043f7f8 100644 --- a/crates/berth-api/src/types.rs +++ b/crates/berth-api/src/types.rs @@ -701,6 +701,22 @@ pub struct Act { pub rows: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub cols: Option, + /// Bytes for the standard input of an action that takes them, in base64: + /// the build context of a berth for `upgrade-server source=here`. They + /// go over HTTP when they are more than the portal carries. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub input_b64: Option, +} + +/// The node's action that runs a berth server in its container from another +/// image, and its argument `source`. +pub mod upgrade_server { + pub const ACTION: &str = "upgrade-server"; + pub const SOURCE: &str = "source"; + /// The published image of the version. + pub const RELEASE: &str = "release"; + /// An image built on the node from the berth that runs the action. + pub const HERE: &str = "here"; } /// What running an action answers: one of the three. diff --git a/crates/berth-core/src/lib.rs b/crates/berth-core/src/lib.rs index 44923d9..f46b8c3 100644 --- a/crates/berth-core/src/lib.rs +++ b/crates/berth-core/src/lib.rs @@ -27,6 +27,7 @@ pub mod runner; pub mod seal; pub mod seed; pub mod server; +pub mod server_image; pub mod store; pub mod tailnet; diff --git a/crates/berth-core/src/server_image.rs b/crates/berth-core/src/server_image.rs new file mode 100644 index 0000000..01d3f37 --- /dev/null +++ b/crates/berth-core/src/server_image.rs @@ -0,0 +1,441 @@ +//! The image of a berth server, built from this berth rather than pulled. +//! +//! Its build context is a tar stream of three files: the Dockerfile CI builds +//! the published image from, `requirements.txt` of the provisioning source, +//! and this program as `berth`. A machine's own Docker engine builds it from +//! standard input, as `docker build -t berth:VERSION -`, so nothing is +//! unpacked there. Only a static Linux program for that machine's CPU goes +//! into it: one that loads the system's C library would not run on the +//! image's system. +//! +//! The tag is local, `berth:VERSION`, so a self-built image never passes for +//! a published one; its labels say where it came from and the repository a +//! release is pulled from again. + +use std::io::Write; +use std::process::{Command, Stdio}; + +/// The Dockerfile of the server's image, as CI builds the published one. +pub const DOCKERFILE: &str = include_str!("../../../container/berth.Dockerfile"); + +/// Where the published images are, and where a self-built server goes back to. +pub const BASELINE: &str = "ghcr.io/helightdev/berth"; + +/// The repository of a self-built image's tag. +pub const REPOSITORY: &str = "berth"; + +/// The value of [`label::SOURCE`] on an image built from a berth. +pub const SELF_BUILT: &str = "self-built"; + +/// The labels the Dockerfile sets. +pub mod label { + pub const VERSION: &str = "org.opencontainers.image.version"; + /// `self-built` or `published`. + pub const SOURCE: &str = "dev.berth.source"; + /// The repository a release is pulled from. + pub const BASELINE: &str = "dev.berth.baseline"; + /// The SHA-256 of the program in it. + pub const BINARY: &str = "dev.berth.binary-sha256"; +} + +/// The CPUs berth is released for on Linux. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Cpu { + Amd64, + Arm64, +} + +impl Cpu { + /// The CPU `uname -m` names, or Docker's name for it. + pub fn named(machine: &str) -> Option { + match machine.trim() { + "x86_64" | "amd64" => Some(Cpu::Amd64), + "aarch64" | "arm64" => Some(Cpu::Arm64), + _ => None, + } + } + + /// The CPU of this machine. + pub fn here() -> Option { + Cpu::named(std::env::consts::ARCH) + } + + pub fn name(self) -> &'static str { + match self { + Cpu::Amd64 => "amd64", + Cpu::Arm64 => "arm64", + } + } + + fn elf_machine(self) -> u16 { + match self { + Cpu::Amd64 => 62, + Cpu::Arm64 => 183, + } + } + + fn of_elf(machine: u16) -> Option { + [Cpu::Amd64, Cpu::Arm64] + .into_iter() + .find(|cpu| cpu.elf_machine() == machine) + } + + /// The release's file of berth for this CPU. + pub fn release_file(self) -> &'static str { + match self { + Cpu::Amd64 => "berth-x86_64-linux.tar.gz", + Cpu::Arm64 => "berth-aarch64-linux.tar.gz", + } + } +} + +/// What to use in place of a program that cannot go into the image. +pub fn release_hint(cpu: Cpu) -> String { + let version = berth_api::VERSION; + format!( + "Run this from berth {version} for Linux, from {} of https://github.com/helightdev/berth/releases/tag/v{version}, or from a static build: cargo build --release --target {}-unknown-linux-musl -p berth", + cpu.release_file(), + match cpu { + Cpu::Amd64 => "x86_64", + Cpu::Arm64 => "aarch64", + } + ) +} + +fn u16_at(bytes: &[u8], at: usize) -> Option { + Some(u16::from_le_bytes(bytes.get(at..at + 2)?.try_into().ok()?)) +} + +fn u32_at(bytes: &[u8], at: usize) -> Option { + Some(u32::from_le_bytes(bytes.get(at..at + 4)?.try_into().ok()?)) +} + +fn u64_at(bytes: &[u8], at: usize) -> Option { + let value = u64::from_le_bytes(bytes.get(at..at + 8)?.try_into().ok()?); + usize::try_from(value).ok() +} + +/// Why `program` cannot run in the image on a machine with `cpu`, if it +/// cannot: it has to be a 64-bit Linux program for that CPU that names no +/// interpreter, which a static build does not. +pub fn unfit(program: &[u8], cpu: Cpu) -> Option { + inspect(program, cpu).err() +} + +fn inspect(program: &[u8], cpu: Cpu) -> Result<(), String> { + const PT_INTERP: u32 = 3; + let short = || "This berth's program header is cut short".to_owned(); + if program.get(..4) != Some(b"\x7fELF") { + let what = match program.get(..4) { + Some([0xcf, 0xfa, 0xed, 0xfe] | [0xca, 0xfe, 0xba, 0xbe]) => "a program for macOS", + Some([b'M', b'Z', ..]) => "a program for Windows", + _ => "not a Linux program", + }; + return Err(format!("This berth is {what}")); + } + if program.get(4) != Some(&2) || program.get(5) != Some(&1) { + return Err("This berth is not a 64-bit little-endian Linux program".into()); + } + let machine = u16_at(program, 18).ok_or_else(short)?; + match Cpu::of_elf(machine) { + Some(built) if built != cpu => { + return Err(format!( + "This berth is built for {}, and the machine runs {}", + built.name(), + cpu.name() + )); + } + None => { + return Err(format!( + "This berth is built for a CPU berth is not released for (ELF machine {machine})" + )); + } + Some(_) => {} + } + let table = u64_at(program, 32).ok_or_else(short)?; + let size = usize::from(u16_at(program, 54).ok_or_else(short)?); + let count = usize::from(u16_at(program, 56).ok_or_else(short)?); + for header in (0..count).map(|index| table.saturating_add(index * size)) { + if u32_at(program, header).ok_or_else(short)? != PT_INTERP { + continue; + } + let offset = u64_at(program, header + 8).ok_or_else(short)?; + let length = u64_at(program, header + 32).ok_or_else(short)?; + let interpreter = program + .get(offset..offset.saturating_add(length)) + .map(|name| { + String::from_utf8_lossy(name) + .trim_end_matches('\0') + .to_owned() + }) + .unwrap_or_default(); + return Err(format!( + "This berth loads the system's C library through {interpreter}, which the image's system may not have: it is not a static build" + )); + } + Ok(()) +} + +pub fn sha256(bytes: &[u8]) -> String { + use sha2::{Digest, Sha256}; + Sha256::digest(bytes) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +/// One file of a tar stream: a ustar header, then its bytes to a whole block. +fn tar_entry(out: &mut Vec, name: &str, mode: u32, bytes: &[u8]) { + let mut header = [0u8; 512]; + let mut put = |at: usize, text: &[u8]| header[at..at + text.len()].copy_from_slice(text); + put(0, name.as_bytes()); + put(100, format!("{mode:07o}\0").as_bytes()); + put(108, b"0000000\0"); + put(116, b"0000000\0"); + put(124, format!("{:011o}\0", bytes.len()).as_bytes()); + put(136, b"00000000000\0"); + put(148, b" "); + put(156, b"0"); + put(257, b"ustar\0"); + put(263, b"00"); + let sum: u32 = header.iter().map(|&byte| u32::from(byte)).sum(); + header[148..156].copy_from_slice(format!("{sum:06o}\0 ").as_bytes()); + out.extend_from_slice(&header); + out.extend_from_slice(bytes); + out.resize(out.len().div_ceil(512) * 512, 0); +} + +/// The build context of the image: the Dockerfile, the provisioning +/// source's `requirements.txt` and the program, as a tar stream. +pub fn tar(program: &[u8], requirements: &[u8]) -> Vec { + let mut out = Vec::with_capacity(program.len() + 16 * 1024); + tar_entry(&mut out, "Dockerfile", 0o644, DOCKERFILE.as_bytes()); + tar_entry(&mut out, "requirements.txt", 0o644, requirements); + tar_entry(&mut out, "berth", 0o755, program); + out.resize(out.len() + 1024, 0); + out +} + +/// The stream compressed with gzip, which `docker build -` reads as it is, +/// or as it was when this machine has no gzip. +pub fn compressed(tar: Vec) -> Vec { + let Ok(mut gzip) = Command::new("gzip") + .arg("-c") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + else { + return tar; + }; + let mut input = gzip.stdin.take().expect("piped"); + let writer = std::thread::spawn(move || { + let _ = input.write_all(&tar); + tar + }); + let output = gzip.wait_with_output(); + let tar = writer.join().expect("the writer of gzip's input"); + match output { + Ok(output) if output.status.success() && !output.stdout.is_empty() => output.stdout, + _ => tar, + } +} + +/// The image's build context, and what the build is told of it. +pub struct Context { + /// The context, compressed. + pub bytes: Vec, + /// The SHA-256 of the program in it. + pub sha256: String, +} + +impl Context { + /// The context of `program`, after checking that it fits a machine with `cpu`. + pub fn of(program: &[u8], requirements: &[u8], cpu: Cpu) -> Result { + if let Some(why) = unfit(program, cpu) { + return Err(format!("{why}. {}", release_hint(cpu))); + } + Ok(Context { + sha256: sha256(program), + bytes: compressed(tar(program, requirements)), + }) + } + + /// The context of this berth, for a machine with `cpu`. Its + /// `requirements.txt` is the provisioning source's in `source`. + pub fn of_this_berth(source: &std::path::Path, cpu: Cpu) -> Result { + let file = source.join("provision/requirements.txt"); + let requirements = + std::fs::read(&file).map_err(|error| format!("{}: {error}", file.display()))?; + let program = std::fs::read("/proc/self/exe") + .map_err(|error| format!("This berth's own program cannot be read: {error}"))?; + Context::of(&program, &requirements, cpu) + } + + /// The `--build-arg` options of `docker build` for it. + pub fn build_args(&self) -> Vec { + [ + format!("BERTH_VERSION={}", berth_api::VERSION), + format!("BERTH_SHA256={}", self.sha256), + ] + .into_iter() + .flat_map(|arg| ["--build-arg".to_owned(), arg]) + .collect() + } +} + +/// The local tag of the image of this berth. +pub fn tag() -> String { + format!("{REPOSITORY}:{}", berth_api::VERSION) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The files of a tar stream: name, mode and bytes. + fn entries(tar: &[u8]) -> Vec<(String, u32, Vec)> { + let mut found = vec![]; + let mut at = 0; + while at + 512 <= tar.len() && tar[at] != 0 { + let header = &tar[at..at + 512]; + let text = |range: std::ops::Range| { + String::from_utf8_lossy(&header[range]) + .trim_end_matches(['\0', ' ']) + .to_owned() + }; + let size = usize::from_str_radix(&text(124..136), 8).unwrap(); + let mode = u32::from_str_radix(&text(100..108), 8).unwrap(); + // The checksum is that of the header with its own field as spaces. + let mut blank = header.to_vec(); + blank[148..156].fill(b' '); + let sum: u32 = blank.iter().map(|&byte| u32::from(byte)).sum(); + assert_eq!(u32::from_str_radix(&text(148..154), 8).unwrap(), sum); + found.push((text(0..100), mode, tar[at + 512..at + 512 + size].to_vec())); + at += 512 + size.div_ceil(512) * 512; + } + found + } + + /// A 64-bit little-endian ELF header for `machine`, with one program + /// header of `kind` that points at `interpreter`. + fn elf(machine: u16, kind: u32, interpreter: &str) -> Vec { + let mut program = vec![0u8; 256]; + program[..6].copy_from_slice(b"\x7fELF\x02\x01"); + program[18..20].copy_from_slice(&machine.to_le_bytes()); + program[32..40].copy_from_slice(&64u64.to_le_bytes()); + program[54..56].copy_from_slice(&56u16.to_le_bytes()); + program[56..58].copy_from_slice(&1u16.to_le_bytes()); + program[64..68].copy_from_slice(&kind.to_le_bytes()); + program[72..80].copy_from_slice(&160u64.to_le_bytes()); + program[96..104].copy_from_slice(&(interpreter.len() as u64 + 1).to_le_bytes()); + program[160..160 + interpreter.len()].copy_from_slice(interpreter.as_bytes()); + program + } + + #[test] + fn only_a_static_linux_program_for_the_machines_cpu_fits() { + const LOAD: u32 = 1; + const INTERP: u32 = 3; + assert_eq!(unfit(&elf(62, LOAD, ""), Cpu::Amd64), None); + assert_eq!(unfit(&elf(183, LOAD, ""), Cpu::Arm64), None); + let other = unfit(&elf(183, LOAD, ""), Cpu::Amd64).unwrap(); + assert!( + other.contains("built for arm64, and the machine runs amd64"), + "{other}" + ); + let glibc = unfit(&elf(62, INTERP, "/lib64/ld-linux-x86-64.so.2"), Cpu::Amd64).unwrap(); + assert!(glibc.contains("/lib64/ld-linux-x86-64.so.2"), "{glibc}"); + assert!(glibc.contains("not a static build"), "{glibc}"); + let macos = unfit(&[0xcf, 0xfa, 0xed, 0xfe, 7, 0, 0, 1], Cpu::Amd64).unwrap(); + assert!(macos.contains("a program for macOS"), "{macos}"); + assert!( + unfit(b"MZ\x90\x00", Cpu::Amd64) + .unwrap() + .contains("Windows") + ); + assert!(unfit(b"#!/bin/sh\n", Cpu::Amd64).is_some()); + // A header cut short is refused rather than read past. + assert!(unfit(&elf(62, LOAD, "")[..40], Cpu::Amd64).is_some()); + } + + #[test] + fn a_refusal_names_the_release_to_use() { + let refused = Context::of(&elf(62, 3, "/lib/ld.so"), b"", Cpu::Arm64) + .err() + .unwrap(); + assert!(refused.contains(Cpu::Arm64.release_file()), "{refused}"); + assert!(refused.contains(berth_api::VERSION), "{refused}"); + assert_eq!(Cpu::named("x86_64"), Some(Cpu::Amd64)); + assert_eq!(Cpu::named("aarch64\n"), Some(Cpu::Arm64)); + assert_eq!(Cpu::named("riscv64"), None); + } + + #[test] + fn the_context_holds_the_dockerfile_the_requirements_and_the_program() { + let program = elf(62, 1, ""); + let tar = tar(&program, b"ansible-core==2.21.6\n"); + assert_eq!(tar.len() % 512, 0); + let found = entries(&tar); + let names: Vec<&str> = found.iter().map(|(name, ..)| name.as_str()).collect(); + assert_eq!(names, ["Dockerfile", "requirements.txt", "berth"]); + assert_eq!(found[0].2, DOCKERFILE.as_bytes()); + assert_eq!(found[1].2, b"ansible-core==2.21.6\n"); + assert_eq!((found[2].1, &found[2].2), (0o755, &program)); + let context = Context::of(&program, b"x\n", Cpu::Amd64).unwrap(); + assert_eq!(context.sha256, sha256(&program)); + assert_eq!(context.sha256.len(), 64); + let args = context.build_args().join(" "); + assert!(args.contains(&format!("--build-arg BERTH_VERSION={}", berth_api::VERSION))); + assert!(args.contains(&format!("--build-arg BERTH_SHA256={}", context.sha256))); + assert_eq!(tag(), format!("berth:{}", berth_api::VERSION)); + } + + #[test] + fn the_compressed_context_is_the_same_stream() { + let tar = tar(&elf(62, 1, ""), b"x\n"); + let packed = compressed(tar.clone()); + if packed == tar { + return; // This machine has no gzip. + } + let mut gunzip = Command::new("gzip") + .arg("-dc") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + gunzip.stdin.take().unwrap().write_all(&packed).unwrap(); + assert_eq!(gunzip.wait_with_output().unwrap().stdout, tar); + } + + /// The file CI builds the published image from, by the workflow. + #[test] + fn the_dockerfile_berth_carries_is_the_one_ci_builds() { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let workflow = std::fs::read_to_string(root.join(".github/workflows/release.yml")).unwrap(); + let image = workflow + .split("\n image:\n") + .nth(1) + .expect("the workflow has a job image"); + let file = image + .lines() + .find_map(|line| line.trim().strip_prefix("file: ")) + .expect("the image job names its Dockerfile"); + let ci = std::fs::read_to_string(root.join(file)).unwrap(); + assert_eq!( + DOCKERFILE, ci, + "berth carries another Dockerfile than the one CI builds, {file}" + ); + // It labels the image as berth's helper reads it. + for label in [ + label::VERSION, + label::SOURCE, + label::BASELINE, + label::BINARY, + ] { + assert!(DOCKERFILE.contains(label), "{label}"); + } + assert!(DOCKERFILE.contains(&format!("BERTH_BASELINE={BASELINE}"))); + assert!(image.contains("BERTH_SOURCE=published")); + } +} diff --git a/crates/berth-page/src/net.rs b/crates/berth-page/src/net.rs index aea92e6..09b6131 100644 --- a/crates/berth-page/src/net.rs +++ b/crates/berth-page/src/net.rs @@ -196,6 +196,35 @@ pub async fn http( )) } +/// One request with a bearer and a JSON body, such as one with more bytes +/// than the portal carries: the answer as it came. +pub async fn send( + method: &str, + server: &str, + path: &str, + bearer: &str, + body: Vec, +) -> Result { + let target = target(server, path)?; + let stream = connect(&target).await?; + let mut client = client::Client::over(stream) + .await + .map_err(|error| describe(&error))?; + let authorization = format!("Bearer {bearer}"); + let mut headers = vec![ + ("host", target.authority.as_str()), + ("accept", "application/json"), + ("content-type", "application/json"), + ]; + if !bearer.is_empty() { + headers.push(("authorization", authorization.as_str())); + } + client + .send(method, &target.path, &headers, body.into()) + .await + .map_err(|error| describe(&error)) +} + pub fn describe(error: &Error) -> String { match error { Error::Api { message, .. } => message.clone(), diff --git a/crates/berth/src/cli/args.rs b/crates/berth/src/cli/args.rs index 3c649fd..cc46a3f 100644 --- a/crates/berth/src/cli/args.rs +++ b/crates/berth/src/cli/args.rs @@ -920,6 +920,31 @@ pub enum Server { /// The server's API documents. #[command(subcommand)] Api(Api), + /// Build the server's image from this berth with this machine's Docker engine. + Image { + /// The image's tag: berth:VERSION, this berth's version, unless another is given. + #[arg(long, value_name = "TAG")] + tag: Option, + }, + /// Run the berth server on a machine with Docker, over SSH: from an image built there from this berth, or from a release. + Deploy(Deploy), +} + +#[cfg(target_os = "linux")] +#[derive(Args, Debug, Clone)] +pub struct Deploy { + /// The machine, as ssh reaches it with your own keys and settings. + #[arg(value_name = "[USER@]HOST")] + pub target: String, + /// The server's container there. + #[arg(long, value_name = "NAME", default_value = "berth")] + pub container: String, + /// The port the server listens on there, on 127.0.0.1. A new container gets 7777 without it. + #[arg(long, value_name = "PORT")] + pub port: Option, + /// Pull the published image of this version instead, as ghcr.io/helightdev/berth:VERSION. + #[arg(long, value_name = "VERSION")] + pub release: Option, } #[cfg(target_os = "linux")] diff --git a/crates/berth/src/cli/client.rs b/crates/berth/src/cli/client.rs index 3206d75..4ec3781 100644 --- a/crates/berth/src/cli/client.rs +++ b/crates/berth/src/cli/client.rs @@ -18,6 +18,8 @@ type Finish = Box; pub struct Client { /// The server's address, as `--server` takes it. pub url: String, + /// The token it was opened with, for a request over HTTP and to open it again. + token: String, portal: Portal, finish: Option, /// The hosts whose sudo password the server in this process was given @@ -131,6 +133,7 @@ impl Client { let client = Client { portal: connect(&url, &token)?, url, + token, finish, sudo_ready: Default::default(), }; @@ -152,6 +155,18 @@ impl Client { Ok(Client { portal: connect(&url, &token)?, url, + token, + finish: None, + sudo_ready: Default::default(), + }) + } + + /// The same server again, as when it was replaced and answers anew. + pub fn reopen(&self) -> Result { + Ok(Client { + portal: connect(&self.url, &self.token)?, + url: self.url.clone(), + token: self.token.clone(), finish: None, sudo_ready: Default::default(), }) @@ -233,10 +248,37 @@ impl Client { Subject::Resource(id) => ("resources.act", json!({"id": id, "action": action})), Subject::Installation => ("installation.act", json!({"action": action})), }; + let large = act + .input_b64 + .as_ref() + .is_some_and(|input| input.len() > berth_api::ops::PORTAL_BYTES); + if let (true, Subject::Host(name)) = (large, subject) { + return self.act_over_http(name, action, act); + } let acted = self.call(op, args, Some(serde_json::to_value(act)?))?; Ok(serde_json::from_value(acted)?) } + /// Run a host's action with more input than the portal carries: the + /// same request, over HTTP. + fn act_over_http( + &self, + host: &str, + action: &str, + act: &berth_api::Act, + ) -> Result { + let path = format!( + "/v1/hosts/{}/actions/{}", + berth_api::client::encode(host), + berth_api::client::encode(action) + ); + let body = serde_json::to_vec(act)?; + let answer = self + .wait(net::send("POST", &self.url, &path, &self.token, body)) + .map_err(Fail::unreachable)?; + answer.json().map_err(Fail::from) + } + /// The actions of a subject. pub fn actions(&self, subject: &Subject) -> Result> { if let Subject::Host(name) = subject { diff --git a/crates/berth/src/cli/mod.rs b/crates/berth/src/cli/mod.rs index 05f5c24..6c30f91 100644 --- a/crates/berth/src/cli/mod.rs +++ b/crates/berth/src/cli/mod.rs @@ -17,6 +17,7 @@ pub mod out; mod pages; mod setup; mod things; +mod upgrade; #[cfg(target_os = "linux")] pub use host::{profile_of as host_profile, save as save_host}; diff --git a/crates/berth/src/cli/things.rs b/crates/berth/src/cli/things.rs index 56868b0..2f7aacb 100644 --- a/crates/berth/src/cli/things.rs +++ b/crates/berth/src/cli/things.rs @@ -44,7 +44,7 @@ pub fn arguments(words: &[String]) -> Result> { } /// How following a session ended. -enum Ended { +pub(super) enum Ended { Exited(Option), /// Left with Ctrl+Q, or Ctrl+C when nothing is typed into it; it runs on. Detached, @@ -57,7 +57,7 @@ fn exited(said: &Value) -> Option> { /// Follow a process session: its two streams to ours, apart. With `stdin`, /// ours is its input and Ctrl+C interrupts it; without, Ctrl+C only leaves. -async fn follow_process(mut stream: PortalStream, stdin: bool) -> Result { +pub(super) async fn follow_process(mut stream: PortalStream, stdin: bool) -> Result { let (sender, mut typed) = tokio::sync::mpsc::unbounded_channel::(); if stdin && !std::io::stdin().is_terminal() { tokio::spawn(async move { @@ -221,7 +221,12 @@ fn finish(client: &Client, host: &str, id: &str, ended: Ended, forget: bool) -> } } -fn attach_stream(client: &Client, host: &str, id: &str, since: u64) -> Result { +pub(super) fn attach_stream( + client: &Client, + host: &str, + id: &str, + since: u64, +) -> Result { client.stream( "sessions.attach", Some(host), @@ -498,16 +503,30 @@ pub(super) fn run_on( }; let action = find_action(client.actions(subject)?, name, &shown, &list)?; let mut given = arguments(words)?; + // This berth goes with the request, as what the node builds. + let here = match subject { + Subject::Host(host) if super::upgrade::wanted(name, &given) => Some(host.as_str()), + _ => None, + }; complete(&action, &mut given, &shown)?; + let input_b64 = match here { + Some(host) => Some(super::upgrade::send_here(client, host, &mut given)?), + None => None, + }; let (rows, cols) = terminal_size(); let act = Act { arguments: given, sudo_password: sudo, rows: Some(rows), cols: Some(cols), + input_b64, ..Act::default() }; let acted = client.act(subject, name, &act)?; + if let (Some(host), Some(session), false) = (here, &acted.session, detach) { + super::upgrade::follow(client, host, &session.id)?; + return Ok(None); + } // A terminal of the host's is attached to here. if acted.job.is_none() && let (Some(session), Subject::Host(host)) = (&acted.session, subject) diff --git a/crates/berth/src/cli/upgrade.rs b/crates/berth/src/cli/upgrade.rs new file mode 100644 index 0000000..f1d76b5 --- /dev/null +++ b/crates/berth/src/cli/upgrade.rs @@ -0,0 +1,171 @@ +//! `upgrade-server source=here`: a node builds the berth server's image from +//! this berth, which sends itself with the request, and runs the server from +//! it. The swap is the last thing the node does, and the server it replaces +//! may be the one this command talks to; so the node's session is followed +//! rather than the server's job, and read again once the server answers anew. + +use super::client::Client; +use super::fail::{Fail, Result}; +use super::out; +use super::things::{Ended, attach_stream, follow_process}; +use berth_api::upgrade_server; +use serde_json::{Value, json}; +use std::collections::BTreeMap; +use std::time::{Duration, Instant}; + +/// How long a replaced server has to answer again. +const BACK_WITHIN: Duration = Duration::from_secs(300); + +/// Whether these are the arguments of `upgrade-server` that build from here. +pub fn wanted(action: &str, given: &BTreeMap) -> bool { + action == upgrade_server::ACTION + && given + .get(upgrade_server::SOURCE) + .is_some_and(|source| source.trim() == upgrade_server::HERE) +} + +/// The CPU of the host, as `uname -m` names it there. +fn cpu_of(client: &Client, host: &str) -> Result { + let body = json!({ + "kind": "process", "command": ["uname", "-m"], "user": "root", + "title": "the CPU, for the server's image", "timeout": 20, + }); + let started = client.on(host, "sessions.start", json!({"wait": "20s"}), Some(body))?; + if let Some(id) = started["id"].as_str() { + let _ = client.on(host, "sessions.delete", json!({"id": id}), None); + } + let said = started["stdout_b64"] + .as_str() + .and_then(berth_api::unb64) + .map(|bytes| String::from_utf8_lossy(&bytes).trim().to_owned()) + .unwrap_or_default(); + match said.is_empty() { + true => Err(Fail::failed(format!( + "{host} did not say what CPU it has (uname -m)" + ))), + false => Ok(said), + } +} + +/// Check this berth against the host and put its build context in `act`, +/// with the version it is. +pub fn send_here( + client: &Client, + host: &str, + given: &mut BTreeMap, +) -> Result { + let version = berth_api::VERSION; + match given.get("version").map(|asked| asked.trim()) { + Some(asked) if !asked.is_empty() && asked != version => { + return Err(Fail::usage(format!( + "source=here sends this berth, which is {version}, not {asked}. Leave out version, or run the berth of {asked}" + ))); + } + _ => {} + } + given.insert("version".into(), version.into()); + let cpu = cpu_of(client, host)?; + let (context, sha256) = crate::local::server_context(&cpu)?; + out::note(format!( + "Sending berth {version} ({}, {:.1} MB) to {host}, which builds the image berth:{version} from it", + &sha256[..12], + context.len() as f64 / 1e6 + )); + Ok(berth_api::b64(&context)) +} + +/// The lines of the end of the host's session. +fn tail(client: &Client, host: &str, id: &str) -> Vec { + let said = client + .on(host, "sessions.output", json!({"id": id, "tail": 15}), None) + .unwrap_or(Value::Null); + said["lines"] + .as_array() + .into_iter() + .flatten() + .filter_map(|line| line.as_str().map(str::to_owned)) + .collect() +} + +/// Follow the node's session to its end. When the connection goes with the +/// server, wait for the server to answer again and read how it ended. +pub fn follow(client: &Client, host: &str, id: &str) -> Result<()> { + let stream = attach_stream(client, host, id, 0)?; + match client.wait(follow_process(stream, false))? { + Ended::Exited(Some(0)) => return Ok(()), + Ended::Exited(code) => return Err(Fail::exit(code.unwrap_or(1))), + Ended::Detached => { + eprintln!( + "\r\nLeft the upgrade running on {host}. berth session logs {host} {id} -f follows it again" + ); + return Ok(()); + } + Ended::Lost => {} + } + out::note(format!( + "The connection to the server ended, as it does when the upgrade replaces it. Waiting for it to answer again (up to {} minutes)", + BACK_WITHIN.as_secs() / 60 + )); + let since = Instant::now(); + loop { + std::thread::sleep(Duration::from_secs(3)); + let session = client.reopen().and_then(|again| { + Ok(( + again.on(host, "sessions.get", json!({"id": id}), None)?, + again, + )) + }); + match session { + Ok((session, again)) if !session["ended"].is_null() => { + for line in tail(&again, host, id) { + println!("{line}"); + } + let version = again + .server_version() + .map(|(version, _)| version) + .unwrap_or_default(); + return match session["exit_code"].as_i64() { + Some(0) => { + out::done(format!("The server answers again, as berth {version}")); + Ok(()) + } + code => Err(Fail::exit(code.unwrap_or(1) as i32)), + }; + } + // Back, and the node is not done yet; or not back yet. + _ if since.elapsed() < BACK_WITHIN => {} + Ok(_) => { + return Err(Fail::failed(format!( + "The upgrade on {host} has not ended after {} minutes. berth session logs {host} {id} -f follows it", + BACK_WITHIN.as_secs() / 60 + ))); + } + Err(error) => { + return Err(Fail::failed(format!( + "The server did not answer again within {} minutes: {}. On {host}, berth session logs {host} {id} shows how the upgrade ended", + BACK_WITHIN.as_secs() / 60, + error.message + ))); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn only_upgrade_server_with_source_here_sends_this_berth() { + let given = |words: &[(&str, &str)]| -> BTreeMap { + words + .iter() + .map(|(name, value)| ((*name).to_owned(), (*value).to_owned())) + .collect() + }; + assert!(wanted("upgrade-server", &given(&[("source", "here")]))); + assert!(!wanted("upgrade-server", &given(&[("source", "release")]))); + assert!(!wanted("upgrade-server", &given(&[("version", "0.6.0")]))); + assert!(!wanted("update", &given(&[("source", "here")]))); + } +} diff --git a/crates/berth/src/client_only.rs b/crates/berth/src/client_only.rs index 266f419..83824f5 100644 --- a/crates/berth/src/client_only.rs +++ b/crates/berth/src/client_only.rs @@ -27,3 +27,17 @@ pub fn ssh(_: &Global, ssh: &Ssh) -> Done<()> { ssh.host ))) } + +/// Only a berth for Linux goes into a server's image. +pub fn server_context(machine: &str) -> Done<(Vec, String)> { + let file = match machine.trim() { + "aarch64" | "arm64" => "berth-aarch64-linux.tar.gz", + _ => "berth-x86_64-linux.tar.gz", + }; + Err(Fail::refused(format!( + "This berth is for {}, and a server's image holds berth for Linux. Run it from berth {} for Linux, from {file} of https://github.com/helightdev/berth/releases/tag/v{}", + std::env::consts::OS, + berth_api::VERSION, + berth_api::VERSION + ))) +} diff --git a/crates/berth/src/local/deploy.rs b/crates/berth/src/local/deploy.rs new file mode 100644 index 0000000..a12ebbd --- /dev/null +++ b/crates/berth/src/local/deploy.rs @@ -0,0 +1,289 @@ +//! The berth server's image built from this berth: for a node's +//! `upgrade-server source=here`, with this machine's engine +//! (`berth server image`), or on any machine with Docker over SSH +//! (`berth server deploy`), which then runs the server from it. + +use crate::cli::args; +use crate::cli::fail::{Fail, Result as Done}; +use crate::cli::out; +use berth_core::paths::Paths; +use berth_core::server_image::{self, Context, Cpu}; +use std::io::Write; +use std::process::{Command, Stdio}; + +/// The helper that runs a server's container from another image, which a +/// node has installed and `deploy` sends. +const HELPER: &str = + include_str!("../../../../provision/ansible/roles/devbox/files/berth-server-upgrade.py"); + +/// The port a server made by `deploy` listens on, on that machine only. +const PORT: u16 = 7777; + +fn context(cpu: Cpu) -> Done { + let source = Paths::locate()?.source()?; + Context::of_this_berth(&source, cpu).map_err(Fail::refused) +} + +fn cpu_named(machine: &str, host: &str) -> Done { + Cpu::named(machine).ok_or_else(|| { + Fail::refused(format!( + "{host} has the CPU {machine}, and berth is released for x86_64 and aarch64 alone" + )) + }) +} + +/// The build context of this berth for a host whose `uname -m` is `machine`, +/// and the SHA-256 of the program in it. +pub fn server_context(machine: &str) -> Done<(Vec, String)> { + let cpu = cpu_named(machine.trim(), "The host")?; + let context = context(cpu)?; + Ok((context.bytes, context.sha256)) +} + +/// Run `command` with `input` on its standard input, its output to ours. +fn fed(mut command: Command, input: &[u8], what: &str) -> Done<()> { + let mut child = command + .stdin(Stdio::piped()) + .spawn() + .map_err(|error| Fail::failed(format!("{what}: {error}")))?; + let mut stdin = child.stdin.take().expect("piped"); + let written = stdin.write_all(input); + drop(stdin); + let status = child.wait()?; + match (status.success(), written) { + (true, Ok(())) => Ok(()), + (true, Err(error)) => Err(Fail::failed(format!("{what}: {error}"))), + (false, _) => Err(Fail::exit(status.code().unwrap_or(1))), + } +} + +/// `berth server image`: the image of this berth, built by this machine's engine. +pub fn image(tag: Option) -> Done<()> { + let cpu = Cpu::here().ok_or_else(|| { + Fail::refused( + "berth is released for x86_64 and aarch64 alone, and this machine has neither", + ) + })?; + let context = context(cpu)?; + let tag = tag.unwrap_or_else(server_image::tag); + let mut build = Command::new("docker"); + build + .args(["build", "--tag", &tag]) + .args(context.build_args()) + .arg("-"); + fed(build, &context.bytes, "docker build")?; + out::done(format!( + "Built {tag} of berth {} ({}) with this machine's engine", + berth_api::VERSION, + &context.sha256[..12] + )); + Ok(()) +} + +/// A word for a POSIX shell, quoted when it needs to be. +fn quoted(word: &str) -> String { + let plain = !word.is_empty() + && word + .chars() + .all(|ch| ch.is_ascii_alphanumeric() || "-_.,:/=@%+".contains(ch)); + match plain { + true => word.to_owned(), + false => format!("'{}'", word.replace('\'', r"'\''")), + } +} + +fn line(words: &[String]) -> String { + words + .iter() + .map(|word| quoted(word)) + .collect::>() + .join(" ") +} + +/// The machine at `target`, reached with the user's own ssh and its settings. +struct Remote { + target: String, +} + +impl Remote { + fn command(&self, words: &[String]) -> Command { + let mut ssh = Command::new("ssh"); + // The target is never read as an option of ssh. + ssh.arg("--").arg(&self.target).arg(line(words)); + ssh + } + + /// What a command there writes, or why it failed. + fn output(&self, words: &[String]) -> Done> { + let done = self + .command(words) + .stdin(Stdio::null()) + .output() + .map_err(|error| Fail::failed(format!("ssh: {error}")))?; + match (done.status.code(), done.status.success()) { + // ssh itself: the machine was not reached. + (Some(255), _) => Err(Fail::unreachable(format!( + "ssh to {} failed: {}", + self.target, + String::from_utf8_lossy(&done.stderr).trim() + ))), + (_, true) => Ok(Ok(String::from_utf8_lossy(&done.stdout).trim().to_owned())), + (_, false) => Ok(Err(String::from_utf8_lossy(&done.stderr).trim().to_owned())), + } + } + + fn run(&self, words: &[String], input: &[u8], what: &str) -> Done<()> { + fed(self.command(words), input, what) + } +} + +fn words(list: &[&str]) -> Vec { + list.iter().map(|word| (*word).to_owned()).collect() +} + +/// The helper, run there with these arguments: it is sent on standard input. +fn helper(arguments: &[String]) -> Vec { + let mut command = words(&["python3", "-"]); + command.extend(arguments.iter().cloned()); + command +} + +/// `berth server deploy`: the server on a machine with Docker, run from this +/// berth's image built there, or from a release with `--release`. +pub fn deploy(deploy: &args::Deploy) -> Done<()> { + if deploy.target.starts_with('-') || deploy.target.is_empty() { + return Err(Fail::usage("berth server deploy takes [USER@]HOST")); + } + let remote = Remote { + target: deploy.target.clone(), + }; + let name = &deploy.container; + let found = remote.output(&words(&[ + "docker", + "container", + "inspect", + "--format", + "{{.Config.Image}}", + name, + ]))?; + let image = match &deploy.release { + Some(version) => format!("{}:{version}", server_image::BASELINE), + None => server_image::tag(), + }; + if deploy.release.is_none() { + let machine = remote + .output(&words(&["uname", "-m"]))? + .map_err(|error| Fail::failed(format!("uname -m on {}: {error}", deploy.target)))?; + let context = context(cpu_named(&machine, &deploy.target)?)?; + let held = remote.output(&words(&[ + "docker", + "image", + "inspect", + "--format", + &format!( + "{{{{index .Config.Labels \"{}\"}}}}", + server_image::label::BINARY + ), + &image, + ]))?; + if held.as_deref() == Ok(context.sha256.as_str()) { + out::note(format!( + "{image} on {} holds this berth already", + deploy.target + )); + } else { + out::note(format!( + "Building {image} on {} from this berth ({}, {:.1} MB)", + deploy.target, + &context.sha256[..12], + context.bytes.len() as f64 / 1e6 + )); + let mut build = words(&["docker", "build", "--tag", &image]); + build.extend(context.build_args()); + build.push("-".into()); + remote.run(&build, &context.bytes, "docker build")?; + } + } + let publish = |port: u16| format!("127.0.0.1:{port}:7777"); + match found { + // A server runs there: the helper makes its container again from the image. + Ok(_) if deploy.release.is_some() && deploy.port.is_some() => { + return Err(Fail::usage(format!( + "{name} runs on {} already: --release keeps its port, and --port goes with an image built from this berth", + deploy.target + ))); + } + Ok(_) => { + let mut arguments = match &deploy.release { + Some(version) => vec![name.clone(), version.clone()], + None => vec![name.clone(), "--image".into(), image.clone()], + }; + if let Some(port) = deploy.port { + arguments.extend(["--publish".into(), publish(port)]); + } + remote.run(&helper(&arguments), HELPER.as_bytes(), "the upgrade helper")?; + } + Err(_) => { + let port = deploy.port.unwrap_or(PORT); + let mut run = words(&[ + "docker", + "run", + "--detach", + "--name", + name, + "--restart", + "unless-stopped", + ]); + run.extend(["--publish".into(), publish(port)]); + for (volume, path) in [("config", "/config"), ("data", "/data")] { + run.extend(["--volume".into(), format!("{name}-{volume}:{path}")]); + } + if deploy.release.is_some() { + run.extend(["--pull".into(), "always".into()]); + } + run.push(image.clone()); + remote.run(&run, &[], "docker run")?; + out::done(format!( + "{name} runs {image} on {}, at 127.0.0.1:{port} there, with its data in the volumes {name}-config and {name}-data", + deploy.target + )); + out::hint(format!( + "A setup key for berth login: ssh {} docker exec {name} berth server setup-key", + deploy.target + )); + return Ok(()); + } + } + out::done(format!("{name} on {} runs {image}", deploy.target)); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn words_for_the_remote_shell_are_quoted() { + assert_eq!(quoted("berth:0.6.0"), "berth:0.6.0"); + assert_eq!(quoted("{{.Config.Image}}"), "'{{.Config.Image}}'"); + assert_eq!(quoted("it's"), r"'it'\''s'"); + assert_eq!(quoted(""), "''"); + let line = line(&words(&[ + "docker", + "build", + "--build-arg", + "BERTH_VERSION=0.6.0", + "-", + ])); + assert_eq!(line, "docker build --build-arg BERTH_VERSION=0.6.0 -"); + } + + #[test] + fn the_helper_it_sends_is_the_nodes() { + assert!(HELPER.contains("def build(name, version, context)")); + assert_eq!( + helper(&words(&["berth", "--image", "berth:0.6.0"])), + words(&["python3", "-", "berth", "--image", "berth:0.6.0"]) + ); + } +} diff --git a/crates/berth/src/local/mod.rs b/crates/berth/src/local/mod.rs index d510ef6..518924f 100644 --- a/crates/berth/src/local/mod.rs +++ b/crates/berth/src/local/mod.rs @@ -4,9 +4,12 @@ //! store, its API documents, and the commands that need the installation's //! own files, such as `berth ssh` with its key. +mod deploy; pub mod serve; mod store_command; +pub use deploy::server_context; + use crate::Result; use crate::cli::args::{self, Global}; use crate::cli::fail::{Fail, Result as Done}; @@ -286,5 +289,7 @@ pub fn server_command(command: args::Server) -> Done<()> { out::json(&document); Ok(()) } + args::Server::Image { tag } => deploy::image(tag), + args::Server::Deploy(deploy) => deploy::deploy(&deploy), } } diff --git a/crates/berth/src/local/serve/actions.rs b/crates/berth/src/local/serve/actions.rs index db7dd17..4ddec5f 100644 --- a/crates/berth/src/local/serve/actions.rs +++ b/crates/berth/src/local/serve/actions.rs @@ -19,7 +19,7 @@ use axum::{ use berth_api::profile::{self, Auth}; use berth_api::{ Act, Acted, Action, Argument, ErrorBody, HostAction, Resource, Rollout, RolloutReport, Runs, - Session, b64, client, code, ops::Role, + Session, b64, client, code, ops::Role, upgrade_server, }; use berth_core::{ apply, @@ -72,7 +72,7 @@ const THROUGH_CONTROLLER: &[&str] = &["secrets", UPGRADE_SERVER]; /// berth's own action that upgrades the berth server in a container on a /// node. It is offered only where the node's controller lists it, which its /// role does when it found such a container. -const UPGRADE_SERVER: &str = "upgrade-server"; +const UPGRADE_SERVER: &str = upgrade_server::ACTION; /// One of berth's own actions. pub(super) struct Builtin { @@ -180,8 +180,20 @@ fn unavailable(mut action: Action, why: Option) -> Action { action } -/// What a request to run an action carries; nothing is an empty one. +/// What a request to run an action carries; nothing is an empty one. Only +/// the action that takes bytes on its input is given them. pub(super) fn parse(body: &Bytes) -> Answer { + let act = parse_with_input(body)?; + match act.input_b64 { + Some(_) => Err(Fail::new( + code::BAD_REQUEST, + "This action takes no input_b64: only a node's upgrade-server with source=here does", + )), + None => Ok(act), + } +} + +fn parse_with_input(body: &Bytes) -> Answer { match body.is_empty() { true => Ok(Act::default()), false => serde_json::from_slice(body) @@ -399,7 +411,10 @@ pub async fn host_act( body: Bytes, ) -> Answer<(StatusCode, Json)> { state.refresh(false); - let act = parse(&body)?; + let act = match action == UPGRADE_SERVER { + true => parse_with_input(&body)?, + false => parse(&body)?, + }; let remote = remote(&state, &name)?; let profile = profile_of(&state, &name); // berth's own are described without asking the host, which they do themselves. @@ -572,7 +587,7 @@ async fn run_action( remote: &Arc, action: &Action, act: Act, - input: Option, + input: Option>, ) -> Answer { let Some(link) = &remote.link else { let problem = remote.problem.clone().unwrap_or_else(|| "no link".into()); @@ -584,7 +599,7 @@ async fn run_action( (json!(act.rows.unwrap_or(24)), json!(act.cols.unwrap_or(80))); } if let Some(input) = input { - body["input_b64"] = json!(b64(format!("{input}\n").as_bytes())); + body["input_b64"] = json!(b64(&input)); } let path = format!("/v1/actions/{}", client::encode(&action.name)); // One to attach to is the session alone, with no job to find again. @@ -1047,10 +1062,32 @@ fn secrets(state: Arc, name: String, act: Act) -> Run { name: "secrets".into(), ..Action::default() }; - run_action(&state, &remote, &action, act, Some(payload.to_string())).await + let input = format!("{payload}\n").into_bytes(); + run_action(&state, &remote, &action, act, Some(input)).await }) } +/// The arguments of `upgrade-server`. +fn upgrade_arguments() -> Vec { + vec![ + Argument { + name: "version".into(), + label: "Version".into(), + help: "The version of berth to run, as its image is tagged: this berth's own unless another is given. With source=here it is that of the berth sent.".into(), + default: berth_api::VERSION.into(), + ..Argument::default() + }, + Argument { + name: upgrade_server::SOURCE.into(), + label: "Source".into(), + help: "release pulls the published image ghcr.io/helightdev/berth:VERSION, and takes a server built from a berth back to it. here builds the image berth:VERSION on the node from the berth that runs the action, which berth run sends from Linux.".into(), + default: upgrade_server::RELEASE.into(), + values: vec![upgrade_server::RELEASE.into(), upgrade_server::HERE.into()], + ..Argument::default() + }, + ] +} + fn upgrade_server_action(seen: &Seen) -> Action { let kept = seen .host_actions @@ -1058,7 +1095,7 @@ fn upgrade_server_action(seen: &Seen) -> Action { let mut action = set_up( UPGRADE_SERVER, "Upgrade the berth server", - "Run the berth server in the container on this node from the image of this berth's version, with the same data and the same options as the running container. It is refused when the container has an option it cannot carry over, and the old container is started again when the new one does not come up. A server that upgrades itself restarts during the job.", + "Run the berth server in the container on this node from the image of this berth's version, with the same data and the same options as the running container. With source=here the node builds the image from the berth that runs the action instead, which sends itself: berth run NODE upgrade-server source=here, from berth for Linux. It is refused when the container has an option it cannot carry over, and the old container is started again when the new one does not come up. A server that upgrades itself restarts during the job.", ); action.group = "Server".into(); action.runs = Runs::Host; @@ -1067,13 +1104,7 @@ fn upgrade_server_action(seen: &Seen) -> Action { seen.name, berth_api::VERSION )); - action.arguments = vec![Argument { - name: "version".into(), - label: "Version".into(), - help: "The version of berth to run, as its image is tagged: this berth's own unless another is given.".into(), - default: berth_api::VERSION.into(), - ..Argument::default() - }]; + action.arguments = upgrade_arguments(); if let Some(kept) = kept { action.help = match kept.help.is_empty() { true => action.help, @@ -1096,21 +1127,83 @@ fn upgrade_server_action(seen: &Seen) -> Action { unavailable(action, why) } +/// What `upgrade-server` passes to the controller: its arguments, and the +/// build context with source=here. A release is asked for with the version +/// alone, as a controller of an older berth knows no source. +fn upgrade_request(act: &mut Act) -> Answer>> { + let given = |name: &str| { + act.arguments + .get(name) + .map(|value| value.trim().to_owned()) + .filter(|value| !value.is_empty()) + }; + let source = given(upgrade_server::SOURCE).unwrap_or_else(|| upgrade_server::RELEASE.into()); + let version = given("version"); + let bad = |message: String| Fail::new(code::BAD_REQUEST, message); + let input = match (source.as_str(), act.input_b64.take()) { + (upgrade_server::RELEASE, None) => None, + (upgrade_server::RELEASE, Some(_)) => { + return Err(bad( + "A release is pulled: source=release takes no build context".into(), + )); + } + (upgrade_server::HERE, Some(context)) => { + let context = berth_api::unb64(&context) + .filter(|context| !context.is_empty()) + .ok_or_else(|| bad("input_b64 is not a build context in base64".into()))?; + if version.is_none() { + return Err(bad( + "source=here needs the version of the berth it sends".into() + )); + } + Some(context) + } + (upgrade_server::HERE, None) => { + return Err(bad( + "source=here needs the build context of the berth that runs it, which berth run NODE upgrade-server source=here sends from berth for Linux".into(), + )); + } + (other, _) => return Err(bad(format!("source is release or here, not {other}"))), + }; + let version = version.unwrap_or_else(|| berth_api::VERSION.to_owned()); + act.arguments = [("version".to_owned(), version)].into(); + if input.is_some() { + act.arguments + .insert(upgrade_server::SOURCE.into(), upgrade_server::HERE.into()); + } + Ok(input) +} + fn upgrade_server(state: Arc, name: String, mut act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; - let version = act - .arguments - .get("version") - .map(|version| version.trim().to_owned()) - .filter(|version| !version.is_empty()) - .unwrap_or_else(|| berth_api::VERSION.to_owned()); - act.arguments = [("version".to_owned(), version)].into(); + let input = upgrade_request(&mut act)?; + // A controller of an older role cannot build: its action has no source. + if input.is_some() { + let profile = profile_of(&state, &name); + let listed = controller_actions(&remote, host_type::of(&profile)).await; + let builds = listed.iter().flatten().any(|kept| { + kept.name == UPGRADE_SERVER + && kept + .arguments + .iter() + .any(|argument| argument.name == upgrade_server::SOURCE) + }); + if !builds { + return Err(Fail::new( + code::PRECONDITION, + format!( + "The controller on {name} cannot build the server's image yet: apply its configuration first, with berth apply {name}" + ), + )); + } + } let action = Action { name: UPGRADE_SERVER.into(), + arguments: upgrade_arguments(), ..Action::default() }; - run_action(&state, &remote, &action, act, None).await + run_action(&state, &remote, &action, act, input).await }) } @@ -2233,6 +2326,15 @@ mod tests { let upgrade = find(&actions).unwrap(); assert!(upgrade.available, "{}", upgrade.why); assert_eq!(upgrade.arguments[0].default, berth_api::VERSION); + // Its source is the published image, or this berth sent with the request. + let source = &upgrade.arguments[1]; + assert_eq!(source.name, upgrade_server::SOURCE); + assert_eq!(source.default, upgrade_server::RELEASE); + assert_eq!( + source.values, + [upgrade_server::RELEASE, upgrade_server::HERE] + ); + assert_eq!(upgrade.role, Role::Admin); assert_eq!(upgrade.runs, Runs::Host); // Listed once: berth's own takes the place of the controller's. assert_eq!( @@ -2252,6 +2354,54 @@ mod tests { assert!(managed.why.contains(berth_api::compat::UPGRADE_DOCS)); } + #[test] + fn upgrading_from_here_takes_the_build_context_and_a_release_takes_none() { + let act = |arguments: &[(&str, &str)], input: Option<&[u8]>| Act { + arguments: arguments + .iter() + .map(|(name, value)| ((*name).to_owned(), (*value).to_owned())) + .collect(), + input_b64: input.map(b64), + ..Act::default() + }; + // A release: the version alone, as a controller of an older role takes it. + let mut release = act(&[("version", " 0.6.1 ")], None); + assert_eq!(upgrade_request(&mut release).ok().unwrap(), None); + assert_eq!( + release.arguments, + [("version".to_owned(), "0.6.1".to_owned())].into() + ); + let mut default = act(&[("source", "release")], None); + assert!(upgrade_request(&mut default).is_ok()); + assert_eq!(default.arguments["version"], berth_api::VERSION); + assert!(!default.arguments.contains_key("source")); + // Here: the context goes to the controller's input, with the version sent. + let mut here = act( + &[("version", "0.6.0"), ("source", "here")], + Some(b"context"), + ); + assert_eq!( + upgrade_request(&mut here).ok().flatten().as_deref(), + Some(&b"context"[..]) + ); + assert_eq!(here.arguments["source"], "here"); + assert_eq!(here.input_b64, None); + let refused = |mut act: Act| upgrade_request(&mut act).unwrap_err().message().to_owned(); + assert!(refused(act(&[("source", "here")], None)).contains("needs the build context")); + assert!( + refused(act(&[("source", "here")], Some(b"context"))).contains("needs the version") + ); + assert!( + refused(act(&[("version", "0.6.0")], Some(b"context"))) + .contains("takes no build context") + ); + assert!(refused(act(&[("source", "there")], None)).contains("release or here")); + // No other action takes input. + let body = Bytes::from(serde_json::to_vec(&act(&[], Some(b"x"))).unwrap()); + assert!(parse(&body).is_err()); + assert!(parse_with_input(&body).is_ok()); + } + #[test] fn a_controller_that_is_too_old_is_said_to_be_old() { let profile = diff --git a/crates/berth/src/local/serve/api.rs b/crates/berth/src/local/serve/api.rs index 1051854..b027344 100644 --- a/crates/berth/src/local/serve/api.rs +++ b/crates/berth/src/local/serve/api.rs @@ -51,6 +51,12 @@ impl Fail { pub fn new(kind: (u16, &str), message: impl Into) -> Fail { Fail(kind.0, kind.1.to_owned(), message.into()) } + + /// What it says, for a test of why something was refused. + #[cfg(test)] + pub fn message(&self) -> &str { + &self.2 + } } impl IntoResponse for Fail { @@ -1292,6 +1298,14 @@ async fn proxy( "The controller's secrets action takes the server's secrets: run the server's secrets action of the host (POST /v1/hosts/NAME/actions/secrets)", )); } + // Building the server's image runs what the caller sends, so it takes the + // admin role that the server's own action asks for. + if method == Method::POST && rest.trim_end_matches('/') == "actions/upgrade-server" { + return Err(Fail::new( + code::FORBIDDEN, + "The controller's upgrade-server replaces the berth server: run the server's upgrade-server action of the host (POST /v1/hosts/NAME/actions/upgrade-server), which an admin may", + )); + } let Some(link) = &remote.link else { let problem = remote.problem.clone().unwrap_or_else(|| "no link".into()); return Err(Fail::new(code::UNREACHABLE, problem)); diff --git a/crates/berth/src/local/serve/tests.rs b/crates/berth/src/local/serve/tests.rs index f0505f5..53e7893 100644 --- a/crates/berth/src/local/serve/tests.rs +++ b/crates/berth/src/local/serve/tests.rs @@ -770,6 +770,19 @@ async fn a_hosts_api_is_passed_through() { .await .unwrap_err(); assert_eq!(secrets.status(), 403); + // Building the server's image goes through the server's own action, which an admin runs. + let upgrade = client + .post::( + "/v1/hosts/box/controller/actions/upgrade-server", + &json!({"arguments": {"version": "0.6.0", "source": "here"}, "input_b64": "eA=="}), + ) + .await + .unwrap_err(); + assert_eq!(upgrade.status(), 403); + assert!( + upgrade.to_string().contains("which an admin may"), + "{upgrade}" + ); // A stream, both ways: what is published through it comes back on it. let mut channel = diff --git a/docs/content/docs/(guide)/reference/cli.mdx b/docs/content/docs/(guide)/reference/cli.mdx index 0fd632d..7dbf800 100644 --- a/docs/content/docs/(guide)/reference/cli.mdx +++ b/docs/content/docs/(guide)/reference/cli.mdx @@ -1478,6 +1478,33 @@ The operations, with their routes and roles. berth server api operations ``` +### berth server image + +Build the server's image from this berth with this machine's Docker engine. + +```text +berth server image [--tag TAG] +``` + +| Argument | What it is | +| --- | --- | +| `--tag TAG` | The image's tag: berth:VERSION, this berth's version, unless another is given. | + +### berth server deploy + +Run the berth server on a machine with Docker, over SSH: from an image built there from this berth, or from a release. + +```text +berth server deploy [USER@]HOST [--container NAME] [--port PORT] [--release VERSION] +``` + +| Argument | What it is | +| --- | --- | +| `[USER@]HOST` | The machine, as ssh reaches it with your own keys and settings. | +| `--container NAME` | The server's container there. By default `berth`. | +| `--port PORT` | The port the server listens on there, on 127.0.0.1. A new container gets 7777 without it. | +| `--release VERSION` | Pull the published image of this version instead, as ghcr.io/helightdev/berth:VERSION. | + ## berth serve Short for berth server run. diff --git a/docs/content/docs/(guide)/use/nodes.mdx b/docs/content/docs/(guide)/use/nodes.mdx index a71e60e..e717631 100644 --- a/docs/content/docs/(guide)/use/nodes.mdx +++ b/docs/content/docs/(guide)/use/nodes.mdx @@ -146,6 +146,45 @@ The `berth-server` recipe installs berth's server in a container named `berth`. When the server upgrades its own container, it restarts during the job. The output stays in the node's session. +### Run the server from this berth + +With `source=here`, the node builds the server's image from the berth that runs the action, so a build of your own runs there without a release: + +```bash +berth run big upgrade-server source=here +``` + +| Step | What happens | +| --- | --- | +| Check | berth asks the node for its CPU and checks that it is a static Linux program for it | +| Send | It sends its build context with the request: the Dockerfile CI builds the published image from, `requirements.txt` and the program itself | +| Build | The node's controller runs `docker build -t berth:VERSION -` with the context on standard input. The node reads it as a stream and does not write it to disk | +| Swap | The container runs from `berth:VERSION`, as for a release | +| Report | berth follows the node's session. When the server replaced is the one it talks to, it waits for the server to answer again and reads how the session ended | + +The checks refuse a berth that cannot run in the image: + +| berth | Use instead | +| --- | --- | +| A release for Linux | It qualifies | +| berth for macOS or Windows | berth for Linux of the same release | +| A build of your own with the system's C library | A static build: `cargo build --release --target x86_64-unknown-linux-musl -p berth` | +| A build for another CPU | The build for the node's CPU | + +The tag `berth:VERSION` is local to the node, so a self-built image never passes for a published one. Its labels give its version, the source `self-built` and the repository of the published images. When an image of that tag already has the same program, the build is skipped. Once the container runs, the helper removes the unused self-built images, except the two newest. + +The action needs the admin role. Its version is that of the berth sent. + +### Back to a release + +`source=release`, the default, pulls the published image again. A server that runs a self-built image goes back to `ghcr.io/helightdev/berth`, the repository its image names: + +```bash +berth run big upgrade-server version=0.7.0 +``` + +It still refuses an older version than the one running. + The action needs the machine to be a node, which needs a server of this berth. So the recipe makes the first update of a server of berth 0.5, as [Upgrading from 0.5](/docs/use/upgrading) shows. ## What berth does not do diff --git a/docs/content/docs/(guide)/use/upgrading.mdx b/docs/content/docs/(guide)/use/upgrading.mdx index dd06321..409d150 100644 --- a/docs/content/docs/(guide)/use/upgrading.mdx +++ b/docs/content/docs/(guide)/use/upgrading.mdx @@ -101,7 +101,7 @@ berth apply big Use the name `berth provisioner list` shows for it. -From then on, `berth run big upgrade-server version=0.7.0` updates the server. +From then on, `berth run big upgrade-server version=0.7.0` updates the server. `source=here` runs it from the berth you run instead, built on the node. [The berth server on a node](/docs/use/nodes#the-berth-server-on-a-node) has the details. ## What is converted, and when diff --git a/docs/openapi/berth.openapi.json b/docs/openapi/berth.openapi.json index b372b2b..2b2dfaf 100644 --- a/docs/openapi/berth.openapi.json +++ b/docs/openapi/berth.openapi.json @@ -45,6 +45,13 @@ "null" ] }, + "input_b64": { + "description": "Bytes for the standard input of an action that takes them, in base64:\nthe build context of a berth for `upgrade-server source=here`. They\ngo over HTTP when they are more than the portal carries.", + "type": [ + "string", + "null" + ] + }, "rows": { "description": "The size of the terminal, for an action that runs in one.", "format": "int32", diff --git a/docs/openapi/berth.operations.json b/docs/openapi/berth.operations.json index 374d396..0fefea4 100644 --- a/docs/openapi/berth.operations.json +++ b/docs/openapi/berth.operations.json @@ -45,6 +45,13 @@ "null" ] }, + "input_b64": { + "description": "Bytes for the standard input of an action that takes them, in base64:\nthe build context of a berth for `upgrade-server source=here`. They\ngo over HTTP when they are more than the portal carries.", + "type": [ + "string", + "null" + ] + }, "rows": { "description": "The size of the terminal, for an action that runs in one.", "format": "int32", diff --git a/provision/ansible/roles/devbox/files/berth-server-upgrade.py b/provision/ansible/roles/devbox/files/berth-server-upgrade.py index 8349cfe..24e2185 100644 --- a/provision/ansible/roles/devbox/files/berth-server-upgrade.py +++ b/provision/ansible/roles/devbox/files/berth-server-upgrade.py @@ -9,12 +9,26 @@ [--publish P]… [--volume SRC:DST]… [--add-host NAME:ADDRESS]… [--group-add GROUP]… The first two are the node's action upgrade-server: the image is the -container's own repository at VERSION. The third is the server recipe's: it -takes the image as given, or keeps the container's own with --keep-image, and -sets the options it names over those of the container. An option it names -replaces the container's one for the same port, mount point or host name. -Nothing changes when the container runs that image with those options -already. +container's own repository at VERSION. A server that runs an image built +from a berth, tagged berth:VERSION, goes back to the repository its image +names as its baseline, ghcr.io/helightdev/berth unless it names another. + +With BERTH_ARG_SOURCE=here the action builds the image instead: standard +input is the build context a berth sends of itself, with its Dockerfile, +requirements.txt and the program as berth. It is checked and handed to +`docker build -t berth:VERSION -` as it is, and nothing is unpacked. The +program has to be a static Linux program for this machine's CPU. An image +of that tag that holds the same program is not built again. + +The third is the server recipe's, and what `berth server deploy` runs once it +has built an image: it takes the image as given, or keeps the container's own +with --keep-image, and sets the options it names over those of the +container. An option it names replaces the container's one for the same +port, mount point or host name. Nothing changes when the container runs +that image with those options already. + +Once a container runs, the images built from a berth that no container +uses are removed, except the two newest. The new container gets the same name, mounts, published ports, restart policy, groups, extra hosts, network, logging, user and environment as the @@ -25,11 +39,14 @@ go to docker in a private file, never on its command line. """ import argparse +import hashlib +import io import json import os import re import subprocess import sys +import tarfile import tempfile import time @@ -40,6 +57,17 @@ # What `docker run` gives a container without --shm-size. DEFAULT_SHM = 64 * 1024 * 1024 UNCHANGED = 'is unchanged' +# Where the published images are, and the labels an image of berth has. +BASELINE = 'ghcr.io/helightdev/berth' +SELF_BUILT_REPOSITORY = 'berth' +SOURCE_LABEL = 'dev.berth.source' +BASELINE_LABEL = 'dev.berth.baseline' +BINARY_LABEL = 'dev.berth.binary-sha256' +SELF_BUILT = 'self-built' +# How many images built from a berth stay when nothing uses them. +KEPT_BUILDS = 2 +# What a build context of berth holds, and nothing else. +CONTEXT = {'Dockerfile', 'requirements.txt', 'berth'} class Refused(Exception): @@ -72,9 +100,22 @@ def version_key(text): return tuple(int(part) for part in numbers.groups()) if numbers else None +def labels_of(image): + return (image.get('Config') or {}).get('Labels') or {} + + def image_version(image, ref): - labels = (image.get('Config') or {}).get('Labels') or {} - return labels.get('org.opencontainers.image.version') or ref.rsplit(':', 1)[-1] + return labels_of(image).get('org.opencontainers.image.version') or ref.rsplit(':', 1)[-1] + + +def release_repository(image, ref): + """Where a release of the server is pulled from: the container's own + repository, or for one built from a berth the baseline its image names.""" + labels = labels_of(image) + own = repository(ref) + if labels.get(SOURCE_LABEL) == SELF_BUILT or own in (SELF_BUILT_REPOSITORY, 'sha256'): + return labels.get(BASELINE_LABEL) or BASELINE + return own # Options of a container that would change what it is if they were dropped, @@ -312,8 +353,8 @@ def upgrade(name, version): raise Refused(f'{version!r} is not a version of berth, such as 0.6.0') container = found_container(name) current = container['Config']['Image'] - target = f'{repository(current)}:{version}' old_image = inspect('image', container['Image']) + target = f'{release_repository(old_image, current)}:{version}' refuse_older(name, image_version(old_image, current), version) options, environment, command = run_options(container, old_image) print(f'Pulling {target}', flush=True) @@ -323,6 +364,7 @@ def upgrade(name, version): return replace(name, container, current, target, options, environment, command, version) print(f'{name} runs {target} now, with its data and options as they were.') + prune() def remake(name, image, pull, keep_image, asked): @@ -354,7 +396,93 @@ def remake(name, image, pull, keep_image, asked): print(f'{name} {UNCHANGED}: it runs {current} with the options asked for.') return replace(name, container, current, target, wanted, environment, command, 'update') - print(f'{name} runs {target} now, with its data and options as they were and those asked for.') + asked_too = ' and those asked for' if asked else '' + print(f'{name} runs {target} now, with its data and options as they were{asked_too}.') + prune() + + +def unfit(program, machine): + """Why `program` cannot run in the image on this machine, if it cannot: + it has to be a 64-bit Linux program for this CPU that names no + interpreter, as a static build does not.""" + if program[:4] != b'\x7fELF': + return 'it is not a Linux program' + if program[4:6] != b'\x02\x01' or len(program) < 64: + return 'it is not a 64-bit Linux program' + number = lambda at, size: int.from_bytes(program[at:at + size], 'little') # noqa: E731 + wanted = {'x86_64': 62, 'amd64': 62, 'aarch64': 183, 'arm64': 183}.get(machine) + if number(18, 2) != wanted: + return f'it is built for another CPU than this machine\'s, {machine}' + table, size, count = number(32, 8), number(54, 2), number(56, 2) + for header in range(table, table + size * count, size or 1): + if header + 4 > len(program): + return 'its program header is cut short' + if number(header, 4) == 3: + return 'it loads the system\'s C library, as it is not a static build' + return None + + +def read_context(context): + """The program in a build context of berth, once the context is one.""" + try: + with tarfile.open(fileobj=io.BytesIO(context), mode='r:*') as tar: + members = tar.getmembers() + names = sorted(member.name for member in members) + if sorted(CONTEXT) != names or not all(member.isfile() for member in members): + raise Refused(f'The build context holds {", ".join(names) or "nothing"}, ' + f'not {", ".join(sorted(CONTEXT))} alone.') + return tar.extractfile('berth').read() + except tarfile.TarError as error: + raise Refused(f'The build context is not a tar stream: {error}') from error + + +def build(name, version, context): + """The node's action with source=here: the image of the berth that sent + `context`, as berth:VERSION, and then the container from it.""" + if not VERSION.match(version): + raise Refused(f'{version!r} is not a version of berth, such as 0.6.0') + container = found_container(name) + program = read_context(context) + machine = os.uname().machine + why = unfit(program, machine) + if why: + raise Refused(f'The berth sent cannot run in a server\'s image here: {why}. ' + 'A release of berth for Linux can.') + digest = hashlib.sha256(program).hexdigest() + target = f'{SELF_BUILT_REPOSITORY}:{version}' + found = docker('image', 'inspect', target, check=False) + held = labels_of(json.loads(found.stdout)[0]).get(BINARY_LABEL) if found.returncode == 0 else None + if held == digest: + print(f'{target} holds this berth already ({digest[:12]}).', flush=True) + else: + print(f'Building {target} of berth {version} ({digest[:12]}) on this machine', flush=True) + built = subprocess.run([DOCKER, 'build', '--tag', target, '--build-arg', f'BERTH_VERSION={version}', + '--build-arg', f'BERTH_SHA256={digest}', '-'], input=context) + if built.returncode != 0: + raise Refused(f'docker build of {target} failed, so {name} runs ' + f'{container["Config"]["Image"]} as before.') + remake(name, target, False, False, []) + + +def prune(): + """Remove the images built from a berth that no container uses, except + the newest few. One a container uses stays, whatever its age.""" + listed = docker('image', 'ls', '--all', '--quiet', '--no-trunc', + '--filter', f'label={SOURCE_LABEL}={SELF_BUILT}', check=False) + ids = list(dict.fromkeys(listed.stdout.split())) if listed.returncode == 0 else [] + if len(ids) <= KEPT_BUILDS: + return + containers = docker('container', 'ls', '--all', '--quiet', '--no-trunc', check=False).stdout.split() + used = {inspect('container', container).get('Image') for container in containers} + images = sorted((inspect('image', image) for image in ids), key=lambda image: image.get('Created') or '', + reverse=True) + for image in images[KEPT_BUILDS:]: + if image['Id'] in used: + continue + tags = image.get('RepoTags') or [] + for reference in tags or [image['Id']]: + docker('image', 'rm', reference, check=False) + print(f'Removed the image {", ".join(tags) or image["Id"][:19]}, built from a berth and used by no container.') def main(): @@ -370,7 +498,13 @@ def main(): asked = [(f'--{flag}', value) for flag in ('publish', 'volume', 'add-host', 'group-add') for value in getattr(args, flag.replace('-', '_')) if value] work = lambda: remake(args.container, args.image, args.pull, args.keep_image, asked) # noqa: E731 + elif len(sys.argv) == 2 and os.environ.get('BERTH_ARG_SOURCE', '').strip() == 'here': + version = os.environ.get('BERTH_ARG_VERSION', '').strip() + work = lambda: build(sys.argv[1], version, sys.stdin.buffer.read()) # noqa: E731 elif len(sys.argv) in (2, 3): + source = os.environ.get('BERTH_ARG_SOURCE', '').strip() or 'release' + if len(sys.argv) == 2 and source != 'release': + sys.exit(f'✗ source is release or here, not {source!r}') version = sys.argv[2] if len(sys.argv) == 3 else os.environ.get('BERTH_ARG_VERSION', '') work = lambda: upgrade(sys.argv[1], version.strip()) # noqa: E731 else: diff --git a/provision/ansible/roles/devbox/vars/main.yml b/provision/ansible/roles/devbox/vars/main.yml index 647bd6a..1c193ff 100644 --- a/provision/ansible/roles/devbox/vars/main.yml +++ b/provision/ansible/roles/devbox/vars/main.yml @@ -125,11 +125,16 @@ devbox_node_tasks: group: Server user: root confirm: Run the berth server in its container from the image of another version? It restarts, and its jobs end. - help: Run the berth server in the container on this node from the image of the version given, with the same data and the same options as the running container. It is refused when the container has an option it cannot carry over, and the old container is started again when the new one does not come up. + help: Run the berth server in the container on this node from the image of the version given, with the same data and the same options as the running container. With source here, the image is built on this node from the berth that runs the action, sent on standard input. It is refused when the container has an option it cannot carry over, and the old container is started again when the new one does not come up. arguments: - name: version label: Version required: true help: The version of berth to run, as its image is tagged, such as 0.6.0. + - name: source + label: Source + default: release + values: [release, here] + help: release pulls the published image of the version. here builds berth:VERSION on this node from the berth that sends it. run: "/usr/local/lib/berth/berth-server-upgrade {{ devbox_berth_server_container | quote }}" devbox_service_dir: /usr/local/lib/berth/services diff --git a/recipes/berth-server/README.md b/recipes/berth-server/README.md index 9d95d92..92df56e 100644 --- a/recipes/berth-server/README.md +++ b/recipes/berth-server/README.md @@ -177,6 +177,47 @@ berth of the new version: berth run big upgrade-server version=0.6.0 ``` +### From your own berth + +A berth for Linux can run the server from itself, without a release. The +machine builds the image `berth:VERSION` from the berth that sends it, and +the container runs from it with its data and options. On a node: + +```bash +berth run big upgrade-server source=here +``` + +On any machine with Docker that your own `ssh` reaches: + +```bash +berth server deploy root@192.0.2.10 +``` + +| Command | Where it builds | +| --- | --- | +| `berth run NODE upgrade-server source=here` | On the node, by its controller | +| `berth server deploy [USER@]HOST` | On `HOST`, over `ssh`. It needs `python3` there for the helper | +| `berth server image` | On this machine, with no container changed | + +`deploy` takes `--container NAME` when the container is not `berth`. With +no container of that name it makes one, at `127.0.0.1:7777` or the +`--port` given, with its data in two volumes. It then says how to get a +setup key. + +Only a static Linux program for the machine's CPU goes into the image, as +a release is. berth says which release to use when it is not one. + +Back to the published image: + +```bash +berth run big upgrade-server version=0.7.0 +berth server deploy root@192.0.2.10 --release 0.7.0 +``` + +A server that runs a self-built image goes back to +`ghcr.io/helightdev/berth`. A version older than the running one is still +refused. + Do not remove the container by hand to update it. A new one from the recipe has only the recipe's options, and loses those passed by hand. diff --git a/tests/test_node.py b/tests/test_node.py index b97cee2..39703a2 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -1,11 +1,14 @@ """A node: the role's path without the desktop, its firewall helper against a fake ufw, the helper that upgrades a berth server's container against a fake docker, and the features of the Incus and Docker plugins that a node takes.""" +import hashlib +import io import json import os import shutil import subprocess import sys +import tarfile import tempfile import unittest from pathlib import Path @@ -125,6 +128,9 @@ def test_the_actions_of_a_node_run_what_it_installs(self): upgrade = actions['upgrade-server'] self.assertTrue(upgrade['run'].startswith('/usr/local/lib/berth/berth-server-upgrade ')) self.assertEqual(upgrade['arguments'][0]['name'], 'version') + # Its source is the published image unless the berth that runs it sends its own. + source = upgrade['arguments'][1] + self.assertEqual((source['name'], source['default'], source['values']), ('source', 'release', ['release', 'here'])) installed = node_named("Install the helper that upgrades a berth server's container") self.assertEqual(installed['ansible.builtin.copy']['dest'], '/usr/local/lib/berth/berth-server-upgrade') @@ -407,8 +413,10 @@ def made(args): save() print('true' if found and found['State']['Running'] else 'false') sys.exit(0 if found else 1) +def container_named(name): + return containers.get(name) or next((c for c in containers.values() if c['Id'] == name), None) if args[:2] == ['container', 'inspect']: - found = containers.get(args[2]) + found = container_named(args[2]) save() if not found: print('Error: No such container', file=sys.stderr); sys.exit(1) @@ -418,6 +426,39 @@ def made(args): if args[2] not in images: print('Error: No such image', file=sys.stderr); sys.exit(1) print(json.dumps([images[args[2]]])); sys.exit(0) +if args[:1] == ['build']: + # The context on standard input; the image gets the labels the Dockerfile sets. + context = sys.stdin.buffer.read() + tag = args[args.index('--tag') + 1] + built = dict(arg.split('=', 1) for arg in args[args.index('--tag'):] if '=' in arg) + number = data['builds'] = data.get('builds', 0) + 1 + image = {'Id': f'sha256:built{number}', 'Created': f'2026-11-{10 + number:02d}T00:00:00Z', 'RepoTags': [tag], + 'Config': dict(images['new']['Config'], Labels={ + 'org.opencontainers.image.version': built['BERTH_VERSION'], 'dev.berth.source': 'self-built', + 'dev.berth.baseline': 'ghcr.io/helightdev/berth', 'dev.berth.binary-sha256': built['BERTH_SHA256']})} + for other in images.values(): + if tag in (other.get('RepoTags') or []): + other['RepoTags'].remove(tag) + images[tag] = images[image['Id']] = image + data['context_bytes'] = len(context) + save(); sys.exit(0) +if args[:2] == ['image', 'ls']: + wanted = args[args.index('--filter') + 1].removeprefix('label=').split('=', 1) + found = {image['Id'] for image in images.values() + if (image['Config'].get('Labels') or {}).get(wanted[0]) == wanted[1]} + print('\n'.join(sorted(found))) + save(); sys.exit(0) +if args[:2] == ['container', 'ls']: + print('\n'.join(container['Id'] for container in containers.values())) + save(); sys.exit(0) +if args[:2] == ['image', 'rm']: + gone = images.get(args[2]) + if gone is None: + print('Error: No such image', file=sys.stderr); save(); sys.exit(1) + if any(container['Image'] == gone['Id'] for container in containers.values()): + print('Error: image is being used', file=sys.stderr); save(); sys.exit(1) + for key in [key for key, image in images.items() if key == args[2] or (args[2] == gone['Id'] and image['Id'] == gone['Id'])]: + images.pop(key) if args[:1] == ['pull']: images[args[1]] = images[images['new']['Id']] = images['new'] save(); sys.exit(0) @@ -489,12 +530,13 @@ def setUp(self): write_program(self.scratch / 'docker', FAKE_DOCKER) self.state = self.scratch / 'docker.json' - def helper(self, *args, found=None, old_image=None, new='0.6.0', new_runs=True, images=None, keep=False): + def helper(self, *args, found=None, old_image=None, new='0.6.0', new_runs=True, images=None, keep=False, + containers=None, source=None, context=None): if not keep: found = found or container() old_image = old_image or image('0.5.2', 'sha256:old') self.state.write_text(json.dumps({ - 'containers': {'berth': found}, + 'containers': dict({'berth': found}, **(containers or {})), 'images': dict({found['Image']: old_image, found['Config']['Image']: old_image, 'new': image(new, 'sha256:new')}, **(images or {})), 'new_runs': new_runs})) @@ -504,8 +546,12 @@ def helper(self, *args, found=None, old_image=None, new='0.6.0', new_runs=True, self.state.write_text(json.dumps(data)) env = dict(os.environ, DEVBOX_DOCKER=str(self.scratch / 'docker'), FAKE_DOCKER=str(self.state), DEVBOX_UPGRADE_SETTLE='0', BERTH_ARG_VERSION=new) - done = subprocess.run([sys.executable, str(UPGRADE), 'berth', *args], capture_output=True, text=True, - env=env, timeout=60) + env.pop('BERTH_ARG_SOURCE', None) + if source: + env['BERTH_ARG_SOURCE'] = source + done = subprocess.run([sys.executable, str(UPGRADE), 'berth', *args], capture_output=True, + input=context or b'', env=env, timeout=60) + done.stdout, done.stderr = done.stdout.decode(), done.stderr.decode() return done, json.loads(self.state.read_text()) def upgrade(self, version, found=None, new_runs=True): @@ -622,5 +668,120 @@ def test_the_recipe_never_goes_back_to_an_older_server(self): self.assertNotIn('run', [call[0] for call in data['calls']]) + # ── A server built from a berth ───────────────────────────────────────── + + def self_built(self, version='0.6.0', **labels): + found = container() + found['Config']['Image'] = f'berth:{version}' + built = image(version, 'sha256:old') + built['Config']['Labels'].update({'dev.berth.source': 'self-built', **labels}) + return found, built + + def test_a_self_built_server_goes_back_to_the_baseline_for_a_release(self): + found, built = self.self_built() + done, data = self.helper(found=found, old_image=built, new='0.6.1') + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn(['pull', 'ghcr.io/helightdev/berth:0.6.1'], data['calls']) + self.assertEqual(self.run_call(data)[-1], 'ghcr.io/helightdev/berth:0.6.1') + # The baseline its image names, when it names one. + found, built = self.self_built(**{'dev.berth.baseline': 'registry.example/berth'}) + done, data = self.helper(found=found, old_image=built, new='0.6.1') + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn(['pull', 'registry.example/berth:0.6.1'], data['calls']) + # Its own version from the release is no step back. + found, built = self.self_built() + done, data = self.helper(found=found, old_image=built, new='0.6.0') + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn(['pull', 'ghcr.io/helightdev/berth:0.6.0'], data['calls']) + + @staticmethod + def program(machine=None, interpreter=None): + """A 64-bit ELF header for this machine's CPU, static unless it names an interpreter.""" + number = {'x86_64': 62, 'aarch64': 183}.get(machine or os.uname().machine, 62) + program = bytearray(256) + program[:6] = b'\x7fELF\x02\x01' + program[18:20] = number.to_bytes(2, 'little') + program[32:40] = (64).to_bytes(8, 'little') + program[54:56] = (56).to_bytes(2, 'little') + program[56:58] = (1).to_bytes(2, 'little') + program[64:68] = (3 if interpreter else 1).to_bytes(4, 'little') + return bytes(program) + + @staticmethod + def context(program, extra=None): + out = io.BytesIO() + with tarfile.open(fileobj=out, mode='w:gz') as tar: + for name, data in [('Dockerfile', b'FROM debian:trixie-slim\n'), ('requirements.txt', b'ansible-core\n'), + ('berth', program), *(extra or [])]: + info = tarfile.TarInfo(name) + info.size = len(data) + tar.addfile(info, io.BytesIO(data)) + return out.getvalue() + + def test_here_builds_the_image_of_the_berth_sent_and_runs_it(self): + if os.uname().machine not in ('x86_64', 'aarch64'): + self.skipTest('berth is released for x86_64 and aarch64') + program = self.program() + context = self.context(program) + done, data = self.helper(source='here', context=context) + self.assertEqual(done.returncode, 0, done.stderr) + digest = hashlib.sha256(program).hexdigest() + build = next(call for call in data['calls'] if call[0] == 'build') + self.assertEqual(build[:3], ['build', '--tag', 'berth:0.6.0']) + self.assertIn(f'BERTH_SHA256={digest}', build) + self.assertIn('BERTH_VERSION=0.6.0', build) + self.assertEqual(build[-1], '-') + self.assertEqual(data['context_bytes'], len(context)) + # The tag is local: it never passes for a published image. + self.assertEqual(self.run_call(data)[-1], 'berth:0.6.0') + self.assertNotIn('pull', [call[0] for call in data['calls']]) + self.assertIn('berth runs berth:0.6.0 now', done.stdout) + # The same berth again builds nothing and changes nothing. + done, data = self.helper(source='here', context=context, keep=True) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn('holds this berth already', done.stdout) + self.assertNotIn('build', [call[0] for call in data['calls']]) + self.assertNotIn('run', [call[0] for call in data['calls']]) + # Another program of the same version is built again, as the same tag. + done, data = self.helper(source='here', context=self.context(program + b'\0'), keep=True) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn('build', [call[0] for call in data['calls']]) + self.assertEqual(self.run_call(data)[-1], 'berth:0.6.0') + + def test_here_refuses_what_cannot_run_in_the_image(self): + other = {'x86_64': 'aarch64'}.get(os.uname().machine, 'x86_64') + for context, said in [ + (self.context(self.program(interpreter=True)), 'not a static build'), + (self.context(self.program(machine=other)), 'another CPU'), + (self.context(b'#!/bin/sh\n'), 'not a Linux program'), + (self.context(self.program(), extra=[('extra.sh', b'')]), 'not Dockerfile, berth, requirements.txt alone'), + (b'not a tar stream', 'not a tar stream')]: + done, data = self.helper(source='here', context=context) + self.assertNotEqual(done.returncode, 0, said) + self.assertIn(said, done.stderr) + self.assertFalse([call for call in data['calls'] if call[0] in ('build', 'run', 'pull')], said) + done, _ = self.helper(source='there') + self.assertIn("source is release or here, not 'there'", done.stderr) + + def test_old_self_built_images_go_and_the_two_newest_and_those_in_use_stay(self): + def built(number, tag): + made = image('0.6.0', f'sha256:b{number}') + made.update({'Created': f'2026-10-{number:02d}T00:00:00Z', 'RepoTags': [tag]}) + made['Config']['Labels'].update({'dev.berth.source': 'self-built'}) + return made + old = {f'berth:0.5.{n}': built(n, f'berth:0.5.{n}') for n in range(1, 6)} + images = {**old, **{made['Id']: made for made in old.values()}} + # Another container runs the oldest of them. + other = dict(container(), Id='beef', Name='/other', Image='sha256:b1') + done, data = self.helper(images=images, containers={'other': other}) + self.assertEqual(done.returncode, 0, done.stderr) + removed = [call[2] for call in data['calls'] if call[:2] == ['image', 'rm']] + self.assertEqual(sorted(removed), ['berth:0.5.2', 'berth:0.5.3']) + self.assertIn('berth:0.5.1', data['images']) + self.assertIn('berth:0.5.5', data['images']) + # A published image is never one of them. + self.assertIn('ghcr.io/helightdev/berth:0.5.2', data['images']) + + if __name__ == '__main__': unittest.main()