From 42303880177207f8d089f4acf0346e8aeda7139e Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Sun, 13 Feb 2022 08:08:18 +0200 Subject: [PATCH] Support multiple WSL distributions (fix #855) (#858) --- src/steps/os/windows.rs | 43 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/steps/os/windows.rs b/src/steps/os/windows.rs index f5bf9cae..254754d5 100644 --- a/src/steps/os/windows.rs +++ b/src/steps/os/windows.rs @@ -60,17 +60,25 @@ pub fn run_scoop(cleanup: bool, run_type: RunType) -> Result<()> { Ok(()) } -pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> { - let wsl = require("wsl")?; +fn get_wsl_distributions(wsl: &Path) -> Result> { + let output = Command::new(wsl).args(&["--list", "-q"]).check_output()?; + Ok(output + .lines() + .filter(|s| !s.is_empty()) + .map(|x| x.replace("\u{0}", "").replace('\r', "")) + .collect()) +} + +fn upgrade_wsl_distribution(wsl: &Path, dist: &str, ctx: &ExecutionContext) -> Result<()> { let topgrade = Command::new(&wsl) - .args(&["bash", "-lc", "which topgrade"]) + .args(&["-d", dist, "bash", "-lc", "which topgrade"]) .check_output() .map_err(|_| SkipStep(String::from("Could not find Topgrade installed in WSL")))?; let mut command = ctx.run_type().execute(&wsl); command - .args(&["bash", "-c"]) - .arg(format!("TOPGRADE_PREFIX=WSL exec {}", topgrade)); + .args(&["-d", dist, "bash", "-c"]) + .arg(format!("TOPGRADE_PREFIX={} exec {}", dist, topgrade)); if ctx.config().yes(Step::Wsl) { command.arg("-y"); @@ -79,6 +87,31 @@ pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> { command.check_run() } +pub fn run_wsl_topgrade(ctx: &ExecutionContext) -> Result<()> { + let wsl = require("wsl")?; + let wsl_distributions = get_wsl_distributions(&wsl)?; + let mut ran = false; + + debug!("WSL distributions: {:?}", wsl_distributions); + + for distribution in wsl_distributions { + let result = upgrade_wsl_distribution(&wsl, &distribution, ctx); + debug!("Upgrading {:?}: {:?}", distribution, result); + if let Err(e) = result { + if e.is::() { + continue; + } + } + ran = true + } + + if ran { + Ok(()) + } else { + Err(SkipStep(String::from("Could not find Topgrade in any WSL disribution")).into()) + } +} + pub fn windows_update(ctx: &ExecutionContext) -> Result<()> { let powershell = powershell::Powershell::windows_powershell();