From 11ac2002a578a3c57740ee077a44efa5a0c4cc2a Mon Sep 17 00:00:00 2001 From: Boni Garcia Date: Thu, 2 Feb 2023 13:16:42 +0100 Subject: [PATCH 1/2] [rust] Fix local architecture discovery in Selenium Manager --- rust/src/config.rs | 24 ++++++++++++++++-------- rust/src/lib.rs | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/rust/src/config.rs b/rust/src/config.rs index bc28734fe0238..2442b06e8bee4 100644 --- a/rust/src/config.rs +++ b/rust/src/config.rs @@ -16,11 +16,12 @@ // under the License. use crate::config::OS::{LINUX, MACOS, WINDOWS}; -use crate::REQUEST_TIMEOUT_SEC; use crate::TTL_BROWSERS_SEC; use crate::TTL_DRIVERS_SEC; use std::env::consts::{ARCH, OS}; +use crate::{run_shell_command, ENV_PROCESSOR_ARCHITECTURE, REQUEST_TIMEOUT_SEC, UNAME_COMMAND}; +use std::env; pub struct ManagerConfig { pub browser_version: String, @@ -36,11 +37,17 @@ pub struct ManagerConfig { impl ManagerConfig { pub fn default() -> ManagerConfig { + let self_os = OS; + let self_arch = if WINDOWS.is(self_os) { + env::var(ENV_PROCESSOR_ARCHITECTURE).unwrap_or_default() + } else { + run_shell_command(self_os, UNAME_COMMAND.to_string()).unwrap_or_default() + }; ManagerConfig { browser_version: "".to_string(), driver_version: "".to_string(), - os: OS.to_string(), - arch: ARCH.to_string(), + os: self_os.to_string(), + arch: self_arch, browser_path: "".to_string(), proxy: "".to_string(), timeout: REQUEST_TIMEOUT_SEC, @@ -107,15 +114,16 @@ pub enum ARCH { } impl ARCH { - pub fn to_str(&self) -> &str { + pub fn to_str_vector(&self) -> Vec<&str> { match self { - ARCH::X32 => "x86", - ARCH::X64 => "x86_64", - ARCH::ARM64 => "aarch64", + ARCH::X32 => vec!["x86", "i386"], + ARCH::X64 => vec!["x86_64", "x64", "i686", "amd64", "ia64"], + ARCH::ARM64 => vec!["aarch64", "arm64", "arm"], } } pub fn is(&self, arch: &str) -> bool { - self.to_str().eq_ignore_ascii_case(arch) + self.to_str_vector() + .contains(&arch.to_ascii_lowercase().as_str()) } } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index e271dbae0ab4e..c981b828a7a0e 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -69,11 +69,13 @@ pub const DASH_DASH_VERSION: &str = "{} --version"; pub const ENV_PROGRAM_FILES: &str = "PROGRAMFILES"; pub const ENV_PROGRAM_FILES_X86: &str = "PROGRAMFILES(X86)"; pub const ENV_LOCALAPPDATA: &str = "LOCALAPPDATA"; +pub const ENV_PROCESSOR_ARCHITECTURE: &str = "PROCESSOR_ARCHITECTURE"; pub const FALLBACK_RETRIES: u32 = 5; pub const WHERE_COMMAND: &str = "where {}"; pub const WHICH_COMMAND: &str = "which {}"; pub const TTL_BROWSERS_SEC: u64 = 0; pub const TTL_DRIVERS_SEC: u64 = 86400; +pub const UNAME_COMMAND: &str = "uname -m"; pub trait SeleniumManager { // ---------------------------------------------------------- From 1740bfef5a23705e83c2a85d6df7475c0b803289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boni=20Garc=C3=ADa?= Date: Thu, 2 Feb 2023 14:13:08 +0100 Subject: [PATCH 2/2] [rust] Enhance uname execution to detect ARM architectures --- rust/src/config.rs | 23 +++++++++++++++++++---- rust/src/lib.rs | 14 ++++++-------- rust/src/main.rs | 2 +- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/rust/src/config.rs b/rust/src/config.rs index 2442b06e8bee4..1498059e44fa7 100644 --- a/rust/src/config.rs +++ b/rust/src/config.rs @@ -19,9 +19,14 @@ use crate::config::OS::{LINUX, MACOS, WINDOWS}; use crate::TTL_BROWSERS_SEC; use crate::TTL_DRIVERS_SEC; -use std::env::consts::{ARCH, OS}; -use crate::{run_shell_command, ENV_PROCESSOR_ARCHITECTURE, REQUEST_TIMEOUT_SEC, UNAME_COMMAND}; +use crate::{ + format_one_arg, run_shell_command, ENV_PROCESSOR_ARCHITECTURE, REQUEST_TIMEOUT_SEC, + UNAME_COMMAND, +}; use std::env; +use std::env::consts::{OS}; + +pub const ARM64_ARCH: &str = "arm64"; pub struct ManagerConfig { pub browser_version: String, @@ -41,7 +46,17 @@ impl ManagerConfig { let self_arch = if WINDOWS.is(self_os) { env::var(ENV_PROCESSOR_ARCHITECTURE).unwrap_or_default() } else { - run_shell_command(self_os, UNAME_COMMAND.to_string()).unwrap_or_default() + let uname_a = format_one_arg(UNAME_COMMAND, "a"); + if run_shell_command(self_os, uname_a) + .unwrap_or_default() + .to_ascii_lowercase() + .contains(ARM64_ARCH) + { + ARM64_ARCH.to_string() + } else { + let uname_m = format_one_arg(UNAME_COMMAND, "m"); + run_shell_command(self_os, uname_m).unwrap_or_default() + } }; ManagerConfig { browser_version: "".to_string(), @@ -118,7 +133,7 @@ impl ARCH { match self { ARCH::X32 => vec!["x86", "i386"], ARCH::X64 => vec!["x86_64", "x64", "i686", "amd64", "ia64"], - ARCH::ARM64 => vec!["aarch64", "arm64", "arm"], + ARCH::ARM64 => vec![ARM64_ARCH, "aarch64", "arm"], } } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index c981b828a7a0e..58913e11b433f 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -75,7 +75,7 @@ pub const WHERE_COMMAND: &str = "where {}"; pub const WHICH_COMMAND: &str = "which {}"; pub const TTL_BROWSERS_SEC: u64 = 0; pub const TTL_DRIVERS_SEC: u64 = 86400; -pub const UNAME_COMMAND: &str = "uname -m"; +pub const UNAME_COMMAND: &str = "uname -{}"; pub trait SeleniumManager { // ---------------------------------------------------------- @@ -173,13 +173,11 @@ pub trait SeleniumManager { break; } if !self.is_safari() { - metadata - .browsers - .push(create_browser_metadata( - browser_name, - &browser_version, - browser_ttl, - )); + metadata.browsers.push(create_browser_metadata( + browser_name, + &browser_version, + browser_ttl, + )); write_metadata(&metadata, self.get_logger()); } if !browser_version.is_empty() { diff --git a/rust/src/main.rs b/rust/src/main.rs index bc6de041facfa..e53e396966a33 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -25,8 +25,8 @@ use exitcode::{DATAERR, UNAVAILABLE}; use selenium_manager::logger::Logger; use selenium_manager::REQUEST_TIMEOUT_SEC; -use selenium_manager::TTL_DRIVERS_SEC; use selenium_manager::TTL_BROWSERS_SEC; +use selenium_manager::TTL_DRIVERS_SEC; use selenium_manager::{ clear_cache, get_manager_by_browser, get_manager_by_driver, SeleniumManager, };