From e84dda0ca59408a845de34363019cabc0e0a38e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Dec 2026 05:10:01 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Build=20sets=20up=20the=20W?= =?UTF-8?q?indows=20VM=20itself:=20creates=20yumush-win=20(dockurr/windows?= =?UTF-8?q?,=20ports=20on=20127.0.0.1=20only,=20ssh=20key=20+=20OEM=20inst?= =?UTF-8?q?all.bat=20generated),=20waits=20for=20the=20OEM=20install,=20an?= =?UTF-8?q?d=20pins=20the=20VM's=20rustc=20to=20the=20host's=20version=20e?= =?UTF-8?q?ach=20build=20like=20the=20Linux=20container.=20Drops=20the=20M?= =?UTF-8?q?issing/Broken=20VM=20states?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/src/release.rs | 166 +++++++++++++++++++------- crates/client/src/release/install.bat | 7 ++ 2 files changed, 132 insertions(+), 41 deletions(-) create mode 100644 crates/client/src/release/install.bat diff --git a/crates/client/src/release.rs b/crates/client/src/release.rs index d25547e..d9ada5a 100644 --- a/crates/client/src/release.rs +++ b/crates/client/src/release.rs @@ -70,6 +70,10 @@ const LINUX_CACHE: &str = ".cache/yumush-build"; 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"; const ARCHIVE: &str = "dist/source.tgz"; const VM: &str = "yumush-win"; +const VM_DIR: &str = "vm/yumush-win"; +const VM_IMAGE: &str = "docker.io/dockurr/windows"; +const VM_AUDIO: &str = "ARGUMENTS=-audiodev none,id=snd0 -device ich9-intel-hda,id=hda1 -device hda-duplex,bus=hda1.0,audiodev=snd0 -device ich9-intel-hda,id=hda2 -device hda-duplex,bus=hda2.0,audiodev=snd0"; +const INSTALL: &str = include_str!("release/install.bat"); const TARGET: &str = "yumush@127.0.0.1"; const PORT: &str = "2222"; const EXTRACT: &str = "Remove-Item -Recurse -Force release -ErrorAction SilentlyContinue; New-Item -ItemType Directory release | Out-Null; tar -xmzf release.tgz -C release"; @@ -156,20 +160,11 @@ impl Cx { } } -enum Vm { - Missing, - Running, - Started, - Broken(String), -} - fn run(source: &Path, lanes: &[Cx]) { - let key = dirs::home_dir() - .unwrap_or_default() - .join("vm/yumush-win/id_ed25519"); + let key = vm_dir().join("id_ed25519"); let first = &lanes[0]; - let vm = match preflight(source) { - Ok(vm) => vm, + let vm_was_running = match preflight(source) { + Ok(running) => running, Err(e) => { first.send(Event::Failed(LANES[0].1[0], e)); first.send(Event::Done); @@ -178,23 +173,23 @@ fn run(source: &Path, lanes: &[Cx]) { }; thread::scope(|scope| { for ((_, jobs), cx) in LANES.iter().zip(lanes) { - let (key, vm) = (&key, &vm); - scope.spawn(move || run_lane(jobs, source, key, cx, vm)); + let key = &key; + scope.spawn(move || run_lane(jobs, source, key, cx)); } }); - if let Vm::Started = vm { + if !vm_was_running { podman_ok(&["stop", VM]); } podman_ok(&["stop", "-t", "2", LINUX]); first.send(Event::Done); } -fn run_lane(jobs: &[Job], source: &Path, key: &Path, cx: &Cx, vm: &Vm) { +fn run_lane(jobs: &[Job], source: &Path, key: &Path, cx: &Cx) { for &job in jobs { let result = if cx.cancelled() { Err("cancelled".to_string()) } else { - run_job(job, source, key, cx, vm) + run_job(job, source, key, cx) }; if let Err(e) = result { let e = if cx.cancelled() { @@ -208,7 +203,7 @@ fn run_lane(jobs: &[Job], source: &Path, key: &Path, cx: &Cx, vm: &Vm) { } } -fn preflight(source: &Path) -> Result { +fn preflight(source: &Path) -> Result { let dirty = Command::new("git") .arg("-C") .arg(source) @@ -219,23 +214,17 @@ fn preflight(source: &Path) -> Result { return Err("uncommitted changes".into()); } snapshot(source)?; - if !podman_ok(&["container", "exists", VM]) { - return Ok(Vm::Missing); - } - let state = Command::new("podman") - .args(["inspect", "-f", "{{.State.Running}}", VM]) + Ok(container_running(VM)) +} + +fn container_running(container: &str) -> bool { + Command::new("podman") + .args(["inspect", "-f", "{{.State.Running}}", container]) .output() - .map_err(|e| e.to_string())?; - if String::from_utf8_lossy(&state.stdout).trim() != "false" { - return Ok(Vm::Running); - } - Ok(match check(Command::new("podman").args(["start", VM])) { - Ok(()) => Vm::Started, - Err(error) => Vm::Broken(error), - }) + .is_ok_and(|out| String::from_utf8_lossy(&out.stdout).trim() == "true") } -fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx, vm: &Vm) -> Result<(), String> { +fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx) -> Result<(), String> { match job { Job::LinuxStart => { if !cfg!(target_os = "linux") { @@ -259,19 +248,13 @@ fn run_job(job: Job, source: &Path, key: &Path, cx: &Cx, vm: &Vm) -> Result<(), build_server(job, source, cx) } Job::WindowsStart => { - if let Vm::Missing = vm { - return skip(cx, job, "no yumush-win"); + if !cfg!(target_os = "linux") { + return skip(cx, job, "not linux"); } cx.send(Event::Started(job)); - if let Vm::Broken(error) = vm { - return Err(error.clone()); - } start_windows(job, source, key, cx) } Job::Windows(level) => { - if let Vm::Missing = vm { - return skip(cx, job, "no yumush-win"); - } cx.send(Event::Started(job)); build_windows(job, level, source, key, cx) } @@ -421,8 +404,105 @@ fn build_server(job: Job, source: &Path, cx: &Cx) -> Result<(), String> { ) } +fn vm_dir() -> PathBuf { + dirs::home_dir().unwrap_or_default().join(VM_DIR) +} + +fn create_windows(job: Job, key: &Path, cx: &Cx) -> Result<(), String> { + if !Path::new("/dev/kvm").exists() { + return Err("no /dev/kvm".into()); + } + let dir = vm_dir(); + for sub in ["storage", "oem", "shared"] { + fs::create_dir_all(dir.join(sub)).map_err(|e| e.to_string())?; + } + if !key.exists() { + check( + Command::new("ssh-keygen") + .args(["-q", "-t", "ed25519", "-N", "", "-C", "yumush-vm", "-f"]) + .arg(key), + )?; + } + let public = fs::read_to_string(key.with_extension("pub")).map_err(|e| e.to_string())?; + fs::write( + dir.join("oem/install.bat"), + INSTALL.replace("{key}", public.trim()), + ) + .map_err(|e| e.to_string())?; + let cores = thread::available_parallelism().map_or(4, |n| n.get()); + let mount = |sub: &str| format!("{}:/{sub}:Z", dir.join(sub).display()); + cx.send(Event::Line(job, "creating Windows VM".into())); + check( + Command::new("podman") + .args(["run", "-d", "--name", VM]) + .args([ + "-e", + "VERSION=11", + "-e", + "RAM_SIZE=12G", + "-e", + "DISK_SIZE=96G", + ]) + .args(["-e", &format!("CPU_CORES={cores}")]) + .args([ + "-e", + "USERNAME=yumush", + "-e", + "PASSWORD=yumush", + "-e", + "USER_PORTS=22", + ]) + .args(["-e", VM_AUDIO]) + .args([ + "--device", + "/dev/kvm", + "--device", + "/dev/net/tun", + "--cap-add", + "NET_ADMIN", + ]) + .args(["-p", "127.0.0.1:8006:8006", "-p", "127.0.0.1:3389:3389"]) + .args(["-p", &format!("127.0.0.1:{PORT}:22")]) + .args([ + "-v", + &mount("storage"), + "-v", + &mount("oem"), + "-v", + &mount("shared"), + ]) + .args(["--stop-timeout", "120", VM_IMAGE]), + ) +} + fn start_windows(job: Job, source: &Path, key: &Path, cx: &Cx) -> Result<(), String> { + if !podman_ok(&["container", "exists", VM]) { + create_windows(job, key, cx)?; + } else if !container_running(VM) { + check(Command::new("podman").args(["start", VM]))?; + } wait_ssh(job, key, cx)?; + while !ssh_ok_with( + key, + "if (Test-Path C:\\OEM\\finished.txt) { exit 0 } else { exit 1 }", + ) { + if cx.cancelled() { + return Err("cancelled".into()); + } + cx.send(Event::Line(job, "installing build tools".into())); + thread::sleep(Duration::from_secs(15)); + } + let rust = host_rust()?; + stream( + &mut ps( + key, + &format!( + "$env:Path += \";$env:USERPROFILE\\.cargo\\bin\"\nrustup toolchain install {rust} --profile minimal\nrustup default {rust}" + ), + ), + job, + cx, + )?; check( scp(key) .arg(source.join(ARCHIVE)) @@ -519,8 +599,12 @@ fn ps(key: &Path, script: &str) -> Command { } fn ssh_ok(key: &Path) -> bool { + ssh_ok_with(key, "echo ok") +} + +fn ssh_ok_with(key: &Path, command: &str) -> bool { ssh(key) - .args(["echo", "ok"]) + .arg(command) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() diff --git a/crates/client/src/release/install.bat b/crates/client/src/release/install.bat new file mode 100644 index 0000000..cd7815d --- /dev/null +++ b/crates/client/src/release/install.bat @@ -0,0 +1,7 @@ +@echo off +powershell -NoProfile -ExecutionPolicy Bypass -Command "Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0; Set-Service sshd -StartupType Automatic; Start-Service sshd; New-NetFirewallRule -Name sshd -DisplayName sshd -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 -ErrorAction SilentlyContinue; Set-Content -Path C:\ProgramData\ssh\administrators_authorized_keys -Value '{key}'; icacls C:\ProgramData\ssh\administrators_authorized_keys /inheritance:r /grant 'Administrators:F' /grant 'SYSTEM:F'; New-ItemProperty -Path 'HKLM:\SOFTWARE\OpenSSH' -Name DefaultShell -Value 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe' -PropertyType String -Force" +curl.exe -L -o C:\vs_BuildTools.exe https://aka.ms/vs/17/release/vs_BuildTools.exe +C:\vs_BuildTools.exe --quiet --wait --norestart --nocache --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended +curl.exe -L -o C:\rustup-init.exe https://win.rustup.rs/x86_64 +C:\rustup-init.exe -y --default-toolchain none --profile minimal +echo done > C:\OEM\finished.txt