| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | | use anyhow::{bail, Context, Result}; |
| 9 | | use serde::{Deserialize, Serialize}; |
| 10 | | use std::path::{Path, PathBuf}; |
| 11 | | use std::time::Duration; |
| 12 | |
|
| 13 | |
|
| 14 | | pub const NEW: &str = "flow.new"; |
| 15 | |
|
| 16 | | pub const PREV: &str = "flow.prev"; |
| 17 | |
|
| 18 | | const PENDING: &str = "update.json"; |
| 19 | |
|
| 20 | | const LOG: &str = "updates.log"; |
| 21 | |
|
| 22 | | const PID: &str = "runner.pid"; |
| 23 | |
|
| 24 | | pub const MAX_STARTS: u32 = 3; |
| 25 | |
|
| 26 | | pub const PROVE_WITHIN: Duration = Duration::from_secs(10 * 60); |
| 27 | |
|
| 28 | |
|
| 29 | | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 30 | | pub struct Pending { |
| 31 | | pub from: String, |
| 32 | | pub to: String, |
| 33 | | |
| 34 | | pub exe: PathBuf, |
| 35 | | pub at_ms: u64, |
| 36 | | |
| 37 | | pub notice_secs: u64, |
| 38 | | |
| 39 | | pub starts: u32, |
| 40 | | } |
| 41 | |
|
| 42 | |
|
| 43 | | #[derive(Debug, PartialEq, Eq)] |
| 44 | | pub struct Installed { |
| 45 | | pub from: String, |
| 46 | | pub to: String, |
| 47 | | pub exe: PathBuf, |
| 48 | | |
| 49 | | pub told: Option<u32>, |
| 50 | | } |
| 51 | |
|
| 52 | |
|
| 53 | 4× | pub fn parse_version(line: &str) -> Option<String> { |
| 54 | 4× | let v = line.trim().strip_prefix("flow ")?.split_whitespace().next()?; |
| 55 | 8× | v.split('.').all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())).then(|| v.to_string()) |
| 56 | 4× | } |
| 57 | |
|
| 58 | |
|
| 59 | 3× | pub fn older(a: &str, b: &str) -> bool { |
| 60 | 17× | let parts = |v: &str| v.split('.').map(|p| p.parse::<u64>().unwrap_or(0)).collect::<Vec<_>>(); |
| 61 | 3× | parts(a) < parts(b) |
| 62 | 3× | } |
| 63 | |
|
| 64 | |
|
| 65 | 2× | fn machine_here() -> Option<u16> { |
| 66 | 2× | Some(match std::env::consts::ARCH { |
| 67 | 2× | "x86_64" => 62, |
| 68 | 0× | "aarch64" => 183, |
| 69 | 0× | "riscv64" => 243, |
| 70 | 0× | _ => return None, |
| 71 | | }) |
| 72 | 2× | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 3× | pub fn check_target(bytes: &[u8]) -> Result<()> { |
| 78 | 3× | if bytes.len() < 20 || &bytes[..4] != b"\x7fELF" { |
| 79 | 1× | bail!("not an ELF executable"); |
| 80 | 2× | } |
| 81 | 2× | if bytes[4] != 2 || bytes[5] != 1 { |
| 82 | 0× | bail!("not a 64-bit little-endian build"); |
| 83 | 2× | } |
| 84 | 2× | let machine = u16::from_le_bytes([bytes[18], bytes[19]]); |
| 85 | 2× | match machine_here() { |
| 86 | 2× | Some(m) if m == machine => Ok(()), |
| 87 | 1× | Some(_) => bail!("a build for another machine (ELF machine {machine}); this runner is {}", std::env::consts::ARCH), |
| 88 | 0× | None => bail!("updates are not supported on {}", std::env::consts::ARCH), |
| 89 | | } |
| 90 | 3× | } |
| 91 | |
|
| 92 | |
|
| 93 | 0× | fn run(file: &Path, args: &[&str], limit: Duration) -> Result<String> { |
| 94 | 0× | let mut child = std::process::Command::new(file) |
| 95 | 0× | .args(args) |
| 96 | 0× | .stdin(std::process::Stdio::null()) |
| 97 | 0× | .stdout(std::process::Stdio::piped()) |
| 98 | 0× | .stderr(std::process::Stdio::piped()) |
| 99 | 0× | .spawn() |
| 100 | 0× | .with_context(|| format!("starting {}", file.display()))?; |
| 101 | 0× | let start = std::time::Instant::now(); |
| 102 | | loop { |
| 103 | 0× | if child.try_wait()?.is_some() { |
| 104 | 0× | break; |
| 105 | 0× | } |
| 106 | 0× | if start.elapsed() > limit { |
| 107 | 0× | let _ = child.kill(); |
| 108 | 0× | let _ = child.wait(); |
| 109 | 0× | bail!("`{} {}` did not finish within {} s", file.display(), args.join(" "), limit.as_secs()); |
| 110 | 0× | } |
| 111 | 0× | std::thread::sleep(Duration::from_millis(50)); |
| 112 | | } |
| 113 | 0× | let out = child.wait_with_output()?; |
| 114 | 0× | if !out.status.success() { |
| 115 | 0× | bail!("`{} {}` failed ({}): {}", file.display(), args.join(" "), out.status, String::from_utf8_lossy(&out.stderr).trim()); |
| 116 | 0× | } |
| 117 | 0× | Ok(String::from_utf8_lossy(&out.stdout).into_owned()) |
| 118 | 0× | } |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | 0× | pub fn check(file: &Path, home: &Path) -> Result<String> { |
| 124 | 0× | let mut head = vec![0u8; 64]; |
| 125 | 0× | let n = std::io::Read::read(&mut std::fs::File::open(file).with_context(|| format!("opening {}", file.display()))?, &mut head)?; |
| 126 | 0× | check_target(&head[..n])?; |
| 127 | 0× | let limit = Duration::from_secs(30); |
| 128 | 0× | let line = run(file, &["--version"], limit)?; |
| 129 | 0× | let version = parse_version(&line).with_context(|| format!("{} does not say it is flow: {:?}", file.display(), line.trim()))?; |
| 130 | 0× | let home_s = home.to_string_lossy(); |
| 131 | 0× | let tiers = run(file, &["--home", &home_s, "tiers"], limit)?; |
| 132 | 0× | if !tiers.lines().any(|l| l.ends_with(": available")) { |
| 133 | 0× | bail!("flow {version} finds no tier it can run here:\n{}", tiers.trim()); |
| 134 | 0× | } |
| 135 | 0× | Ok(version) |
| 136 | 0× | } |
| 137 | |
|
| 138 | 2× | fn now_ms() -> u64 { |
| 139 | 2× | std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0) |
| 140 | 2× | } |
| 141 | |
|
| 142 | 2× | fn log(home: &Path, event: &str, from: &str, to: &str, why: &str) { |
| 143 | | use std::io::Write; |
| 144 | 2× | let line = serde_json::json!({"at_ms": now_ms(), "event": event, "from": from, "to": to, "why": why}); |
| 145 | 2× | if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(home.join(LOG)) { |
| 146 | 2× | let _ = writeln!(f, "{line}"); |
| 147 | 2× | } |
| 148 | 2× | } |
| 149 | |
|
| 150 | |
|
| 151 | 9× | pub fn pending(home: &Path) -> Option<Pending> { |
| 152 | 9× | serde_json::from_slice(&std::fs::read(home.join(PENDING)).ok()?).ok() |
| 153 | 9× | } |
| 154 | |
|
| 155 | 5× | fn save(home: &Path, p: &Pending) -> Result<()> { |
| 156 | 5× | let tmp = home.join(format!("{PENDING}.tmp")); |
| 157 | 5× | std::fs::write(&tmp, serde_json::to_vec_pretty(p)?)?; |
| 158 | 5× | std::fs::rename(&tmp, home.join(PENDING))?; |
| 159 | 5× | Ok(()) |
| 160 | 5× | } |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | 1× | pub fn swap(exe: &Path, staged: &Path) -> Result<()> { |
| 165 | 1× | let prev = exe.with_file_name(PREV); |
| 166 | 1× | let _ = std::fs::remove_file(&prev); |
| 167 | 1× | if std::fs::hard_link(exe, &prev).is_err() { |
| 168 | 0× | std::fs::copy(exe, &prev).with_context(|| format!("keeping {} as {}", exe.display(), prev.display()))?; |
| 169 | 1× | } |
| 170 | 1× | std::fs::rename(staged, exe).with_context(|| format!("putting {} in place of {}", staged.display(), exe.display()))?; |
| 171 | 1× | Ok(()) |
| 172 | 1× | } |
| 173 | |
|
| 174 | |
|
| 175 | 1× | pub fn restore(exe: &Path) -> Result<()> { |
| 176 | 1× | let prev = exe.with_file_name(PREV); |
| 177 | 1× | if !prev.is_file() { |
| 178 | 0× | bail!("{} is not there to go back to", prev.display()); |
| 179 | 1× | } |
| 180 | 1× | std::fs::rename(&prev, exe).with_context(|| format!("putting {} back", prev.display()))?; |
| 181 | 1× | Ok(()) |
| 182 | 1× | } |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | 0× | pub fn install(home: &crate::home::Home, exe: &Path, file: &Path, notice: Duration, dry_run: bool, allow_older: bool) -> Result<Installed> { |
| 188 | 0× | if !cfg!(unix) { |
| 189 | 0× | bail!("runners update themselves on Linux only, so far"); |
| 190 | 0× | } |
| 191 | 0× | if home.config.runner.as_ref().and_then(|r| r.update.as_ref()).is_some_and(|u| !u.allow) { |
| 192 | 0× | bail!("flow.toml says [runner.update] allow = false: this runner takes no updates"); |
| 193 | 0× | } |
| 194 | 0× | if let Some(p) = pending(&home.root) { |
| 195 | 0× | bail!("the update from {} to {} is not proven yet; wait for it (or for its rollback)", p.from, p.to); |
| 196 | 0× | } |
| 197 | 0× | let from = env!("CARGO_PKG_VERSION").to_string(); |
| 198 | 0× | let staged = exe.with_file_name(NEW); |
| 199 | 0× | let _ = std::fs::remove_file(&staged); |
| 200 | 0× | std::fs::copy(file, &staged).with_context(|| format!("staging {} as {}", file.display(), staged.display()))?; |
| 201 | 0× | crate::portable::set_mode(&staged, 0o755)?; |
| 202 | 0× | let checked = check(&staged, &home.root).and_then(|to| { |
| 203 | 0× | if older(&to, &from) && !allow_older { |
| 204 | 0× | bail!("flow {to} is older than this runner's {from} (--allow-older to go back)"); |
| 205 | 0× | } |
| 206 | 0× | Ok(to) |
| 207 | 0× | }); |
| 208 | 0× | let to = match checked { |
| 209 | 0× | Ok(v) => v, |
| 210 | 0× | Err(e) => { |
| 211 | 0× | let _ = std::fs::remove_file(&staged); |
| 212 | 0× | return Err(e); |
| 213 | | } |
| 214 | | }; |
| 215 | 0× | if dry_run { |
| 216 | 0× | let _ = std::fs::remove_file(&staged); |
| 217 | 0× | return Ok(Installed { from, to, exe: exe.to_path_buf(), told: None }); |
| 218 | 0× | } |
| 219 | 0× | swap(exe, &staged)?; |
| 220 | 0× | let p = |
| 221 | 0× | Pending { from: from.clone(), to: to.clone(), exe: exe.to_path_buf(), at_ms: now_ms(), notice_secs: notice.as_secs(), starts: 0 }; |
| 222 | 0× | save(&home.root, &p)?; |
| 223 | 0× | log(&home.root, "installed", &from, &to, "flow runner update --from"); |
| 224 | 0× | let told = running(&home.root).filter(|pid| tell(*pid)); |
| 225 | 0× | Ok(Installed { from, to, exe: exe.to_path_buf(), told }) |
| 226 | 0× | } |
| 227 | |
|
| 228 | |
|
| 229 | | #[cfg(unix)] |
| 230 | 0× | fn tell(pid: u32) -> bool { |
| 231 | 0× | unsafe { libc::kill(pid as libc::pid_t, libc::SIGUSR1) == 0 } |
| 232 | 0× | } |
| 233 | |
|
| 234 | | #[cfg(not(unix))] |
| 235 | | fn tell(_pid: u32) -> bool { |
| 236 | | false |
| 237 | | } |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | 1× | fn running(home: &Path) -> Option<u32> { |
| 244 | 1× | let pid: u32 = std::fs::read_to_string(home.join(PID)).ok()?.trim().parse().ok()?; |
| 245 | 1× | let cmdline = std::fs::read(format!("/proc/{pid}/cmdline")).ok()?; |
| 246 | 1× | is_runner(&cmdline).then_some(pid) |
| 247 | 1× | } |
| 248 | |
|
| 249 | |
|
| 250 | 6× | fn is_runner(cmdline: &[u8]) -> bool { |
| 251 | 230× | let args: Vec<&[u8]> = cmdline.split(|b| *b == 0).filter(|a| !a.is_empty()).collect(); |
| 252 | 6× | let Some((argv0, rest)) = args.split_first() else { return false }; |
| 253 | 39× | let name = argv0.rsplit(|b| *b == b'/').next().unwrap_or(argv0); |
| 254 | 5× | name.starts_with(b"flow") && rest.last() == Some(&&b"runner"[..]) |
| 255 | 6× | } |
| 256 | |
|
| 257 | |
|
| 258 | 0× | pub fn note_runner(home: &Path) { |
| 259 | 0× | let _ = std::fs::write(home.join(PID), std::process::id().to_string()); |
| 260 | 0× | } |
| 261 | |
|
| 262 | |
|
| 263 | 0× | pub fn forget_runner(home: &Path) { |
| 264 | 0× | if std::fs::read_to_string(home.join(PID)).is_ok_and(|p| p.trim() == std::process::id().to_string()) { |
| 265 | 0× | let _ = std::fs::remove_file(home.join(PID)); |
| 266 | 0× | } |
| 267 | 0× | } |
| 268 | |
|
| 269 | |
|
| 270 | | #[derive(Debug, PartialEq, Eq)] |
| 271 | | pub enum Start { |
| 272 | | |
| 273 | | Settled, |
| 274 | | |
| 275 | | Trial(Pending), |
| 276 | | |
| 277 | | RolledBack(Pending), |
| 278 | | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | 6× | pub fn on_start(home: &Path) -> Result<Start> { |
| 283 | 6× | let Some(mut p) = pending(home) else { return Ok(Start::Settled) }; |
| 284 | 4× | p.starts += 1; |
| 285 | 4× | if p.starts > MAX_STARTS { |
| 286 | 1× | roll_back(home, &p, &format!("{MAX_STARTS} starts without reaching the hub"))?; |
| 287 | 1× | return Ok(Start::RolledBack(p)); |
| 288 | 3× | } |
| 289 | 3× | save(home, &p)?; |
| 290 | 3× | Ok(Start::Trial(p)) |
| 291 | 6× | } |
| 292 | |
|
| 293 | |
|
| 294 | | const NOTE: &str = "update.note"; |
| 295 | |
|
| 296 | |
|
| 297 | 2× | fn note(home: &Path, note: &str) { |
| 298 | 2× | let _ = std::fs::write(home.join(NOTE), note); |
| 299 | 2× | } |
| 300 | |
|
| 301 | |
|
| 302 | 0× | pub fn take_note(home: &Path) -> Option<String> { |
| 303 | 0× | let n = std::fs::read_to_string(home.join(NOTE)).ok()?; |
| 304 | 0× | let _ = std::fs::remove_file(home.join(NOTE)); |
| 305 | 0× | Some(n).filter(|n| !n.trim().is_empty()) |
| 306 | 0× | } |
| 307 | |
|
| 308 | |
|
| 309 | 1× | pub fn proven(home: &Path) { |
| 310 | 1× | if let Some(p) = pending(home) { |
| 311 | 1× | let _ = std::fs::remove_file(home.join(PENDING)); |
| 312 | 1× | log(home, "proven", &p.from, &p.to, "reached the hub"); |
| 313 | 1× | note(home, &format!("runs flow {} (was {}); proven", p.to, p.from)); |
| 314 | 1× | eprintln!("flow runner: flow {} proven (it reached the hub); flow {} stays as {PREV}", p.to, p.from); |
| 315 | 1× | } |
| 316 | 1× | } |
| 317 | |
|
| 318 | |
|
| 319 | 1× | pub fn roll_back(home: &Path, p: &Pending, why: &str) -> Result<()> { |
| 320 | 1× | restore(&p.exe)?; |
| 321 | 1× | let _ = std::fs::remove_file(home.join(PENDING)); |
| 322 | 1× | log(home, "rolled back", &p.to, &p.from, why); |
| 323 | 1× | note(home, &format!("rolled back from flow {} to {}: {why}", p.to, p.from)); |
| 324 | 1× | eprintln!("flow runner: rolled back from flow {} to {} ({why})", p.to, p.from); |
| 325 | 1× | Ok(()) |
| 326 | 1× | } |
| 327 | |
|
| 328 | | #[cfg(test)] |
| 329 | | mod tests { |
| 330 | | use super::*; |
| 331 | |
|
| 332 | | #[test] |
| 333 | 1× | fn versions_are_read_and_compared() { |
| 334 | 1× | assert_eq!(parse_version("flow 0.3.1 (local build)\n").as_deref(), Some("0.3.1")); |
| 335 | 1× | assert_eq!(parse_version("flow 0.10.0"), Some("0.10.0".into())); |
| 336 | 1× | assert_eq!(parse_version("oxy 0.6.0"), None); |
| 337 | 1× | assert_eq!(parse_version("flow banana"), None); |
| 338 | 1× | assert!(older("0.3.1", "0.10.0"), "by number, not by text"); |
| 339 | 1× | assert!(!older("0.3.1", "0.3.1")); |
| 340 | 1× | assert!(older("0.3", "0.3.1")); |
| 341 | 1× | } |
| 342 | |
|
| 343 | | #[test] |
| 344 | 1× | fn only_a_build_for_this_machine_passes() { |
| 345 | 1× | let me = std::fs::read(std::env::current_exe().unwrap()).unwrap(); |
| 346 | 1× | check_target(&me[..64]).unwrap(); |
| 347 | 1× | assert!(check_target(b"#!/bin/sh\necho flow 9.9.9\n").unwrap_err().to_string().contains("not an ELF")); |
| 348 | 1× | let mut other = me[..64].to_vec(); |
| 349 | 1× | other[18..20].copy_from_slice(&(if std::env::consts::ARCH == "riscv64" { 62u16 } else { 243u16 }).to_le_bytes()); |
| 350 | 1× | assert!(check_target(&other).unwrap_err().to_string().contains("another machine")); |
| 351 | 1× | } |
| 352 | |
|
| 353 | | #[test] |
| 354 | 1× | fn a_swap_keeps_the_old_build_and_three_failed_starts_bring_it_back() { |
| 355 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 356 | 1× | let (bin, home) = (dir.path().join("bin"), dir.path().join("home")); |
| 357 | 1× | std::fs::create_dir_all(&bin).unwrap(); |
| 358 | 1× | std::fs::create_dir_all(&home).unwrap(); |
| 359 | 1× | let exe = bin.join("flow"); |
| 360 | 1× | std::fs::write(&exe, "old").unwrap(); |
| 361 | 1× | std::fs::write(bin.join(NEW), "new").unwrap(); |
| 362 | 1× | swap(&exe, &bin.join(NEW)).unwrap(); |
| 363 | 1× | assert_eq!(std::fs::read_to_string(&exe).unwrap(), "new"); |
| 364 | 1× | assert_eq!(std::fs::read_to_string(bin.join(PREV)).unwrap(), "old"); |
| 365 | 1× | assert!(!bin.join(NEW).exists()); |
| 366 | |
|
| 367 | 1× | assert_eq!(on_start(&home).unwrap(), Start::Settled, "no update, nothing to do"); |
| 368 | 1× | let p = Pending { from: "0.3.1".into(), to: "0.3.2".into(), exe: exe.clone(), at_ms: 1, notice_secs: 600, starts: 0 }; |
| 369 | 1× | save(&home, &p).unwrap(); |
| 370 | 3× | for n in 1..=MAX_STARTS { |
| 371 | 3× | assert_eq!(on_start(&home).unwrap(), Start::Trial(Pending { starts: n, ..p.clone() })); |
| 372 | | } |
| 373 | 1× | assert!(matches!(on_start(&home).unwrap(), Start::RolledBack(_))); |
| 374 | 1× | assert_eq!(std::fs::read_to_string(&exe).unwrap(), "old", "the previous build is back"); |
| 375 | 1× | assert!(pending(&home).is_none()); |
| 376 | 1× | let log = std::fs::read_to_string(home.join(LOG)).unwrap(); |
| 377 | 1× | assert!(log.contains("rolled back") && log.contains("0.3.2"), "{log}"); |
| 378 | 1× | } |
| 379 | |
|
| 380 | | #[test] |
| 381 | 1× | fn only_a_running_runner_is_told() { |
| 382 | 1× | assert!(is_runner(b"/usr/local/bin/flow\0runner\0")); |
| 383 | 1× | assert!(is_runner(b"flow\0--home\0/srv/flow\0runner\0")); |
| 384 | 1× | assert!(!is_runner(b"flow\0runner\0update\0--from\0x\0"), "not the update command itself"); |
| 385 | 1× | assert!(!is_runner(b"/usr/bin/vim\0runner\0")); |
| 386 | 1× | assert!(!is_runner(b"")); |
| 387 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 388 | 1× | std::fs::write(dir.path().join(PID), std::process::id().to_string()).unwrap(); |
| 389 | 1× | assert_eq!(running(dir.path()), None, "this test is not a flow runner"); |
| 390 | 1× | } |
| 391 | |
|
| 392 | | #[test] |
| 393 | 1× | fn a_proven_build_stays() { |
| 394 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 395 | 1× | let exe = dir.path().join("flow"); |
| 396 | 1× | std::fs::write(&exe, "new").unwrap(); |
| 397 | 1× | let p = Pending { from: "0.3.1".into(), to: "0.3.2".into(), exe: exe.clone(), at_ms: 1, notice_secs: 0, starts: 1 }; |
| 398 | 1× | save(dir.path(), &p).unwrap(); |
| 399 | 1× | proven(dir.path()); |
| 400 | 1× | assert!(pending(dir.path()).is_none()); |
| 401 | 1× | assert_eq!(on_start(dir.path()).unwrap(), Start::Settled); |
| 402 | 1× | assert!(std::fs::read_to_string(dir.path().join(LOG)).unwrap().contains("proven")); |
| 403 | 1× | } |
| 404 | | } |