Server auto-update. Build's Linux lane runs in a podman container (yumush-linux, Ubuntu 24.04 / glibc 2.39, your exact rustc, caches in ~/.cache/yumush-build) and adds a Server job. Publishing a release that holds server-x86_64-linux probes the new binary with --version, swaps it in (old kept as server.old) and restarts in place. Server answers --version
Browse files at this commite81ccb7953970ab1dae81cf4822e979f364eb101Parent: b3f134e- feat
- server auto-update. Build's Linux lane runs in a podman container (yumush-linux, Ubuntu 24.04 / glibc 2.39, your exact rustc, caches in ~/.cache/yumush-build) and adds a Server job. Publishing a release that holds server-x86_64-linux probes the new binary with --version, swaps it in (old kept as server.old) and restarts in place. Server answers --version
- new ReleaseProblem::ServerBroken
12 changed files +252 -46
- crates/client/lang/en/builder_release.txt +2 -1
- crates/client/lang/tr/builder_release.txt +2 -1
- crates/client/src/gui/builder/releases.rs +2 -0
- crates/client/src/release.rs +138 -38
- crates/common/src/error.rs +2 -0
- crates/common/src/update.rs +3 -0
- crates/server/src/call/admin.rs +1 -5
- crates/server/src/call/release.rs +8 -0
- crates/server/src/lib.rs +1 -0
- crates/server/src/main.rs +9 -0
- crates/server/src/network.rs +12 -0
- crates/server/src/update.rs +72 -1
crates/client/lang/en/builder_release.txtmodified+2 -1
| @@ -18,7 +18,7 @@ failed = Failed | ||
| 18 | 18 | linux_v1 = Linux v1 |
| 19 | 19 | linux_v3 = Linux v3 |
| 20 | 20 | windows_vm = Windows VM |
| 21 | -this_machine = This machine | |
| 21 | +this_machine = Linux container | |
| 22 | 22 | prepare = Prepare |
| 23 | 23 | windows_v1 = Windows v1 |
| 24 | 24 | windows_v3 = Windows v3 |
| @@ -29,3 +29,4 @@ open_releases = Open Releases | ||
| 29 | 29 | dismiss = Dismiss |
| 30 | 30 | ready = Ready to publish |
| 31 | 31 | discard = Discard |
| 32 | +server = Server |
crates/client/lang/tr/builder_release.txtmodified+2 -1
| @@ -18,7 +18,7 @@ failed = Başarısız | ||
| 18 | 18 | linux_v1 = Linux v1 |
| 19 | 19 | linux_v3 = Linux v3 |
| 20 | 20 | windows_vm = Windows VM |
| 21 | -this_machine = Bu makine | |
| 21 | +this_machine = Linux kapsayıcı | |
| 22 | 22 | prepare = Hazırlık |
| 23 | 23 | windows_v1 = Windows v1 |
| 24 | 24 | windows_v3 = Windows v3 |
| @@ -29,3 +29,4 @@ open_releases = Sürümleri aç | ||
| 29 | 29 | dismiss = Kapat |
| 30 | 30 | ready = Yayına hazır |
| 31 | 31 | discard = Vazgeç |
| 32 | +server = Sunucu |
crates/client/src/gui/builder/releases.rsmodified+2 -0
| @@ -171,8 +171,10 @@ impl Desk { | ||
| 171 | 171 | fn job_label(job: Job) -> &'static str { |
| 172 | 172 | let w = &w().builder_release; |
| 173 | 173 | match job { |
| 174 | + Job::LinuxStart => w.prepare, | |
| 174 | 175 | Job::Linux(Level::V1) => w.linux_v1, |
| 175 | 176 | Job::Linux(Level::V3) => w.linux_v3, |
| 177 | + Job::Server => w.server, | |
| 176 | 178 | Job::WindowsStart => w.prepare, |
| 177 | 179 | Job::Windows(Level::V1) => w.windows_v1, |
| 178 | 180 | Job::Windows(Level::V3) => w.windows_v3, |
crates/client/src/release.rsmodified+138 -38
| @@ -17,7 +17,9 @@ pub enum Level { | ||
| 17 | 17 | |
| 18 | 18 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 19 | 19 | pub enum Job { |
| 20 | + LinuxStart, | |
| 20 | 21 | Linux(Level), |
| 22 | + Server, | |
| 21 | 23 | WindowsStart, |
| 22 | 24 | Windows(Level), |
| 23 | 25 | } |
| @@ -29,7 +31,15 @@ pub enum Lane { | ||
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | pub const LANES: [(Lane, &[Job]); 2] = [ |
| 32 | - (Lane::Host, &[Job::Linux(Level::V1), Job::Linux(Level::V3)]), | |
| 34 | + ( | |
| 35 | + Lane::Host, | |
| 36 | + &[ | |
| 37 | + Job::LinuxStart, | |
| 38 | + Job::Linux(Level::V1), | |
| 39 | + Job::Server, | |
| 40 | + Job::Linux(Level::V3), | |
| 41 | + ], | |
| 42 | + ), | |
| 33 | 43 | ( |
| 34 | 44 | Lane::Vm, |
| 35 | 45 | &[ |
| @@ -54,7 +64,10 @@ pub enum Event { | ||
| 54 | 64 | Done, |
| 55 | 65 | } |
| 56 | 66 | |
| 57 | -const TREE: &str = "target/release-src"; | |
| 67 | +const LINUX: &str = "yumush-linux"; | |
| 68 | +const LINUX_IMAGE: &str = "docker.io/library/ubuntu:24.04"; | |
| 69 | +const LINUX_CACHE: &str = ".cache/yumush-build"; | |
| 70 | +const LINUX_PACKAGES: &str = "ca-certificates curl git build-essential pkg-config clang cmake nasm libpipewire-0.3-dev libspa-0.2-dev libva-dev libdrm-dev libgbm-dev libxkbcommon-x11-dev libxkbcommon-dev libxcb1-dev libwayland-dev libvulkan-dev libfontconfig-dev libfreetype-dev libasound2-dev"; | |
| 58 | 71 | const ARCHIVE: &str = "dist/source.tgz"; |
| 59 | 72 | const VM: &str = "yumush-win"; |
| 60 | 73 | const TARGET: &str = "yumush@127.0.0.1"; |
| @@ -117,6 +130,7 @@ impl Build { | ||
| 117 | 130 | |
| 118 | 131 | pub fn cancel(&self) { |
| 119 | 132 | self.cancel.store(true, Ordering::SeqCst); |
| 133 | + podman_ok(&["stop", "-t", "2", LINUX]); | |
| 120 | 134 | for slot in &self.children { |
| 121 | 135 | if let Some(child) = slot.lock().unwrap().as_mut() { |
| 122 | 136 | let _ = child.kill(); |
| @@ -171,6 +185,7 @@ fn run(source: &Path, lanes: &[Cx]) { | ||
| 171 | 185 | if let Vm::Started = vm { |
| 172 | 186 | podman_ok(&["stop", VM]); |
| 173 | 187 | } |
| 188 | + podman_ok(&["stop", "-t", "2", LINUX]); | |
| 174 | 189 | first.send(Event::Done); |
| 175 | 190 | } |
| 176 | 191 | |
| @@ -222,6 +237,13 @@ fn preflight(source: &Path) -> Result<Vm, String> { | ||
| 222 | 237 | |
| 223 | 238 | fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx, vm: &Vm) -> Result<(), String> { |
| 224 | 239 | match job { |
| 240 | + Job::LinuxStart => { | |
| 241 | + if !cfg!(target_os = "linux") { | |
| 242 | + return skip(cx, job, "not linux"); | |
| 243 | + } | |
| 244 | + cx.send(Event::Started(job)); | |
| 245 | + start_linux(job, source, cx) | |
| 246 | + } | |
| 225 | 247 | Job::Linux(level) => { |
| 226 | 248 | if !cfg!(target_os = "linux") { |
| 227 | 249 | return skip(cx, job, "not linux"); |
| @@ -229,6 +251,13 @@ fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx, vm: &Vm) -> Result<(), | ||
| 229 | 251 | cx.send(Event::Started(job)); |
| 230 | 252 | build_linux(job, level, source, cx) |
| 231 | 253 | } |
| 254 | + Job::Server => { | |
| 255 | + if !cfg!(target_os = "linux") { | |
| 256 | + return skip(cx, job, "not linux"); | |
| 257 | + } | |
| 258 | + cx.send(Event::Started(job)); | |
| 259 | + build_server(job, source, cx) | |
| 260 | + } | |
| 232 | 261 | Job::WindowsStart => { |
| 233 | 262 | if let Vm::Missing = vm { |
| 234 | 263 | return skip(cx, job, "no yumush-win"); |
| @@ -268,59 +297,130 @@ fn toml_text(cpu: &str) -> String { | ||
| 268 | 297 | } |
| 269 | 298 | |
| 270 | 299 | fn snapshot(source: &Path) -> Result<(), String> { |
| 271 | - let tree = source.join(TREE); | |
| 272 | - if tree.exists() { | |
| 273 | - fs::remove_dir_all(&tree).map_err(|e| e.to_string())?; | |
| 274 | - } | |
| 275 | - fs::create_dir_all(&tree).map_err(|e| e.to_string())?; | |
| 276 | 300 | fs::create_dir_all(source.join("dist")).map_err(|e| e.to_string())?; |
| 277 | - let tgz = source.join(ARCHIVE); | |
| 278 | 301 | check( |
| 279 | 302 | Command::new("git") |
| 280 | 303 | .arg("-C") |
| 281 | 304 | .arg(source) |
| 282 | 305 | .args(["archive", "--format=tar.gz", "-o"]) |
| 283 | - .arg(&tgz) | |
| 306 | + .arg(source.join(ARCHIVE)) | |
| 284 | 307 | .arg("HEAD"), |
| 285 | - )?; | |
| 286 | - check( | |
| 287 | - Command::new("tar") | |
| 288 | - .arg("-xmzf") | |
| 289 | - .arg(&tgz) | |
| 290 | - .arg("-C") | |
| 291 | - .arg(&tree), | |
| 292 | 308 | ) |
| 293 | 309 | } |
| 294 | 310 | |
| 295 | -fn build_linux(job: Job, level: Level, source: &Path, cx: &Cx) -> Result<(), String> { | |
| 296 | - let (name, cpu) = flags(level); | |
| 297 | - fs::write( | |
| 298 | - source.join("dist").join(format!("{name}.toml")), | |
| 299 | - toml_text(cpu), | |
| 300 | - ) | |
| 301 | - .map_err(|e| e.to_string())?; | |
| 302 | - let mut cmd = Command::new("cargo"); | |
| 303 | - cmd.current_dir(source.join(TREE)) | |
| 304 | - .args(["build", "--release", "-p", "client", "--target-dir"]) | |
| 305 | - .arg(source.join(format!("target/release-{name}"))) | |
| 306 | - .arg("--config") | |
| 307 | - .arg(source.join(format!("dist/{name}.toml"))) | |
| 308 | - .env("CARGO_TERM_PROGRESS_WHEN", "always") | |
| 309 | - .env("CARGO_TERM_PROGRESS_WIDTH", "200") | |
| 310 | - .env("CARGO_TERM_COLOR", "never") | |
| 311 | - .stdout(Stdio::null()); | |
| 312 | - let built = source.join(format!("target/release-{name}/release/client")); | |
| 313 | - if built.exists() { | |
| 314 | - fs::remove_file(&built).map_err(|e| e.to_string())?; | |
| 311 | +fn linux_cache() -> PathBuf { | |
| 312 | + dirs::home_dir().unwrap_or_default().join(LINUX_CACHE) | |
| 313 | +} | |
| 314 | + | |
| 315 | +fn host_rust() -> Result<String, String> { | |
| 316 | + let out = Command::new("rustc") | |
| 317 | + .arg("--version") | |
| 318 | + .output() | |
| 319 | + .map_err(|e| e.to_string())?; | |
| 320 | + String::from_utf8_lossy(&out.stdout) | |
| 321 | + .split_whitespace() | |
| 322 | + .nth(1) | |
| 323 | + .map(str::to_string) | |
| 324 | + .ok_or_else(|| "rustc version unknown".to_string()) | |
| 325 | +} | |
| 326 | + | |
| 327 | +fn in_linux(script: &str) -> Command { | |
| 328 | + let mut cmd = Command::new("podman"); | |
| 329 | + cmd.args(["exec", "-w", "/build"]) | |
| 330 | + .args(["-e", "PATH=/root/.cargo/bin:/usr/local/bin:/usr/bin:/bin"]) | |
| 331 | + .args(["-e", "DEBIAN_FRONTEND=noninteractive"]) | |
| 332 | + .args(["-e", "CARGO_TERM_PROGRESS_WHEN=always"]) | |
| 333 | + .args(["-e", "CARGO_TERM_PROGRESS_WIDTH=200"]) | |
| 334 | + .args(["-e", "CARGO_TERM_COLOR=never"]) | |
| 335 | + .args([LINUX, "bash", "-ec", script]) | |
| 336 | + .stdout(Stdio::piped()); | |
| 337 | + cmd | |
| 338 | +} | |
| 339 | + | |
| 340 | +fn start_linux(job: Job, source: &Path, cx: &Cx) -> Result<(), String> { | |
| 341 | + let cache = linux_cache(); | |
| 342 | + for dir in ["cargo", "rustup", "target"] { | |
| 343 | + fs::create_dir_all(cache.join(dir)).map_err(|e| e.to_string())?; | |
| 315 | 344 | } |
| 316 | - stream(&mut cmd, job, cx)?; | |
| 317 | - let dest = source.join("dist").join(format!("x86_64_{name}-linux")); | |
| 345 | + if !podman_ok(&["container", "exists", LINUX]) { | |
| 346 | + cx.send(Event::Line(job, "creating container".into())); | |
| 347 | + let mount = |dir: &str, at: &str| format!("{}:{at}:Z", cache.join(dir).display()); | |
| 348 | + check( | |
| 349 | + Command::new("podman") | |
| 350 | + .args(["run", "-d", "--name", LINUX]) | |
| 351 | + .args(["-v", &mount("cargo", "/root/.cargo")]) | |
| 352 | + .args(["-v", &mount("rustup", "/root/.rustup")]) | |
| 353 | + .args(["-v", &mount("target", "/build/target")]) | |
| 354 | + .args([LINUX_IMAGE, "sleep", "infinity"]), | |
| 355 | + )?; | |
| 356 | + } else { | |
| 357 | + check(Command::new("podman").args(["start", LINUX]))?; | |
| 358 | + } | |
| 359 | + check( | |
| 360 | + Command::new("podman") | |
| 361 | + .arg("cp") | |
| 362 | + .arg(source.join(ARCHIVE)) | |
| 363 | + .arg(format!("{LINUX}:/build/src.tgz")), | |
| 364 | + )?; | |
| 365 | + let rust = host_rust()?; | |
| 366 | + let script = format!( | |
| 367 | + "command -v clang >/dev/null || {{ apt-get update; apt-get install -y --no-install-recommends {LINUX_PACKAGES}; }} | |
| 368 | +[ -x /root/.cargo/bin/rustup ] || curl -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain none | |
| 369 | +rustup toolchain install {rust} --profile minimal | |
| 370 | +rustup default {rust} | |
| 371 | +rm -rf src && mkdir src && tar -xmzf src.tgz -C src" | |
| 372 | + ); | |
| 373 | + stream(&mut in_linux(&script), job, cx)?; | |
| 374 | + cx.send(Event::Finished(job)); | |
| 375 | + Ok(()) | |
| 376 | +} | |
| 377 | + | |
| 378 | +fn build_in_linux( | |
| 379 | + job: Job, | |
| 380 | + package: &str, | |
| 381 | + level: Level, | |
| 382 | + dest: &str, | |
| 383 | + source: &Path, | |
| 384 | + cx: &Cx, | |
| 385 | +) -> Result<(), String> { | |
| 386 | + let (name, cpu) = flags(level); | |
| 387 | + let script = format!( | |
| 388 | + "cd src | |
| 389 | +rm -f /build/target/release-{name}/release/{package} | |
| 390 | +cargo build --release -p {package} --target-dir /build/target/release-{name} --config 'target.x86_64-unknown-linux-gnu.rustflags=[\"-C\", \"target-cpu={cpu}\"]'" | |
| 391 | + ); | |
| 392 | + stream(&mut in_linux(&script), job, cx)?; | |
| 393 | + let built = linux_cache().join(format!("target/release-{name}/release/{package}")); | |
| 394 | + let dest = source.join("dist").join(dest); | |
| 318 | 395 | fs::copy(&built, &dest).map_err(|e| e.to_string())?; |
| 319 | 396 | cx.send(Event::Built(job, dest)); |
| 320 | 397 | cx.send(Event::Finished(job)); |
| 321 | 398 | Ok(()) |
| 322 | 399 | } |
| 323 | 400 | |
| 401 | +fn build_linux(job: Job, level: Level, source: &Path, cx: &Cx) -> Result<(), String> { | |
| 402 | + let (name, _) = flags(level); | |
| 403 | + build_in_linux( | |
| 404 | + job, | |
| 405 | + "client", | |
| 406 | + level, | |
| 407 | + &format!("x86_64_{name}-linux"), | |
| 408 | + source, | |
| 409 | + cx, | |
| 410 | + ) | |
| 411 | +} | |
| 412 | + | |
| 413 | +fn build_server(job: Job, source: &Path, cx: &Cx) -> Result<(), String> { | |
| 414 | + build_in_linux( | |
| 415 | + job, | |
| 416 | + "server", | |
| 417 | + Level::V1, | |
| 418 | + common::update::SERVER_TARGET, | |
| 419 | + source, | |
| 420 | + cx, | |
| 421 | + ) | |
| 422 | +} | |
| 423 | + | |
| 324 | 424 | fn start_windows(job: Job, source: &Path, key: &Path, cx: &Cx) -> Result<(), String> { |
| 325 | 425 | wait_ssh(job, key, cx)?; |
| 326 | 426 | check( |
crates/common/src/error.rsmodified+2 -0
| @@ -165,4 +165,6 @@ pub enum ReleaseProblem { | ||
| 165 | 165 | SameTarget, |
| 166 | 166 | #[error("file not staged")] |
| 167 | 167 | NotStaged, |
| 168 | + #[error("server binary does not start")] | |
| 169 | + ServerBroken, | |
| 168 | 170 | } |
crates/common/src/update.rsmodified+3 -0
| @@ -3,6 +3,9 @@ use bitcode::{Decode, Encode}; | ||
| 3 | 3 | pub const ALPN: &[u8] = b"yumush-update/1"; |
| 4 | 4 | pub const FILE_MAX: u64 = 512 << 20; |
| 5 | 5 | pub const MARK: &str = "yumush-target:"; |
| 6 | +pub const SERVER_TARGET: &str = "server-x86_64-linux"; | |
| 7 | +pub const SERVER_MARK: &str = "yumush-target:server-x86_64-linux;"; | |
| 8 | +pub const SERVER_VERSION: &str = "yumush-server"; | |
| 6 | 9 | const TARGET_MAX: usize = 32; |
| 7 | 10 | |
| 8 | 11 | pub type Sha = [u8; 32]; |
crates/server/src/call/admin.rsmodified+1 -5
| @@ -690,11 +690,7 @@ impl Handle for RestartServer { | ||
| 690 | 690 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 691 | 691 | let me = me.user()?; |
| 692 | 692 | tracing::info!("restart requested by {}", me.username); |
| 693 | - let restart = cx.sh.services.restart.clone(); | |
| 694 | - tokio::spawn(async move { | |
| 695 | - tokio::time::sleep(std::time::Duration::from_millis(300)).await; | |
| 696 | - restart.notify_one(); | |
| 697 | - }); | |
| 693 | + cx.sh.services.restart_soon(); | |
| 698 | 694 | Ok(()) |
| 699 | 695 | } |
| 700 | 696 | } |
crates/server/src/call/release.rsmodified+8 -0
| @@ -67,6 +67,9 @@ impl Handle for PublishRelease { | ||
| 67 | 67 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<ReleaseInfo, ServerError> { |
| 68 | 68 | let me = me.user()?; |
| 69 | 69 | let files = cx.sh.releases.staged(&self.files)?; |
| 70 | + let releases = cx.sh.releases.clone(); | |
| 71 | + let picked = files.clone(); | |
| 72 | + let next = tokio::task::spawn_blocking(move || releases.prepare_server(&picked)).await??; | |
| 70 | 73 | let before = db::release::live(cx.db).await?; |
| 71 | 74 | let seq = db::release::insert(self.required, &files, cx.db).await?; |
| 72 | 75 | let after = db::release::live(cx.db).await?; |
| @@ -84,6 +87,11 @@ impl Handle for PublishRelease { | ||
| 84 | 87 | .collect(); |
| 85 | 88 | cx.sh.releases.discard(&stopped); |
| 86 | 89 | cx.emit(To::Everyone, Event::Release); |
| 90 | + if let Some(next) = next { | |
| 91 | + next.swap()?; | |
| 92 | + tracing::info!("server binary replaced by release {seq}"); | |
| 93 | + cx.sh.services.restart_soon(); | |
| 94 | + } | |
| 87 | 95 | |
| 88 | 96 | Ok(info(entry)) |
| 89 | 97 | } |
crates/server/src/lib.rsmodified+1 -0
| @@ -67,6 +67,7 @@ fn sibling_directory(database_path: &str, name: &str) -> Result<PathBuf, ServerE | ||
| 67 | 67 | |
| 68 | 68 | pub async fn showtime() -> Result<network::Exit, ServerError> { |
| 69 | 69 | let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); |
| 70 | + update::remember_exe(); | |
| 70 | 71 | |
| 71 | 72 | let config = load_config()?; |
| 72 | 73 |
crates/server/src/main.rsmodified+9 -0
| @@ -8,6 +8,7 @@ async fn main() -> Result<(), server::error::ServerError> { | ||
| 8 | 8 | .with_max_level(tracing::Level::INFO) |
| 9 | 9 | .init(); |
| 10 | 10 | |
| 11 | + std::hint::black_box(common::update::SERVER_MARK); | |
| 11 | 12 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 12 | 13 | match args |
| 13 | 14 | .iter() |
| @@ -15,6 +16,14 @@ async fn main() -> Result<(), server::error::ServerError> { | ||
| 15 | 16 | .collect::<Vec<_>>() |
| 16 | 17 | .as_slice() |
| 17 | 18 | { |
| 19 | + ["--version"] => { | |
| 20 | + println!( | |
| 21 | + "{} {}", | |
| 22 | + common::update::SERVER_VERSION, | |
| 23 | + env!("CARGO_PKG_VERSION") | |
| 24 | + ); | |
| 25 | + return Ok(()); | |
| 26 | + } | |
| 18 | 27 | ["builder", username] => set_role(username, Role::Builder).await?, |
| 19 | 28 | ["unbuilder", username] => set_role(username, Role::User).await?, |
| 20 | 29 | _ => { |
crates/server/src/network.rsmodified+12 -0
| @@ -34,6 +34,18 @@ use crate::{ | ||
| 34 | 34 | const HELLO_DEADLINE: Duration = Duration::from_secs(5); |
| 35 | 35 | const RESTART_DRAIN: Duration = Duration::from_secs(2); |
| 36 | 36 | |
| 37 | +const RESTART_DELAY: Duration = Duration::from_millis(300); | |
| 38 | + | |
| 39 | +impl Services { | |
| 40 | + pub fn restart_soon(&self) { | |
| 41 | + let restart = self.restart.clone(); | |
| 42 | + tokio::spawn(async move { | |
| 43 | + tokio::time::sleep(RESTART_DELAY).await; | |
| 44 | + restart.notify_one(); | |
| 45 | + }); | |
| 46 | + } | |
| 47 | +} | |
| 48 | + | |
| 37 | 49 | pub enum Exit { |
| 38 | 50 | Stopped, |
| 39 | 51 | Restart, |
crates/server/src/update.rsmodified+72 -1
| @@ -12,7 +12,10 @@ use common::{ | ||
| 12 | 12 | open::Stage, |
| 13 | 13 | permission, |
| 14 | 14 | time::now_ms, |
| 15 | - update::{Ask, FILE_MAX, Install, Me, Offer, Progress, Sha, Status, hex, sha256, target_of}, | |
| 15 | + update::{ | |
| 16 | + Ask, FILE_MAX, Install, Me, Offer, Progress, SERVER_TARGET, SERVER_VERSION, Sha, Status, | |
| 17 | + hex, sha256, target_of, | |
| 18 | + }, | |
| 16 | 19 | }; |
| 17 | 20 | use quinn::{Connection, RecvStream, VarInt}; |
| 18 | 21 | use tokio::io::AsyncReadExt; |
| @@ -25,6 +28,55 @@ use crate::{ | ||
| 25 | 28 | }; |
| 26 | 29 | |
| 27 | 30 | const PACK_LEVEL: i32 = 19; |
| 31 | +const PROBE: std::time::Duration = std::time::Duration::from_secs(5); | |
| 32 | + | |
| 33 | +static EXE: std::sync::LazyLock<Option<PathBuf>> = | |
| 34 | + std::sync::LazyLock::new(|| std::env::current_exe().ok()); | |
| 35 | + | |
| 36 | +pub fn remember_exe() { | |
| 37 | + std::sync::LazyLock::force(&EXE); | |
| 38 | +} | |
| 39 | + | |
| 40 | +fn broken(why: impl std::fmt::Display) -> ServerError { | |
| 41 | + warn!("new server binary rejected: {why}"); | |
| 42 | + CommonError::Release(ReleaseProblem::ServerBroken).into() | |
| 43 | +} | |
| 44 | + | |
| 45 | +fn probe(path: &Path) -> Result<(), ServerError> { | |
| 46 | + let mut child = std::process::Command::new(path) | |
| 47 | + .arg("--version") | |
| 48 | + .stdout(std::process::Stdio::piped()) | |
| 49 | + .stderr(std::process::Stdio::null()) | |
| 50 | + .spawn() | |
| 51 | + .map_err(broken)?; | |
| 52 | + let start = std::time::Instant::now(); | |
| 53 | + while child.try_wait().map_err(broken)?.is_none() { | |
| 54 | + if start.elapsed() > PROBE { | |
| 55 | + let _ = child.kill(); | |
| 56 | + return Err(broken("timed out")); | |
| 57 | + } | |
| 58 | + std::thread::sleep(std::time::Duration::from_millis(50)); | |
| 59 | + } | |
| 60 | + let output = child.wait_with_output().map_err(broken)?; | |
| 61 | + let said = String::from_utf8_lossy(&output.stdout); | |
| 62 | + if output.status.success() && said.starts_with(SERVER_VERSION) { | |
| 63 | + return Ok(()); | |
| 64 | + } | |
| 65 | + Err(broken(said.trim())) | |
| 66 | +} | |
| 67 | + | |
| 68 | +pub struct NextServer { | |
| 69 | + next: PathBuf, | |
| 70 | + exe: PathBuf, | |
| 71 | +} | |
| 72 | + | |
| 73 | +impl NextServer { | |
| 74 | + pub fn swap(self) -> Result<(), ServerError> { | |
| 75 | + std::fs::rename(&self.exe, self.exe.with_extension("old"))?; | |
| 76 | + std::fs::rename(&self.next, &self.exe)?; | |
| 77 | + Ok(()) | |
| 78 | + } | |
| 79 | +} | |
| 28 | 80 | const BOARD_KEEP_MS: u64 = 7 * 24 * 60 * 60 * 1000; |
| 29 | 81 | const CHUNK: usize = 64 << 10; |
| 30 | 82 | |
| @@ -187,6 +239,25 @@ impl Releases { | ||
| 187 | 239 | Ok(picked) |
| 188 | 240 | } |
| 189 | 241 | |
| 242 | + pub fn prepare_server(&self, staged: &[Staged]) -> Result<Option<NextServer>, ServerError> { | |
| 243 | + let Some(server) = staged.iter().find(|staged| staged.target == SERVER_TARGET) else { | |
| 244 | + return Ok(None); | |
| 245 | + }; | |
| 246 | + let exe = EXE.clone().ok_or_else(|| broken("own path unknown"))?; | |
| 247 | + let next = exe.with_extension("new"); | |
| 248 | + let packed = std::fs::File::open(path_of(&self.directory, &server.file))?; | |
| 249 | + let mut decoder = zstd::Decoder::new(packed)?; | |
| 250 | + let mut out = std::fs::File::create(&next)?; | |
| 251 | + std::io::copy(&mut decoder, &mut out)?; | |
| 252 | + drop(out); | |
| 253 | + use std::os::unix::fs::PermissionsExt; | |
| 254 | + std::fs::set_permissions(&next, std::fs::Permissions::from_mode(0o755))?; | |
| 255 | + probe(&next).inspect_err(|_| { | |
| 256 | + let _ = std::fs::remove_file(&next); | |
| 257 | + })?; | |
| 258 | + Ok(Some(NextServer { next, exe })) | |
| 259 | + } | |
| 260 | + | |
| 190 | 261 | pub fn unstage(&self, files: &[Sha]) { |
| 191 | 262 | let mut map = self.staged.lock().unwrap(); |
| 192 | 263 | for file in files { |