diff --git a/docs/architecture.md b/docs/architecture.md index 7c36d52..e5b8f49 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -275,7 +275,8 @@ sequenceDiagram `prepared`. 3. **Run.** `ops::deploy::execute` activates every node at once, its profiles in order, as root through `systemd-run`. Each line of output is prefixed with `[node.profile]`; the exit status is - the first failure's. + the first failure's. Inside the unit, `activate-rs`'s output goes through a `cat` that, once + the connection is gone, gives way to one discarding it, so a lost connection can't stop it. ### Builds diff --git a/docs/design.md b/docs/design.md index bf48e6a..17ffc9e 100644 --- a/docs/design.md +++ b/docs/design.md @@ -211,7 +211,8 @@ own fields. The module and the broker refuse any other operation or field. a typical use. - **Limits.** `auto` operations are rate-limited per requester, so a looping agent can't hammer boxes or APIs. Pending requests are capped per requester and overall to limit notification spam. -- **Restarts** drop pending requests and leases. +- **Restarts** drop pending requests and leases. Deploying the broker's box doesn't restart it; + restart `slop-broker.service` for a new version or configuration to take effect. ### Approvals web app @@ -393,8 +394,12 @@ broker runs it over SSH. 6. **Activate.** On every node at once, the broker runs deploy-rs's `activate-rs` on each profile in deploy-rs's order. - It runs as root through the target's `sudo` command (`sudo -u` by default), inside a - transient unit (`systemd-run`), so the activation finishes even if the connection drops, - including when the broker deploys its own box and restarts. + transient unit (`systemd-run`), so the activation finishes even if the connection drops. + Its output streams back until then and is discarded after, since `activate-rs` stops when + it can't write. + - Switching doesn't restart the broker (`restartIfChanged = false`), so it can deploy its own + box and report the result. A new version or configuration of the broker takes effect when + it's next restarted. - Output comes back prefixed with the node. - Deploys run in parallel; an agent that needs an order sends separate requests. diff --git a/nix/modules/nixos/broker.nix b/nix/modules/nixos/broker.nix index f1ac31c..c226783 100644 --- a/nix/modules/nixos/broker.nix +++ b/nix/modules/nixos/broker.nix @@ -270,6 +270,9 @@ in wantedBy = [ "multi-user.target" ]; after = [ "network-online.target" ]; wants = [ "network-online.target" ]; + # Switching doesn't restart the broker, so it can deploy its own box and see the deploy + # through; a new version or configuration takes effect at the next restart. + restartIfChanged = false; serviceConfig = { ExecStart = "${cfg.package}/bin/slop-broker serve --config ${brokerConfig}"; LoadCredential = diff --git a/src/ops/deploy.rs b/src/ops/deploy.rs index d0201c9..6ed7295 100644 --- a/src/ops/deploy.rs +++ b/src/ops/deploy.rs @@ -238,11 +238,16 @@ async fn inspect( Ok(Changes::parse(&String::from_utf8_lossy(&stdout))) } +/// Runs its arguments with their output to stdout until that breaks, then to nowhere, so a lost +/// connection can't stop an activation halfway: `activate-rs` panics when it can't write. +const UNTIL_DISCONNECTED: &str = r#"set -o pipefail; "$@" 2>&1 | { cat || cat >/dev/null; }"#; + fn activate_command(ssh: &SshConfig, node: &str, profile: &BuiltProfile, boot: bool, run: &str) -> Result> { let mut words: Vec = ssh::target(ssh, node)?.sudo.split_whitespace().map(Into::into).collect(); words.push("root".into()); - // A transient unit, so activation finishes even if this connection drops + // A transient unit, so activation finishes even if this connection drops or the process + // running it is killed words.extend( [ "/run/current-system/sw/bin/systemd-run", @@ -256,6 +261,7 @@ fn activate_command(ssh: &SshConfig, node: &str, profile: &BuiltProfile, boot: b .map(Into::into), ); words.push(format!("--unit=slop-deploy-{}-{}", &run[..8], profile.profile)); + words.extend(["/run/current-system/sw/bin/bash", "-c", UNTIL_DISCONNECTED, "slop-deploy"].map(Into::into)); // What deploy-rs runs, without magic rollback words.push(format!("{}/activate-rs", profile.path)); @@ -492,4 +498,36 @@ mod tests { assert!(words.contains(&"--unit=slop-deploy-01234567-system".to_owned())); assert_eq!(words.last().unwrap(), "--boot"); } + + #[test] + fn activation_outlives_its_output() { + use std::io::Read; + use std::process::{Command, Stdio}; + + let directory = tempfile::tempdir().unwrap(); + let marker = directory.path().join("finished"); + let activation = format!("sleep 0.5; for i in $(seq 1000); do echo line $i; done; touch {}", marker.display()); + + // The connection's end of the output closes before the activation writes anything. + let mut child = Command::new("bash") + .args(["-c", UNTIL_DISCONNECTED, "slop-deploy", "bash", "-c", &activation]) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + drop(child.stdout.take()); + + assert!(child.wait().unwrap().success()); + assert!(marker.exists()); + + // While it's there, output goes through. + let mut child = Command::new("bash") + .args(["-c", UNTIL_DISCONNECTED, "slop-deploy", "sh", "-c", "echo out; echo err >&2; exit 3"]) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + let mut output = String::new(); + child.stdout.take().unwrap().read_to_string(&mut output).unwrap(); + assert_eq!(output, "out\nerr\n"); + assert_eq!(child.wait().unwrap().code(), Some(3)); + } }