diff --git a/.agents/skills/install-berth/SKILL.md b/.agents/skills/install-berth/SKILL.md index 393efe9..55399e6 100644 --- a/.agents/skills/install-berth/SKILL.md +++ b/.agents/skills/install-berth/SKILL.md @@ -96,7 +96,9 @@ then registers the provisioners. Check with `berth run big status` and The features never erase a disk. They make an Incus storage pool in a folder, or use one the user names in `devbox_incus_pool`. A server with Incus or Docker already keeps its pools, networks, trusted clients, containers and guests, and -a provisioner registered for it by hand becomes the node's. +a provisioner registered for it by hand becomes the node's when it names the +node's address. The apply names any it left alone, with the command that makes +it the node's: `berth provisioner run NAME adopt node=big`. Virtual machines need KVM. On a rented virtual server without it, set `devbox_incus_virtual_machines` to `false` in `ansible_vars`. For Sysbox beside @@ -111,7 +113,8 @@ port 7777 on this machine only, Tailscale, `tailscale serve` with TLS and a ```bash curl -fsSL https://github.com/helightdev/berth/archive/refs/heads/main.tar.gz \ - | tar xz --strip-components=1 --wildcards '*/recipes' + | tar xz --strip-components=1 --wildcards '*/recipes' \ + '*/provision/ansible/roles/devbox/files/berth-server-upgrade.py' cd recipes/berth-server sudo ./install.sh -e tailscale_auth_key=tskey-auth-EXAMPLE ``` @@ -123,11 +126,16 @@ socket. Add the node from the server with `berth host add big node --user root --type node`, then as in the node's section above. -A second run changes nothing in place. It leaves the container `berth` as it -is and starts it when it was stopped. On a node, the node's action -`upgrade-server` updates it to the version of the berth that asks. Elsewhere, -run `sudo docker pull ghcr.io/helightdev/berth`, `sudo docker rm -f berth`, -then `sudo ./install.sh`. The data in `/srv/berth` stays. +A second run changes nothing in place. The container `berth` keeps its image, +data and options. A stopped one is started. To update the +server, fetch the recipe of the new version and run +`sudo ./install.sh -e berth_server_update=true`. It makes the container again +from the new image with the options of the old one, and starts the old one +again if the new one fails. On a node, the node's action `upgrade-server` does +the same. Never update with `docker rm -f berth`: a new container loses the +options passed by hand, such as the name `incus-host` the provisioners of +berth 0.5 reach. A server of berth 0.5 is updated before its clients and hosts: +the page Upgrading from 0.5 in the docs has the order. ## Things that go wrong diff --git a/README.md b/README.md index e677992..01a587c 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,10 @@ same screens. `install.sh` installs it on macOS too, and on Windows: irm https://raw.githubusercontent.com/helightdev/berth/main/install.ps1 | iex ``` +Coming from berth 0.5? Update the server first, then the clients and the +hosts, as [Upgrading from 0.5](docs/content/docs/%28guide%29/use/upgrading.mdx) +says. + ## Quick start ```bash @@ -65,6 +69,7 @@ The documentation site is built from [`docs/`](docs) (`cd docs && npm install && - [Getting started](docs/content/docs/%28guide%29/start/install.mdx): installing berth and a first host. - [Deploying](docs/content/docs/%28guide%29/deploy/index.mdx): the install methods, and testing in QEMU. - [Using berth](docs/content/docs/%28guide%29/use/index.mdx): the terminal UI and commands, sign-ins, secrets and updates. +- [Upgrading from 0.5](docs/content/docs/%28guide%29/use/upgrading.mdx): the order, what is converted, and going back. - [Extending](docs/content/docs/%28guide%29/extend/index.mdx): plugins, services and `berth serve`. - [Reference](docs/content/docs/%28guide%29/reference/index.mdx): tools, `box`, profile fields, building and releasing. - The API reference of berthd and `berth serve`, generated from [`docs/openapi/`](docs/openapi). diff --git a/crates/berth-api/src/compat.rs b/crates/berth-api/src/compat.rs new file mode 100644 index 0000000..604148b --- /dev/null +++ b/crates/berth-api/src/compat.rs @@ -0,0 +1,119 @@ +//! What berth says when two of its programs are of different versions: a +//! client and its server, or the server and a host's controller. Each is one +//! sentence that names both versions and what to update, the same for the +//! command line and the screens. + +use crate::{API, HostType, VERSION}; + +/// The lowest `api` of a host's controller this berth can work with. +pub const NEEDS_HOST_API: u32 = 2; + +/// Where updating from an older berth is explained. +pub const UPGRADE_DOCS: &str = + "https://github.com/helightdev/berth/blob/main/docs/content/docs/(guide)/use/upgrading.mdx"; + +/// The operations of berth 0.5 that this server does not have, which only a +/// client of that version still calls. +pub const OLDER_OPS: &[&str] = &["tasks.list", "tasks.run", "tasks.choices", "actions.act"]; + +/// Why a client refuses to work with the server at `url`, which says it is +/// berth `version` of `api`; `None` when the server is new enough. +pub fn older_server(url: &str, version: &str, api: u32) -> Option { + (api < API).then(|| { + format!( + "The berth server at {url} is berth {version} (API {api}), older than this berth {VERSION} (API {API}), and would not understand what it is sent. Update the server first, then the clients and the hosts: {UPGRADE_DOCS}" + ) + }) +} + +/// What updates the controller on a host of this type. +pub fn controller_update(host: &str, host_type: HostType) -> String { + match host_type { + HostType::Workstation | HostType::Node => format!("berth apply {host}"), + HostType::Managed | HostType::Ssh => format!("berth run {host} install-controller"), + } +} + +/// What a host says when its controller is berth `version` of `api`, too +/// old for this berth; `None` when it is new enough. +pub fn older_controller( + host: &str, + version: &str, + api: u32, + host_type: HostType, +) -> Option { + (api < NEEDS_HOST_API).then(|| controller_is_older(host, version, host_type)) +} + +/// That the controller on `host`, berth `version`, is too old for this berth, +/// and what updates it. +pub fn controller_is_older(host: &str, version: &str, host_type: HostType) -> String { + format!( + "The controller on {host} is berth {version}, older than this berth {VERSION}. Update it with {}", + controller_update(host, host_type) + ) +} + +/// What the server answers a client that calls an operation it does not +/// have: for one of an older berth, that the client is older. +pub fn no_operation(name: &str) -> String { + match OLDER_OPS.contains(&name) { + true => format!( + "No operation {name}: it is a call of berth 0.5, and this server is berth {VERSION}. Update berth where this call comes from: {UPGRADE_DOCS}" + ), + false => format!("No operation {name}"), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn an_older_server_is_named_with_both_versions() { + assert_eq!(older_server("https://pier.example", VERSION, API), None); + let said = older_server("https://pier.example", "0.5.2", 1).unwrap(); + assert!(said.contains("is berth 0.5.2 (API 1)"), "{said}"); + assert!( + said.contains(&format!("this berth {VERSION} (API {API})")), + "{said}" + ); + assert!(said.contains("Update the server first"), "{said}"); + assert!(said.ends_with(UPGRADE_DOCS), "{said}"); + } + + #[test] + fn an_older_controller_says_what_updates_it() { + assert_eq!( + older_controller("studio", VERSION, NEEDS_HOST_API, HostType::Workstation), + None + ); + let said = older_controller("studio", "0.5.2", 1, HostType::Workstation).unwrap(); + assert!(said.starts_with("The controller on studio is berth 0.5.2, older than this berth")); + assert!( + said.ends_with("Update it with berth apply studio"), + "{said}" + ); + let said = older_controller("big", "0.5.2", 1, HostType::Node).unwrap(); + assert!(said.ends_with("berth apply big"), "{said}"); + let said = older_controller("pier", "0.5.2", 1, HostType::Managed).unwrap(); + assert!( + said.ends_with("berth run pier install-controller"), + "{said}" + ); + } + + #[test] + fn a_call_of_an_older_client_says_so() { + assert_eq!(no_operation("hosts.nothing"), "No operation hosts.nothing"); + assert!(no_operation("tasks.list").contains("a call of berth 0.5")); + // None of them is an operation now. + for name in OLDER_OPS { + assert!( + crate::ops::find(crate::ops::SERVE, name).is_none(), + "{name}" + ); + assert!(crate::ops::find(crate::ops::HOST, name).is_none(), "{name}"); + } + } +} diff --git a/crates/berth-api/src/lib.rs b/crates/berth-api/src/lib.rs index b84b4db..b365743 100644 --- a/crates/berth-api/src/lib.rs +++ b/crates/berth-api/src/lib.rs @@ -3,6 +3,7 @@ #[cfg(feature = "client")] pub mod client; +pub mod compat; pub mod escape; pub mod ops; #[cfg(feature = "client")] diff --git a/crates/berth-core/src/import.rs b/crates/berth-core/src/import.rs index a880361..cc0cef5 100644 --- a/crates/berth-core/src/import.rs +++ b/crates/berth-core/src/import.rs @@ -53,6 +53,18 @@ pub const NAMES_MARKER: &str = "import:names"; /// older berths kept of joining the tailnet without a key berth makes. pub const TAILNET_MARKER: &str = "import:tailnet"; +/// Every import's marker: a store that lacks one is taken in by this berth. +pub const MARKERS: &[&str] = &[ + MARKER, + PROFILES_MARKER, + KEYS_MARKER, + ACCESS_MARKER, + RESOURCES_MARKER, + PROVISIONERS_MARKER, + NAMES_MARKER, + TAILNET_MARKER, +]; + /// What a row of the import is. #[derive(Debug, Default, PartialEq, Eq)] pub struct Counts { @@ -217,17 +229,25 @@ pub(crate) fn take_in_names(store: &Store) -> Result { } /// The hosts whose profiles the tailnet's import changed: those that held a -/// pasted key, and those that were to be joined by hand. +/// pasted key, and those berth 0.5 never joined by itself. #[derive(Debug, Default, PartialEq)] pub(crate) struct TakenTailnet { pub pasted: Vec, - pub by_hand: Vec, + pub optional: Vec, } +/// The role's variable that lets an apply go on, without the tailnet, when +/// berth cannot make the host's key. +pub const TAILNET_OPTIONAL: &str = "devbox_tailnet_optional"; + /// The eighth import, in one change with its marker: every stored profile, /// of every scope, loses its pasted enrollment key and the switch of keys /// berth makes ([`berth_api::profile::take_old_tailnet`]), so no key lingers -/// in the store. Says which hosts of berth's own scope it changed. +/// in the store. berth 0.5 joined a host only with a pasted key or with that +/// switch on, which was off unless set, and an apply without either left the +/// host off the tailnet. A profile of such a host keeps that: it joins once +/// berth can make its key, and until then an apply goes on without it. Says +/// which hosts of berth's own scope it changed. pub(crate) fn take_in_tailnet(store: &Store) -> Result { let mut scopes = std::collections::BTreeSet::new(); if let crate::store::Rows::State(rows) = store.export(crate::store::Table::State)? { @@ -242,13 +262,27 @@ pub(crate) fn take_in_tailnet(store: &Store) -> Result { let Ok(mut config) = serde_json::from_slice::(&value) else { continue; }; - let Some(old) = berth_api::profile::take_old_tailnet(&mut config) else { + if !config.is_object() { continue; - }; + } + let joins = berth_api::profile::joins_tailnet(&config); + let old = berth_api::profile::take_old_tailnet(&mut config); + let optional = joins + && old.is_none_or(|old| old.by_hand && !old.pasted) + && config["ansible_vars"].get(TAILNET_OPTIONAL).is_none(); + if optional { + if !config["ansible_vars"].is_object() { + config["ansible_vars"] = json!({}); + } + config["ansible_vars"][TAILNET_OPTIONAL] = json!(true); + } + if old.is_none() && !optional { + continue; + } if scope == OWN { - match (old.pasted, old.by_hand) { + match (old.is_some_and(|old| old.pasted), optional) { (true, _) => taken.pasted.push(name.clone()), - (false, true) => taken.by_hand.push(name.clone()), + (false, true) => taken.optional.push(name.clone()), _ => {} } } @@ -270,33 +304,46 @@ pub(crate) fn take_in_tailnet(store: &Store) -> Result { /// What the tailnet's import tells the user, once, or `None` when it /// changed nothing they need to know. pub(crate) fn tailnet_message(taken: &TakenTailnet, store: &Store) -> Result> { - if taken.pasted.is_empty() && taken.by_hand.is_empty() { + if taken.pasted.is_empty() && taken.optional.is_empty() { return Ok(None); } let list = |names: &[String]| names.join(", "); + let joins = |names: &[String]| if names.len() == 1 { "joins" } else { "join" }; + let has_key = store + .names(kind::SECRET, OWN)? + .iter() + .any(|name| name == berth_api::profile::TAILNET_API_KEY); + let secret = berth_api::profile::TAILNET_API_KEY; let mut said = String::from("berth makes the keys hosts join the tailnet with itself now."); if !taken.pasted.is_empty() { said += &format!( - " The keys pasted into the profiles of {} are removed.", - list(&taken.pasted) + " The keys pasted into the profiles of {0} are removed. {0} {1} the tailnet on the next apply, unless already on it", + list(&taken.pasted), + joins(&taken.pasted) ); + said += &match has_key { + true => ".".to_owned(), + false => format!(", once the secret {secret} is set: berth secret set {secret}."), + }; + } + if !taken.optional.is_empty() { + let names = list(&taken.optional); + said += &match has_key { + true => format!( + " {names} {} the tailnet on the next apply, unless already on it.", + joins(&taken.optional) + ), + false => format!( + " {names} {} the tailnet once the secret {secret} is set: berth secret set {secret}. Until then an apply leaves {} off the tailnet, as berth 0.5 did.", + joins(&taken.optional), + if taken.optional.len() == 1 { + "it" + } else { + "them" + } + ), + }; } - let joining: Vec = taken.pasted.iter().chain(&taken.by_hand).cloned().collect(); - let has_key = store - .names(kind::SECRET, OWN)? - .iter() - .any(|name| name == berth_api::profile::TAILNET_API_KEY); - said += &format!( - " {} join the tailnet on the next apply, unless already on it", - list(&joining) - ); - said += &match has_key { - true => ".".to_owned(), - false => format!( - ", once the secret {0} is set: berth secret set {0}.", - berth_api::profile::TAILNET_API_KEY - ), - }; Ok(Some(said)) } @@ -1537,7 +1584,7 @@ mod tests { taken, TakenTailnet { pasted: vec!["studio".into()], - by_hand: vec!["dock".into()], + optional: vec!["attic".into(), "dock".into()], } ); assert!(read(&store, kind::META, TAILNET_MARKER).is_some()); @@ -1549,9 +1596,14 @@ mod tests { json_row(&store, kind::PROFILE, "pier").unwrap(), json!({"hostname": "pier"}) ); + // berth 0.5 joined neither of these by itself, and an apply goes on without the tailnet still. assert_eq!( json_row(&store, kind::PROFILE, "attic").unwrap(), - json!({"hostname": "attic", "builder": {}}) + json!({"hostname": "attic", "builder": {}, "ansible_vars": {(TAILNET_OPTIONAL): true}}) + ); + assert_eq!( + json_row(&store, kind::PROFILE, "dock").unwrap(), + json!({"hostname": "dock", "ansible_vars": {(TAILNET_OPTIONAL): true}}) ); // No key is left in any row of the store, of any scope. let crate::store::Rows::State(all) = store.export(crate::store::Table::State).unwrap() @@ -1565,7 +1617,7 @@ mod tests { let said = tailnet_message(&taken, &store).unwrap().unwrap(); assert_eq!( said, - "berth makes the keys hosts join the tailnet with itself now. The keys pasted into the profiles of studio are removed. studio, dock join the tailnet on the next apply, unless already on it, once the secret tailnet_api_key is set: berth secret set tailnet_api_key." + "berth makes the keys hosts join the tailnet with itself now. The keys pasted into the profiles of studio are removed. studio joins the tailnet on the next apply, unless already on it, once the secret tailnet_api_key is set: berth secret set tailnet_api_key. attic, dock join the tailnet once the secret tailnet_api_key is set: berth secret set tailnet_api_key. Until then an apply leaves them off the tailnet, as berth 0.5 did." ); store .write( @@ -1585,10 +1637,22 @@ mod tests { tailnet_message(&TakenTailnet::default(), &store).unwrap(), None ); + // One host joins, several join. + let one = TakenTailnet { + pasted: vec![], + optional: vec!["dock".into()], + }; + assert!( + tailnet_message(&one, &store) + .unwrap() + .unwrap() + .contains(" dock joins the tailnet on the next apply") + ); // Once is enough: the next start finds the marker and changes nothing. + let before = store.export(crate::store::Table::State).unwrap(); let found = installation(&paths); run(&paths, &found, &store).unwrap(); - assert_eq!(take_in_tailnet(&store).unwrap(), TakenTailnet::default()); + assert_eq!(store.export(crate::store::Table::State).unwrap(), before); } #[test] @@ -1759,9 +1823,11 @@ mod tests { old_hosts(&paths); let store = open(&paths); // In the layout of now: an empty key file was the user's keys and berth's. + // berth 0.5 did not join it to the tailnet by itself, and an apply still goes on without. assert_eq!( json_row(&store, kind::PROFILE, "studio").unwrap(), - json!({"hostname": "studio", "builder": {}, "access": {"address": "192.0.2.10", "auth": "ssh"}}) + json!({"hostname": "studio", "builder": {}, "access": {"address": "192.0.2.10", "auth": "ssh"}, + "ansible_vars": {(TAILNET_OPTIONAL): true}}) ); assert!(json_row(&store, kind::PROFILE, "attic").is_some()); assert_eq!( diff --git a/crates/berth-core/src/lib.rs b/crates/berth-core/src/lib.rs index a9d7224..44923d9 100644 --- a/crates/berth-core/src/lib.rs +++ b/crates/berth-core/src/lib.rs @@ -31,3 +31,23 @@ pub mod store; pub mod tailnet; pub type Result = std::result::Result>; + +/// A reason found before anything began not to begin: the host or this +/// server cannot do it now, as when a secret it needs is not set. A caller +/// says it as a refusal rather than as a request that was wrong. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Refused(pub String); + +impl std::fmt::Display for Refused { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.0) + } +} + +impl std::error::Error for Refused {} + +/// Whether `error` is a refusal before anything began: [`Refused`], or a +/// host of a type that cannot do it. +pub fn is_refused(error: &(dyn std::error::Error + 'static)) -> bool { + error.is::() || error.is::() +} diff --git a/crates/berth-core/src/provision.rs b/crates/berth-core/src/provision.rs index fcdb978..9202f61 100644 --- a/crates/berth-core/src/provision.rs +++ b/crates/berth-core/src/provision.rs @@ -186,11 +186,11 @@ pub(crate) fn secrets( pub(crate) fn needs_secrets(name: &str, missing: &[String]) -> Result<()> { match missing { [] => Ok(()), - missing => Err(format!( + missing => Err(crate::Refused(format!( "{name} needs the secret{} {}: berth secret set NAME", if missing.len() == 1 { "" } else { "s" }, missing.join(", ") - ) + )) .into()), } } @@ -529,7 +529,7 @@ pub fn prepare(paths: &Paths, store: &Store, host: &str, asked: &NewResource) -> // A new machine is on no tailnet, so a host that joins one needs a key // berth can make, before anything is created. if let Some(problem) = crate::tailnet::problem(paths, &profile.config, host) { - return Err(problem.into()); + return Err(crate::Refused(problem).into()); } let (scope, side) = profile.place.side(); // Asked again after a failure, the provisioner finds the machine by its token. diff --git a/crates/berth-core/src/seed.rs b/crates/berth-core/src/seed.rs index 3207b89..97c9ff4 100644 --- a/crates/berth-core/src/seed.rs +++ b/crates/berth-core/src/seed.rs @@ -39,7 +39,7 @@ pub fn cloud(paths: &Paths, store: &Store, name: &str, delivered: bool) -> Resul crate::host_type::guard(&host, crate::host_type::Capability::Seed)?; // Nothing is published or made for a host that could not join. if let Some(problem) = tailnet::problem(paths, &host.config, name) { - return Err(problem.into()); + return Err(crate::Refused(problem).into()); } let source = paths.source()?; let dirs = apply::plugin_dirs(&plugins::found( diff --git a/crates/berth-core/src/store/installation.rs b/crates/berth-core/src/store/installation.rs index 2242d79..7935528 100644 --- a/crates/berth-core/src/store/installation.rs +++ b/crates/berth-core/src/store/installation.rs @@ -75,8 +75,31 @@ impl Installation { } /// Open the store with its keys, finishing or undoing a key change that - /// was interrupted. + /// was interrupted. A SQLite store an older berth wrote is copied first + /// ([`keep_before_converting`]). pub fn open(&self) -> crate::Result { + let _held = self.hold_converting()?; + self.open_held() + } + + /// Held while a store is copied and converted, so that two berths copy + /// it once, before either converts it. + fn hold_converting(&self) -> crate::Result> { + match &self.target { + Target::Sqlite(file) => Ok(Some(wait_lock( + &beside(file, "convert.lock"), + "converting the store", + )?)), + Target::Postgres(_) => Ok(None), + } + } + + fn open_held(&self) -> crate::Result { + if let Target::Sqlite(file) = &self.target + && let Some(said) = keep_before_converting(file)? + { + eprintln!("berth: {said}"); + } let store = Store::connect_trusting(&self.target, self.ca.as_deref())?; let keys = self.keys(&store)?; Ok(store.with_keys(keys, Some(self.key_file.clone()))) @@ -169,13 +192,192 @@ pub fn shared(paths: &Paths) -> crate::Result> { if let Some(store) = opened.get(&key) { return Ok(store.clone()); } + let store = Arc::new(open_converting(paths)?); + opened.insert(key, store.clone()); + Ok(store) +} + +/// Open the installation's store and take in what older versions kept. A +/// store an older berth wrote is copied first, as it was, for that berth to +/// go back to: once converted it reads it no more. +pub fn open_converting(paths: &Paths) -> crate::Result { let installation = Installation::of(paths); - let store = Arc::new(installation.open()?); + // Held until the imports are done too, which convert it as well. + let _held = installation.hold_converting()?; + let store = installation.open_held()?; + if let Target::Postgres(_) = installation.target + && let Some(old) = older_writer(&store)? + { + eprintln!( + "berth: berth {} converts the store in PostgreSQL that berth {old} wrote, which berth {old} cannot read afterwards. Going back needs a dump made before, with pg_dump: {}", + berth_api::VERSION, + berth_api::compat::UPGRADE_DOCS + ); + } crate::import::run(paths, &installation, &store)?; - opened.insert(key, store.clone()); Ok(store) } +/// The version of berth that last took the store in, when it is older than +/// this one: a marker of this berth's imports is missing. +fn older_writer(store: &Store) -> crate::Result> { + let names = store.names(super::kind::META, super::OWN)?; + // A store no berth took in yet is a new one. + if !names.iter().any(|name| name == crate::import::MARKER) { + return Ok(None); + } + if crate::import::MARKERS + .iter() + .all(|marker| names.iter().any(|name| name == marker)) + { + return Ok(None); + } + let mut versions: Vec = vec![]; + for name in &names { + if let Some(value) = store.read_json(super::kind::META, super::OWN, name)? + && let Some(version) = value["version"].as_str() + { + versions.push(version.to_owned()); + } + } + versions.sort_by_key(|version| version_key(version)); + Ok(Some(versions.pop().unwrap_or_else(|| "0.5".into()))) +} + +fn version_key(version: &str) -> Vec { + version + .split(|c: char| !c.is_ascii_digit()) + .filter_map(|part| part.parse().ok()) + .collect() +} + +/// Before this berth converts a SQLite store an older one wrote, for the +/// first time, a consistent copy of it goes beside it, named for the schema +/// and the berth it had, in place of any copy made before. The key file is +/// not copied: the copy opens with the same one. Says what it did, or why +/// not, and nothing when the store needs no conversion. +pub fn keep_before_converting(file: &Path) -> crate::Result> { + use rusqlite::{Connection, OpenFlags}; + if !file.is_file() { + return Ok(None); + } + let db = Connection::open_with_flags( + file, + OpenFlags::SQLITE_OPEN_READ_WRITE | OpenFlags::SQLITE_OPEN_NO_MUTEX, + )?; + db.busy_timeout(std::time::Duration::from_secs(5))?; + let schema: i64 = db.query_row("PRAGMA user_version", [], |row| row.get(0))?; + // A file no berth wrote yet, or one of a newer berth, which opening refuses. + if schema == 0 || schema as usize > super::sqlite::MIGRATIONS.len() { + return Ok(None); + } + // A sealed store's values are not text, and then say no version. + let markers: Vec<(String, Vec)> = match db.prepare( + "SELECT name, value FROM state WHERE kind = 'meta' AND scope = '' AND name LIKE 'import:%'", + ) { + Ok(mut statement) => statement + .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))? + .collect::>()?, + // A schema older than the table of state. + Err(_) => vec![], + }; + // An older schema, or a store an older berth took in: one that has the + // first import's marker and lacks one of this berth's. A store with none + // was made by this berth, as a copy or before its first import. + let marked = |marker: &str| markers.iter().any(|(name, _)| name == marker); + let older = (schema as usize) < super::sqlite::MIGRATIONS.len() + || (marked(crate::import::MARKER) && !crate::import::MARKERS.iter().all(|m| marked(m))); + if !older { + return Ok(None); + } + let mut versions: Vec = markers + .iter() + .filter_map(|(_, value)| { + serde_json::from_slice::(value).ok()?["version"] + .as_str() + .map(str::to_owned) + }) + .collect(); + versions.sort_by_key(|version| version_key(version)); + let old = versions.pop(); + let suffix = match &old { + Some(version) => format!("berth-{version}-schema-{schema}"), + None => format!("schema-{schema}"), + }; + let copy = beside(file, &suffix); + let dir = file.parent().unwrap_or(Path::new(".")); + let wal = PathBuf::from(format!("{}-wal", file.display())); + let size = fs::metadata(file)?.len() + fs::metadata(wal).map_or(0, |wal| wal.len()); + let named = match &old { + Some(version) => format!("berth {version}"), + None => "the older berth".to_owned(), + }; + if free_space(dir).is_some_and(|free| free < size + size / 10) { + return Ok(Some(format!( + "{} has no room for a copy of the store ({size} bytes), so none was made before berth {} converts it. {named} cannot read it afterwards: {}", + dir.display(), + berth_api::VERSION, + berth_api::compat::UPGRADE_DOCS + ))); + } + // Made empty and private first, as the copy holds what the store does. + let partial = beside(file, &format!("{suffix}.partial")); + let _ = fs::remove_file(&partial); + { + use std::os::unix::fs::OpenOptionsExt; + fs::OpenOptions::new() + .write(true) + .create_new(true) + .mode(0o600) + .open(&partial)?; + } + if let Err(error) = db.execute("VACUUM INTO ?1", [partial.to_string_lossy()]) { + let _ = fs::remove_file(&partial); + return Ok(Some(format!( + "No copy of the store was made before berth {} converts it ({error}). {named} cannot read it afterwards: {}", + berth_api::VERSION, + berth_api::compat::UPGRADE_DOCS + ))); + } + drop(db); + fs::rename(&partial, ©)?; + // Only the latest copy is kept. + let prefix = format!( + "{}.", + file.file_name().unwrap_or_default().to_string_lossy() + ); + for entry in fs::read_dir(dir)?.flatten() { + let name = entry.file_name().to_string_lossy().into_owned(); + let Some(rest) = name.strip_prefix(&prefix) else { + continue; + }; + let earlier = (rest.starts_with("berth-") || rest.starts_with("schema-")) + && rest.contains("schema-") + && !rest.ends_with(".partial"); + if earlier && entry.path() != copy { + let _ = fs::remove_file(entry.path()); + } + } + Ok(Some(format!( + "before berth {} converts the store, a copy of it as {named} left it is in {}. {named} needs that copy to go back: {}", + berth_api::VERSION, + copy.display(), + berth_api::compat::UPGRADE_DOCS + ))) +} + +/// The bytes free for this user in `dir`, when the system says. +fn free_space(dir: &Path) -> Option { + use std::os::unix::ffi::OsStrExt; + let path = std::ffi::CString::new(dir.as_os_str().as_bytes()).ok()?; + let mut stat: libc::statvfs = unsafe { std::mem::zeroed() }; + // SAFETY: the path is a C string and the struct is written by the call. + if unsafe { libc::statvfs(path.as_ptr(), &mut stat) } != 0 { + return None; + } + Some(stat.f_bavail as u64 * stat.f_frsize as u64) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/berth-core/src/store/mod.rs b/crates/berth-core/src/store/mod.rs index 077f6b9..79a7799 100644 --- a/crates/berth-core/src/store/mod.rs +++ b/crates/berth-core/src/store/mod.rs @@ -9,7 +9,10 @@ mod sqlite; #[cfg(test)] mod suite; -pub use installation::{Installation, default_file, hold_lock, shared, wait_lock}; +pub use installation::{ + Installation, default_file, hold_lock, keep_before_converting, open_converting, shared, + wait_lock, +}; pub use postgres::PostgresBackend; pub use sqlite::SqliteBackend; diff --git a/crates/berth-core/tests/fixtures/store-0.5.2.db b/crates/berth-core/tests/fixtures/store-0.5.2.db new file mode 100644 index 0000000..403f5c1 Binary files /dev/null and b/crates/berth-core/tests/fixtures/store-0.5.2.db differ diff --git a/crates/berth-core/tests/upgrade.rs b/crates/berth-core/tests/upgrade.rs new file mode 100644 index 0000000..feebfc5 --- /dev/null +++ b/crates/berth-core/tests/upgrade.rs @@ -0,0 +1,138 @@ +//! A store that berth 0.5.2 wrote, with made-up hosts and fake keys, opened by +//! this berth: it is copied as it was, converted once, and the next start +//! changes nothing. + +use berth_core::paths::Paths; +use berth_core::store::{self, kind}; +use std::os::unix::fs::PermissionsExt; + +const FIXTURE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/fixtures/store-0.5.2.db"); + +fn installation() -> (tempfile::TempDir, Paths) { + let dir = tempfile::tempdir().unwrap(); + let paths = Paths { + config_dir: dir.path().join("config"), + data_dir: dir.path().join("data"), + }; + std::fs::create_dir_all(&paths.data_dir).unwrap(); + let file = paths.data_dir.join("berth.db"); + std::fs::copy(FIXTURE, &file).unwrap(); + std::fs::set_permissions(&file, std::fs::Permissions::from_mode(0o600)).unwrap(); + (dir, paths) +} + +fn schema(file: &std::path::Path) -> i64 { + let db = rusqlite::Connection::open(file).unwrap(); + db.query_row("PRAGMA user_version", [], |row| row.get(0)) + .unwrap() +} + +fn rows(store: &store::Store) -> store::Rows { + store.export(store::Table::State).unwrap() +} + +#[test] +fn a_store_of_berth_0_5_2_is_copied_then_converted_once() { + let (_dir, paths) = installation(); + let file = paths.data_dir.join("berth.db"); + assert_eq!(schema(&file), 4); + + let store = store::open_converting(&paths).unwrap(); + let copy = paths.data_dir.join("berth.db.berth-0.5.2-schema-4"); + assert!(copy.is_file()); + assert_eq!( + std::fs::metadata(©).unwrap().permissions().mode() & 0o777, + 0o600 + ); + // The copy is the store as berth 0.5.2 left it, which that berth opens. + assert_eq!(schema(©), 4); + let old = rusqlite::Connection::open(©).unwrap(); + let level: String = old + .query_row( + "SELECT CAST(value AS TEXT) FROM state WHERE kind = 'profile' AND scope = '' AND name = 'studio'", + [], + |row| row.get(0), + ) + .unwrap(); + assert!(level.contains("\"level\":\"workstation\""), "{level}"); + assert!(level.contains("tskey-auth-kFAKE"), "{level}"); + + // The store itself is converted: names of now, no pasted key, every marker. + assert!(store.schema_version().unwrap() > 4); + let studio = store + .read_json(kind::PROFILE, store::OWN, "studio") + .unwrap() + .unwrap(); + assert_eq!( + berth_api::profile::host_type(&studio), + berth_api::HostType::Workstation + ); + assert!(studio.get("level").is_none() && studio.get("tailscale_auth_key").is_none()); + let pier = store + .read_json(kind::PROFILE, store::OWN, "pier") + .unwrap() + .unwrap(); + assert_eq!( + berth_api::profile::host_type(&pier), + berth_api::HostType::Managed + ); + // dock had berth 0.5 make its key, so it joins as before; pier never joined. + let dock = store + .read_json(kind::PROFILE, store::OWN, "dock") + .unwrap() + .unwrap(); + assert!( + dock["ansible_vars"] + .get("devbox_tailnet_optional") + .is_none(), + "{dock}" + ); + assert_eq!(pier["ansible_vars"]["devbox_tailnet_optional"], true); + let names = store.names(kind::META, store::OWN).unwrap(); + for marker in berth_core::import::MARKERS { + assert!(names.iter().any(|name| name == marker), "{marker}"); + } + let secrets = store.names(kind::SECRET, store::OWN).unwrap(); + assert!(secrets.iter().any(|name| name == "tailnet_api_key")); + let converted = rows(&store); + drop(store); + + // The next start finds nothing to do, and makes no copy. + std::fs::remove_file(©).unwrap(); + let store = store::open_converting(&paths).unwrap(); + assert_eq!(rows(&store), converted); + assert!(!copy.exists()); + let left: Vec = std::fs::read_dir(&paths.data_dir) + .unwrap() + .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()) + .filter(|name| name.contains("schema-")) + .collect(); + assert!(left.is_empty(), "{left:?}"); +} + +#[test] +fn only_the_latest_copy_is_kept() { + let (_dir, paths) = installation(); + let earlier = paths.data_dir.join("berth.db.berth-0.4.1-schema-3"); + std::fs::write(&earlier, b"an earlier copy").unwrap(); + let said = store::keep_before_converting(&paths.data_dir.join("berth.db")) + .unwrap() + .unwrap(); + assert!( + said.contains("berth 0.5.2 needs that copy to go back"), + "{said}" + ); + assert!(!earlier.exists()); + assert!( + paths + .data_dir + .join("berth.db.berth-0.5.2-schema-4") + .is_file() + ); + // A store of this berth needs no copy. + store::open_converting(&paths).unwrap(); + assert_eq!( + store::keep_before_converting(&paths.data_dir.join("berth.db")).unwrap(), + None + ); +} diff --git a/crates/berth-page/src/app.rs b/crates/berth-page/src/app.rs index 9fd6f81..f259848 100644 --- a/crates/berth-page/src/app.rs +++ b/crates/berth-page/src/app.rs @@ -150,6 +150,8 @@ pub struct App { quit: bool, /// An action of the open host to begin once the server has listed them. pending: Option<&'static str>, + /// Why the server is too old for these screens, once it said its version. + older: Rc>>, } impl App { @@ -171,7 +173,9 @@ impl App { }) .unwrap_or_default(); // A server on this machine is never kept: it is found again each run. - if !device::local(&server) { + // Another server named for this run, without its token, leaves the + // kept server and token alone until it signs in. + if !device::local(&server) && !token.is_empty() { device::store(Kept::Server, &server); device::store(Kept::Token, &token); } @@ -219,6 +223,7 @@ impl App { wanted: None, quit: false, pending: None, + older: Rc::default(), }; // A setup key signs in with the token it collects, whatever was kept. if let (Some(key), false) = (linked.setup_key, app.server.is_empty()) { @@ -281,6 +286,15 @@ impl App { self.retry = None; match Portal::open(&self.server, &self.token) { Ok(portal) => { + // A server older than these screens would not understand what they send. + let (older, url) = (self.older.clone(), self.server.clone()); + portal.call("version", None, json!({}), Value::Null, move |result| { + if let Ok(said) = result { + let version = said["version"].as_str().unwrap_or(""); + let api = said["api"].as_u64().unwrap_or(0) as u32; + *older.borrow_mut() = berth_api::compat::older_server(&url, version, api); + } + }); // Who the token is here decides what the page offers. let admin = Admin::new(portal.clone(), self.data.clone()); admin.load(); @@ -300,6 +314,13 @@ impl App { /// Back to the token dialog, forgetting the token. fn sign_out(&mut self, why: &str) { + self.ask_token(why); + device::store(Kept::Token, ""); + } + + /// Back to the token dialog. The kept token stays until the one typed + /// next replaces it. + fn ask_token(&mut self, why: &str) { self.portal = None; self.admin = None; *self.data.borrow_mut() = Data::default(); @@ -311,7 +332,6 @@ impl App { self.host.disconnect(); *self.hosts.borrow_mut() = Hosts::default(); self.token.clear(); - device::store(Kept::Token, ""); self.typing = Input::default(); self.refused = why.to_owned(); self.screen = Screen::Token; @@ -342,6 +362,7 @@ impl App { match installed.tick() { Outcome::SignIn(token) => { self.token = token; + device::store(Kept::Server, &self.server); device::store(Kept::Token, &self.token); self.connect(); } @@ -365,6 +386,16 @@ impl App { } else if !self.status.is_empty() && self.said.1.elapsed() >= SAID_FOR { self.status.clear(); } + if let Some(older) = self.older.borrow_mut().take() { + self.portal = None; + self.admin = None; + self.host.disconnect(); + *self.hosts.borrow_mut() = Hosts::default(); + self.retry = None; + self.refused = older; + self.screen = Screen::Token; + return; + } match self.portal.as_ref().map(Portal::link) { Some(Link::Open) => { self.proven = true; @@ -372,13 +403,14 @@ impl App { self.status.clear(); } } - // A token that never opened the portal is asked for again; one that - // has is the right one, and the server is only away. - Some(Link::Refused) if !self.proven && !self.local() => { - self.sign_out("The server did not open its portal for that token."); + // A token the server refused is asked for again. The kept one stays + // until another is given or the user signs out: a server that does + // not answer, or not yet, never costs the token. + Some(Link::Refused) if !self.local() => { + self.ask_token("The server did not take that token. Enter another, or sign out."); return; } - Some(Link::Refused | Link::Lost) => { + Some(Link::Refused | Link::Unreached | Link::Lost) => { self.portal = None; self.host.disconnect(); self.retry = Some(Instant::now()); @@ -666,8 +698,10 @@ impl App { ], State::Old(version) => vec![ note( - format!( - "The host's tools ({version}) are older than this page needs. Applying its configuration, where berth serve runs, brings them up to date." + berth_api::compat::controller_is_older( + &self.name, + version, + self.host.host_type, ), theme::WARN, ), @@ -1420,7 +1454,15 @@ impl App { let mut lines: Vec = vec![]; for host in hosts.list.iter() { let name = host["name"].as_str().unwrap_or(""); + let older = host["api"] + .as_u64() + .is_some_and(|api| api < u64::from(berth_api::compat::NEEDS_HOST_API)); let state = match (host["reachable"].as_bool(), host["version"].as_str()) { + // A controller too old for this berth is updated before it is worked with. + (Some(true), Some(version)) if older => Span::styled( + format!("● {version} old controller"), + Style::new().fg(theme::WARN), + ), (Some(true), Some(version)) => { Span::styled(format!("● {version}"), Style::new().fg(theme::GOOD)) } diff --git a/crates/berth-page/src/portal.rs b/crates/berth-page/src/portal.rs index 6f88998..e70be8c 100644 --- a/crates/berth-page/src/portal.rs +++ b/crates/berth-page/src/portal.rs @@ -12,8 +12,11 @@ use std::rc::{Rc, Weak}; pub enum Link { Connecting, Open, - /// It closed before it was open: a wrong token, or nobody there. + /// The server refused the token. Refused, + /// It closed before it was open, and not by the server's refusal: nobody + /// answered there, or not in time. + Unreached, /// It was open and is not any more. Lost, } @@ -89,6 +92,10 @@ impl Inner { } } Said::Closed => self.closed(), + Said::Refused => { + self.link.set(Link::Refused); + self.closed(); + } } } @@ -115,7 +122,8 @@ impl Inner { fn closed(&self) { self.link.set(match self.link.get() { Link::Open | Link::Lost => Link::Lost, - _ => Link::Refused, + Link::Refused => Link::Refused, + Link::Connecting | Link::Unreached => Link::Unreached, }); let calls: Vec = self .calls @@ -372,9 +380,12 @@ mod tests { } #[test] - fn a_socket_that_never_opens_is_refused() { + fn a_socket_that_never_opens_is_unreached_unless_the_server_refused_it() { let (wire, portal) = portal(); wire.say(Said::Closed); + assert_eq!(portal.link(), Link::Unreached); + let (wire, portal) = self::portal(); + wire.say(Said::Refused); assert_eq!(portal.link(), Link::Refused); } } diff --git a/crates/berth-page/src/settings.rs b/crates/berth-page/src/settings.rs index 2ebe87b..e98649d 100644 --- a/crates/berth-page/src/settings.rs +++ b/crates/berth-page/src/settings.rs @@ -112,28 +112,55 @@ impl Settings { private_write(&file, text.as_bytes()) } - /// Take in the server and token berth-remote kept in `old`, once: when - /// berth has no server of its own, they become berth's, and the old file - /// goes. Says which server was taken in. - pub fn take_in(&self, old: Option<&Path>) -> std::io::Result> { + /// Take in the server and token berth-remote kept in `old`, once. When + /// berth has no server, or the same one without a token, they become + /// berth's and the old file goes. When berth uses another server, or the + /// same with another token, the file stays, and that is said once. + pub fn take_in(&self, old: Option<&Path>) -> std::io::Result { let Some(old) = old else { - return Ok(None); + return Ok(TakenIn::Nothing); }; let file = old.join("settings.json"); let Ok(bytes) = std::fs::read(&file) else { - return Ok(None); + return Ok(TakenIn::Nothing); }; let kept: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null); let text = |key: &str| kept[key].as_str().unwrap_or("").trim().to_owned(); let (server, token) = (text("server"), text("token")); - let taken = match (server.is_empty(), self.get(SERVER)) { - (false, None) => { + // A file that names no server holds nothing to take in, and is left as it is. + if server.is_empty() { + return Ok(TakenIn::Nothing); + } + let same = + |one: &str, other: &str| one.trim_end_matches('/') == other.trim_end_matches('/'); + let taken = match (self.get(SERVER), self.get(TOKEN)) { + (None, _) => { self.set(&[(SERVER, &server), (TOKEN, &token)])?; - Some(server) + TakenIn::Taken(server) + } + (Some(ours), None) if same(&ours, &server) && !token.is_empty() => { + self.set(&[(TOKEN, &token)])?; + TakenIn::Taken(server) + } + // Its contents are berth's already. + (Some(ours), ours_token) + if same(&ours, &server) + && (token.is_empty() || ours_token.as_deref() == Some(&token)) => + { + TakenIn::Same + } + (Some(ours), _) => { + if self.get(NOTED).as_deref() == Some(server.as_str()) { + return Ok(TakenIn::Nothing); + } + self.set(&[(NOTED, &server)])?; + return Ok(TakenIn::Left { + theirs: server, + ours, + file, + }); } - _ => None, }; - // Its token is in berth's settings now, or berth uses another server. for gone in [file.clone(), file.with_extension("json.partial")] { match std::fs::remove_file(&gone) { Err(error) if error.kind() != std::io::ErrorKind::NotFound => return Err(error), @@ -145,6 +172,28 @@ impl Settings { } } +/// The setting that remembers which server of berth-remote's settings was +/// said to be left, so it is said once. +const NOTED: &str = "berth_remote_noted"; + +/// What became of berth-remote's settings. +#[derive(Debug, PartialEq, Eq)] +pub enum TakenIn { + /// There were none, or they were said to be left before. + Nothing, + /// berth uses their server and token now; the old file is gone. + Taken(String), + /// berth had them already; the old file is gone. + Same, + /// berth uses another server, or the same with another token: the file + /// stays where it is. + Left { + theirs: String, + ours: String, + file: PathBuf, + }, +} + /// Make the folder of the settings, which only its owner can open. fn private_dir(dir: &Path) -> std::io::Result<()> { let mut builder = std::fs::DirBuilder::new(); @@ -218,38 +267,96 @@ mod tests { assert_eq!(left, "{not json"); } + fn remote(scratch: &Path, text: &str) -> PathBuf { + let old = scratch.join("berth-remote"); + std::fs::create_dir_all(&old).unwrap(); + std::fs::write(old.join("settings.json"), text).unwrap(); + old + } + #[test] fn berth_remotes_server_is_taken_in_once() { let scratch = tempfile::tempdir().unwrap(); - let old = scratch.path().join("berth-remote"); - std::fs::create_dir_all(&old).unwrap(); - std::fs::write( - old.join("settings.json"), + let old = remote( + scratch.path(), r#"{"server": "https://studio.example", "token": "s3cret"}"#, - ) - .unwrap(); + ); let settings = Settings::new(Some(scratch.path().join("berth"))); assert_eq!( - settings.take_in(Some(&old)).unwrap().as_deref(), - Some("https://studio.example") + settings.take_in(Some(&old)).unwrap(), + TakenIn::Taken("https://studio.example".into()) ); assert_eq!(settings.get(TOKEN).as_deref(), Some("s3cret")); assert!(!old.exists()); - assert_eq!(settings.take_in(Some(&old)).unwrap(), None); + assert_eq!(settings.take_in(Some(&old)).unwrap(), TakenIn::Nothing); + } - // A berth that uses a server already keeps it. - std::fs::create_dir_all(&old).unwrap(); - std::fs::write( - old.join("settings.json"), - r#"{"server": "https://dock.example", "token": "other"}"#, - ) - .unwrap(); - assert_eq!(settings.take_in(Some(&old)).unwrap(), None); + #[test] + fn berth_remotes_token_for_berths_own_server_is_taken_in() { + let scratch = tempfile::tempdir().unwrap(); + let settings = Settings::new(Some(scratch.path().join("berth"))); + settings.set(&[(SERVER, "https://studio.example")]).unwrap(); + let old = remote( + scratch.path(), + r#"{"server": "https://studio.example/", "token": "s3cret"}"#, + ); assert_eq!( - settings.get(SERVER).as_deref(), - Some("https://studio.example") + settings.take_in(Some(&old)).unwrap(), + TakenIn::Taken("https://studio.example/".into()) + ); + assert_eq!(settings.get(TOKEN).as_deref(), Some("s3cret")); + assert!(!old.exists()); + + // The same server and token are berth's already: the file goes. + let old = remote( + scratch.path(), + r#"{"server": "https://studio.example", "token": "s3cret"}"#, ); - assert!(!old.join("settings.json").exists()); + assert_eq!(settings.take_in(Some(&old)).unwrap(), TakenIn::Same); + assert!(!old.exists()); + } + + #[test] + fn berth_remotes_other_server_is_left_and_said_once() { + let scratch = tempfile::tempdir().unwrap(); + let settings = Settings::new(Some(scratch.path().join("berth"))); + settings + .set(&[(SERVER, "https://studio.example"), (TOKEN, "mine")]) + .unwrap(); + for other in [ + r#"{"server": "https://dock.example", "token": "other"}"#, + r#"{"server": "https://studio.example", "token": "other"}"#, + ] { + let old = remote(scratch.path(), other); + let file = old.join("settings.json"); + let taken = settings.take_in(Some(&old)).unwrap(); + assert!( + matches!(&taken, TakenIn::Left { file: left, ours, .. } + if *left == file && ours == "https://studio.example"), + "{taken:?}" + ); + // The file stays, and is not spoken of again. + assert!(file.exists()); + assert_eq!(settings.take_in(Some(&old)).unwrap(), TakenIn::Nothing); + assert_eq!(settings.get(TOKEN).as_deref(), Some("mine")); + assert_eq!( + settings.get(SERVER).as_deref(), + Some("https://studio.example") + ); + std::fs::remove_dir_all(&old).unwrap(); + } + } + + #[test] + fn a_file_of_berth_remote_with_no_server_is_left_as_it_is() { + let scratch = tempfile::tempdir().unwrap(); + let settings = Settings::new(Some(scratch.path().join("berth"))); + for text in ["{not json", r#"{"token": "s3cret"}"#] { + let old = remote(scratch.path(), text); + assert_eq!(settings.take_in(Some(&old)).unwrap(), TakenIn::Nothing); + assert!(old.join("settings.json").exists()); + } + assert_eq!(settings.get(TOKEN), None); } #[cfg(unix)] diff --git a/crates/berth-page/src/socket.rs b/crates/berth-page/src/socket.rs index 186174d..122b00f 100644 --- a/crates/berth-page/src/socket.rs +++ b/crates/berth-page/src/socket.rs @@ -16,6 +16,9 @@ pub enum Said { Bytes(Vec), /// The socket is closed, or never opened. Closed, + /// The server answered the handshake and refused what was shown, as a + /// wrong token: 401 or 403. + Refused, } /// An open socket; dropping it closes it. @@ -98,11 +101,14 @@ async fn run( mut out: UnboundedReceiver, said: mpsc::Sender, ) { + // Whether the server itself refused the handshake, beside why it failed. let opened = async { - let stream = net::connect(&target).await?; + let stream = net::connect(&target) + .await + .map_err(|error| (false, error))?; client::websocket_at(stream, &target.authority, &target.path, &token) .await - .map_err(|error| net::describe(&error)) + .map_err(|error| (matches!(error.status(), 401 | 403), net::describe(&error))) }; let opened = tokio::select! { opened = tokio::time::timeout(CONNECT, opened) => opened.ok(), @@ -113,9 +119,16 @@ async fn run( } } => return, }; - let Some(Ok(socket)) = opened else { - let _ = said.send(Said::Closed); - return; + let socket = match opened { + Some(Ok(socket)) => socket, + Some(Err((true, _))) => { + let _ = said.send(Said::Refused); + return; + } + _ => { + let _ = said.send(Said::Closed); + return; + } }; let _ = said.send(Said::Open); let (mut write, mut read) = socket.split(); diff --git a/crates/berth-tui/src/host.rs b/crates/berth-tui/src/host.rs index 19bdd6c..84af691 100644 --- a/crates/berth-tui/src/host.rs +++ b/crates/berth-tui/src/host.rs @@ -11,7 +11,7 @@ use std::collections::BTreeMap; use std::time::{Duration, Instant}; /// The lowest `api` of a host this berth can work with. -pub const NEEDS_API: u32 = 2; +pub use berth_api::compat::NEEDS_HOST_API as NEEDS_API; /// How often the overview asks for stats and sessions again. const POLL: Duration = Duration::from_secs(10); /// Lines of a process session's transcript kept for scrolling back. diff --git a/crates/berth/src/cli/args.rs b/crates/berth/src/cli/args.rs index 1ec29bf..7edcf76 100644 --- a/crates/berth/src/cli/args.rs +++ b/crates/berth/src/cli/args.rs @@ -171,7 +171,7 @@ pub enum Command { Logout, /// Install the latest release in this program's place. Update(Update), - /// The version of this berth. + /// The version of this berth, and of the server it uses. Version, /// The completions of a shell, to source from its start-up file. Completion { @@ -920,6 +920,10 @@ pub struct ServerRun { /// Allow an address other than this machine's own. #[arg(long, requires = "listen")] pub expose: bool, + /// Taken and left unused: the page in the browser of berth 0.5 is gone, + /// and containers and units of that version still name it. + #[arg(long, hide = true)] + pub web: bool, } #[cfg(target_os = "linux")] diff --git a/crates/berth/src/cli/client.rs b/crates/berth/src/cli/client.rs index f3f4717..3206d75 100644 --- a/crates/berth/src/cli/client.rs +++ b/crates/berth/src/cli/client.rs @@ -30,6 +30,31 @@ pub fn settings() -> Settings { Settings::new(settings::folder(|name| std::env::var(name).ok())) } +/// Take in what berth-remote kept, once, before a server is looked for, and +/// say what became of it. Nothing is lost: its file goes only once berth has +/// what was in it. +pub fn take_in_remote(kept: &Settings, old: Option<&std::path::Path>) { + use super::out; + match kept.take_in(old) { + Ok(settings::TakenIn::Taken(server)) => { + out::note(format!("berth-remote's server is berth's now: {server}")) + } + Ok(settings::TakenIn::Left { theirs, ours, file }) => out::note(format!( + "berth-remote's settings in {} name the server {theirs}, and berth uses {ours}: the file is left as it is. To use {theirs}: berth login {theirs}", + file.display() + )), + Ok(settings::TakenIn::Nothing | settings::TakenIn::Same) => {} + Err(error) => out::warn(format!( + "berth-remote's settings were not taken in, and are left as they are: {error}" + )), + } +} + +/// berth-remote's folder of settings for this user. +pub fn remote_folder() -> Option { + settings::old_folder(|name| std::env::var(name).ok()) +} + fn variable(name: &str) -> Option { std::env::var(name) .ok() @@ -65,12 +90,16 @@ fn token(global: &Global, url: &str, kept: &Settings) -> Result { /// The server this run uses, if one is named: by a flag, the environment, /// or the settings. pub fn named(global: &Global) -> Option { + named_in(global, &settings()) +} + +fn named_in(global: &Global, kept: &Settings) -> Option { global .server .clone() .filter(|server| !server.trim().is_empty()) .or_else(|| variable("BERTH_SERVER")) - .or_else(|| settings().get(settings::SERVER)) + .or_else(|| kept.get(settings::SERVER)) .map(|server| berth_page::device::address(&server)) } @@ -78,9 +107,15 @@ impl Client { /// Reach the server this run uses. pub fn open(global: &Global) -> Result { let kept = settings(); - let (url, token, finish) = match named(global) { + take_in_remote(&kept, remote_folder().as_deref()); + Client::open_in(global, &kept) + } + + /// Reach the server this run uses, with these settings. + pub fn open_in(global: &Global, kept: &Settings) -> Result { + let (url, token, finish) = match named_in(global, kept) { Some(url) => { - let token = token(global, &url, &kept)?; + let token = token(global, &url, kept)?; (url, token, None) } None => match crate::local::server(false)? { @@ -92,15 +127,44 @@ impl Client { } }, }; - let portal = connect(&url, &token)?; - Ok(Client { + let in_process = finish.is_some(); + let client = Client { + portal: connect(&url, &token)?, url, - portal, finish, sudo_ready: Default::default(), + }; + // One in this process is this berth; any other may be older. + if !in_process { + let (version, api) = client.server_version()?; + if let Some(older) = berth_api::compat::older_server(&client.url, &version, api) { + return Err(Fail::refused(older)); + } + } + Ok(client) + } + + /// Reach the server this run uses, whatever its version, only to ask it. + pub fn open_any(global: &Global, kept: &Settings) -> Result { + let url = + named_in(global, kept).ok_or_else(|| Fail::unreachable("No berth server is set"))?; + let token = token(global, &url, kept)?; + Ok(Client { + portal: connect(&url, &token)?, + url, + finish: None, + sudo_ready: Default::default(), }) } + /// The server's version and the number of its API. + pub fn server_version(&self) -> Result<(String, u32)> { + let said = self.call("version", json!({}), None)?; + let version = said["version"].as_str().unwrap_or("").to_owned(); + let api = said["api"].as_u64().unwrap_or(0) as u32; + Ok((version, api)) + } + /// Whether the server runs in this process, and ends with it. pub fn in_process(&self) -> bool { self.url == berth_page::device::HERE diff --git a/crates/berth/src/cli/host.rs b/crates/berth/src/cli/host.rs index 848eb38..0132af9 100644 --- a/crates/berth/src/cli/host.rs +++ b/crates/berth/src/cli/host.rs @@ -48,6 +48,10 @@ pub fn command(global: &Global, command: Host) -> Result<()> { /// The state of a host in a word or two, as the home list shows it. fn state_of(host: &Value) -> String { if host["reachable"] == true { + let api = host["api"].as_u64().unwrap_or(u64::MAX); + if api < u64::from(berth_api::compat::NEEDS_HOST_API) { + return styled("old controller", Style::Warn); + } return match &host["behind"] { Value::Null => styled("answers", Style::Good), _ => styled("behind", Style::Warn), diff --git a/crates/berth/src/cli/mod.rs b/crates/berth/src/cli/mod.rs index e07e56b..05f5c24 100644 --- a/crates/berth/src/cli/mod.rs +++ b/crates/berth/src/cli/mod.rs @@ -315,13 +315,7 @@ fn run(cli: Cli) -> Result<()> { Command::Login(login) => login::login(&global, &login), Command::Logout => login::logout(), Command::Update(update) => crate::update::command(&update), - Command::Version => { - out::data( - &serde_json::json!({"version": berth_api::VERSION, "api": berth_api::API}), - || println!("berth {}", berth_api::VERSION), - ); - Ok(()) - } + Command::Version => version(&global), Command::Completion { shell } => { let mut tree = self::command(); clap_complete::generate(shell, &mut tree, "berth", &mut std::io::stdout()); @@ -335,6 +329,44 @@ fn run(cli: Cli) -> Result<()> { } } +/// `berth version`: this berth's, and that of the server it is set to use, +/// which an older server still answers. +fn version(global: &args::Global) -> Result<()> { + use serde_json::json; + let server = client::named(global).map(|url| { + match Client::open_any(global, &client::settings()) + .and_then(|client| client.server_version()) + { + Ok((version, api)) => json!({"url": url, "version": version, "api": api}), + Err(fail) => json!({"url": url, "error": fail.message}), + } + }); + let mut said = json!({"version": berth_api::VERSION, "api": berth_api::API}); + if let Some(server) = &server { + said["server"] = server.clone(); + } + out::data(&said, || { + println!("berth {} (API {})", berth_api::VERSION, berth_api::API); + if let Some(server) = &server { + let url = server["url"].as_str().unwrap_or(""); + match server["version"].as_str() { + Some(version) => { + println!("server {url}: berth {version} (API {})", server["api"]); + let api = server["api"].as_u64().unwrap_or(0) as u32; + if let Some(older) = berth_api::compat::older_server(url, version, api) { + out::warn(older); + } + } + None => println!( + "server {url}: not reached ({})", + server["error"].as_str().unwrap_or("") + ), + } + } + }); + Ok(()) +} + #[cfg(test)] mod tests { use super::*; @@ -383,6 +415,20 @@ mod tests { assert_eq!(format!("{:?}", cli.command), format!("{:?}", cli2.command)); } + #[cfg(target_os = "linux")] + #[test] + fn the_command_line_of_a_server_of_0_5_still_starts_it() { + // Containers and units of berth 0.5 run the server with --web. + let cli = parse("serve --listen 0.0.0.0:7777 --expose --web").unwrap(); + assert!(matches!(cli.command, Some(Command::Serve(serve)) if serve.web && serve.expose)); + let help = command() + .find_subcommand_mut("serve") + .unwrap() + .render_help() + .to_string(); + assert!(!help.contains("--web"), "{help}"); + } + #[test] fn a_host_may_have_the_name_of_an_action() { let cli = parse("host show list").unwrap(); diff --git a/crates/berth/src/cli/old.rs b/crates/berth/src/cli/old.rs index 549758a..bb90335 100644 --- a/crates/berth/src/cli/old.rs +++ b/crates/berth/src/cli/old.rs @@ -147,10 +147,46 @@ fn is_action(noun: &str, word: &str) -> bool { .is_some_and(|thing| thing.find_subcommand(word).is_some()) } +/// The table of the old spellings on the page about upgrading, between its +/// markers, as a test writes it. +#[cfg(test)] +fn table() -> String { + let mut out = String::from("| berth 0.5 | Now |\n| --- | --- |\n"); + for (old, new) in OLD { + out += &format!("| `berth {}` | `{new}` |\n", old.join(" ")); + } + out +} + #[cfg(test)] mod tests { use super::*; + /// The page about upgrading from 0.5, from the repository's root. + const PAGE: &str = "docs/content/docs/(guide)/use/upgrading.mdx"; + const START: &str = "{/* old-spellings:start */}\n"; + const END: &str = "{/* old-spellings:end */}"; + + #[test] + fn the_upgrade_page_lists_the_old_spellings() { + let file = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../..") + .join(PAGE); + let text = std::fs::read_to_string(&file).unwrap(); + let (before, rest) = text.split_once(START).expect("the start marker"); + let (_, after) = rest.split_once(END).expect("the end marker"); + // Blank lines around it, so that MDX reads it as a table. + let page = format!("{before}{START}\n{}\n{END}{after}", table()); + if std::env::var_os("BERTH_UPDATE_DOCS").is_some() { + std::fs::write(&file, &page).unwrap(); + return; + } + assert!( + text == page, + "{PAGE} is stale: write it again with BERTH_UPDATE_DOCS=1 cargo test -p berth" + ); + } + fn said(line: &str) -> Option { let args: Vec = std::iter::once("berth") .chain(line.split_whitespace()) diff --git a/crates/berth/src/cli/setup.rs b/crates/berth/src/cli/setup.rs index 03580e0..428a5cc 100644 --- a/crates/berth/src/cli/setup.rs +++ b/crates/berth/src/cli/setup.rs @@ -255,6 +255,10 @@ fn stopped(stop: Stop) -> Fail { question.label, question.flag )), Stop::Problems(problems) => { + // Only a secret the server lacks stops it: the command line was right. + let refused = problems + .iter() + .all(|problem| problem.field == Some(berth_tui::fields::Field::Tailscale)); let said: Vec = problems .iter() .map(|problem| match problem.field { @@ -269,7 +273,11 @@ fn stopped(stop: Stop) -> Fail { None => problem.text.clone(), }) .collect(); - Fail::usage(format!("Not saved: {}", said.join("; "))) + let said = format!("Not saved: {}", said.join("; ")); + match refused { + true => Fail::refused(said), + false => Fail::usage(said), + } } Stop::Refused(why) if why == "Stopped" => Fail::exit(130), Stop::Refused(why) => Fail::usage(why), diff --git a/crates/berth/src/local/mod.rs b/crates/berth/src/local/mod.rs index 80ba973..d510ef6 100644 --- a/crates/berth/src/local/mod.rs +++ b/crates/berth/src/local/mod.rs @@ -213,6 +213,9 @@ pub fn push_image(global: &Global, destination: &str, tag: &str) -> Done<()> { Ok(()) } +/// What `berth serve --web` says, as berth 0.5 had a page in the browser. +pub const WEB_GONE: &str = "--web does nothing any more: berth has no page in the browser, and its screens are berth ui. Take --web out of this command line"; + /// `berth server …`: the server of this machine itself. pub fn server_command(command: args::Server) -> Done<()> { let paths = Paths::locate()?; @@ -225,6 +228,9 @@ pub fn server_command(command: args::Server) -> Done<()> { if run.expose { words.push("--expose"); } + if run.web { + out::warn(WEB_GONE); + } Ok(serve::command(&paths, &words)?) } args::Server::SetupKey { revoke } => { diff --git a/crates/berth/src/local/serve/actions.rs b/crates/berth/src/local/serve/actions.rs index 9fa89c0..40fb0a2 100644 --- a/crates/berth/src/local/serve/actions.rs +++ b/crates/berth/src/local/serve/actions.rs @@ -6,7 +6,7 @@ use super::{ Remote, Serve, - api::{Answer, App, Fail, caller, internal, job_info, remote, report_path}, + api::{Answer, App, Fail, Said, caller, internal, job_info, remote, report_path, turned_down}, auth::Access, setup::{own_place, saved_profile}, }; @@ -53,6 +53,9 @@ pub(super) struct Seen<'a> { pub observed: Option, /// Why its controller did not answer, when it was asked and did not. pub unanswered: Option<&'a str>, + /// That its controller is too old for this berth, and how to update it, + /// when that is why it did not answer. + pub older: Option<&'a str>, /// The caller is on the machine the server runs on: in its process, or /// on its socket. pub local: bool, @@ -171,19 +174,51 @@ pub(super) fn answer(acted: Acted) -> (StatusCode, Json) { (status, Json(acted)) } -/// The actions a host's controller lists, asked for with a time limit. -async fn controller_actions(remote: &Remote) -> Result, String> { - let link = remote - .link - .as_ref() - .ok_or_else(|| remote.problem.clone().unwrap_or_else(|| "no link".into()))?; - match tokio::time::timeout(Duration::from_secs(15), link.get::("/v1/actions")).await { - Ok(Ok(listed)) => Ok(berth_tui::host::list(listed, "actions")), - Ok(Err(error)) => Err(error.to_string()), - Err(_) => Err("no answer in time".into()), +/// Why a host's controller did not list its actions. +enum Unlisted { + /// It did not answer. + Unanswered(String), + /// It is older than this berth: what to say, with how to update it. + Older(String), +} + +impl Unlisted { + fn said(self) -> String { + match self { + Unlisted::Unanswered(said) | Unlisted::Older(said) => said, + } } } +/// The actions a host's controller lists, asked for with a time limit. A +/// controller that does not know the call is asked its version, so that one +/// of an older berth is said to be old rather than broken. +async fn controller_actions( + remote: &Remote, + host_type: HostType, +) -> Result, Unlisted> { + let link = remote.link.as_ref().ok_or_else(|| { + Unlisted::Unanswered(remote.problem.clone().unwrap_or_else(|| "no link".into())) + })?; + let error = + match tokio::time::timeout(Duration::from_secs(15), link.get::("/v1/actions")).await + { + Ok(Ok(listed)) => return Ok(berth_tui::host::list(listed, "actions")), + Ok(Err(error)) => error.to_string(), + Err(_) => return Err(Unlisted::Unanswered("no answer in time".into())), + }; + Err(match remote.version().await { + Ok(version) => berth_api::compat::older_controller( + &remote.name, + &version.version, + version.api, + host_type, + ) + .map_or(Unlisted::Unanswered(error), Unlisted::Older), + Err(_) => Unlisted::Unanswered(error), + }) +} + /// What a host's controller says the host is, when it answers. async fn observed(remote: &Remote) -> Option { remote @@ -280,12 +315,13 @@ pub async fn host_actions( // A host of the type ssh has no controller to ask. let asked = match host_type::of(&profile) { HostType::Ssh => None, - _ => Some(controller_actions(&remote).await), + host_type => Some(controller_actions(&remote, host_type).await), }; - let (listed, unanswered) = match &asked { - Some(Ok(listed)) => (Some(listed.as_slice()), None), - Some(Err(error)) => (None, Some(error.as_str())), - None => (None, None), + let (listed, unanswered, older) = match &asked { + Some(Ok(listed)) => (Some(listed.as_slice()), None, None), + Some(Err(Unlisted::Unanswered(error))) => (None, Some(error.as_str()), None), + Some(Err(Unlisted::Older(said))) => (None, None, Some(said.as_str())), + None => (None, None, None), }; let secrets = secret_names(&state); let observed = observed(&remote).await; @@ -297,6 +333,7 @@ pub async fn host_actions( host_type: host_type::of(&profile), observed, unanswered, + older, local: access.is_none(), }))) } @@ -326,22 +363,29 @@ pub async fn host_act( // berth's own are described without asking the host, which they do themselves. let builtin = HOST.iter().find(|builtin| builtin.name == action); let ssh_only = host_type::of(&profile) == HostType::Ssh; - let mut unanswered = None; + let (mut unanswered, mut older) = (None, None); let listed = match builtin { Some(_) if ssh_only || !THROUGH_CONTROLLER.contains(&action.as_str()) => None, // One that runs through the controller is described as the host answers. - Some(_) => match controller_actions(&remote).await { + Some(_) => match controller_actions(&remote, host_type::of(&profile)).await { Ok(listed) => Some(listed), - Err(error) => { + Err(Unlisted::Unanswered(error)) => { unanswered = Some(error); None } + Err(Unlisted::Older(said)) => { + older = Some(said); + None + } }, None if ssh_only => Some(vec![]), None => Some( - controller_actions(&remote) + controller_actions(&remote, host_type::of(&profile)) .await - .map_err(|error| Fail::new(code::UNREACHABLE, error))?, + .map_err(|unlisted| match unlisted { + Unlisted::Older(said) => Fail::new(code::PRECONDITION, said), + unlisted => Fail::new(code::UNREACHABLE, unlisted.said()), + })?, ), }; let secrets = secret_names(&state); @@ -358,12 +402,17 @@ pub async fn host_act( host_type: host_type::of(&profile), observed, unanswered: unanswered.as_deref(), + older: older.as_deref(), local: access.is_none(), }); let found = actions .iter() .find(|known| known.name == action) - .ok_or_else(|| no_action(&name, &action, &actions))?; + .ok_or_else(|| match &older { + // The controller's own actions are not known when it is too old to list them. + Some(said) => Fail::new(code::PRECONDITION, said.clone()), + None => no_action(&name, &action, &actions), + })?; let local = access.is_none(); admit(&state, &caller(access), found, &act)?; if local && builtin.is_some() { @@ -391,6 +440,7 @@ fn installation_list() -> Vec { host_type: HostType::Workstation, observed: None, unanswered: None, + older: None, local: false, }; INSTALLATION @@ -598,8 +648,7 @@ fn install_controller_action(seen: &Seen) -> Action { fn install_controller(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; - let bad = - |error: Box| Fail::new(code::BAD_REQUEST, error.to_string()); + let bad = |error: Box| turned_down(error); let host = own_place(&name).and_then(|place| Host::load_at(&state.paths, place).map_err(bad))?; let password = act @@ -630,8 +679,7 @@ fn install_controller(state: Arc, name: String, act: Act) -> Run { fn apply(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; - let bad = - |error: Box| Fail::new(code::BAD_REQUEST, error.to_string()); + let bad = |error: Box| turned_down(error); let host = own_place(&name).and_then(|place| Host::load_at(&state.paths, place).map_err(bad))?; let password = act @@ -695,12 +743,11 @@ fn seed(state: Arc, name: String, _: Act) -> Run { let (paths, registries) = (state.paths.clone(), Store::own(state.store.clone())); // It may publish the release and runs the seed builder, and waits for both. let seed = tokio::task::spawn_blocking(move || { - berth_core::seed::cloud(&paths, ®istries, &name, false) - .map_err(|error| error.to_string()) + berth_core::seed::cloud(&paths, ®istries, &name, false).map_err(Said::from) }) .await .map_err(internal)? - .map_err(|error| Fail::new(code::BAD_REQUEST, error))?; + .map_err(turned_down)?; // A seed's key joins the tailnet for a day, which whoever starts the machine needs to know. Ok(Acted { result: Some(json!({"user_data": seed.user_data, "tailnet": seed.tailnet})), @@ -777,8 +824,7 @@ fn installer_action(seen: &Seen) -> Action { fn installer(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { - let bad = - |error: Box| Fail::new(code::BAD_REQUEST, error.to_string()); + let bad = |error: Box| turned_down(error); let text = |key: &str| act.arguments.get(key).map_or("", |value| value.trim()); let device = text("device").to_owned(); let password = act.arguments.get("password").cloned().unwrap_or_default(); @@ -799,7 +845,7 @@ fn installer(state: Arc, name: String, act: Act) -> Run { && !iso.is_file() { return Err(Fail::new( - code::BAD_REQUEST, + code::PRECONDITION, format!("The profile's ISO {} is not there", iso.display()), )); } @@ -924,6 +970,7 @@ fn secrets_action(seen: &Seen) -> Action { return unavailable(action, Some(refusal.why)); } let why = match (seen.host_actions, kept, seen.unanswered) { + _ if seen.older.is_some() => seen.older.map(str::to_owned), (_, _, Some(error)) => Some(format!( "The controller on {} does not answer, and the secrets go through it: {error}", seen.name @@ -940,8 +987,7 @@ fn secrets_action(seen: &Seen) -> Action { fn secrets(state: Arc, name: String, act: Act) -> Run { Box::pin(async move { let remote = remote(&state, &name)?; - let bad = - |error: Box| Fail::new(code::BAD_REQUEST, error.to_string()); + let bad = |error: Box| turned_down(error); let host = Host::load_at(&state.paths, own_place(&name)?).map_err(bad)?; let payload = Store::own(state.store.clone()) .host_payload(&host.config) @@ -984,14 +1030,16 @@ fn upgrade_server_action(seen: &Seen) -> Action { }; } let why = match (seen.host_type, seen.unanswered) { + (HostType::Node, _) if seen.older.is_some() => seen.older.map(str::to_owned), (HostType::Node, Some(error)) => Some(format!( "The controller on {} does not answer, and the upgrade runs through it: {error}", seen.name )), (HostType::Node, None) => None, (other, _) => Some(format!( - "{} is a host of the type {other}: only a node's controller upgrades a berth server it runs", - seen.name + "{} is a host of the type {other}: only a node's controller upgrades a berth server it runs. A server of berth 0.5, and one on no node, is updated by its recipe: {}", + seen.name, + berth_api::compat::UPGRADE_DOCS )), }; unavailable(action, why) @@ -1656,6 +1704,7 @@ mod tests { host_type, observed: None, unanswered: None, + older: None, local: false, } } @@ -1693,6 +1742,34 @@ mod tests { // Only a node's controller does it. let managed = upgrade_server_action(&seen(HostType::Managed, Some(&with), &profile)); assert!(!managed.available && managed.why.contains("only a node's controller")); + assert!(managed.why.contains(berth_api::compat::UPGRADE_DOCS)); + } + + #[test] + fn a_controller_that_is_too_old_is_said_to_be_old() { + let profile = + json!({"type": "managed", "access": {"address": "192.0.2.12", "user": "root"}}); + let said = + berth_api::compat::older_controller("big", "0.5.2", 1, HostType::Managed).unwrap(); + let mut old = seen(HostType::Managed, None, &profile); + old.older = Some(&said); + let actions = host_list(&old); + let secrets = actions + .iter() + .find(|action| action.name == "secrets") + .unwrap(); + assert!(!secrets.available); + assert_eq!(secrets.why, said); + assert!( + secrets.why.ends_with("berth run big install-controller"), + "{}", + secrets.why + ); + let install = actions + .iter() + .find(|action| action.name == "install-controller") + .unwrap(); + assert!(install.available, "{}", install.why); } #[test] diff --git a/crates/berth/src/local/serve/api.rs b/crates/berth/src/local/serve/api.rs index ff35f66..30d54ee 100644 --- a/crates/berth/src/local/serve/api.rs +++ b/crates/berth/src/local/serve/api.rs @@ -102,6 +102,50 @@ pub(super) fn internal(error: impl std::fmt::Display) -> Fail { Fail::new(code::INTERNAL, error.to_string()) } +/// What the core said when it did not do something: its words, and whether +/// it refused before anything began. It crosses threads, as an error of the +/// core does not. +#[derive(Debug)] +pub(super) struct Said { + pub refused: bool, + pub message: String, +} + +impl From> for Said { + fn from(error: Box) -> Said { + Said { + refused: berth_core::is_refused(error.as_ref()), + message: error.to_string(), + } + } +} + +impl From for Said { + fn from(message: String) -> Said { + Said { + refused: false, + message, + } + } +} + +impl std::fmt::Display for Said { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&self.message) + } +} + +/// A request the core turned down: refused before anything began is a +/// failed precondition, which berth's commands exit 3 for; anything else is +/// a request that does not fit. +pub(super) fn turned_down(said: impl Into) -> Fail { + let said = said.into(); + match said.refused { + true => Fail::new(code::PRECONDITION, said.message), + false => Fail::new(code::BAD_REQUEST, said.message), + } +} + // ── What the answers look like ────────────────────────────────────────────── #[derive(Serialize, ToSchema)] @@ -312,6 +356,8 @@ pub fn router(state: Arc, guarded: bool) -> Router { ); } } + // Not behind the guard: an address in a browser, as the recipes of berth 0.5 printed it. + router = router.route("/", get(front)); // Added after the guard, which they are not behind: they check their key themselves. router = router.merge(keyed_routes().split_for_parts().0); router @@ -319,6 +365,23 @@ pub fn router(state: Arc, guarded: bool) -> Router { .with_state(state) } +/// What a browser finds at the server's address. It says nothing of the +/// server, as anyone who reaches it may read it. +pub const FRONT: &str = "This is a berth server. + +berth has no page in the browser any more. Use berth on your own machine, +on Linux, macOS or Windows: + + berth login https://THIS-SERVER + berth ui + +A link that ends in #setup-key=KEY holds a setup key: berth login asks for it. +"; + +async fn front() -> Response { + ([(header::CONTENT_TYPE, "text/plain; charset=utf-8")], FRONT).into_response() +} + /// What a request shows to be let in: a bearer, and the protocols of a /// WebSocket, because a browser cannot set a header on one. fn shown(headers: &HeaderMap) -> Vec { @@ -412,7 +475,7 @@ async fn portal( None => (ops::SERVE, String::new()), }; let op = ops::find(table, name) - .ok_or_else(|| (code::NOT_FOUND, format!("No operation {name}")))?; + .ok_or_else(|| (code::NOT_FOUND, berth_api::compat::no_operation(name)))?; if !op.server_only.is_empty() { return Err((code::FORBIDDEN, op.server_only.into())); } diff --git a/crates/berth/src/local/serve/resources.rs b/crates/berth/src/local/serve/resources.rs index c8ea00d..a99eae0 100644 --- a/crates/berth/src/local/serve/resources.rs +++ b/crates/berth/src/local/serve/resources.rs @@ -7,7 +7,7 @@ use super::{ Serve, actions::{admit, answer, parse, refused}, - api::{Answer, App, Fail, Ws, caller, internal, job_info}, + api::{Answer, App, Fail, Said, Ws, caller, internal, job_info, turned_down}, auth::Access, }; use axum::{ @@ -39,9 +39,12 @@ fn bad(error: impl std::fmt::Display) -> Fail { /// A refusal of the core as an answer: a missing resource is 404, one that /// is busy 409, anything else a request that does not fit. -fn refusal(error: impl std::fmt::Display) -> Fail { - let message = error.to_string(); - if message.starts_with("No resource ") || message.starts_with("No provisioner ") { +fn refusal(error: impl Into) -> Fail { + let said = error.into(); + let message = said.message.clone(); + if said.refused { + turned_down(said) + } else if message.starts_with("No resource ") || message.starts_with("No provisioner ") { Fail::new(code::NOT_FOUND, message) } else if message.starts_with("Something else is being done to") || message.contains(" already, ") @@ -59,8 +62,8 @@ fn store(state: &Serve) -> Store { /// Run work of the core off the runtime: it waits for the provisioner. async fn blocking( work: impl FnOnce() -> berth_core::Result + Send + 'static, -) -> Answer> { - tokio::task::spawn_blocking(move || work().map_err(|error| error.to_string())) +) -> Answer> { + tokio::task::spawn_blocking(move || work().map_err(Said::from)) .await .map_err(internal) } @@ -244,7 +247,7 @@ async fn run_action( if !acting.changes() { let answered = blocking(move || acting.run(&paths, &options, &mut std::io::sink())) .await? - .map_err(|error| Fail::new(code::UNREACHABLE, error))?; + .map_err(|error| Fail::new(code::UNREACHABLE, error.message))?; return Ok(Acted { result: Some(answered.value()), ..Acted::default() diff --git a/crates/berth/src/local/serve/tests.rs b/crates/berth/src/local/serve/tests.rs index 7fce8ec..376f3ec 100644 --- a/crates/berth/src/local/serve/tests.rs +++ b/crates/berth/src/local/serve/tests.rs @@ -294,6 +294,43 @@ fn message(id: u64, topic: &str, keep: &str) -> berth_api::Message { .unwrap() } +#[tokio::test] +async fn a_browser_and_a_client_of_berth_0_5_are_told_what_to_do() { + let dir = tempfile::tempdir().unwrap(); + let state = state_in(dir.path(), Some("s3cret-token")); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap(); + let router = api::router(state, true); + tokio::spawn(async move { axum::serve(listener, router).await }); + let connect = || async { tokio::net::TcpStream::connect(address).await.unwrap() }; + + // The address the recipes of 0.5 printed answers without a token, and says only what to run. + let mut client = Client::over(connect().await).await.unwrap(); + let front = client + .send("GET", "/", &[], Default::default()) + .await + .unwrap(); + assert_eq!(front.status, 200); + let text = String::from_utf8(front.body.to_vec()).unwrap(); + assert_eq!(text, api::FRONT); + assert!(text.contains("berth login https://THIS-SERVER") && !text.contains('<')); + assert!(!text.contains(berth_api::VERSION)); + let refused = client.get::("/v1/hosts").await.unwrap_err(); + assert_eq!(refused.status(), 401); + + // A call only berth 0.5 makes says that the client is older. + let socket = client::websocket_at(connect().await, "berth", "/v1/portal", "s3cret-token") + .await + .unwrap(); + let portal = berth_api::portal::Portal::over(socket); + let old = portal + .call("tasks.list", Some("studio"), Value::Null, None) + .await + .unwrap_err(); + assert_eq!(old.status(), 404); + assert!(old.to_string().contains("a call of berth 0.5"), "{old}"); +} + #[tokio::test(flavor = "multi_thread")] async fn hosts_jobs_and_the_inbox_over_the_api() { let served = served().await; @@ -1445,8 +1482,13 @@ async fn the_server_is_set_up_through_the_api() { assert!(!kept.to_string().contains("tailscale_a"), "{kept}"); // A seed of a host that joins the tailnet is refused, with what to do, // while the server has no API key of the tailnet to make its key with. + // It is a refusal before anything began, which the command line exits 3 for. let (status, said) = send(client, "POST", "/v1/hosts/studio/actions/seed", Value::Null).await; - assert_eq!(status, 400, "{said}"); + assert_eq!(status, 412, "{said}"); + assert_eq!( + crate::cli::fail::status_of(status), + crate::cli::fail::REFUSED + ); let message = said["error"]["message"].as_str().unwrap(); assert!( message.contains("berth secret set tailnet_api_key") @@ -2589,8 +2631,9 @@ async fn machines_are_created_and_deleted_through_the_api() { // Refusals are answers of their own, before there is a job. let asked = json!({"provisioner": "vm", "host": "studio", "arguments": {"server": "https://192.0.2.10:8443"}}); + // A secret the server lacks is a refusal, which the command line exits 3 for. let (status, said) = send(client, "POST", "/v1/resources", asked.clone()).await; - assert_eq!(status, 400, "{said}"); + assert_eq!(status, 412, "{said}"); assert!(message(&said).contains("api_token"), "{said}"); let secret = json!({"value": "tok-s3cret"}); send(client, "PUT", "/v1/secrets/api_token", secret).await; diff --git a/crates/berth/src/local/serve/tests/remote.rs b/crates/berth/src/local/serve/tests/remote.rs index 77d1697..3efda10 100644 --- a/crates/berth/src/local/serve/tests/remote.rs +++ b/crates/berth/src/local/serve/tests/remote.rs @@ -107,3 +107,138 @@ async fn the_screens_sign_in_with_a_setup_key_and_use_the_portal() { until("refusal", || refused.link() != Link::Connecting); assert_eq!(refused.link(), Link::Refused); } + +/// A server of berth 0.5.2 as its portal answers: every call with its version. +async fn older_server(token: &'static str) -> String { + use axum::extract::ws::{Message, WebSocketUpgrade}; + let portal = axum::routing::get(move |ws: WebSocketUpgrade| async move { + ws.protocols([token]).on_upgrade(|mut socket| async move { + while let Some(Ok(Message::Text(text))) = socket.recv().await { + let asked: Value = serde_json::from_str(text.as_str()).unwrap(); + let said = json!({"id": asked["id"], "result": {"version": "0.5.2", "api": 1, "mode": "serve"}}); + if socket.send(Message::Text(said.to_string().into())).await.is_err() { + break; + } + } + }) + }); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let router = axum::Router::new().route(berth_api::ops::PORTAL, portal); + tokio::spawn(async move { axum::serve(listener, router).await }); + url +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_client_refuses_an_older_server_and_still_reads_its_version() { + use crate::cli::{args::Global, client::Client, fail}; + let url = older_server("s3cret-token").await; + let dir = tempfile::tempdir().unwrap(); + let token = dir.path().join("token"); + std::fs::write(&token, "s3cret-token").unwrap(); + let global = Global { + server: Some(url.clone()), + token_file: Some(token), + ..Global::default() + }; + let kept = Settings::new(Some(dir.path().join("berth"))); + let (opened, version) = tokio::task::spawn_blocking(move || { + let opened = Client::open_in(&global, &kept).map(|_| ()); + let version = Client::open_any(&global, &kept).and_then(|client| client.server_version()); + (opened, version) + }) + .await + .unwrap(); + let refused = opened.unwrap_err(); + assert_eq!(refused.status, fail::REFUSED); + assert!( + refused + .message + .contains("is berth 0.5.2 (API 1), older than this berth") + && refused.message.contains("Update the server first"), + "{}", + refused.message + ); + assert_eq!(version.unwrap(), ("0.5.2".to_owned(), 1)); +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_screens_say_that_an_older_server_is_updated_first() { + let url = older_server("s3cret-token").await; + let client = tempfile::tempdir().unwrap(); + let kept = client.path().join("berth"); + let linked = Linked { + server: Some(url.clone()), + token: Some("s3cret-token".into()), + setup_key: None, + }; + berth_page::device::install(linked, Settings::new(Some(kept.clone()))); + let mut app = berth_page::App::new(); + until("the older server", || { + app.tick(); + screen(&mut app).contains("older than this berth") + }); + // The token stays for when the server is updated. + assert_eq!( + Settings::new(Some(kept)).get(settings::TOKEN).as_deref(), + Some("s3cret-token") + ); +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_screens_keep_the_token_of_a_server_that_does_not_answer_or_refuses_it() { + // Nobody listens at this address. + let closed = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let away = format!("http://{}", closed.local_addr().unwrap()); + drop(closed); + let client = tempfile::tempdir().unwrap(); + let kept = client.path().join("berth"); + let settings = Settings::new(Some(kept.clone())); + settings + .set(&[(settings::SERVER, &away), (settings::TOKEN, "s3cret-kept")]) + .unwrap(); + let linked = Linked { + server: None, + token: None, + setup_key: None, + }; + berth_page::device::install(linked, Settings::new(Some(kept.clone()))); + let mut app = berth_page::App::new(); + let started = std::time::Instant::now(); + while started.elapsed() < Duration::from_secs(2) { + app.tick(); + berth_page::pump(); + std::thread::sleep(Duration::from_millis(20)); + } + assert_eq!( + settings.get(settings::TOKEN).as_deref(), + Some("s3cret-kept") + ); + drop(app); + + // A server that refuses it asks for another, and the kept one stays until then. + let dir = tempfile::tempdir().unwrap(); + let state = state_in(dir.path(), Some("s3cret-token")); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let router = api::router(state, true); + tokio::spawn(async move { axum::serve(listener, router).await }); + settings + .set(&[(settings::SERVER, &url), (settings::TOKEN, "s3cret-kept")]) + .unwrap(); + let linked = Linked { + server: None, + token: None, + setup_key: None, + }; + berth_page::device::install(linked, Settings::new(Some(kept.clone()))); + let mut app = berth_page::App::new(); + until("the refusal", || { + app.tick(); + screen(&mut app).contains("did not take that token") + }); + assert_eq!( + settings.get(settings::TOKEN).as_deref(), + Some("s3cret-kept") + ); +} diff --git a/crates/berth/src/main.rs b/crates/berth/src/main.rs index c1d7e8e..fec16e3 100644 --- a/crates/berth/src/main.rs +++ b/crates/berth/src/main.rs @@ -46,15 +46,7 @@ fn screens(global: &cli::args::Global, host: Option<&str>) -> cli::fail::Result< } let var = |name: &str| std::env::var(name).ok(); let settings = Settings::new(settings::folder(var)); - match settings.take_in(settings::old_folder(var).as_deref()) { - Ok(Some(server)) => { - cli::out::note(format!("berth-remote's server is berth's now: {server}")) - } - Ok(None) => {} - Err(error) => cli::out::warn(format!( - "berth-remote's settings were not taken in: {error}" - )), - } + cli::client::take_in_remote(&settings, settings::old_folder(var).as_deref()); let mut linked = berth_page::device::Linked { server: global .server diff --git a/docs/content/docs/(guide)/reference/cli.mdx b/docs/content/docs/(guide)/reference/cli.mdx index 633de06..5bbbb7f 100644 --- a/docs/content/docs/(guide)/reference/cli.mdx +++ b/docs/content/docs/(guide)/reference/cli.mdx @@ -1511,7 +1511,7 @@ berth update [--check] [--from URL] ## berth version -The version of this berth. +The version of this berth, and of the server it uses. ```text berth version diff --git a/docs/content/docs/(guide)/use/index.mdx b/docs/content/docs/(guide)/use/index.mdx index 13382af..66815e5 100644 --- a/docs/content/docs/(guide)/use/index.mdx +++ b/docs/content/docs/(guide)/use/index.mdx @@ -20,4 +20,5 @@ Once a host is installed, you look after it from berth, from sign-ins and tokens npm, Docker and Nexus logins, and publishing the release. Nightly restic snapshots of the agent's data. What updates itself, what you update, and the limits. + The order of the updates since berth 0.5, and the way back. diff --git a/docs/content/docs/(guide)/use/meta.json b/docs/content/docs/(guide)/use/meta.json index 4e73710..54e29e4 100644 --- a/docs/content/docs/(guide)/use/meta.json +++ b/docs/content/docs/(guide)/use/meta.json @@ -1,5 +1,22 @@ { "title": "Using berth", "icon": "SquareTerminal", - "pages": ["terminal-ui", "overview", "cli", "types", "nodes", "server", "store", "access", "accounts", "sign-ins", "secrets", "registries", "backups", "moving", "updates"] + "pages": [ + "terminal-ui", + "overview", + "cli", + "types", + "nodes", + "server", + "store", + "access", + "accounts", + "sign-ins", + "secrets", + "registries", + "backups", + "moving", + "updates", + "upgrading" + ] } diff --git a/docs/content/docs/(guide)/use/nodes.mdx b/docs/content/docs/(guide)/use/nodes.mdx index ece3f4b..a71e60e 100644 --- a/docs/content/docs/(guide)/use/nodes.mdx +++ b/docs/content/docs/(guide)/use/nodes.mdx @@ -9,7 +9,7 @@ A node is a server that berth sets up and keeps up to date, without a desktop. I | Part | On a node | | --- | --- | | System | Ubuntu 24.04 or later on x86_64, its time zone and locale, and Ubuntu's security updates without reboots | -| SSH | Keys only. The SSH user takes berth's key, and root keeps a login with keys when berth logs in as root | +| SSH | Keys only. The SSH user takes berth's key, and root keeps a login with keys when berth logs in as root or root had one before | | Controller | berthd and box, with the actions status, update, reboot and secrets. Sessions run as the SSH user | | Firewall | `ufw`: incoming traffic is refused except SSH, the tailnet and what the features open | | Tailnet | Tailscale, joined as a workstation joins ([Tailnet](/docs/use/access)) | @@ -44,7 +44,7 @@ An `ssh` or `managed` host becomes a node with `berth host set big type=node`. A | Setting | Default | Meaning | | --- | --- | --- | -| `devbox_ssh_from` | `any` | Who reaches SSH from outside the tailnet: `any`, or one address or network. The address an apply comes from always does | +| `devbox_ssh_from` | `any` | Who reaches SSH from outside the tailnet: `any`, or one address or network. The address an apply comes from always does. A server taken in keeps SSH as limited as its firewall had it | | `devbox_harden_ssh` | `true` | Keys only for SSH. It is skipped while the SSH user has no key | | `devbox_manage_firewall` | `true` | Set the node's firewall | | `devbox_install_tailscale` | `true` | Install Tailscale and join the tailnet with a key berth makes ([Joining the tailnet](/docs/use/access#joining-the-tailnet)) | @@ -99,10 +99,12 @@ An apply with the same features doesn't change them. A feature taken out of the ## Take in a server that exists -A server that the `incus-host` recipe set up, or that has Incus or Docker installed by hand, is added and applied as a node like a new one. The features take what is there: +A server that the `incus-host` recipe of berth 0.5 set up, or that has Incus or Docker installed by hand, is added and applied as a node like a new one. The features take what is there: - Incus with a storage pool keeps its pools, networks, profiles and trusted clients. Only the API address, when it has none, and berth's certificate are added. - An installed Docker engine stays, with its containers and volumes, also one from Docker's own packages. +- A firewall rule made by hand that lets SSH in from some addresses only stays, and the node does not open SSH to every address. The address the apply comes from is let in too. Delete the rule by hand to open SSH wider. +- Root keeps a login with keys it had, such as the one the berth server in its container uses. The node never opens root to passwords. - A provisioner registered by hand for the node's server becomes the node's instead of a second one, with its settings as they were. That happens only when berth itself can tell: the server it names is on the node's address, the same name or a name that berth's server finds at the same address. The recipe's `https://incus-host:8444` passes when the server reaches the node as `node`, as both resolve to the gateway of the server's container. - berth leaves a provisioner as it is when only the node says it's the same, and also one of another node. The node's word may be the provisioner's certificate, which is public. It may also be the provisioner's server among the aliases. The apply says so in a line and doesn't register a second one. If you know it reaches the node, make it the node's and apply again: @@ -113,6 +115,8 @@ A server that the `incus-host` recipe set up, or that has Incus or Docker instal This is also the way for the provisioner of the local Docker engine of a berth server in a container on the node. +Apply the node from the berth server that holds the provisioners registered for this machine, such as the server of the recipe. Those provisioners are that server's. A berth elsewhere doesn't see them, and registers new ones beside them. + Then check the node and its provisioners: ```bash @@ -135,12 +139,15 @@ The `berth-server` recipe installs berth's server in a container named `berth`. | User and environment | The variables the image does not set | A Docker socket mounted into it stays. +- Logging, such as the `max-size` and `max-file` a `daemon.json` gives every container, is carried over too. - An option it cannot carry over, such as `--privileged` or devices, stops it before anything changes. Upgrade such a container by hand. - The old container is stopped and kept until the new one keeps running. If the new one stops, the old one runs again. - It refuses to go to an older version than the one running. When the server upgrades its own container, it restarts during the job. The output stays in the node's session. +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 - It never makes a storage pool on a disk, which would erase it. Create such a pool with `incus storage create` and name it in `devbox_incus_pool`. diff --git a/docs/content/docs/(guide)/use/updates.mdx b/docs/content/docs/(guide)/use/updates.mdx index a5ea6c1..da2c7f5 100644 --- a/docs/content/docs/(guide)/use/updates.mdx +++ b/docs/content/docs/(guide)/use/updates.mdx @@ -28,7 +28,9 @@ berth update # download it, check its SHA-256 and replace this progr `berth update` takes the same archive as the install script. It offers only full releases, not pre-releases such as `0.4.0-rc.1`, and moves from a pre-release to its release. A container is updated by pulling a newer image. `berthd` and `box` travel inside `berth`. A host gets the new ones when you apply its configuration. Its overview says when its tools are of another version than your berth. -A berthd of berth 0.5 says the host's type by its older name, and berth reads it. For sessions, actions and files berth needs the berthd of API 2, and until then the overview offers to install the controller or apply the configuration, which brings berthd up to date. An older berth that meets a newer berthd is out of date: update it. It refuses the profile a newer berth left on the host rather than misread it. `berth update --from URL` takes the releases from a mirror instead, and the setting `releases` keeps one. +A berthd of berth 0.5 says the host's type by its older name, and berth reads it. Its sessions, files and `berth exec` work. Its actions and port forwarding need the berthd of API 2: until then `berth ls` marks the host `old controller`, and the host's page and its actions say what updates it, `berth apply HOST` or, for a managed host, `berth run HOST install-controller`. An older berth that meets a newer berthd is out of date: update it. It refuses the profile a newer berth left on the host rather than misread it. `berth update --from URL` takes the releases from a mirror instead, and the setting `releases` keeps one. + +A client refuses a server with an older API, and says to update the server first. [Upgrading from 0.5](/docs/use/upgrading) has the order for a server, its clients and its hosts. ## Hosts that are behind diff --git a/docs/content/docs/(guide)/use/upgrading.mdx b/docs/content/docs/(guide)/use/upgrading.mdx new file mode 100644 index 0000000..dd06321 --- /dev/null +++ b/docs/content/docs/(guide)/use/upgrading.mdx @@ -0,0 +1,208 @@ +--- +title: Upgrading from 0.5 +description: What changed since berth 0.5, the order of the updates, and the way back. +icon: CircleArrowUp +--- + +This berth has one set of screens and one command line, both over a server's API. Many commands and names changed, and the store is converted the first time this berth opens it. Update in the order below, and make the backup first. + +## The order + +| Step | What | Why then | +| --- | --- | --- | +| 1 | Back up the store | berth 0.5 cannot read the store once this berth has converted it | +| 2 | Update the server | A client of this berth refuses a server of berth 0.5 | +| 3 | Update the clients | A client of berth 0.5 cannot work with the new server | +| 4 | Update the hosts' controllers | berth needs their actions, which the controller of berth 0.5 does not have | +| 5 | Make the server's machine a node, if it hosts Incus or Docker | Optional. It then gets `upgrade-server` for the next update | + +### 1. Back up + +For a server of the `berth-server` recipe, stop it and copy its folder: + +```bash +sudo docker stop berth +sudo tar -C /srv -czf ~/berth-0.5.tar.gz berth +sudo docker start berth +``` + +For a berth without a server, copy `~/.local/share/berth` and `~/.config/berth` while no berth runs. A store in PostgreSQL needs a dump, with the password in `~/.pgpass` rather than on the command line: + +```bash +pg_dump --format=custom --file=berth-0.5.dump --host=192.0.2.40 --username=berth berth +``` + +### 2. The server + +Fetch the recipe of the new version and run it with `berth_server_update`. It pulls the new image and makes the container again with the data and every option of the old one, such as the name `incus-host` and the Docker socket. The old container runs again if the new one does not come up: + +```bash +curl -fsSL https://github.com/helightdev/berth/archive/refs/heads/main.tar.gz \ + | tar xz --strip-components=1 --wildcards '*/recipes' \ + '*/provision/ansible/roles/devbox/files/berth-server-upgrade.py' +cd recipes/berth-server +sudo ./install.sh -e berth_server_update=true +``` + +Pass the options you gave the first run again, such as `-e berth_server_node=true`. Do not remove the container by hand. A new container from the recipe gets the recipe's options alone. [Update it](https://github.com/helightdev/berth/blob/main/recipes/berth-server/README.md#update-it) in the recipe's guide has the details. + +For a server in a container you made yourself, run the same helper with the version to run: + +```bash +sudo python3 provision/ansible/roles/devbox/files/berth-server-upgrade.py berth 0.6.0 +``` + +A `berth serve` that runs as a service is updated with `berth update`, then restarted. Its command line may keep `--web`, which does nothing now. + +### 3. The clients + +```bash +berth update +``` + +Install berth where berth-remote was used: the [installation](/docs/start/install) replaces it. berth takes in the server and token berth-remote kept, on the first command that needs a server: + +| berth has | What happens to berth-remote's settings | +| --- | --- | +| No server | Its server and token become berth's, and its file goes | +| The same server | Its token is taken when berth has none, and its file goes | +| Another server, or the same with another token | Its file stays. berth says so once, with `berth login` for the other server | + +A server that does not answer never costs berth the token it kept. Only `berth logout`, signing out in the screens, or a new token in place of one the server refused replaces it. + +### 4. The hosts + +`berth ls` marks each host whose controller is of berth 0.5 as `old controller`. Update them: + +| Host type | Command | +| --- | --- | +| `workstation` or `node` | `berth apply HOST` | +| `managed` | `berth run HOST install-controller` | + +Until then sessions, files and `berth exec` work, and the host's actions and its page in the screens say that the controller is older. + +### 5. The server's machine as a node + +The `incus-host` recipe is gone. A machine it set up becomes a [node](/docs/use/nodes). Run the server recipe with `berth_server_node`. Add the machine from a berth connected to that server, because the provisioners of the machine are that server's: + +```bash +sudo ./install.sh -e berth_server_node=true -e berth_server_update=true +berth host add big node --user root --type node \ + --feature docker/engine --feature incus/server +berth apply big +``` + +The apply makes the recipe's Incus provisioners the node's. It says so for each, and names any it left alone. The provisioner of the server's local Docker engine is one of those, as berth cannot tell that it is the node's engine. Make it the node's by hand, and apply once more: + +```bash +berth provisioner run docker adopt node=big +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. + +## What is converted, and when + +The first start of this berth on a store converts it, whether that is the server in its container or a command on your machine. + +| What | Becomes | +| --- | --- | +| The schema of the SQLite file or the database | The schema of this berth | +| `level` and `profile` in every profile | `type` and `target` | +| `controller` as a type | `managed` | +| Keys pasted into profiles to join the tailnet | Removed. berth makes the keys with the secret `tailnet_api_key` | +| A host berth 0.5 never joined to the tailnet itself | Keeps applying without the tailnet until berth can make its key: its profile gets `devbox_tailnet_optional` | + +The conversion happens once. A second start doesn't change the store. + +## Going back + +Before it converts a SQLite store, berth writes a copy of it beside the store, named for the version and schema it had, such as `berth.db.berth-0.5.2-schema-4`, and says where. Only the latest copy is kept. With no room for it, berth says so and converts the store without one. + +berth 0.5 cannot read a converted store, and in a container it stops again and again. To go back, stop the new berth, put the copy in place and start berth 0.5: + +```bash +sudo docker rm -f berth +cd /srv/berth/data/berth +sudo mv berth.db berth.db.converted +sudo rm -f berth.db-wal berth.db-shm +sudo mv berth.db.berth-0.5.2-schema-4 berth.db +``` + +Then run the recipe of berth 0.5 again, or `berth serve` of that version. What changed after the upgrade is lost. A store in PostgreSQL goes back from the dump of step 1 with `pg_restore`. berth doesn't copy it, and prints a line when it converts one. + +## Commands + +Each old spelling fails with the new one named, so a script does not do something it did not mean. `berth serve --web` is the exception: it works and says that `--web` does nothing. + +{/* old-spellings:start */} + +| berth 0.5 | Now | +| --- | --- | +| `berth list` | `berth host list, or berth ls` | +| `berth remove` | `berth host remove NAME` | +| `berth profile` | `berth host export NAME, berth host import NAME FILE or berth host set NAME KEY=VALUE` | +| `berth credentials` | `berth host credentials NAME` | +| `berth adopt` | `berth host adopt NAME [ADDRESS]` | +| `berth iso` | `berth host iso NAME` | +| `berth seed` | `berth host seed NAME` | +| `berth inventory` | `berth host inventory [NAME…]` | +| `berth provisioners` | `berth provisioner list` | +| `berth provision` | `berth host create NAME --provisioner PROVISIONER` | +| `berth instance` | `berth host start NAME, berth host stop NAME or berth host remove NAME --destroy` | +| `berth deploy` | `berth resource create NAME --provisioner PROVISIONER --spec FILE` | +| `berth actions` | `berth action list HOST` | +| `berth manage` | `berth run HOST ACTION` | +| `berth attach` | `berth session attach HOST [ID]` | +| `berth sessions` | `berth session list HOST` | +| `berth output` | `berth session logs HOST ID` | +| `berth call` | `berth service call HOST SERVICE METHOD` | +| `berth plugins` | `berth plugin list, add, remove or update` | +| `berth connect` | `berth login URL` | +| `berth setup-key` | `berth server setup-key` | +| `berth store` | `berth server store` | +| `berth openapi` | `berth server api openapi` | +| `berth asyncapi` | `berth server api asyncapi` | +| `berth operations` | `berth server api operations` | +| `berth publish` | `berth registry publish DESTINATION --host HOST` | +| `berth image` | `berth registry push-image DESTINATION` | +| `berth host install` | `berth host apply NAME for a managed host, or berth run HOST install-controller` | +| `berth host type` | `berth host set NAME type=TYPE` | +| `berth key new` | `berth key create NAME` | +| `berth key add` | `berth key import NAME FILE` | +| `berth key host` | `berth host verify NAME [--accept]` | +| `berth registry repo` | `berth registry add-repo NAME FORMAT REPOSITORY` | +| `berth apply --behind` | `berth rollout` | +| `berth apply --all` | `berth rollout --all` | +| `berth channel` | `berth channel listen HOST` | + +{/* old-spellings:end */} + +## Names + +| Before | Now | +| --- | --- | +| A host's `level` | Its `type` | +| The level `controller` | The type `managed` | +| A profile's `profile`, such as `laptop` | Its `target` | +| Tasks of a host | Its actions: `berth run HOST ACTION` | +| `berth connect URL` | `berth login URL` | +| berth-remote | berth: `berth ui` opens the screens | +| The page in the browser | The screens and the command line. The server's address shows a line about `berth login` | +| The `incus-host` recipe | A node with the features `incus/server` and `docker/engine` | +| `tailscale_auth_key` and `tailscale_auto_key` in a profile | The secret `tailnet_api_key`, from which berth makes each host's key | + +## API clients + +The API is number 2 now, and `GET /v1/version` says so in `api`. A client of berth's own refuses a server with an older number. A call that only berth 0.5 makes gets an error that names berth 0.5. + +| Before | Now | +| --- | --- | +| `level` in hosts and profiles | `type` | +| `profile` in a profile | `target` | +| `/v1/tasks`, `tasks.list`, `tasks.run` | `/v1/actions`, `actions.list`, `actions.run` | +| The server's `actions.act` | `hosts.act`, `installation.act`, `provisioners.act` and `resources.act` | + +The [API reference](/docs/api/serve) lists every operation. diff --git a/provision/ansible/roles/devbox/defaults/main.yml b/provision/ansible/roles/devbox/defaults/main.yml index 22b21de..b4514c5 100644 --- a/provision/ansible/roles/devbox/defaults/main.yml +++ b/provision/ansible/roles/devbox/defaults/main.yml @@ -109,6 +109,10 @@ devbox_base_commands: [git, rg, jq, curl, python3, pip, uv, node, npm, gcc, g++, devbox_install_playwright: true devbox_install_docker: true devbox_install_tailscale: true +# An apply of a host off the tailnet goes on without it when berth cannot +# make the host's key, rather than stop. berth sets it once for the hosts of +# berth 0.5 that it never joined by itself; it joins them once it can. +devbox_tailnet_optional: false # Web console on port 9090, reachable over the tailnet or an SSH tunnel. devbox_install_cockpit: true devbox_cockpit_packages: [cockpit, cockpit-storaged, cockpit-packagekit, cockpit-files] diff --git a/provision/ansible/roles/devbox/files/berth-server-upgrade.py b/provision/ansible/roles/devbox/files/berth-server-upgrade.py index 58ea111..8349cfe 100644 --- a/provision/ansible/roles/devbox/files/berth-server-upgrade.py +++ b/provision/ansible/roles/devbox/files/berth-server-upgrade.py @@ -1,18 +1,30 @@ #!/usr/bin/env python3 -"""Run the berth server in a container on this node from the image of another -version, with the data and the options of the running container. +"""Run the berth server in a container from the image of another version, or +make it again with options of its own, keeping the data and the options of +the running container. - berth-server-upgrade CONTAINER the version in BERTH_ARG_VERSION + berth-server-upgrade CONTAINER the version in BERTH_ARG_VERSION berth-server-upgrade CONTAINER VERSION + berth-server-upgrade CONTAINER --image IMAGE [--pull] [--keep-image] + [--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. The new container gets the same name, mounts, published ports, restart -policy, groups, extra hosts, networks, user and environment as the old one, so -it keeps the server's data and what it reaches. An option this does not know -how to carry over stops it before anything changes. The old container is -renamed and stopped, not removed, until the new one runs; if the new one does -not come up, the old one is started again. Environment values go to docker in -a private file, never on its command line. +policy, groups, extra hosts, network, logging, user and environment as the +old one, so it keeps the server's data and what it reaches. An option this +does not know how to carry over stops it before anything changes. The old +container is renamed and stopped, not removed, until the new one runs; if the +new one does not come up, the old one is started again. Environment values +go to docker in a private file, never on its command line. """ +import argparse import json import os import re @@ -25,6 +37,9 @@ VERSION = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$') # How long the new container has to stay running before the old one goes. SETTLE = float(os.environ.get('DEVBOX_UPGRADE_SETTLE', '10')) +# What `docker run` gives a container without --shm-size. +DEFAULT_SHM = 64 * 1024 * 1024 +UNCHANGED = 'is unchanged' class Refused(Exception): @@ -57,13 +72,22 @@ def version_key(text): return tuple(int(part) for part in numbers.groups()) if numbers else None +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] + + # Options of a container that would change what it is if they were dropped, # and which this does not carry over: one that is set refuses the upgrade. +# What docker itself fills in for every container, such as the masked paths, +# the cgroup namespace or the console size, is not among them. UNCARRIED = { 'Privileged': 'it runs privileged', 'CapAdd': 'it has capabilities added', 'CapDrop': 'it has capabilities dropped', 'Devices': 'it has devices', + 'DeviceRequests': 'it asks for devices such as GPUs', + 'DeviceCgroupRules': 'it has device rules', 'SecurityOpt': 'it has security options', 'Sysctls': 'it has sysctls', 'Tmpfs': 'it has tmpfs mounts', @@ -71,61 +95,78 @@ def version_key(text): 'Links': 'it has links', 'Dns': 'it has DNS servers of its own', 'DnsSearch': 'it has DNS search domains of its own', + 'DnsOptions': 'it has DNS options of its own', 'Ulimits': 'it has ulimits', 'PidMode': 'it shares a process namespace', - 'IpcMode': None, 'UTSMode': 'it shares a UTS namespace', 'UsernsMode': 'it has a user namespace mode', + 'CgroupParent': 'it has a cgroup parent of its own', 'CpuShares': 'it has CPU shares', + 'CpuQuota': 'it has a CPU quota', + 'CpuPeriod': 'it has a CPU period', 'CpusetCpus': 'it is pinned to CPUs', + 'CpusetMems': 'it is pinned to memory nodes', + 'BlkioWeight': 'it has a block I/O weight', 'MemoryReservation': 'it has a memory reservation', - 'MemorySwap': None, - 'PidsLimit': 'it has a limit of processes', + 'OomKillDisable': 'it is kept from the OOM killer', + 'OomScoreAdj': 'it has an OOM score of its own', + 'ReadonlyRootfs': 'its root file system is read-only', + 'AutoRemove': 'it is removed when it stops', + 'StorageOpt': 'it has storage options', } -def run_options(container, old_image): - """The options of `docker run` that make a container like this one, and - its environment beyond what its image sets. Refused when it has one - this does not carry over.""" +def refusals_of(container, image_config): config, host = container['Config'], container['HostConfig'] - image_config = old_image.get('Config') or {} - refusals = [] - for key, why in UNCARRIED.items(): - value = host.get(key) - if key == 'IpcMode': - if value not in (None, '', 'private', 'shareable'): - refusals.append(f'it shares an IPC namespace ({value})') - continue - if key == 'MemorySwap': - if value not in (None, 0, -1) and value != 2 * (host.get('Memory') or 0): - refusals.append('it has a swap limit of its own') - continue - if value not in (None, '', 0, False, [], {}): - refusals.append(why) + refusals = [why for key, why in UNCARRIED.items() + if host.get(key) not in (None, '', 0, False, [], {})] + ipc = host.get('IpcMode') + if ipc not in (None, '', 'private', 'shareable'): + refusals.append(f'it shares an IPC namespace ({ipc})') + swap = host.get('MemorySwap') + if swap not in (None, 0, -1) and swap != 2 * (host.get('Memory') or 0): + refusals.append('it has a swap limit of its own') runtime = host.get('Runtime') or 'runc' if runtime != 'runc': refusals.append(f'it runs under the runtime {runtime}') - log = (host.get('LogConfig') or {}).get('Type') or 'json-file' - if log != 'json-file' or (host.get('LogConfig') or {}).get('Config'): - refusals.append('it logs otherwise than by default') if (config.get('Entrypoint') or None) != (image_config.get('Entrypoint') or None): refusals.append('its entry point is not its image\'s') - if (config.get('Cmd') or None) != (image_config.get('Cmd') or None) and config.get('Cmd'): - command = config['Cmd'] - else: - command = [] if (config.get('WorkingDir') or '') != (image_config.get('WorkingDir') or ''): refusals.append('its working directory is not its image\'s') if config.get('Healthcheck') and config.get('Healthcheck') != image_config.get('Healthcheck'): refusals.append('it has a health check of its own') - networks = list(((container.get('NetworkSettings') or {}).get('Networks') or {}).keys()) - mode = host.get('NetworkMode') or 'default' - if len(networks) > 1: + if len(((container.get('NetworkSettings') or {}).get('Networks') or {})) > 1: refusals.append('it is on more than one network') + for mount in container.get('Mounts') or []: + if mount.get('Type') not in ('bind', 'volume'): + refusals.append(f"it has a {mount.get('Type')} mount at {mount.get('Destination')}") + return refusals + + +def mount_option(mount): + source = mount['Source'] if mount.get('Type') == 'bind' else mount['Name'] + # An anonymous volume too is named, so the new container takes the same data. + modes = [part for part in (mount.get('Mode') or '').split(',') if part and part not in ('rw', 'ro')] + if not mount.get('RW', True): + modes.insert(0, 'ro') + return f"{source}:{mount['Destination']}" + (':' + ','.join(modes) if modes else '') + + +def run_options(container, old_image): + """The options of `docker run` that make a container like this one, its + environment beyond what its image sets, and its command when it is not + the image's. Refused when it has one this does not carry over.""" + config, host = container['Config'], container['HostConfig'] + image_config = old_image.get('Config') or {} + refusals = refusals_of(container, image_config) if refusals: raise Refused('The container cannot be carried over as it is: ' + '; '.join(refusals) + '. Upgrade it by hand.') + if (config.get('Cmd') or None) != (image_config.get('Cmd') or None) and config.get('Cmd'): + command = config['Cmd'] + else: + command = [] + mode = host.get('NetworkMode') or 'default' options = ['--detach', '--name', container['Name'].lstrip('/')] policy = host.get('RestartPolicy') or {} @@ -134,9 +175,10 @@ def run_options(container, old_image): options += ['--restart', policy['Name'] + (f':{retries}' if policy['Name'] == 'on-failure' and retries else '')] if mode not in ('default', 'bridge'): options += ['--network', mode] - if container['Config'].get('Hostname') and not container['Id'].startswith(container['Config']['Hostname']) \ - and mode != 'host': - options += ['--hostname', container['Config']['Hostname']] + if config.get('Hostname') and not container['Id'].startswith(config['Hostname']) and mode != 'host': + options += ['--hostname', config['Hostname']] + if config.get('Domainname'): + options += ['--domainname', config['Domainname']] for port, bindings in sorted((host.get('PortBindings') or {}).items()): target = port.removesuffix('/tcp') for binding in bindings or []: @@ -146,16 +188,10 @@ def run_options(container, old_image): address = f'[{address}]' options += ['--publish', f'{address}:{published}:{target}' if address else (f'{published}:{target}' if published else target)] + if host.get('PublishAllPorts'): + options += ['--publish-all'] for mount in container.get('Mounts') or []: - read_only = '' if mount.get('RW', True) else ':ro' - if mount.get('Type') == 'bind': - options += ['--volume', f"{mount['Source']}:{mount['Destination']}{read_only}"] - elif mount.get('Type') == 'volume': - # An anonymous volume too is named, so the new container takes the same data. - options += ['--volume', f"{mount['Name']}:{mount['Destination']}{read_only}"] - else: - raise Refused(f"The container has a {mount.get('Type')} mount at {mount.get('Destination')}, " - 'which this does not carry over. Upgrade it by hand.') + options += ['--volume', mount_option(mount)] for group in host.get('GroupAdd') or []: options += ['--group-add', group] for extra in host.get('ExtraHosts') or []: @@ -166,6 +202,19 @@ def run_options(container, old_image): options += ['--memory', str(host['Memory'])] if host.get('NanoCpus'): options += ['--cpus', str(host['NanoCpus'] / 1e9)] + if host.get('ShmSize') and host['ShmSize'] != DEFAULT_SHM: + options += ['--shm-size', str(host['ShmSize'])] + # The daemon's own log options, such as max-size in daemon.json, are set on + # every container: they are carried as they are, like any other. + log = host.get('LogConfig') or {} + if log.get('Type'): + options += ['--log-driver', log['Type']] + for key, value in sorted((log.get('Config') or {}).items()): + options += ['--log-opt', f'{key}={value}'] + if config.get('StopSignal') and config['StopSignal'] != image_config.get('StopSignal'): + options += ['--stop-signal', config['StopSignal']] + if config.get('StopTimeout') is not None and config.get('StopTimeout') != image_config.get('StopTimeout'): + options += ['--stop-timeout', str(config['StopTimeout'])] if (config.get('User') or '') != (image_config.get('User') or ''): options += ['--user', config['User']] image_labels = image_config.get('Labels') or {} @@ -177,33 +226,50 @@ def run_options(container, old_image): return options, environment, command +def pairs(options): + """The options after --detach and the name, as (flag, value) with None + for a flag without one.""" + found, rest = [], options[3:] + alone = {'--init', '--publish-all'} + while rest: + flag = rest.pop(0) + found.append((flag, None if flag in alone else rest.pop(0))) + return found + + +def key_of(flag, value): + """What an option is about, so one asked for replaces the container's.""" + if flag == '--publish': + return flag, value.rsplit(':', 1)[-1] + if flag == '--volume': + return flag, value.split(':')[1] if ':' in value else value + if flag == '--add-host': + return flag, re.split('[:=]', value, maxsplit=1)[0] + return flag, value + + +def with_asked(options, asked): + """The container's options with those asked for in place of the ones for + the same port, mount point or host name, and the others added.""" + head, kept = options[:3], pairs(options) + keys = {key_of(flag, value) for flag, value in asked} + kept = [(flag, value) for flag, value in kept + if flag not in ('--publish', '--volume', '--add-host', '--group-add') or key_of(flag, value) not in keys] + merged = kept + [option for option in asked if option not in kept] + out = list(head) + for flag, value in merged: + out += [flag] if value is None else [flag, value] + return out + + def running(name): done = docker('container', 'inspect', '--format', '{{.State.Running}}', name, check=False) return done.returncode == 0 and done.stdout.strip() == 'true' -def upgrade(name, version): - if not VERSION.match(version): - raise Refused(f'{version!r} is not a version of berth, such as 0.6.0') - found = docker('container', 'inspect', name, check=False) - if found.returncode != 0: - raise Refused(f'There is no container {name} on this node.') - container = json.loads(found.stdout)[0] - current = container['Config']['Image'] - target = f'{repository(current)}:{version}' - old_image = inspect('image', container['Image']) - labels = (old_image.get('Config') or {}).get('Labels') or {} - running_version = labels.get('org.opencontainers.image.version') or current.rsplit(':', 1)[-1] - if version_key(running_version) and version_key(version) and version_key(version) < version_key(running_version): - raise Refused(f'{name} runs berth {running_version}, which is newer than {version}. ' - 'An older server may not read what a newer one stored, so it is not done here.') - options, environment, command = run_options(container, old_image) - print(f'Pulling {target}', flush=True) - docker('pull', target, capture=False) - if inspect('image', target)['Id'] == container['Image']: - print(f'{name} runs {target} already.') - return - previous = f'{name}-before-{version}' +def replace(name, container, current, target, options, environment, command, label): + """Run `target` as `name` with these options, in place of the container.""" + previous = f'{name}-before-{label}' docker('container', 'rm', '--force', previous, check=False) with tempfile.NamedTemporaryFile('w', prefix='berth-env-', delete=False) as env_file: os.chmod(env_file.name, 0o600) @@ -225,16 +291,92 @@ def upgrade(name, version): finally: os.unlink(env_file.name) docker('container', 'rm', previous) + + +def found_container(name): + found = docker('container', 'inspect', name, check=False) + if found.returncode != 0: + raise Refused(f'There is no container {name} here.') + return json.loads(found.stdout)[0] + + +def refuse_older(name, running_version, version): + if version_key(running_version) and version_key(version) and version_key(version) < version_key(running_version): + raise Refused(f'{name} runs berth {running_version}, which is newer than {version}. ' + 'An older server may not read what a newer one stored, so it is not done here.') + + +def upgrade(name, version): + """The node's action: the container's repository at `version`.""" + 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) + current = container['Config']['Image'] + target = f'{repository(current)}:{version}' + old_image = inspect('image', container['Image']) + refuse_older(name, image_version(old_image, current), version) + options, environment, command = run_options(container, old_image) + print(f'Pulling {target}', flush=True) + docker('pull', target, capture=False) + if inspect('image', target)['Id'] == container['Image']: + print(f'{name} runs {target} already.') + return + replace(name, container, current, target, options, environment, command, version) print(f'{name} runs {target} now, with its data and options as they were.') +def remake(name, image, pull, keep_image, asked): + """The recipe's: `image`, or the container's own, with `asked` set over + the container's options.""" + container = found_container(name) + current = container['Config']['Image'] + old_image = inspect('image', container['Image']) + options, environment, command = run_options(container, old_image) + target = current if keep_image else image + if pull and not keep_image: + print(f'Pulling {target}', flush=True) + docker('pull', target, capture=False) + found = docker('image', 'inspect', target, check=False) + if found.returncode != 0: + if keep_image: + raise Refused(f'The image {target} of {name} is not here any more. Update it with the image to run.') + docker('pull', target, capture=False) + found = docker('image', 'inspect', target) + new_image = json.loads(found.stdout)[0] + if keep_image and new_image['Id'] != container['Image']: + # The tag was pulled again since: making the container again would update it unasked. + target = container['Image'] + new_image = old_image + refuse_older(name, image_version(old_image, current), image_version(new_image, target)) + wanted = with_asked(options, asked) + same = sorted(pairs(wanted), key=str) == sorted(pairs(options), key=str) + if new_image['Id'] == container['Image'] and same: + 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.') + + def main(): - if len(sys.argv) not in (2, 3): - sys.exit(__doc__.strip().splitlines()[2].strip()) - name = sys.argv[1] - version = sys.argv[2] if len(sys.argv) == 3 else os.environ.get('BERTH_ARG_VERSION', '') + if len(sys.argv) > 2 and sys.argv[2].startswith('--'): + parser = argparse.ArgumentParser(prog='berth-server-upgrade') + parser.add_argument('container') + parser.add_argument('--image', required=True) + parser.add_argument('--pull', action='store_true') + parser.add_argument('--keep-image', action='store_true') + for flag in ('--publish', '--volume', '--add-host', '--group-add'): + parser.add_argument(flag, action='append', default=[]) + args = parser.parse_args() + 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) in (2, 3): + 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: + sys.exit('Usage: berth-server-upgrade CONTAINER [VERSION | --image IMAGE …]') try: - upgrade(name, version.strip()) + work() except Refused as refused: print(f'✗ {refused}', file=sys.stderr) sys.exit(1) diff --git a/provision/ansible/roles/devbox/tasks/node_berth.yml b/provision/ansible/roles/devbox/tasks/node_berth.yml index 8f3c40f..3e16d57 100644 --- a/provision/ansible/roles/devbox/tasks/node_berth.yml +++ b/provision/ansible/roles/devbox/tasks/node_berth.yml @@ -46,6 +46,26 @@ dest: /usr/local/sbin/devbox-node-firewall mode: '0755' when: devbox_manage_firewall +# A machine taken in may let SSH in from some addresses only, by rules made by +# hand such as those of the incus-host recipe of 0.5. Those stay and are not +# widened: SSH stays open to them and to where this apply came from. +- name: Read the firewall's rules + ansible.builtin.command: ufw show added + register: devbox_node_ufw_added + changed_when: false + failed_when: false + when: devbox_manage_firewall +- name: Keep SSH as limited as it was + ansible.builtin.set_fact: + devbox_node_ssh_limited: >- + {{ devbox_ssh_from == 'any' + and (devbox_node_ufw_added.stdout_lines | default([]) | select('match', '^ufw allow from [^ ]+ to any port 22( proto tcp)?$') | list | length > 0) + and (devbox_node_ufw_added.stdout_lines | default([]) | select('match', '^ufw allow (in )?(22|22/tcp|ssh|OpenSSH)( |$)') | list | length == 0) }} + when: devbox_manage_firewall +- name: Say that SSH stays limited + ansible.builtin.debug: + msg: SSH stays open only to the addresses the firewall's own rules name, and to the address this apply came from. Delete those rules by hand to open it to every address. + when: devbox_manage_firewall and devbox_node_ssh_limited | bool - name: Set the node's firewall ansible.builtin.command: /usr/local/sbin/devbox-node-firewall args: @@ -55,8 +75,8 @@ when: devbox_manage_firewall vars: devbox_node_firewall: >- - {{ ([('allow 22/tcp' if devbox_ssh_from == 'any' else 'allow from ' ~ devbox_ssh_from ~ ' to any port 22 proto tcp')] - + (['allow from ' ~ devbox_apply_from ~ ' to any port 22 proto tcp'] if devbox_apply_from | length > 0 and devbox_ssh_from != 'any' else []) + {{ (([] if devbox_node_ssh_limited | bool else [('allow 22/tcp' if devbox_ssh_from == 'any' else 'allow from ' ~ devbox_ssh_from ~ ' to any port 22 proto tcp')]) + + (['allow from ' ~ devbox_apply_from ~ ' to any port 22 proto tcp'] if devbox_apply_from | length > 0 and (devbox_ssh_from != 'any' or devbox_node_ssh_limited | bool) else []) + (['allow from ' ~ devbox_bootstrap_ssh_cidr ~ ' to any port 22 proto tcp'] if devbox_bootstrap_ssh_cidr | length > 0 else []) + (['allow in on tailscale0 to any port 22 proto tcp', 'allow 41641/udp'] if devbox_install_tailscale else []) + devbox_firewall_rules) | unique }} diff --git a/provision/ansible/roles/devbox/tasks/node_ssh.yml b/provision/ansible/roles/devbox/tasks/node_ssh.yml index 0c91805..b5a8bac 100644 --- a/provision/ansible/roles/devbox/tasks/node_ssh.yml +++ b/provision/ansible/roles/devbox/tasks/node_ssh.yml @@ -38,8 +38,24 @@ register: devbox_node_keys changed_when: false failed_when: false +# A machine taken in may let root in with keys already, as the one the berth +# server recipes of 0.5 set up does for the server in its container. What +# berth wrote before is kept as it is. +- name: See whether root logs in with keys here already + ansible.builtin.shell: | + if [ -f /etc/ssh/sshd_config.d/00-devbox.conf ]; then + awk 'tolower($1) == "permitrootlogin" {print $2}' /etc/ssh/sshd_config.d/00-devbox.conf + elif grep -qE '^(ssh-|ecdsa-|sk-)' /root/.ssh/authorized_keys 2>/dev/null; then + /usr/sbin/sshd -T 2>/dev/null | awk '$1 == "permitrootlogin" {print $2}' + else + echo no + fi + register: devbox_node_root_login + changed_when: false + failed_when: false # Only keys log in. Root keeps its login with keys when berth logs in as root, -# so that this berth is never locked out; any other SSH user leaves root none. +# so that this berth is never locked out, and when it had one before; any +# other SSH user leaves root none. It is never opened to passwords. - name: Require SSH keys, and keep root's login as berth uses it ansible.builtin.copy: # sshd uses the first value it reads, so this has to sort before other drop-ins. @@ -48,7 +64,7 @@ content: | PasswordAuthentication no KbdInteractiveAuthentication no - PermitRootLogin {{ 'prohibit-password' if devbox_node_user == 'root' else 'no' }} + PermitRootLogin {{ 'prohibit-password' if devbox_node_user == 'root' or (devbox_node_root_login.stdout | default('no') | trim) in ['yes', 'prohibit-password', 'without-password'] else 'no' }} validate: /usr/sbin/sshd -t -f %s register: devbox_node_sshd when: devbox_harden_ssh and devbox_node_keys.rc == 0 diff --git a/provision/ansible/roles/devbox/tasks/tailnet_check.yml b/provision/ansible/roles/devbox/tasks/tailnet_check.yml index eb7d631..66ab22d 100644 --- a/provision/ansible/roles/devbox/tasks/tailnet_check.yml +++ b/provision/ansible/roles/devbox/tasks/tailnet_check.yml @@ -2,7 +2,8 @@ # A host that joins the tailnet and is not on it needs a one-use key: the # one of its seed, or berth's command for one. Without either the play stops # here, before it changes anything, with what berth said is missing. A host -# on the tailnet already needs no key. +# on the tailnet already needs no key, and one with devbox_tailnet_optional +# goes on without the tailnet, as berth 0.5 did. - name: Inspect Tailscale enrollment ansible.builtin.command: tailscale status --json register: devbox_tailnet_status @@ -22,3 +23,15 @@ - not devbox_tailnet_enrolled | bool - devbox_tailscale_auth_key | length == 0 - devbox_tailscale_key_command | length == 0 + - not devbox_tailnet_optional | default(false) | bool +- name: Say that the host stays off the tailnet + ansible.builtin.debug: + msg: >- + This host is not on the tailnet, and berth cannot make its key yet + ({{ devbox_tailnet_problem if devbox_tailnet_problem | length > 0 else 'nothing gave it one' }}). + The apply goes on without the tailnet, and berth reaches the host over SSH at its address. + when: + - not devbox_tailnet_enrolled | bool + - devbox_tailscale_auth_key | length == 0 + - devbox_tailscale_key_command | length == 0 + - devbox_tailnet_optional | default(false) | bool diff --git a/recipes/README.md b/recipes/README.md index 94700d2..8f432d8 100644 --- a/recipes/README.md +++ b/recipes/README.md @@ -21,12 +21,13 @@ first user collects the server's token, with `berth login URL`, so the token nev ## Get the recipes -A recipe needs only this folder. This fetches it into `recipes/` in the -current directory: +A recipe needs this folder, and `berth-server` also the helper that updates +the server's container. This fetches them into the current directory: ```bash curl -fsSL https://github.com/helightdev/berth/archive/refs/heads/main.tar.gz \ - | tar xz --strip-components=1 --wildcards '*/recipes' + | tar xz --strip-components=1 --wildcards '*/recipes' \ + '*/provision/ansible/roles/devbox/files/berth-server-upgrade.py' ``` The recipes share the tasks in `shared/`, so fetch the folder as a whole. diff --git a/recipes/berth-server/README.md b/recipes/berth-server/README.md index 0016ea9..9d95d92 100644 --- a/recipes/berth-server/README.md +++ b/recipes/berth-server/README.md @@ -25,7 +25,8 @@ On the machine itself: ```bash curl -fsSL https://github.com/helightdev/berth/archive/refs/heads/main.tar.gz \ - | tar xz --strip-components=1 --wildcards '*/recipes' + | tar xz --strip-components=1 --wildcards '*/recipes' \ + '*/provision/ansible/roles/devbox/files/berth-server-upgrade.py' cd recipes/berth-server sudo ./install.sh -e tailscale_auth_key=tskey-auth-EXAMPLE ``` @@ -39,8 +40,9 @@ From another machine that has Ansible: ansible-playbook -i 192.0.2.10, -u root site.yml -e tailscale_auth_key=tskey-auth-EXAMPLE ``` -A second run changes nothing that is already in place. It leaves a container -named `berth` as it is, and starts it when it was stopped. +A second run changes nothing that is already in place. A container named +`berth` keeps its image, data and options. A stopped one is started. It is made again only when it lacks one of the recipe's +options, such as the port or a folder in `/srv/berth`. ## Use it @@ -105,9 +107,8 @@ berth apply big The apply installs Incus and Docker, and the server registers their provisioners, which reach them on this machine. Docker's socket and the root login give root on this machine, so whoever has the server's token controls -the whole machine. A container from an earlier run keeps its options: remove -it with `sudo docker rm -f berth` and run the recipe again to give it these. -The data in `/srv/berth` stays. +the whole machine. A container from an earlier run keeps its data and its +own options and gets these: the run makes it again with both. Once the machine is a node, its action `upgrade-server` updates the server in place. See [Update it](#update-it). @@ -123,6 +124,7 @@ there, or keep your own file and pass it with `-e @my-vars.yml`. | `berth_server_dir` | `/srv/berth` | Where the server keeps its settings, data and SSH keys | | `berth_server_port` | `7777` | The port on this machine only | | `berth_server_tailscale_serve` | `true` | Put the server on the tailnet with TLS | +| `berth_server_update` | `false` | Pull the image and run the server from it when it is newer. See [Update it](#update-it) | | `berth_server_node` | `false` | Let the server apply this machine as a node. See [The server on a node](#the-server-on-a-node) | | `tailscale_auth_key` | empty | An enrollment key. Without one Tailscale is not installed | | `tailscale_login_server` | empty | The address of a Headscale server | @@ -139,25 +141,45 @@ from berth, which makes them through the tailnet's API with the secret ## Update it -On a node, run the node's action `upgrade-server` from a berth of the new -version. It starts the server from the image of that version with the same -data and options, and starts the old one again when the new one does not -come up: +Run the recipe again with `berth_server_update`. It pulls the image and, +when it is newer, makes the container again from it: ```bash -berth run big upgrade-server version=0.6.0 +sudo ./install.sh -e berth_server_update=true +``` + +The new container keeps the data in `/srv/berth` and every option of the old +one: its mounts, ports, extra host names, groups, environment, logging and +restart policy. An option the recipe sets, such as the port, takes the value +of this run. The old container is stopped and kept until the new one runs, +and starts again if the new one does not come up. A container with an option +the helper cannot carry over, such as `--privileged`, is left as it is, and +the run says which option it was. + +On a machine that runs Incus, the container keeps the name `incus-host`, at +which the provisioners of berth 0.5 reach it. A run without +`berth_server_update` leaves the image as it is. + +Pin a version with its tag: + +```bash +sudo ./install.sh -e berth_server_update=true -e berth_server_image=ghcr.io/helightdev/berth:0.6.0 ``` -Elsewhere, update it by hand. The data is in `/srv/berth`. A new container -picks up where the old one stopped. Pull the image, remove the container and -run the recipe again: +A server never goes back to an older version this way. Download the recipe +of the new version first, with the command in [Run it](#run-it), as each +version's recipe knows its own server. + +Once the machine is a node, its action `upgrade-server` does the same from a +berth of the new version: ```bash -sudo docker pull ghcr.io/helightdev/berth -sudo docker rm -f berth -sudo ./install.sh +berth run big upgrade-server version=0.6.0 ``` +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. + ## Good to know - The tailnet address needs HTTPS certificates switched on for your tailnet. diff --git a/recipes/berth-server/site.yml b/recipes/berth-server/site.yml index 5e989fb..dd77077 100644 --- a/recipes/berth-server/site.yml +++ b/recipes/berth-server/site.yml @@ -9,6 +9,20 @@ vars_files: - vars.yml tasks: + - name: Check that the helper that updates the server's container came with the recipe + ansible.builtin.stat: + path: "{{ playbook_dir }}/../../provision/ansible/roles/devbox/files/berth-server-upgrade.py" + delegate_to: localhost + become: false + register: berth_server_helper_found + + - name: Stop when it did not + ansible.builtin.assert: + that: berth_server_helper_found.stat.exists + fail_msg: >- + provision/ansible/roles/devbox/files/berth-server-upgrade.py is not beside recipes/. + Download the recipe with the command in its README, which takes that file too. + - name: Check that this is an Ubuntu the recipe knows ansible.builtin.assert: that: @@ -61,21 +75,37 @@ when: "'Status: active' not in firewall_state.stdout" changed_when: true + # A machine the incus-host recipe of berth 0.5 set up runs Incus, and the + # provisioners registered then reach it as incus-host, and Docker through + # its socket: the container keeps both. + - name: See whether this machine runs Incus + ansible.builtin.stat: + path: /var/lib/incus/server.crt + register: berth_server_incus + - name: Find the group of Docker's socket ansible.builtin.command: stat --format %g /var/run/docker.sock register: docker_socket_group changed_when: false - when: berth_server_node | bool + when: berth_server_node | bool or berth_server_incus.stat.exists - name: Run the berth server ansible.builtin.include_tasks: ../shared/berth-server.yml vars: + berth_server_helper: "{{ playbook_dir }}/../../provision/ansible/roles/devbox/files/berth-server-upgrade.py" # On a node the server reaches this machine as `node`, and its Docker # engine through the socket. The server in the image is no root, so it # joins the socket's group. - berth_server_run_options: >- - {{ ('--add-host node:host-gateway --volume /var/run/docker.sock:/var/run/docker.sock --group-add ' - ~ (docker_socket_group.stdout | trim)) if berth_server_node | bool else '' }} + berth_server_engine: "{{ berth_server_node | bool or berth_server_incus.stat.exists }}" + berth_server_options: >- + {{ ['--publish', '127.0.0.1:' ~ berth_server_port ~ ':7777', + '--volume', berth_server_dir ~ '/config:/config', + '--volume', berth_server_dir ~ '/data:/data', + '--volume', berth_server_dir ~ '/ssh:/home/berth/.ssh'] + + (['--add-host', 'node:host-gateway'] if berth_server_node | bool else []) + + (['--add-host', 'incus-host:host-gateway'] if berth_server_incus.stat.exists else []) + + (['--volume', '/var/run/docker.sock:/var/run/docker.sock', + '--group-add', docker_socket_group.stdout | trim] if berth_server_engine | bool else []) }} # Only the public half comes out of the container; berth makes its key there. - name: Read the berth server's public key diff --git a/recipes/berth-server/vars.yml b/recipes/berth-server/vars.yml index 61c1f0c..323cd8d 100644 --- a/recipes/berth-server/vars.yml +++ b/recipes/berth-server/vars.yml @@ -12,12 +12,17 @@ berth_server_port: 7777 # It needs the tailnet's HTTPS certificates switched on, and does nothing # on a machine that is on no tailnet. berth_server_tailscale_serve: true +# Pull berth_server_image and run the server from it when it is newer. A +# container that is there keeps its data and its options either way, and +# gets those of this file that it lacks. +# ./install.sh -e berth_server_update=true +berth_server_update: false # Make this machine a node of the server, a server berth applies itself: # the server gets root's login here with its own key, reaches this machine by # the name `node`, and gets the socket of its Docker engine. Then add it from -# the server, as the README says. A container from an earlier run keeps its -# options: remove it first (the data stays) to give it these. +# the server, as the README says. A container from an earlier run gets these +# and keeps its own. berth_server_node: false # ── The tailnet ─────────────────────────────────────────────────────────── diff --git a/recipes/shared/berth-server.yml b/recipes/shared/berth-server.yml index 4f6da2d..542d486 100644 --- a/recipes/shared/berth-server.yml +++ b/recipes/shared/berth-server.yml @@ -23,20 +23,33 @@ # Its port stays on this machine: the token gives full control of every host. - name: Start the berth server in a container - ansible.builtin.command: >- - docker run --detach --name berth --restart unless-stopped - --publish 127.0.0.1:{{ berth_server_port }}:7777 - --volume {{ berth_server_dir }}/config:/config - --volume {{ berth_server_dir }}/data:/data - --volume {{ berth_server_dir }}/ssh:/home/berth/.ssh - {{ berth_server_run_options | default('') }} - {{ berth_server_image | quote }} + ansible.builtin.command: + argv: "{{ ['docker', 'run', '--detach', '--name', 'berth', '--restart', 'unless-stopped'] + + berth_server_options + [berth_server_image] }}" when: berth_container.rc != 0 changed_when: true +# A container from an earlier run, by this recipe or by hand, keeps its data +# and every option of its own, such as a socket or a name it reaches: the +# helper makes it again only when the image or the options above differ, and +# starts the old one again when the new one does not come up. +- name: Bring the berth server's container up to date, with its options + ansible.builtin.script: + cmd: >- + {{ berth_server_helper | quote }} berth --image {{ berth_server_image | quote }} + {{ '--pull' if berth_server_update | bool else '--keep-image' }} + {% for flag in berth_server_options | batch(2) %}{{ flag[0] }} {{ flag[1] | quote }} {% endfor %} + executable: python3 + register: berth_container_remade + changed_when: "'is unchanged' not in berth_container_remade.stdout" + when: berth_container.rc == 0 + - name: Start a berth server that was stopped ansible.builtin.command: docker start berth - when: berth_container.rc == 0 and berth_container.stdout | trim != 'true' + when: + - berth_container.rc == 0 + - berth_container_remade is not changed + - berth_container.stdout | trim != 'true' changed_when: true - name: See whether the tailnet serves the berth server @@ -61,7 +74,7 @@ host: 127.0.0.1 port: "{{ berth_server_port }}" timeout: 60 - when: berth_container.rc != 0 + when: berth_container.rc != 0 or berth_container_remade is changed # The first user collects the token with this key, so whoever ran the # recipe hands on the key and never the token. Later runs leave it be. diff --git a/tests/e2e/e2e_restore.py b/tests/e2e/e2e_restore.py index b276d25..4cbe8a5 100644 --- a/tests/e2e/e2e_restore.py +++ b/tests/e2e/e2e_restore.py @@ -183,7 +183,9 @@ def test_6_without_the_key_file_berth_serve_refuses_and_writes_nothing(self): self.assertIn(str(absent), said) self.assertNotIn('Listening on', said) self.assertTrue(digest(database) == before, 'berth serve changed the database') - # SQLite's own files beside the database are no change; a key file or a new database would be. - self.assertEqual({path.name for path in self.keyless.iterdir()} - {'berth.db-wal', 'berth.db-shm'}, {'berth.db'}) + # SQLite's own files and berth's empty lock beside the database are no change; a key file, a copy or a new database would be. + beside = {'berth.db-wal', 'berth.db-shm', 'berth.db.convert.lock'} + self.assertEqual({path.name for path in self.keyless.iterdir()} - beside, {'berth.db'}) + self.assertEqual((self.keyless / 'berth.db.convert.lock').stat().st_size, 0) self.assertFalse((station.data / 'berth/serve.token').exists()) self.assertFalse((station.data / 'berth/keys').exists()) diff --git a/tests/e2e/e2e_update.py b/tests/e2e/e2e_update.py index 35cb294..9711f7d 100644 --- a/tests/e2e/e2e_update.py +++ b/tests/e2e/e2e_update.py @@ -7,14 +7,19 @@ (harness.extended()), which applied `up` 1.0.0 from the registry and the shipped `shipit`; Update leaves `up` at 2.0.0 and the host current, which ShippedChanged starts from. UpdateServed uses the shared state of berth serve -(harness.served_extended()), which applied `served` 1.0.0.""" +(harness.served_extended()), which applied `served` 1.0.0. FromBerth052 needs +no host: it opens a store that berth 0.5.2 wrote.""" import json +import shutil import unittest +from pathlib import Path from harness import (AGENT, HOST, SCRATCH, Workstation, extended, light_vars, needs_e2e, package_registry, plugin_version, random_text, served_extended, unb64, wait_for) PACKAGE = 'berth-plugin-up' +# A store berth 0.5.2 wrote, with made-up hosts and fake keys. +STORE_052 = Path(__file__).resolve().parents[2] / 'crates/berth-core/tests/fixtures/store-0.5.2.db' def version_of(applied, plugin): @@ -238,3 +243,38 @@ def run(command): kept.check(self, run) self.assertEqual(self.serve.call('GET', '/v1/kv/update/mark')['value'], {'mark': mark}) self.assertEqual(run('id -un').strip(), AGENT) + + +@needs_e2e +class FromBerth052(unittest.TestCase): + """The first start of this berth on the store of berth 0.5.2 copies it, + converts it and says so once; the next start says nothing.""" + + @classmethod + def setUpClass(cls): + cls.station = Workstation('from-0.5.2', ssh_key=False, trust=False) + cls.store = cls.station.data / 'berth/berth.db' + cls.store.parent.mkdir(parents=True, exist_ok=True, mode=0o700) + shutil.copy(STORE_052, cls.store) + cls.store.chmod(0o600) + + def test_the_store_is_copied_converted_and_left_alone_after(self): + first = self.station.berth('host', 'export', 'studio') + copy = self.store.with_name('berth.db.berth-0.5.2-schema-4') + self.assertIn(f'a copy of it as berth 0.5.2 left it is in {copy}', first.stderr) + self.assertIn('3 profiles say type for level and target for profile now', first.stderr) + self.assertEqual(copy.stat().st_mode & 0o777, 0o600) + studio = json.loads(first.stdout) + self.assertNotIn('level', studio) + self.assertNotIn('tailscale_auth_key', studio) + pier = json.loads(self.station.berth('host', 'export', 'pier').stdout) + self.assertEqual(pier['type'], 'managed') + self.assertTrue(pier['ansible_vars']['devbox_tailnet_optional']) + # The next start finds nothing to convert or copy. + copy.unlink() + again = self.station.berth('host', 'export', 'studio') + self.assertEqual(json.loads(again.stdout), studio) + self.assertNotIn('berth:', again.stderr) + self.assertFalse(copy.exists()) + secrets = self.station.berth('secret', 'list').stdout + self.assertIn('tailnet_api_key', secrets) diff --git a/tests/e2e/run.py b/tests/e2e/run.py index aec9613..edf5089 100644 --- a/tests/e2e/run.py +++ b/tests/e2e/run.py @@ -77,9 +77,10 @@ ], # The bare host first: the ways berth logs in, and berth's controller # alone, which leave it as they found it. Then a host that only ever - # takes berth's own key. The provisioners' tests need no host at all. - # Last, a node, which stays one. - 'dock': ['e2e_access', 'e2e_controller.Controller', 'e2e_own_key', 'e2e_provisioners', 'e2e_node'], + # takes berth's own key. The provisioners' tests and the one of a store + # of berth 0.5.2 need no host at all. Last, a node, which stays one. + 'dock': ['e2e_access', 'e2e_controller.Controller', 'e2e_own_key', 'e2e_provisioners', + 'e2e_update.FromBerth052', 'e2e_node'], } diff --git a/tests/fixtures/berth-server-0.5.2.json b/tests/fixtures/berth-server-0.5.2.json new file mode 100644 index 0000000..540455a --- /dev/null +++ b/tests/fixtures/berth-server-0.5.2.json @@ -0,0 +1,243 @@ +{ + "_about": "docker inspect of a berth 0.5.2 server container that the incus-host recipe of 0.5.2 ran, on a Docker daemon with max-size and max-file in daemon.json; paths, ids and the group are made up", + "container": { + "Config": { + "AttachStderr": true, + "AttachStdin": false, + "AttachStdout": true, + "Cmd": [ + "serve", + "--listen", + "0.0.0.0:7777", + "--expose", + "--web" + ], + "Domainname": "", + "Entrypoint": [ + "tini", + "--", + "berth" + ], + "Env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "XDG_CONFIG_HOME=/config", + "XDG_DATA_HOME=/data", + "TERM=xterm-256color" + ], + "ExposedPorts": { + "7777/tcp": {} + }, + "Hostname": "4d1c0ffee000", + "Image": "ghcr.io/helightdev/berth:0.5.2", + "Labels": {}, + "OpenStdin": false, + "StdinOnce": false, + "Tty": false, + "User": "berth", + "Volumes": { + "/config": {}, + "/data": {} + }, + "WorkingDir": "" + }, + "HostConfig": { + "AutoRemove": false, + "Binds": [ + "/srv/berth/ssh:/home/berth/.ssh", + "/var/run/docker.sock:/var/run/docker.sock", + "/srv/berth/config:/config", + "/srv/berth/data:/data" + ], + "BlkioDeviceReadBps": [], + "BlkioDeviceReadIOps": [], + "BlkioDeviceWriteBps": [], + "BlkioDeviceWriteIOps": [], + "BlkioWeight": 0, + "BlkioWeightDevice": [], + "CapAdd": null, + "CapDrop": null, + "Cgroup": "", + "CgroupParent": "", + "CgroupnsMode": "private", + "ConsoleSize": [ + 0, + 0 + ], + "ContainerIDFile": "", + "CpuCount": 0, + "CpuPercent": 0, + "CpuPeriod": 0, + "CpuQuota": 0, + "CpuRealtimePeriod": 0, + "CpuRealtimeRuntime": 0, + "CpuShares": 0, + "CpusetCpus": "", + "CpusetMems": "", + "DeviceCgroupRules": null, + "DeviceRequests": null, + "Devices": [], + "Dns": null, + "DnsOptions": [], + "DnsSearch": [], + "ExtraHosts": [ + "incus-host:host-gateway" + ], + "GroupAdd": [ + "989" + ], + "IOMaximumBandwidth": 0, + "IOMaximumIOps": 0, + "IpcMode": "private", + "Isolation": "", + "Links": null, + "LogConfig": { + "Config": { + "max-file": "5", + "max-size": "10m" + }, + "Type": "json-file" + }, + "MaskedPaths": [ + "/proc/acpi", + "/proc/asound", + "/proc/interrupts", + "/proc/kcore", + "/proc/keys", + "/proc/latency_stats", + "/proc/sched_debug", + "/proc/scsi", + "/proc/timer_list", + "/proc/timer_stats", + "/sys/devices/virtual/powercap", + "/sys/firmware", + "/sys/devices/system/cpu/cpu0/thermal_throttle" + ], + "Memory": 0, + "MemoryReservation": 0, + "MemorySwap": 0, + "MemorySwappiness": null, + "NanoCpus": 0, + "NetworkMode": "bridge", + "OomKillDisable": false, + "OomScoreAdj": 0, + "PidMode": "", + "PidsLimit": null, + "PortBindings": { + "7777/tcp": [ + { + "HostIp": "127.0.0.1", + "HostPort": "7777" + } + ] + }, + "Privileged": false, + "PublishAllPorts": false, + "ReadonlyPaths": [ + "/proc/bus", + "/proc/fs", + "/proc/irq", + "/proc/sys", + "/proc/sysrq-trigger" + ], + "ReadonlyRootfs": false, + "RestartPolicy": { + "MaximumRetryCount": 0, + "Name": "unless-stopped" + }, + "Runtime": "runc", + "SecurityOpt": null, + "ShmSize": 67108864, + "UTSMode": "", + "Ulimits": [], + "UsernsMode": "", + "VolumeDriver": "", + "VolumesFrom": null + }, + "Id": "4d1c0ffee0000000000000000000000000000000000000000000000000000000", + "Image": "sha256:0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a", + "Mounts": [ + { + "Destination": "/data", + "Mode": "", + "Propagation": "rprivate", + "RW": true, + "Source": "/srv/berth/data", + "Type": "bind" + }, + { + "Destination": "/home/berth/.ssh", + "Mode": "", + "Propagation": "rprivate", + "RW": true, + "Source": "/srv/berth/ssh", + "Type": "bind" + }, + { + "Destination": "/var/run/docker.sock", + "Mode": "", + "Propagation": "rprivate", + "RW": true, + "Source": "/var/run/docker.sock", + "Type": "bind" + }, + { + "Destination": "/config", + "Mode": "", + "Propagation": "rprivate", + "RW": true, + "Source": "/srv/berth/config", + "Type": "bind" + } + ], + "Name": "/berth", + "NetworkSettings": { + "Networks": { + "bridge": { + "Gateway": "172.17.0.1", + "IPAddress": "172.17.0.2", + "MacAddress": "02:42:ac:11:00:02", + "NetworkID": "" + } + } + }, + "State": { + "Running": true, + "Status": "running" + } + }, + "image": { + "Config": { + "ArgsEscaped": true, + "Cmd": [ + "serve", + "--listen", + "0.0.0.0:7777", + "--expose", + "--web" + ], + "Entrypoint": [ + "tini", + "--", + "berth" + ], + "Env": [ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "XDG_CONFIG_HOME=/config", + "XDG_DATA_HOME=/data", + "TERM=xterm-256color" + ], + "ExposedPorts": { + "7777/tcp": {} + }, + "User": "berth", + "Volumes": { + "/config": {}, + "/data": {} + } + }, + "Id": "sha256:0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a", + "RepoTags": [ + "ghcr.io/helightdev/berth:0.5.2" + ] + } +} diff --git a/tests/test_node.py b/tests/test_node.py index 312f009..48fd8b6 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -94,7 +94,8 @@ def test_root_keeps_a_login_with_keys_and_others_none(self): task = node_named('Require SSH keys, and keep root\'s login as berth uses it') content = task['ansible.builtin.copy']['content'] self.assertIn('PasswordAuthentication no', content) - self.assertIn("PermitRootLogin {{ 'prohibit-password' if devbox_node_user == 'root' else 'no' }}", content) + self.assertIn("PermitRootLogin {{ 'prohibit-password' if devbox_node_user == 'root' or", content) + self.assertNotIn("'yes' if", content) # Only when the SSH user has a key, which berth's own is, so no apply locks berth out. self.assertEqual(task['when'], 'devbox_harden_ssh and devbox_node_keys.rc == 0') given = node_named('Give the SSH user the keys of the berth that applies this') @@ -288,6 +289,64 @@ def test_ssh_stays_whatever_ufw_makes_of_a_rule(self): self.assertIn("allow 22/tcp comment 'berth'", self.kept()['rules']) +@unittest.skipUnless(yaml and PLAYBOOK, 'Run with the Ansible venv Python for the playbook') +class TakingIn(unittest.TestCase): + """What a node keeps of a machine set up before: the firewall's rules for + SSH and root's login with keys, as the role's own expressions make them.""" + + def render(self, facts, wanted): + """`wanted` as the role's tasks make it from these facts, run by ansible-playbook.""" + limited = node_named('Keep SSH as limited as it was') + firewall = node_named("Set the node's firewall") + sshd = node_named("Require SSH keys, and keep root's login as berth uses it") + scratch = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, scratch) + tasks = [{'name': 'Facts', 'ansible.builtin.set_fact': facts}, + {'name': limited['name'], 'ansible.builtin.set_fact': limited['ansible.builtin.set_fact']}, + {'name': 'Show', 'ansible.builtin.copy': {'dest': str(scratch / 'shown.json'), 'content': ( + '{{ {"firewall": devbox_node_firewall, "sshd": sshd} | to_json }}')}, + 'vars': dict(firewall['vars'], sshd=sshd['ansible.builtin.copy']['content'])}] + playbook = scratch / 'play.yml' + playbook.write_text(yaml.safe_dump([{'hosts': 'all', 'gather_facts': False, 'vars': { + 'devbox_manage_firewall': True, 'devbox_ssh_from': 'any', 'devbox_apply_from': '192.0.2.5', + 'devbox_bootstrap_ssh_cidr': '', 'devbox_install_tailscale': False, 'devbox_firewall_rules': [], + 'devbox_node_user': 'berth'}, 'tasks': tasks}])) + done = subprocess.run([PLAYBOOK, '-i', 'localhost,', '--connection', 'local', str(playbook)], + capture_output=True, text=True, timeout=120, + env=dict(os.environ, ANSIBLE_LOCALHOST_WARNING='0')) + self.assertEqual(done.returncode, 0, done.stdout + done.stderr) + return json.loads((scratch / 'shown.json').read_text())[wanted] + + def test_ssh_that_was_limited_by_hand_is_not_opened(self): + added = {'stdout_lines': ["Added user rules (see 'ufw status' for running firewall):", + 'ufw allow from 198.51.100.7 to any port 22 proto tcp', + 'ufw allow in on incusbr0']} + rules = self.render({'devbox_node_ufw_added': added}, 'firewall') + self.assertNotIn('allow 22/tcp', rules) + self.assertIn('allow from 192.0.2.5 to any port 22 proto tcp', rules) + # One rule that opens SSH to all is no limit to keep. + added['stdout_lines'].append('ufw allow 22/tcp') + self.assertIn('allow 22/tcp', self.render({'devbox_node_ufw_added': added}, 'firewall')) + # A machine with no rules gets the profile's. + self.assertIn('allow 22/tcp', self.render({'devbox_node_ufw_added': {'stdout_lines': []}}, 'firewall')) + + def test_root_keeps_a_login_with_keys_it_had(self): + for found, kept in (('prohibit-password', 'prohibit-password'), ('yes', 'prohibit-password'), + ('without-password', 'prohibit-password'), ('no', 'no'), ('', 'no')): + facts = {'devbox_node_ufw_added': {'stdout_lines': []}, 'devbox_node_root_login': {'stdout': found}} + self.assertIn(f'PermitRootLogin {kept}\n', self.render(facts, 'sshd'), found) + facts = {'devbox_node_ufw_added': {'stdout_lines': []}, 'devbox_node_root_login': {'stdout': 'no'}, + 'devbox_node_user': 'root'} + self.assertIn('PermitRootLogin prohibit-password\n', self.render(facts, 'sshd')) + + def test_the_facts_are_read_before_they_are_used(self): + names = [task['name'] for task in node_tasks()] + self.assertLess(names.index('See whether root logs in with keys here already'), + names.index("Require SSH keys, and keep root's login as berth uses it")) + self.assertLess(names.index("Read the firewall's rules"), names.index('Keep SSH as limited as it was')) + self.assertLess(names.index('Keep SSH as limited as it was'), names.index("Set the node's firewall")) + + FAKE_DOCKER = r'''#!/usr/bin/env python3 import json, os, sys state = os.environ['FAKE_DOCKER'] @@ -299,7 +358,50 @@ def test_ssh_stays_whatever_ufw_makes_of_a_rule(self): data['env_mode'] = oct(os.stat(args[args.index('--env-file') + 1]).st_mode & 0o777) def save(): json.dump(data, open(state, 'w')) -containers = data['containers'] +containers, images = data['containers'], data['images'] +VALUED = {'--name', '--restart', '--network', '--hostname', '--domainname', '--publish', '--volume', + '--group-add', '--add-host', '--memory', '--cpus', '--shm-size', '--log-driver', '--log-opt', + '--stop-signal', '--stop-timeout', '--user', '--label', '--env-file'} +def made(args): + # A container as docker inspect shows one that `docker run ARGS` made. + given, rest = {}, list(args) + while rest and rest[0].startswith('--'): + flag = rest.pop(0) + given.setdefault(flag, []).append(rest.pop(0) if flag in VALUED else True) + ref, command = rest[0], rest[1:] + image = images[ref] + one = lambda flag, default=None: given.get(flag, [default])[-1] + env = list(image['Config'].get('Env') or []) + if '--env-file' in given: + env += [line for line in open(one('--env-file')).read().splitlines() if line] + ports = {} + for published in given.get('--publish', []): + parts = published.rsplit(':', 2) + ports.setdefault(parts[-1] + '/tcp', []).append({'HostIp': parts[0] if len(parts) == 3 else '', + 'HostPort': parts[-2] if len(parts) > 1 else ''}) + mounts = [] + for volume in given.get('--volume', []): + source, target, *mode = volume.split(':') + mounts.append({'Type': 'bind' if source.startswith('/') else 'volume', 'Source': source, 'Name': source, + 'Destination': target, 'Mode': ','.join(mode), 'RW': 'ro' not in mode}) + restart = (one('--restart', 'no') or 'no').split(':') + return { + 'Id': 'f00d' + str(len(data['calls'])).rjust(12, '0'), 'Name': '/' + one('--name'), 'Image': image['Id'], + 'State': {'Running': data.get('new_runs', True)}, + 'Config': {'Image': ref, 'Hostname': 'f00d' + str(len(data['calls'])).rjust(8, '0'), 'Env': env, + 'Cmd': command or image['Config'].get('Cmd'), 'Entrypoint': image['Config'].get('Entrypoint'), + 'User': one('--user', image['Config'].get('User') or ''), + 'WorkingDir': image['Config'].get('WorkingDir') or '', 'Labels': {}}, + 'HostConfig': {'RestartPolicy': {'Name': restart[0], 'MaximumRetryCount': int(restart[1]) if len(restart) > 1 else 0}, + 'NetworkMode': one('--network', 'bridge'), 'PortBindings': ports, + 'GroupAdd': given.get('--group-add') or None, 'ExtraHosts': given.get('--add-host') or None, + 'LogConfig': {'Type': one('--log-driver', 'json-file'), + 'Config': dict(opt.split('=', 1) for opt in given.get('--log-opt', []))}, + 'Runtime': 'runc', 'IpcMode': 'private', 'ShmSize': 67108864, 'CgroupnsMode': 'private', + 'MaskedPaths': ['/proc/acpi', '/proc/kcore']}, + 'NetworkSettings': {'Networks': {one('--network', 'bridge'): {}}}, + 'Mounts': mounts, + } if args[:2] == ['container', 'inspect'] and args[2] == '--format': found = containers.get(args[-1]) save() @@ -312,24 +414,35 @@ def save(): print('Error: No such container', file=sys.stderr); sys.exit(1) print(json.dumps([found])); sys.exit(0) if args[:2] == ['image', 'inspect']: - save(); print(json.dumps([data['images'][args[2]]])); sys.exit(0) + save() + 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] == ['pull']: - data['images'][args[1]] = data['images']['new'] + images[args[1]] = images[images['new']['Id']] = images['new'] save(); sys.exit(0) if args[:2] == ['container', 'rename']: containers[args[3]] = containers.pop(args[2]) if args[:2] == ['container', 'stop']: containers[args[2]]['State']['Running'] = False -if args[:2] == ['container', 'start']: - containers[args[2]]['State']['Running'] = True +if args[:2] in (['container', 'start'], ['start']): + containers[args[-1]]['State']['Running'] = True if args[:2] == ['container', 'rm']: containers.pop(args[-1], None) if args[:1] == ['run']: - name = args[args.index('--name') + 1] - containers[name] = {'State': {'Running': data.get('new_runs', True)}} + container = made(args[1:]) + containers[container['Name'][1:]] = container +if args[:1] == ['exec'] and args[-3:] == ['berth', 'server', 'setup-key']: + print('berth-setup-made-up-key') +if args[:1] == ['exec'] and args[-3:] == ['berth', 'key', 'show']: + print('ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMadeUpMadeUpMadeUpMadeUpMadeUpMadeUpMadeUp berth') save() ''' +# The berth server's container that the incus-host recipe of berth 0.5.2 ran, +# as docker inspect shows it on a daemon with log options in daemon.json. +FOUND_052 = json.loads((SRC / 'tests/fixtures/berth-server-0.5.2.json').read_text()) + def container(**host): """The berth server's container as the recipe ran it, with what a real one has besides.""" @@ -376,22 +489,37 @@ def setUp(self): write_program(self.scratch / 'docker', FAKE_DOCKER) self.state = self.scratch / 'docker.json' - def upgrade(self, version, found=None, new_runs=True): - self.state.write_text(json.dumps({ - 'containers': {'berth': found or container()}, - 'images': {'sha256:old': image('0.5.2', 'sha256:old'), 'new': image(version, 'sha256:new')}, - 'new_runs': new_runs})) + def helper(self, *args, found=None, old_image=None, new='0.6.0', new_runs=True, images=None, keep=False): + 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}, + 'images': dict({found['Image']: old_image, found['Config']['Image']: old_image, + 'new': image(new, 'sha256:new')}, **(images or {})), + 'new_runs': new_runs})) + else: + data = json.loads(self.state.read_text()) + data['calls'] = [] + 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=version) - done = subprocess.run([sys.executable, str(UPGRADE), 'berth'], capture_output=True, text=True, + 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) return done, json.loads(self.state.read_text()) + def upgrade(self, version, found=None, new_runs=True): + return self.helper(found=found, new=version, new_runs=new_runs) + + @staticmethod + def run_call(data): + return next(call for call in data['calls'] if call[0] == 'run') + def test_the_new_container_keeps_the_data_and_the_options(self): done, data = self.upgrade('0.6.0') self.assertEqual(done.returncode, 0, done.stderr) self.assertIn('berth runs ghcr.io/helightdev/berth:0.6.0 now', done.stdout) - run = next(call for call in data['calls'] if call[0] == 'run') + run = self.run_call(data) words = ' '.join(run) for kept in ('--name berth', '--restart unless-stopped', '--publish 127.0.0.1:7777:7777', '--volume /srv/berth/data:/data', '--volume /srv/berth/ssh:/home/berth/.ssh', @@ -405,11 +533,35 @@ def test_the_new_container_keeps_the_data_and_the_options(self): self.assertEqual(sorted(data['containers']), ['berth']) self.assertIn(['pull', 'ghcr.io/helightdev/berth:0.6.0'], data['calls']) + def test_a_container_of_0_5_2_on_a_daemon_with_log_options_is_carried_over(self): + found, old = FOUND_052['container'], FOUND_052['image'] + new = dict(old, Id='sha256:new', Config=dict(old['Config'], Cmd=['serve', '--listen', '0.0.0.0:7777', '--expose'])) + done, data = self.helper(found=found, old_image=old, images={'new': new}) + self.assertEqual(done.returncode, 0, done.stderr) + run = self.run_call(data) + words = ' '.join(run) + for kept in ('--log-driver json-file', '--log-opt max-file=5', '--log-opt max-size=10m', + '--add-host incus-host:host-gateway', '--group-add 989', + '--volume /var/run/docker.sock:/var/run/docker.sock', '--volume /srv/berth/config:/config', + '--publish 127.0.0.1:7777:7777', '--restart unless-stopped'): + self.assertIn(kept, words) + # What docker fills in for every container is not an option of this one. + for left in ('--shm-size', '--hostname', '--user', '--privileged', 'MaskedPaths'): + self.assertNotIn(left, words) + # Its command was its image's, so the new image's own runs: no --web of 0.5.2. + self.assertEqual(run[-1], 'ghcr.io/helightdev/berth:0.6.0') + self.assertEqual(data['env_file'], '') + def test_an_option_it_cannot_carry_over_changes_nothing(self): done, data = self.upgrade('0.6.0', found=container(Privileged=True, Devices=[{'PathOnHost': '/dev/kvm'}])) self.assertNotEqual(done.returncode, 0) self.assertIn('it runs privileged; it has devices', done.stderr) self.assertFalse([call for call in data['calls'] if call[0] in ('pull', 'run', 'container') and call[1:2] != ['inspect']]) + for host in ({'ReadonlyRootfs': True}, {'DeviceRequests': [{'Count': -1}]}, {'Runtime': 'sysbox-runc'}, + {'AutoRemove': True}): + done, _ = self.upgrade('0.6.0', found=container(**host)) + self.assertNotEqual(done.returncode, 0, host) + self.assertIn('Upgrade it by hand', done.stderr) def test_an_older_version_is_refused(self): done, data = self.upgrade('0.5.1') @@ -427,6 +579,48 @@ def test_the_old_container_runs_again_when_the_new_one_stops(self): self.assertTrue(data['containers']['berth']['State']['Running']) self.assertIn(['container', 'rename', 'berth-before-0.6.0', 'berth'], data['calls']) + def test_the_recipe_sets_its_options_over_the_containers_and_keeps_the_others(self): + asked = ['--publish', '127.0.0.1:7878:7777', '--volume', '/srv/berth/data:/data', + '--add-host', 'node:host-gateway', '--add-host', 'incus-host:host-gateway', '--group-add', '989'] + done, data = self.helper('--image', 'ghcr.io/helightdev/berth', '--pull', *asked) + self.assertEqual(done.returncode, 0, done.stderr) + run = self.run_call(data) + words = ' '.join(run) + self.assertIn('--publish 127.0.0.1:7878:7777', words) + self.assertNotIn('127.0.0.1:7777:7777', words) + self.assertEqual(words.count('--add-host incus-host:host-gateway'), 1) + self.assertEqual(words.count('--group-add 989'), 1) + for kept in ('--add-host node:host-gateway', '--volume /var/run/docker.sock:/var/run/docker.sock'): + self.assertIn(kept, words) + self.assertEqual(run[-1], 'ghcr.io/helightdev/berth') + # Run again with the same, it changes nothing. + done, data = self.helper('--image', 'ghcr.io/helightdev/berth', '--pull', *asked, keep=True) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn('berth is unchanged', done.stdout) + self.assertNotIn('run', [call[0] for call in data['calls']]) + + def test_without_an_update_the_recipe_keeps_the_image(self): + done, data = self.helper('--image', 'ghcr.io/helightdev/berth', '--keep-image', + '--publish', '127.0.0.1:7777:7777') + self.assertEqual(done.returncode, 0, done.stderr) + self.assertIn('berth is unchanged', done.stdout) + self.assertNotIn('pull', [call[0] for call in data['calls']]) + done, data = self.helper('--image', 'ghcr.io/helightdev/berth', '--keep-image', + '--add-host', 'node:host-gateway') + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(self.run_call(data)[-1], 'ghcr.io/helightdev/berth:0.5.2') + # A tag pulled again since is not taken up without an update. + done, data = self.helper('--image', 'x', '--keep-image', '--add-host', 'node:host-gateway', + images={'ghcr.io/helightdev/berth:0.5.2': image('0.6.0', 'sha256:new')}) + self.assertEqual(done.returncode, 0, done.stderr) + self.assertEqual(self.run_call(data)[-1], 'sha256:old') + + def test_the_recipe_never_goes_back_to_an_older_server(self): + done, data = self.helper('--image', 'ghcr.io/helightdev/berth:0.5.1', '--pull', new='0.5.1') + self.assertNotEqual(done.returncode, 0) + self.assertIn('which is newer than 0.5.1', done.stderr) + self.assertNotIn('run', [call[0] for call in data['calls']]) + if __name__ == '__main__': unittest.main() diff --git a/tests/test_server_recipe.py b/tests/test_server_recipe.py new file mode 100644 index 0000000..44c40ac --- /dev/null +++ b/tests/test_server_recipe.py @@ -0,0 +1,161 @@ +"""The berth server recipe's container, run end to end by ansible-playbook +against a fake docker: a first run, a run over a container that the +incus-host recipe of berth 0.5.2 made, and a second run that changes nothing.""" +import json +import os +import shutil +import socket +import subprocess +import tempfile +import unittest +from pathlib import Path + +from test_node import FAKE_DOCKER, FOUND_052, PLAYBOOK, UPGRADE, image, write_program, yaml + +SRC = Path(__file__).resolve().parents[1] +RECIPE = SRC / 'recipes/berth-server' +SHARED = SRC / 'recipes/shared/berth-server.yml' + +# The tasks of site.yml that run the server, in their order. +SERVER_TASKS = ['See whether this machine runs Incus', "Find the group of Docker's socket", 'Run the berth server'] + +# A listener on the server's port for the recipe to wait for, as the server in a new container is. +LISTENER = ''' +import socket, sys, time +server = socket.socket(); server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +server.bind(("127.0.0.1", int(sys.argv[1]))); server.listen(); server.settimeout(1) +end = time.time() + 30 +while time.time() < end: + try: + server.accept()[0].close() + except OSError: + pass +''' + + +def free_port(): + with socket.socket() as probe: + probe.bind(('127.0.0.1', 0)) + return probe.getsockname()[1] + + +@unittest.skipUnless(yaml and PLAYBOOK, 'Run with the Ansible venv Python for the playbook') +class ServerRecipe(unittest.TestCase): + def setUp(self): + self.scratch = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, self.scratch) + bin_dir = self.scratch / 'bin' + bin_dir.mkdir() + # Each `docker run` also opens the port, so the recipe finds the server answering. + write_program(bin_dir / 'docker', FAKE_DOCKER + f''' +if args[:1] == ['run']: + import subprocess + subprocess.Popen([sys.executable, '-c', {LISTENER!r}, os.environ['FAKE_PORT']], start_new_session=True, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) +''') + self.state = self.scratch / 'docker.json' + self.port = free_port() + self.incus = self.scratch / 'incus-server.crt' + (self.scratch / 'docker.sock').write_text('') + self.env = dict(os.environ, PATH=f'{bin_dir}:{os.environ["PATH"]}', FAKE_DOCKER=str(self.state), + FAKE_PORT=str(self.port), DEVBOX_DOCKER=str(bin_dir / 'docker'), + DEVBOX_UPGRADE_SETTLE='0', ANSIBLE_NOCOLOR='1', ANSIBLE_LOCALHOST_WARNING='0') + self.playbook = self.write_playbook() + + def write_playbook(self): + """The recipe's play with only the tasks that run the server, the + paths of this machine put in scratch.""" + play = yaml.safe_load((RECIPE / 'site.yml').read_text())[0] + tasks = [task for task in play['tasks'] if task['name'] in SERVER_TASKS] + self.assertEqual([task['name'] for task in tasks], SERVER_TASKS) + tasks[0]['ansible.builtin.stat']['path'] = str(self.incus) + tasks[1]['ansible.builtin.command'] = f'stat --format %g {self.scratch / "docker.sock"}' + shared = yaml.safe_load(SHARED.read_text()) + for task in shared: + # The folders belong to the user 1000, which only root or that user can make them. + if 'ansible.builtin.file' in task and os.getuid() not in (0, 1000): + task['ansible.builtin.file'].pop('owner') + task['ansible.builtin.file'].pop('group') + (self.scratch / 'shared.yml').write_text(yaml.safe_dump(shared, sort_keys=False)) + tasks[2]['ansible.builtin.include_tasks'] = str(self.scratch / 'shared.yml') + tasks[2]['vars']['berth_server_helper'] = str(UPGRADE) + variables = yaml.safe_load((RECIPE / 'vars.yml').read_text()) + variables.update(berth_server_dir=str(self.scratch / 'srv'), berth_server_port=self.port, + berth_server_tailscale_serve=False, tailscale_hostname='pier') + playbook = self.scratch / 'site.yml' + playbook.write_text(yaml.safe_dump([{ + 'name': play['name'], 'hosts': 'all', 'gather_facts': False, 'vars': variables, 'tasks': tasks}], + sort_keys=False)) + return playbook + + def docker(self, found=None): + new = image('0.6.0', 'sha256:new') + images = {'new': new, 'sha256:new': new, 'ghcr.io/helightdev/berth': new} + containers = {} + if found: + images.update({found['Image']: FOUND_052['image'], found['Config']['Image']: FOUND_052['image']}) + containers['berth'] = found + self.state.write_text(json.dumps({'containers': containers, 'images': images})) + + def apply(self, *extra): + data = json.loads(self.state.read_text()) + data['calls'] = [] + self.state.write_text(json.dumps(data)) + done = subprocess.run( + [PLAYBOOK, '--connection', 'local', '--inventory', 'localhost,', str(self.playbook), *extra], + capture_output=True, text=True, env=self.env, timeout=300) + self.assertEqual(done.returncode, 0, done.stdout + done.stderr) + return done, json.loads(self.state.read_text()) + + @staticmethod + def runs(data): + return [call for call in data['calls'] if call[0] == 'run'] + + def test_an_update_over_the_incus_host_recipes_container_keeps_what_it_reached(self): + self.docker(found=FOUND_052['container']) + done, data = self.apply('-e', 'berth_server_node=true', '-e', 'berth_server_update=true') + [run] = self.runs(data) + words = ' '.join(run) + for kept in ('--add-host incus-host:host-gateway', '--add-host node:host-gateway', + '--volume /var/run/docker.sock:/var/run/docker.sock', '--group-add 989', + '--log-opt max-size=10m', '--restart unless-stopped', + f'--publish 127.0.0.1:{self.port}:7777', f'--volume {self.scratch}/srv/data:/data'): + self.assertIn(kept, words) + self.assertNotIn('/srv/berth/data:/data', words) + self.assertIn(['pull', 'ghcr.io/helightdev/berth'], data['calls']) + self.assertEqual(run[-1], 'ghcr.io/helightdev/berth') + # The server keeps its token, so no setup key is made for a first user. + self.assertNotIn(['exec', 'berth', 'berth', 'server', 'setup-key'], data['calls']) + + # The same run again finds everything in place. + done, data = self.apply('-e', 'berth_server_node=true', '-e', 'berth_server_update=true') + self.assertEqual(self.runs(data), []) + self.assertRegex(done.stdout, r'localhost\s*: ok=\d+\s+changed=0\b') + + def test_a_run_without_an_update_keeps_the_image(self): + self.docker(found=FOUND_052['container']) + done, data = self.apply() + self.assertNotIn('pull', [call[0] for call in data['calls']]) + [run] = self.runs(data) + self.assertEqual(run[-1], 'ghcr.io/helightdev/berth:0.5.2') + self.assertIn('--add-host incus-host:host-gateway', ' '.join(run)) + + def test_a_first_run_on_a_machine_with_incus_gives_the_name_incus_host(self): + self.incus.write_text('made-up certificate') + self.docker() + done, data = self.apply() + [run] = self.runs(data) + words = ' '.join(run) + for given in ('--name berth', '--add-host incus-host:host-gateway', + '--volume /var/run/docker.sock:/var/run/docker.sock', f'--publish 127.0.0.1:{self.port}:7777'): + self.assertIn(given, words) + self.assertNotIn('node:host-gateway', words) + self.assertIn(['exec', 'berth', 'berth', 'server', 'setup-key'], data['calls']) + # Then it is in place. + done, data = self.apply() + self.assertEqual(self.runs(data), []) + self.assertRegex(done.stdout, r'localhost\s*: ok=\d+\s+changed=0\b') + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_tailnet_role.py b/tests/test_tailnet_role.py index 6fcf5f4..7933edb 100644 --- a/tests/test_tailnet_role.py +++ b/tests/test_tailnet_role.py @@ -108,6 +108,16 @@ def test_a_host_off_the_tailnet_without_a_way_to_a_key_stops_with_berths_words(s self.assertIn('secret tailnet_api_key', done.stdout) self.assertIn('devbox_install_tailscale to false', done.stdout) + def test_a_host_of_berth_0_5_that_was_never_joined_goes_on_without_the_tailnet(self): + problem = 'studio joins the tailnet at Tailscale, and berth cannot make its key. berth secret set tailnet_api_key' + done = self.run_tasks('tailnet.yml', LOGGED_OUT, devbox_tailnet_problem=problem, + devbox_tailnet_optional=True) + self.assertEqual(done.returncode, 0, done.stdout + done.stderr) + self.assertIn('The apply goes on without the tailnet', done.stdout) + self.assertIn(problem, done.stdout) + # Nothing tried to join it. + self.assertEqual(set(self.calls_made()), {'status --json'}) + def test_a_way_to_a_key_lets_the_play_go_on(self): command = ['touch', str(self.made)] self.assertEqual(self.run_tasks('tailnet_check.yml', LOGGED_OUT,