From 4cb1fc5f230ea470b7a812d3138015221a3e1558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20=C3=81lvaro?= Date: Sun, 2 Jun 2024 12:42:22 +0200 Subject: [PATCH 1/2] feat(platform): Add is_arm64 method On Apple Silicon the architecture is ARM64 instead of AArch64 --- salt/utils/platform.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/salt/utils/platform.py b/salt/utils/platform.py index c6ca7fe8cae9..7d83ba2095a3 100644 --- a/salt/utils/platform.py +++ b/salt/utils/platform.py @@ -231,6 +231,14 @@ def is_aarch64(): return platform.machine().startswith("aarch64") +@real_memoize +def is_arm64(): + """ + Simple function to return if host is ARM64 or not + """ + return platform.machine().startswith("arm64") + + def spawning_platform(): """ Returns True if multiprocessing.get_start_method(allow_none=False) returns "spawn" From 64e7a45072780c9678092837f1f490c769c1712d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20=C3=81lvaro?= Date: Mon, 3 Jun 2024 17:33:10 +0200 Subject: [PATCH 2/2] feat(mac_brew_pkg): Allow homebrew in all systems but Windows --- changelog/66609.changed.md | 1 + salt/modules/mac_brew_pkg.py | 33 ++++++++++++++----- .../pytests/unit/modules/test_mac_brew_pkg.py | 27 +++++++++++++-- 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 changelog/66609.changed.md diff --git a/changelog/66609.changed.md b/changelog/66609.changed.md new file mode 100644 index 000000000000..758a33fae8c5 --- /dev/null +++ b/changelog/66609.changed.md @@ -0,0 +1 @@ +Allow mac_brew_pkg to be used in systems different from macOS diff --git a/salt/modules/mac_brew_pkg.py b/salt/modules/mac_brew_pkg.py index 1a264b21ba9c..0b2884450bad 100644 --- a/salt/modules/mac_brew_pkg.py +++ b/salt/modules/mac_brew_pkg.py @@ -9,6 +9,8 @@ Typically, this is set to ``/usr/local`` for Intel Macs and ``/opt/homebrew`` for Apple Silicon Macs. +For Linux systems, the default prefix is: ``/home/linuxbrew/.linuxbrew``. + .. important:: If you feel that Salt should be using this module to manage packages on a minion, and it is using a different module (or gives an error similar to @@ -25,6 +27,7 @@ import salt.utils.json import salt.utils.path import salt.utils.pkg +import salt.utils.platform import salt.utils.versions from salt.exceptions import CommandExecutionError, MinionError, SaltInvocationError @@ -36,11 +39,11 @@ def __virtual__(): """ - Confine this module to macOS with Homebrew. + Confine this module to systems with Homebrew. """ - if __grains__["os"] != "MacOS": - return False, "brew module is macos specific" - if not _homebrew_bin(): + if salt.utils.platform.is_windows(): + return False, "Homebrew does not support Windows" + if not _homebrew_bin(quiet=True): return False, "The 'brew' binary was not found" return __virtualname__ @@ -109,10 +112,10 @@ def _homebrew_os_bin(): original_path = os.environ.get("PATH") try: - # Add "/opt/homebrew" temporary to the PATH for Apple Silicon if - # the PATH does not include "/opt/homebrew" + # Temporary add the default Homebrew's prefix to PATH + # if prefix is not contained yet. current_path = original_path or "" - homebrew_path = "/opt/homebrew/bin" + homebrew_path = _homebrew_default_prefix() + "/bin" if homebrew_path not in current_path.split(os.path.pathsep): extended_path = os.path.pathsep.join([current_path, homebrew_path]) os.environ["PATH"] = extended_path.lstrip(os.path.pathsep) @@ -129,14 +132,14 @@ def _homebrew_os_bin(): return brew -def _homebrew_bin(): +def _homebrew_bin(quiet=False): """ Returns the full path to the homebrew binary in the homebrew installation folder """ ret = homebrew_prefix() if ret is not None: ret += "/bin/brew" - else: + elif not quiet: log.warning("Failed to find homebrew prefix") return ret @@ -180,6 +183,18 @@ def _list_pkgs_from_context(versions_as_list): return ret +def _homebrew_default_prefix(): + """ + Returns the default homebrew prefix based on the OS and CPU architecture. + """ + if salt.utils.platform.is_darwin(): + if salt.utils.platform.is_arm64(): + return "/opt/homebrew" + return "/usr/local" + + return "/home/linuxbrew/.linuxbrew" + + def homebrew_prefix(): """ Returns the full path to the homebrew prefix. diff --git a/tests/pytests/unit/modules/test_mac_brew_pkg.py b/tests/pytests/unit/modules/test_mac_brew_pkg.py index 6cd46f1ebb5d..e0b04ce40cf1 100644 --- a/tests/pytests/unit/modules/test_mac_brew_pkg.py +++ b/tests/pytests/unit/modules/test_mac_brew_pkg.py @@ -520,7 +520,7 @@ def test_homebrew_prefix_returns_none_even_with_execution_errors(): # '_homebrew_os_bin' function tests: 1 -def test_homebrew_os_bin_fallback_apple_silicon(): +def test_homebrew_os_bin_appends_prefix_to_path(): """ Test the path to the homebrew executable for Apple Silicon. @@ -543,10 +543,33 @@ def mock_utils_path_which(*args): return apple_silicon_homebrew_bin return None - with patch("salt.utils.path.which", mock_utils_path_which): + with patch("salt.utils.platform.is_darwin", return_value=True), patch( + "salt.utils.platform.is_arm64", return_value=True + ), patch("salt.utils.path.which", mock_utils_path_which): assert mac_brew._homebrew_os_bin() == apple_silicon_homebrew_bin +# '_homebrew_default_prefix' function tests: 1 + + +def test_homebrew_default_prefix(): + """ + Tests homebrew's default prefix + """ + with patch("salt.utils.platform.is_darwin", return_value=True), patch( + "salt.utils.platform.is_arm64", return_value=True + ): + assert mac_brew._homebrew_default_prefix() == "/opt/homebrew" + + with patch("salt.utils.platform.is_darwin", return_value=True), patch( + "salt.utils.platform.is_arm64", return_value=False + ): + assert mac_brew._homebrew_default_prefix() == "/usr/local" + + with patch("salt.utils.platform.is_darwin", return_value=False): + assert mac_brew._homebrew_default_prefix() == "/home/linuxbrew/.linuxbrew" + + # '_homebrew_bin' function tests: 1