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 | linux_v1 = Linux v1 | 18 | linux_v1 = Linux v1 |
| 19 | linux_v3 = Linux v3 | 19 | linux_v3 = Linux v3 |
| 20 | windows_vm = Windows VM | 20 | windows_vm = Windows VM |
| 21 | -this_machine = This machine | 21 | +this_machine = Linux container |
| 22 | prepare = Prepare | 22 | prepare = Prepare |
| 23 | windows_v1 = Windows v1 | 23 | windows_v1 = Windows v1 |
| 24 | windows_v3 = Windows v3 | 24 | windows_v3 = Windows v3 |
| @@ -29,3 +29,4 @@ open_releases = Open Releases | |||
| 29 | dismiss = Dismiss | 29 | dismiss = Dismiss |
| 30 | ready = Ready to publish | 30 | ready = Ready to publish |
| 31 | discard = Discard | 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 | linux_v1 = Linux v1 | 18 | linux_v1 = Linux v1 |
| 19 | linux_v3 = Linux v3 | 19 | linux_v3 = Linux v3 |
| 20 | windows_vm = Windows VM | 20 | windows_vm = Windows VM |
| 21 | -this_machine = Bu makine | 21 | +this_machine = Linux kapsayıcı |
| 22 | prepare = Hazırlık | 22 | prepare = Hazırlık |
| 23 | windows_v1 = Windows v1 | 23 | windows_v1 = Windows v1 |
| 24 | windows_v3 = Windows v3 | 24 | windows_v3 = Windows v3 |
| @@ -29,3 +29,4 @@ open_releases = Sürümleri aç | |||
| 29 | dismiss = Kapat | 29 | dismiss = Kapat |
| 30 | ready = Yayına hazır | 30 | ready = Yayına hazır |
| 31 | discard = Vazgeç | 31 | discard = Vazgeç |
| 32 | +server = Sunucu | ||
crates/client/src/gui/builder/releases.rsmodified+2 -0
| @@ -171,8 +171,10 @@ impl Desk { | |||
| 171 | fn job_label(job: Job) -> &'static str { | 171 | fn job_label(job: Job) -> &'static str { |
| 172 | let w = &w().builder_release; | 172 | let w = &w().builder_release; |
| 173 | match job { | 173 | match job { |
| 174 | + Job::LinuxStart => w.prepare, | ||
| 174 | Job::Linux(Level::V1) => w.linux_v1, | 175 | Job::Linux(Level::V1) => w.linux_v1, |
| 175 | Job::Linux(Level::V3) => w.linux_v3, | 176 | Job::Linux(Level::V3) => w.linux_v3, |
| 177 | + Job::Server => w.server, | ||
| 176 | Job::WindowsStart => w.prepare, | 178 | Job::WindowsStart => w.prepare, |
| 177 | Job::Windows(Level::V1) => w.windows_v1, | 179 | Job::Windows(Level::V1) => w.windows_v1, |
| 178 | Job::Windows(Level::V3) => w.windows_v3, | 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 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 18 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 19 | pub enum Job { | 19 | pub enum Job { |
| 20 | + LinuxStart, | ||
| 20 | Linux(Level), | 21 | Linux(Level), |
| 22 | + Server, | ||
| 21 | WindowsStart, | 23 | WindowsStart, |
| 22 | Windows(Level), | 24 | Windows(Level), |
| 23 | } | 25 | } |
| @@ -29,7 +31,15 @@ pub enum Lane { | |||
| 29 | } | 31 | } |
| 30 | 32 | ||
| 31 | pub const LANES: [(Lane, &[Job]); 2] = [ | 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 | Lane::Vm, | 44 | Lane::Vm, |
| 35 | &[ | 45 | &[ |
| @@ -54,7 +64,10 @@ pub enum Event { | |||
| 54 | Done, | 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 | const ARCHIVE: &str = "dist/source.tgz"; | 71 | const ARCHIVE: &str = "dist/source.tgz"; |
| 59 | const VM: &str = "yumush-win"; | 72 | const VM: &str = "yumush-win"; |
| 60 | const TARGET: &str = "yumush@127.0.0.1"; | 73 | const TARGET: &str = "yumush@127.0.0.1"; |
| @@ -117,6 +130,7 @@ impl Build { | |||
| 117 | 130 | ||
| 118 | pub fn cancel(&self) { | 131 | pub fn cancel(&self) { |
| 119 | self.cancel.store(true, Ordering::SeqCst); | 132 | self.cancel.store(true, Ordering::SeqCst); |
| 133 | + podman_ok(&["stop", "-t", "2", LINUX]); | ||
| 120 | for slot in &self.children { | 134 | for slot in &self.children { |
| 121 | if let Some(child) = slot.lock().unwrap().as_mut() { | 135 | if let Some(child) = slot.lock().unwrap().as_mut() { |
| 122 | let _ = child.kill(); | 136 | let _ = child.kill(); |
| @@ -171,6 +185,7 @@ fn run(source: &Path, lanes: &[Cx]) { | |||
| 171 | if let Vm::Started = vm { | 185 | if let Vm::Started = vm { |
| 172 | podman_ok(&["stop", VM]); | 186 | podman_ok(&["stop", VM]); |
| 173 | } | 187 | } |
| 188 | + podman_ok(&["stop", "-t", "2", LINUX]); | ||
| 174 | first.send(Event::Done); | 189 | first.send(Event::Done); |
| 175 | } | 190 | } |
| 176 | 191 | ||
| @@ -222,6 +237,13 @@ fn preflight(source: &Path) -> Result<Vm, String> { | |||
| 222 | 237 | ||
| 223 | fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx, vm: &Vm) -> Result<(), String> { | 238 | fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx, vm: &Vm) -> Result<(), String> { |
| 224 | match job { | 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 | Job::Linux(level) => { | 247 | Job::Linux(level) => { |
| 226 | if !cfg!(target_os = "linux") { | 248 | if !cfg!(target_os = "linux") { |
| 227 | return skip(cx, job, "not linux"); | 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 | cx.send(Event::Started(job)); | 251 | cx.send(Event::Started(job)); |
| 230 | build_linux(job, level, source, cx) | 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 | Job::WindowsStart => { | 261 | Job::WindowsStart => { |
| 233 | if let Vm::Missing = vm { | 262 | if let Vm::Missing = vm { |
| 234 | return skip(cx, job, "no yumush-win"); | 263 | return skip(cx, job, "no yumush-win"); |
| @@ -268,59 +297,130 @@ fn toml_text(cpu: &str) -> String { | |||
| 268 | } | 297 | } |
| 269 | 298 | ||
| 270 | fn snapshot(source: &Path) -> Result<(), String> { | 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 | fs::create_dir_all(source.join("dist")).map_err(|e| e.to_string())?; | 300 | fs::create_dir_all(source.join("dist")).map_err(|e| e.to_string())?; |
| 277 | - let tgz = source.join(ARCHIVE); | ||
| 278 | check( | 301 | check( |
| 279 | Command::new("git") | 302 | Command::new("git") |
| 280 | .arg("-C") | 303 | .arg("-C") |
| 281 | .arg(source) | 304 | .arg(source) |
| 282 | .args(["archive", "--format=tar.gz", "-o"]) | 305 | .args(["archive", "--format=tar.gz", "-o"]) |
| 283 | - .arg(&tgz) | 306 | + .arg(source.join(ARCHIVE)) |
| 284 | .arg("HEAD"), | 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> { | 311 | +fn linux_cache() -> PathBuf { |
| 296 | - let (name, cpu) = flags(level); | 312 | + dirs::home_dir().unwrap_or_default().join(LINUX_CACHE) |
| 297 | - fs::write( | 313 | +} |
| 298 | - source.join("dist").join(format!("{name}.toml")), | 314 | + |
| 299 | - toml_text(cpu), | 315 | +fn host_rust() -> Result<String, String> { |
| 300 | - ) | 316 | + let out = Command::new("rustc") |
| 301 | - .map_err(|e| e.to_string())?; | 317 | + .arg("--version") |
| 302 | - let mut cmd = Command::new("cargo"); | 318 | + .output() |
| 303 | - cmd.current_dir(source.join(TREE)) | 319 | + .map_err(|e| e.to_string())?; |
| 304 | - .args(["build", "--release", "-p", "client", "--target-dir"]) | 320 | + String::from_utf8_lossy(&out.stdout) |
| 305 | - .arg(source.join(format!("target/release-{name}"))) | 321 | + .split_whitespace() |
| 306 | - .arg("--config") | 322 | + .nth(1) |
| 307 | - .arg(source.join(format!("dist/{name}.toml"))) | 323 | + .map(str::to_string) |
| 308 | - .env("CARGO_TERM_PROGRESS_WHEN", "always") | 324 | + .ok_or_else(|| "rustc version unknown".to_string()) |
| 309 | - .env("CARGO_TERM_PROGRESS_WIDTH", "200") | 325 | +} |
| 310 | - .env("CARGO_TERM_COLOR", "never") | 326 | + |
| 311 | - .stdout(Stdio::null()); | 327 | +fn in_linux(script: &str) -> Command { |
| 312 | - let built = source.join(format!("target/release-{name}/release/client")); | 328 | + let mut cmd = Command::new("podman"); |
| 313 | - if built.exists() { | 329 | + cmd.args(["exec", "-w", "/build"]) |
| 314 | - fs::remove_file(&built).map_err(|e| e.to_string())?; | 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)?; | 345 | + if !podman_ok(&["container", "exists", LINUX]) { |
| 317 | - let dest = source.join("dist").join(format!("x86_64_{name}-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 | fs::copy(&built, &dest).map_err(|e| e.to_string())?; | 395 | fs::copy(&built, &dest).map_err(|e| e.to_string())?; |
| 319 | cx.send(Event::Built(job, dest)); | 396 | cx.send(Event::Built(job, dest)); |
| 320 | cx.send(Event::Finished(job)); | 397 | cx.send(Event::Finished(job)); |
| 321 | Ok(()) | 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 | fn start_windows(job: Job, source: &Path, key: &Path, cx: &Cx) -> Result<(), String> { | 424 | fn start_windows(job: Job, source: &Path, key: &Path, cx: &Cx) -> Result<(), String> { |
| 325 | wait_ssh(job, key, cx)?; | 425 | wait_ssh(job, key, cx)?; |
| 326 | check( | 426 | check( |
crates/common/src/error.rsmodified+2 -0
| @@ -165,4 +165,6 @@ pub enum ReleaseProblem { | |||
| 165 | SameTarget, | 165 | SameTarget, |
| 166 | #[error("file not staged")] | 166 | #[error("file not staged")] |
| 167 | NotStaged, | 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 | pub const ALPN: &[u8] = b"yumush-update/1"; | 3 | pub const ALPN: &[u8] = b"yumush-update/1"; |
| 4 | pub const FILE_MAX: u64 = 512 << 20; | 4 | pub const FILE_MAX: u64 = 512 << 20; |
| 5 | pub const MARK: &str = "yumush-target:"; | 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 | const TARGET_MAX: usize = 32; | 9 | const TARGET_MAX: usize = 32; |
| 7 | 10 | ||
| 8 | pub type Sha = [u8; 32]; | 11 | pub type Sha = [u8; 32]; |
crates/server/src/call/admin.rsmodified+1 -5
| @@ -690,11 +690,7 @@ impl Handle for RestartServer { | |||
| 690 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { | 690 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { |
| 691 | let me = me.user()?; | 691 | let me = me.user()?; |
| 692 | tracing::info!("restart requested by {}", me.username); | 692 | tracing::info!("restart requested by {}", me.username); |
| 693 | - let restart = cx.sh.services.restart.clone(); | 693 | + cx.sh.services.restart_soon(); |
| 694 | - tokio::spawn(async move { | ||
| 695 | - tokio::time::sleep(std::time::Duration::from_millis(300)).await; | ||
| 696 | - restart.notify_one(); | ||
| 697 | - }); | ||
| 698 | Ok(()) | 694 | Ok(()) |
| 699 | } | 695 | } |
| 700 | } | 696 | } |
crates/server/src/call/release.rsmodified+8 -0
| @@ -67,6 +67,9 @@ impl Handle for PublishRelease { | |||
| 67 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<ReleaseInfo, ServerError> { | 67 | async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<ReleaseInfo, ServerError> { |
| 68 | let me = me.user()?; | 68 | let me = me.user()?; |
| 69 | let files = cx.sh.releases.staged(&self.files)?; | 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 | let before = db::release::live(cx.db).await?; | 73 | let before = db::release::live(cx.db).await?; |
| 71 | let seq = db::release::insert(self.required, &files, cx.db).await?; | 74 | let seq = db::release::insert(self.required, &files, cx.db).await?; |
| 72 | let after = db::release::live(cx.db).await?; | 75 | let after = db::release::live(cx.db).await?; |
| @@ -84,6 +87,11 @@ impl Handle for PublishRelease { | |||
| 84 | .collect(); | 87 | .collect(); |
| 85 | cx.sh.releases.discard(&stopped); | 88 | cx.sh.releases.discard(&stopped); |
| 86 | cx.emit(To::Everyone, Event::Release); | 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 | Ok(info(entry)) | 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 | pub async fn showtime() -> Result<network::Exit, ServerError> { | 68 | pub async fn showtime() -> Result<network::Exit, ServerError> { |
| 69 | let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); | 69 | let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); |
| 70 | + update::remember_exe(); | ||
| 70 | 71 | ||
| 71 | let config = load_config()?; | 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 | .with_max_level(tracing::Level::INFO) | 8 | .with_max_level(tracing::Level::INFO) |
| 9 | .init(); | 9 | .init(); |
| 10 | 10 | ||
| 11 | + std::hint::black_box(common::update::SERVER_MARK); | ||
| 11 | let args: Vec<String> = std::env::args().skip(1).collect(); | 12 | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 12 | match args | 13 | match args |
| 13 | .iter() | 14 | .iter() |
| @@ -15,6 +16,14 @@ async fn main() -> Result<(), server::error::ServerError> { | |||
| 15 | .collect::<Vec<_>>() | 16 | .collect::<Vec<_>>() |
| 16 | .as_slice() | 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 | ["builder", username] => set_role(username, Role::Builder).await?, | 27 | ["builder", username] => set_role(username, Role::Builder).await?, |
| 19 | ["unbuilder", username] => set_role(username, Role::User).await?, | 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 | const HELLO_DEADLINE: Duration = Duration::from_secs(5); | 34 | const HELLO_DEADLINE: Duration = Duration::from_secs(5); |
| 35 | const RESTART_DRAIN: Duration = Duration::from_secs(2); | 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 | pub enum Exit { | 49 | pub enum Exit { |
| 38 | Stopped, | 50 | Stopped, |
| 39 | Restart, | 51 | Restart, |
crates/server/src/update.rsmodified+72 -1
| @@ -12,7 +12,10 @@ use common::{ | |||
| 12 | open::Stage, | 12 | open::Stage, |
| 13 | permission, | 13 | permission, |
| 14 | time::now_ms, | 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 | use quinn::{Connection, RecvStream, VarInt}; | 20 | use quinn::{Connection, RecvStream, VarInt}; |
| 18 | use tokio::io::AsyncReadExt; | 21 | use tokio::io::AsyncReadExt; |
| @@ -25,6 +28,55 @@ use crate::{ | |||
| 25 | }; | 28 | }; |
| 26 | 29 | ||
| 27 | const PACK_LEVEL: i32 = 19; | 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 | const BOARD_KEEP_MS: u64 = 7 * 24 * 60 * 60 * 1000; | 80 | const BOARD_KEEP_MS: u64 = 7 * 24 * 60 * 60 * 1000; |
| 29 | const CHUNK: usize = 64 << 10; | 81 | const CHUNK: usize = 64 << 10; |
| 30 | 82 | ||
| @@ -187,6 +239,25 @@ impl Releases { | |||
| 187 | Ok(picked) | 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 | pub fn unstage(&self, files: &[Sha]) { | 261 | pub fn unstage(&self, files: &[Sha]) { |
| 191 | let mut map = self.staged.lock().unwrap(); | 262 | let mut map = self.staged.lock().unwrap(); |
| 192 | for file in files { | 263 | for file in files { |