|
| 1 | +//! `burnwall upgrade` (alias `self-upgrade`) — fetch and install the latest |
| 2 | +//! release, handling the two things that make a manual `irm … | iex` fail: |
| 3 | +//! |
| 4 | +//! 1. **A running proxy holds `burnwall.exe` open** — Windows can't overwrite a |
| 5 | +//! live executable. We stop the proxy first (and restart it after). |
| 6 | +//! 2. **The upgrade process IS `burnwall.exe`** — it holds its *own* file. On |
| 7 | +//! Windows we rename our running binary aside (`burnwall.exe.old`, which is |
| 8 | +//! permitted even while running) so the installer can write a fresh one; the |
| 9 | +//! stale `.old` is cleaned up on the next upgrade. |
| 10 | +//! |
| 11 | +//! Mirror of [`super::self_rollback`], which goes the other direction. |
| 12 | +
|
| 13 | +#[cfg(windows)] |
| 14 | +use std::path::Path; |
| 15 | + |
| 16 | +use anyhow::{Context, Result}; |
| 17 | +use clap::Args; |
| 18 | + |
| 19 | +const REPO: &str = "intbot/burnwall"; |
| 20 | + |
| 21 | +#[derive(Args, Debug)] |
| 22 | +pub struct UpgradeArgs { |
| 23 | + /// Print what would run without doing it. |
| 24 | + #[arg(long)] |
| 25 | + pub dry_run: bool, |
| 26 | + /// Don't restart the proxy afterward, even if it was running. |
| 27 | + #[arg(long)] |
| 28 | + pub no_restart: bool, |
| 29 | +} |
| 30 | + |
| 31 | +pub fn run_cmd(args: UpgradeArgs) -> Result<()> { |
| 32 | + let url = installer_url(); |
| 33 | + println!("⬆ Upgrading Burnwall to the latest release"); |
| 34 | + println!(" Installer URL: {url}"); |
| 35 | + |
| 36 | + if args.dry_run { |
| 37 | + println!(" Would: stop the proxy (if running) → run the installer → restart it."); |
| 38 | + if cfg!(windows) { |
| 39 | + println!(" Would run: irm {url} | iex"); |
| 40 | + } else { |
| 41 | + println!(" Would run: curl --proto '=https' --tlsv1.2 -LsSf {url} | sh"); |
| 42 | + } |
| 43 | + return Ok(()); |
| 44 | + } |
| 45 | + |
| 46 | + // 1. Stop the running proxy so the binary can be replaced. |
| 47 | + let was_running = matches!(super::daemon::running_pid(), Ok(Some(_))); |
| 48 | + if was_running { |
| 49 | + println!(" Stopping the running proxy so the binary can be replaced…"); |
| 50 | + let _ = super::stop::run_cmd(super::stop::StopArgs {}); |
| 51 | + } |
| 52 | + |
| 53 | + // The canonical install path, captured before any rename so the restart |
| 54 | + // targets the freshly-written binary. |
| 55 | + let exe = std::env::current_exe().context("locating the burnwall executable")?; |
| 56 | + |
| 57 | + // 2. Install the latest release. |
| 58 | + #[cfg(windows)] |
| 59 | + win_upgrade(&url, &exe)?; |
| 60 | + #[cfg(not(windows))] |
| 61 | + run_installer(&url)?; |
| 62 | + |
| 63 | + println!(" ✓ Installed the latest release."); |
| 64 | + |
| 65 | + // 3. Restart the proxy if it was running. |
| 66 | + if was_running && !args.no_restart { |
| 67 | + match std::process::Command::new(&exe) |
| 68 | + .args(["start", "--daemon"]) |
| 69 | + .status() |
| 70 | + { |
| 71 | + Ok(s) if s.success() => println!(" Restarted the proxy on the new version."), |
| 72 | + _ => println!(" (could not auto-restart — run `burnwall start --daemon`)"), |
| 73 | + } |
| 74 | + } else if was_running { |
| 75 | + println!(" (not restarted — run `burnwall start --daemon` when ready)"); |
| 76 | + } |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +fn installer_url() -> String { |
| 81 | + // `releases/latest/download/…` always resolves to the newest release asset. |
| 82 | + let filename = if cfg!(windows) { |
| 83 | + "burnwall-installer.ps1" |
| 84 | + } else { |
| 85 | + "burnwall-installer.sh" |
| 86 | + }; |
| 87 | + format!("https://github.com/{REPO}/releases/latest/download/{filename}") |
| 88 | +} |
| 89 | + |
| 90 | +#[cfg(not(windows))] |
| 91 | +fn run_installer(url: &str) -> Result<()> { |
| 92 | + let status = std::process::Command::new("sh") |
| 93 | + .arg("-c") |
| 94 | + .arg(format!("curl --proto '=https' --tlsv1.2 -LsSf '{url}' | sh")) |
| 95 | + .status() |
| 96 | + .context("running shell installer")?; |
| 97 | + if !status.success() { |
| 98 | + anyhow::bail!("installer exited with status {status}"); |
| 99 | + } |
| 100 | + Ok(()) |
| 101 | +} |
| 102 | + |
| 103 | +/// Windows: rename our own running binary aside so the installer can write a |
| 104 | +/// fresh one at the original path, then restore on failure. |
| 105 | +#[cfg(windows)] |
| 106 | +fn win_upgrade(url: &str, exe: &Path) -> Result<()> { |
| 107 | + let old = exe.with_extension("exe.old"); |
| 108 | + // Best-effort: clear a leftover from a previous upgrade. |
| 109 | + let _ = std::fs::remove_file(&old); |
| 110 | + // Windows permits renaming a running executable (it can't overwrite it). |
| 111 | + std::fs::rename(exe, &old) |
| 112 | + .with_context(|| format!("moving current binary aside ({} → .old)", exe.display()))?; |
| 113 | + |
| 114 | + let result = run_installer_ps(url); |
| 115 | + if result.is_err() { |
| 116 | + // Restore the original binary so we never leave the user without one. |
| 117 | + let _ = std::fs::rename(&old, exe); |
| 118 | + } |
| 119 | + result |
| 120 | +} |
| 121 | + |
| 122 | +#[cfg(windows)] |
| 123 | +fn run_installer_ps(url: &str) -> Result<()> { |
| 124 | + let status = std::process::Command::new("powershell.exe") |
| 125 | + .args([ |
| 126 | + "-NoProfile", |
| 127 | + "-ExecutionPolicy", |
| 128 | + "Bypass", |
| 129 | + "-Command", |
| 130 | + &format!("irm {url} | iex"), |
| 131 | + ]) |
| 132 | + .status() |
| 133 | + .context("running PowerShell installer")?; |
| 134 | + if !status.success() { |
| 135 | + anyhow::bail!("installer exited with status {status}"); |
| 136 | + } |
| 137 | + Ok(()) |
| 138 | +} |
0 commit comments