use std::path::{Path, PathBuf}; use std::sync::OnceLock; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; use forge_core::ci::{CiRun, RunState}; use forge_core::config::SelfUpdateConfig; use forge_core::token::sha256_reader; use futures_util::StreamExt; use futures_util::stream::BoxStream; use http::header::{CONTENT_LENGTH, CONTENT_TYPE, HeaderValue}; use http_body_util::StreamBody; use sha2::{Digest, Sha256}; use tokio::io::AsyncWriteExt; use tokio_util::io::ReaderStream; use topcoat::router::{Body, HeaderMap, StatusCode, response::Response}; use crate::state::AppState; pub use forge_ci::proto::RESTART_EXIT_CODE; pub const SHA_HEADER: &str = "Forge-Binary-Sha256"; pub const TARGET_HEADER: &str = "Forge-Runner-Target"; const SANITY_TIMEOUT: Duration = Duration::from_secs(10); static DEPLOY_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(()); static RESTART: tokio::sync::Notify = tokio::sync::Notify::const_new(); static RESTART_REQUESTED: AtomicBool = AtomicBool::new(false); pub struct Running { pub path: PathBuf, pub sha256: String, } static RUNNING: OnceLock> = OnceLock::new(); pub fn running() -> Option<&'static Running> { RUNNING .get_or_init(|| match load_running() { Ok(running) => { tracing::info!(sha256 = %running.sha256, "self-update: running binary hashed"); Some(running) } Err(e) => { tracing::error!(error = %e, "self-update: cannot hash the running binary"); None } }) .as_ref() } fn load_running() -> std::io::Result { let path = std::env::current_exe()?; let sha256 = sha256_reader(std::fs::File::open(image_path())?)?.1; Ok(Running { path, sha256 }) } fn image_path() -> PathBuf { let proc_exe = PathBuf::from("/proc/self/exe"); if proc_exe.exists() { proc_exe } else { std::env::current_exe().unwrap_or(proc_exe) } } pub async fn restart_requested() { RESTART.notified().await; } pub fn request_restart() { RESTART_REQUESTED.store(true, Ordering::SeqCst); RESTART.notify_one(); } pub fn boot_id() -> &'static str { static ID: OnceLock = OnceLock::new(); ID.get_or_init(|| { let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map_or(0, |d| d.as_nanos()); format!("{:x}{nanos:x}", std::process::id()) }) } pub fn restart_pending() -> bool { RESTART_REQUESTED.load(Ordering::SeqCst) } pub fn target() -> String { format!("{}-{}", std::env::consts::ARCH, std::env::consts::OS) } fn target_matches(requested: Option<&str>) -> bool { requested == Some(target().as_str()) } pub fn should_deploy( cfg: &SelfUpdateConfig, repo_full_name: &str, run: &CiRun, newest_on_ref: Option<&str>, ) -> bool { cfg.enabled && matches!(run.state, RunState::Success) && run.trigger == "push" && run.pull_number.is_none() && repo_full_name.eq_ignore_ascii_case(&cfg.repo) && run.git_ref == format!("refs/heads/{}", cfg.branch) && newest_on_ref == Some(run.id.as_str()) } pub fn skip_reason(candidate_sha: &str, running_sha: &str, bad_list: &str) -> Option<&'static str> { if candidate_sha.eq_ignore_ascii_case(running_sha) { return Some("same as the running binary"); } let listed = bad_list .lines() .map(str::trim) .filter(|line| !line.is_empty() && !line.starts_with('#')) .any(|line| line.eq_ignore_ascii_case(candidate_sha)); listed.then_some("listed in the bad list") } fn sibling(exe: &Path, suffix: &str) -> PathBuf { let name = exe .file_name() .map(|name| name.to_string_lossy().into_owned()) .unwrap_or_default(); exe.with_file_name(format!("{name}.{suffix}")) } pub async fn maybe_deploy(state: &AppState, run: &CiRun) { let cfg = &state.config.self_update; if !cfg.enabled || !matches!(run.state, RunState::Success) { return; } let repo = match state.store.repo_by_id(&run.repo_id).await { Ok(Some(repo)) => repo, Ok(None) => return, Err(e) => { tracing::error!(error = %e, "self-update: loading the repo failed"); return; } }; let full_name = format!("{}/{}", repo.owner_name, repo.name); let newest = match state .store .latest_run_on_ref(&run.repo_id, &run.git_ref) .await { Ok(newest) => newest, Err(e) => { tracing::error!(error = %e, "self-update: loading the newest run failed"); return; } }; if !should_deploy(cfg, &full_name, run, newest.as_ref().map(|r| r.id.as_str())) { return; } tracing::info!(run_id = %run.id, "self-update: green push run on the deploy branch"); let state = state.clone(); let run = run.clone(); tokio::spawn(async move { deploy(state, run).await }); } pub async fn deploy(state: AppState, run: CiRun) { let Ok(_guard) = DEPLOY_LOCK.try_lock() else { tracing::info!(run_id = %run.id, "self-update: a deploy is already running, skipping"); return; }; if let Err(e) = deploy_inner(&state, &run).await { tracing::error!(run_id = %run.id, error = %e, "self-update: deploy failed"); crate::alerts::self_update_failed(&state, &run.id, &e).await; } } async fn deploy_inner(state: &AppState, run: &CiRun) -> Result<(), String> { let cfg = &state.config.self_update; let running = running().ok_or("the running binary was not hashed at startup")?; let jobs = state .store .jobs_for_run(&run.id) .await .map_err(|e| e.to_string())?; let Some(job) = jobs.iter().find(|job| job.name == cfg.job) else { tracing::info!(run_id = %run.id, job = %cfg.job, "self-update: no such job in the run, skipping"); return Ok(()); }; let configured = match cfg.artifact.as_str() { "dist_forge" => "forge-{target}", other => other, }; let artifact_name = configured.replace("{target}", &target()); let artifact = state .store .artifact(&job.id, &artifact_name) .await .map_err(|e| e.to_string())?; let Some(artifact) = artifact else { tracing::info!(run_id = %run.id, artifact = %artifact_name, "self-update: no such artifact, skipping"); return Ok(()); }; let read = state .blobs .read(&crate::ci::artifact_key(&artifact.id)) .await .map_err(|e| e.to_string())? .ok_or("the artifact bytes are missing")?; let staged = sibling(&running.path, "new"); let sha = match write_staged(read.stream, &staged).await { Ok(sha) => sha, Err(e) => { let _ = std::fs::remove_file(&staged); return Err(e); } }; if !sha.eq_ignore_ascii_case(&artifact.sha256) { let _ = std::fs::remove_file(&staged); return Err(format!( "artifact sha256 {sha} does not match the recorded {}", artifact.sha256 )); } let bad = std::fs::read_to_string(sibling(&running.path, "bad")).unwrap_or_default(); if let Some(reason) = skip_reason(&sha, &running.sha256, &bad) { let _ = std::fs::remove_file(&staged); tracing::info!(sha256 = %sha, reason, "self-update: skipped"); return Ok(()); } if let Err(e) = sanity_check(&staged).await { let _ = std::fs::remove_file(&staged); return Err(e); } if let Err(e) = swap(&running.path, &staged) { let _ = std::fs::remove_file(&staged); return Err(e); } tracing::info!( old_sha256 = %running.sha256, new_sha256 = %sha, exe = %running.path.display(), "self-update: binary replaced, restarting" ); crate::alerts::self_update_deployed(state, &sha.chars().take(12).collect::()).await; RESTART_REQUESTED.store(true, Ordering::SeqCst); RESTART.notify_one(); Ok(()) } async fn write_staged( mut stream: BoxStream<'static, std::io::Result>, path: &Path, ) -> Result { use std::os::unix::fs::PermissionsExt; let mut file = tokio::fs::File::create(path) .await .map_err(|e| format!("cannot create {}: {e}", path.display()))?; let mut hasher = Sha256::new(); while let Some(chunk) = stream.next().await { let chunk = chunk.map_err(|e| e.to_string())?; hasher.update(&chunk); file.write_all(&chunk).await.map_err(|e| e.to_string())?; } file.flush().await.map_err(|e| e.to_string())?; file.sync_all().await.map_err(|e| e.to_string())?; drop(file); std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)) .map_err(|e| e.to_string())?; Ok(hex::encode(hasher.finalize())) } async fn sanity_check(path: &Path) -> Result<(), String> { use std::process::Stdio; let mut child = tokio::process::Command::new(path) .arg("--version") .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()) .kill_on_drop(true) .spawn() .map_err(|e| format!("the new binary does not run: {e}"))?; match tokio::time::timeout(SANITY_TIMEOUT, child.wait()).await { Ok(Ok(status)) if status.success() => Ok(()), Ok(Ok(status)) => Err(format!("the new binary exited with {status} for --version")), Ok(Err(e)) => Err(format!("waiting for the new binary failed: {e}")), Err(_) => Err("the new binary did not answer --version within 10 seconds".into()), } } fn swap(exe: &Path, staged: &Path) -> Result<(), String> { let prev = sibling(exe, "prev"); if prev.exists() { std::fs::remove_file(&prev).map_err(|e| format!("cannot remove the old prev: {e}"))?; } if std::fs::hard_link(exe, &prev).is_err() { std::fs::copy(exe, &prev).map_err(|e| format!("cannot keep the previous binary: {e}"))?; } std::fs::rename(staged, exe).map_err(|e| format!("cannot replace the binary: {e}")) } pub fn advertise(state: &AppState, request: &HeaderMap, res: &mut Response) { if !state.config.self_update.enabled { return; } if !matches!(res.status(), StatusCode::OK | StatusCode::NO_CONTENT) { return; } let requested = request.get(TARGET_HEADER).and_then(|v| v.to_str().ok()); if !target_matches(requested) { return; } let Some(running) = running() else { return; }; if let Ok(value) = HeaderValue::from_str(&running.sha256) { res.headers_mut().insert(SHA_HEADER, value); } } const BODY_FRAME_BYTES: usize = 64 * 1024; pub async fn binary_response() -> Result { let running = running().ok_or("the running binary was not hashed at startup")?; let file = tokio::fs::File::open(image_path()) .await .map_err(|e| e.to_string())?; let len = file.metadata().await.map_err(|e| e.to_string())?.len(); let frames = ReaderStream::with_capacity(file, BODY_FRAME_BYTES) .map(|chunk| chunk.map(http_body::Frame::data)); let mut res = Response::new(Body::new(StreamBody::new(frames))); let headers = res.headers_mut(); headers.insert( CONTENT_TYPE, HeaderValue::from_static("application/octet-stream"), ); headers.insert(CONTENT_LENGTH, HeaderValue::from(len)); if let Ok(value) = HeaderValue::from_str(&running.sha256) { headers.insert(SHA_HEADER, value); } Ok(res) } #[cfg(test)] mod tests { use super::*; use forge_core::ci::CiRun; fn cfg() -> SelfUpdateConfig { SelfUpdateConfig { enabled: true, repo: "Tahinli/forge".into(), branch: "main".into(), job: "build".into(), artifact: "forge-{target}".into(), } } fn run(trigger: &str, git_ref: &str, state: RunState, pull_number: Option) -> CiRun { CiRun { id: "run-7".into(), repo_id: "repo-1".into(), number: 7, trigger: trigger.into(), git_ref: git_ref.into(), commit_oid: "a".repeat(40), pull_number, actor_id: "user-1".into(), state, config_error: None, created_at: "now".into(), finished_at: None, } } fn good() -> CiRun { run("push", "refs/heads/main", RunState::Success, None) } #[test] fn only_the_newest_successful_push_to_the_deploy_branch_deploys() { let on = cfg(); let mut off = cfg(); off.enabled = false; let failed = |state| run("push", "refs/heads/main", state, None); let cases = vec![ ( "good push", &on, "Tahinli/forge", good(), Some("run-7"), true, ), ( "repo name case", &on, "tahinli/FORGE", good(), Some("run-7"), true, ), ( "other repo", &on, "Tahinli/other", good(), Some("run-7"), false, ), ( "disabled", &off, "Tahinli/forge", good(), Some("run-7"), false, ), ( "older run", &on, "Tahinli/forge", good(), Some("run-9"), false, ), ("no latest run", &on, "Tahinli/forge", good(), None, false), ( "pull request run", &on, "Tahinli/forge", run( "pull_request", "refs/pull/3/head", RunState::Success, Some(3), ), Some("run-7"), false, ), ( "pull request on branch ref", &on, "Tahinli/forge", run( "pull_request", "refs/heads/main", RunState::Success, Some(3), ), Some("run-7"), false, ), ( "push with a pull number", &on, "Tahinli/forge", run("push", "refs/heads/main", RunState::Success, Some(3)), Some("run-7"), false, ), ( "feature branch", &on, "Tahinli/forge", run("push", "refs/heads/feature", RunState::Success, None), Some("run-7"), false, ), ( "tag", &on, "Tahinli/forge", run("tag", "refs/tags/v1", RunState::Success, None), Some("run-7"), false, ), ( "manual", &on, "Tahinli/forge", run("manual", "refs/heads/main", RunState::Success, None), Some("run-7"), false, ), ( "failure", &on, "Tahinli/forge", failed(RunState::Failure), Some("run-7"), false, ), ( "cancelled", &on, "Tahinli/forge", failed(RunState::Cancelled), Some("run-7"), false, ), ( "error", &on, "Tahinli/forge", failed(RunState::Error), Some("run-7"), false, ), ( "running", &on, "Tahinli/forge", failed(RunState::Running), Some("run-7"), false, ), ]; for (case, config, repo, run, latest, want) in cases { assert_eq!( should_deploy(config, repo, &run, latest), want, "case {case}" ); } } #[test] fn same_sha_as_running_is_skipped() { let sha = "ab12"; assert_eq!( skip_reason(sha, sha, ""), Some("same as the running binary") ); assert_eq!( skip_reason("AB12", sha, ""), Some("same as the running binary") ); assert_eq!(skip_reason("cd34", sha, ""), None); } #[test] fn bad_list_entries_are_skipped() { let bad = "# rolled back 2026-10-01\n1111\n 2222 \n\n"; assert_eq!( skip_reason("2222", "ffff", bad), Some("listed in the bad list") ); assert_eq!( skip_reason("1111", "ffff", bad), Some("listed in the bad list") ); assert_eq!(skip_reason("rolled", "ffff", bad), None); assert_eq!(skip_reason("3333", "ffff", bad), None); } #[test] fn target_must_match_this_build() { assert!(target_matches(Some(&target()))); assert!(!target_matches(None)); assert!(!target_matches(Some("riscv64-plan9"))); } #[test] fn sha256_of_known_bytes() { let digest = sha256_reader(&b"abc"[..]).unwrap().1; assert_eq!( digest, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" ); } }