From 83d4c3ce6e403659435a55de44bca2ca533c6a42 Mon Sep 17 00:00:00 2001 From: yengliong Date: Wed, 23 Oct 2024 16:41:18 +0800 Subject: [PATCH] [NEX-14264] Download file to /opt dir in TiberOS There might not be sufficient space in the /var directory to store the new image. It is recommended to download the image to the persistent /opt directory. Signed-off-by: yengliong --- inbm-lib/inbm_lib/path_prefixes.py | 2 + inbm/Changelog.md | 2 + .../dispatcher/sota/constants.py | 4 +- .../dispatcher/sota/os_updater.py | 4 +- inbm/dispatcher-agent/dispatcher/sota/sota.py | 44 +++++++++++++------ .../etc/apparmor.d/usr.bin.inbm-dispatcher | 1 + .../tests/unit/sota/test_sota.py | 3 +- 7 files changed, 43 insertions(+), 17 deletions(-) diff --git a/inbm-lib/inbm_lib/path_prefixes.py b/inbm-lib/inbm_lib/path_prefixes.py index 42e37d24b..f7d63456d 100644 --- a/inbm-lib/inbm_lib/path_prefixes.py +++ b/inbm-lib/inbm_lib/path_prefixes.py @@ -19,6 +19,7 @@ INTEL_MANAGEABILITY_CACHE_PATH_PREFIX = INBM_PATH / 'cache' INTEL_MANAGEABILITY_BINARY_SEARCH_PATHS = [ C_COLON / 'Windows' / 'System32' / 'wbem'] # wmic tool + INTEL_MANAGEABILITY_OPT = INBM_PATH / 'opt' LOG_PATH = INTEL_MANAGEABILITY_VAR_PATH_PREFIX / 'log' else: ROOT = Path('/') @@ -32,4 +33,5 @@ ROOT / 'usr' / 'sbin', ROOT / 'usr' / 'bin', ROOT / 'sbin'] + INTEL_MANAGEABILITY_OPT = ROOT / 'opt' LOG_PATH = ROOT / 'var' / 'log' diff --git a/inbm/Changelog.md b/inbm/Changelog.md index dcc711274..f5727f9a6 100644 --- a/inbm/Changelog.md +++ b/inbm/Changelog.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ## NEXT - YYYY-MM-DD +### Changed + - (NEX-14264) Download file to /opt directory in TiberOS ## 4.2.6.1 - 2024-10-18 ### Added diff --git a/inbm/dispatcher-agent/dispatcher/sota/constants.py b/inbm/dispatcher-agent/dispatcher/sota/constants.py index 43181d863..e752e8726 100644 --- a/inbm/dispatcher-agent/dispatcher/sota/constants.py +++ b/inbm/dispatcher-agent/dispatcher/sota/constants.py @@ -5,7 +5,7 @@ SPDX-License-Identifier: Apache-2.0 """ -from inbm_lib.path_prefixes import INTEL_MANAGEABILITY_CACHE_PATH_PREFIX +from inbm_lib.path_prefixes import INTEL_MANAGEABILITY_CACHE_PATH_PREFIX, INTEL_MANAGEABILITY_OPT from inbm_common_lib.utility import get_canonical_representation_of_path # Mender file path @@ -32,6 +32,8 @@ # Device local cache for SOTA SOTA_CACHE = str(INTEL_MANAGEABILITY_CACHE_PATH_PREFIX / 'repository-tool' / 'sota') +# Download folder for SOTA in TiberOS +SOTA_OPT_PATH = str(INTEL_MANAGEABILITY_OPT/ 'sota') FAILED = "Failed" SUCCESS = "Success" diff --git a/inbm/dispatcher-agent/dispatcher/sota/os_updater.py b/inbm/dispatcher-agent/dispatcher/sota/os_updater.py index 9566c3ffa..686da2bdc 100644 --- a/inbm/dispatcher-agent/dispatcher/sota/os_updater.py +++ b/inbm/dispatcher-agent/dispatcher/sota/os_updater.py @@ -18,7 +18,7 @@ from inbm_lib.constants import DOCKER_CHROOT_PREFIX, CHROOT_PREFIX from .command_list import CommandList -from .constants import MENDER_FILE_PATH, SOTA_CACHE +from .constants import MENDER_FILE_PATH, SOTA_OPT_PATH from .converter import size_to_bytes from .sota_error import SotaError from ..common import uri_utilities @@ -436,7 +436,7 @@ def download_only(self) -> list[str]: parsed_uri = urlparse(self._uri) filename = os.path.basename(parsed_uri.path) if filename: - file_path = os.path.join(SOTA_CACHE, filename) + file_path = os.path.join(SOTA_OPT_PATH, filename) cmds = [update_tool_write_command(self._signature, file_path)] return CommandList(cmds).cmd_list \ No newline at end of file diff --git a/inbm/dispatcher-agent/dispatcher/sota/sota.py b/inbm/dispatcher-agent/dispatcher/sota/sota.py index 63b80e7de..20e497b53 100644 --- a/inbm/dispatcher-agent/dispatcher/sota/sota.py +++ b/inbm/dispatcher-agent/dispatcher/sota/sota.py @@ -23,7 +23,7 @@ from dispatcher.dispatcher_exception import DispatcherException from .command_handler import run_commands, print_execution_summary, get_command_status -from .constants import SUCCESS, SOTA_STATE, SOTA_CACHE, PROCEED_WITHOUT_ROLLBACK_DEFAULT +from .constants import SUCCESS, SOTA_STATE, SOTA_CACHE, PROCEED_WITHOUT_ROLLBACK_DEFAULT, SOTA_OPT_PATH from .downloader import Downloader from .log_helper import get_log_destination from .os_factory import ISotaOs, SotaOsFactory @@ -223,18 +223,36 @@ def execute(self, proceed_without_rollback: bool, skip_sleeps: bool = False) -> if self.sota_cmd is None: raise SotaError('sota_cmd is None') release_date = self._parsed_manifest['release_date'] - if not os.path.exists(SOTA_CACHE): - try: - os.mkdir(SOTA_CACHE) - except OSError as e: - logger.debug(f"SOTA cache directory {SOTA_CACHE} cannot be created: {e}") - raise SotaError("SOTA cache directory cannot be created") from e - elif not os.path.isdir(SOTA_CACHE): - logger.debug( - f"SOTA cache directory {SOTA_CACHE} already exists and is not a directory") - raise SotaError( - "SOTA cache directory already exists and is not a directory") - sota_cache_repo = DirectoryRepo(SOTA_CACHE) + + # In TiberOS, we use different path to download the file + # TODO: Remove Mariner when confirmed that TiberOS is in use + os_type = detect_os() + if os_type == LinuxDistType.tiber.name or os_type == LinuxDistType.Mariner.name: + if not os.path.exists(SOTA_OPT_PATH): + try: + os.mkdir(SOTA_OPT_PATH) + except OSError as e: + logger.debug(f"SOTA cache directory {SOTA_OPT_PATH} cannot be created: {e}") + raise SotaError("SOTA cache directory cannot be created") from e + elif not os.path.isdir(SOTA_OPT_PATH): + logger.debug( + f"SOTA cache directory {SOTA_OPT_PATH} already exists and is not a directory") + raise SotaError( + "SOTA cache directory already exists and is not a directory") + sota_cache_repo = DirectoryRepo(SOTA_OPT_PATH) + else: + if not os.path.exists(SOTA_CACHE): + try: + os.mkdir(SOTA_CACHE) + except OSError as e: + logger.debug(f"SOTA cache directory {SOTA_CACHE} cannot be created: {e}") + raise SotaError("SOTA cache directory cannot be created") from e + elif not os.path.isdir(SOTA_CACHE): + logger.debug( + f"SOTA cache directory {SOTA_CACHE} already exists and is not a directory") + raise SotaError( + "SOTA cache directory already exists and is not a directory") + sota_cache_repo = DirectoryRepo(SOTA_CACHE) time_to_wait_before_reboot = 2 if not skip_sleeps else 0 diff --git a/inbm/dispatcher-agent/fpm-template/etc/apparmor.d/usr.bin.inbm-dispatcher b/inbm/dispatcher-agent/fpm-template/etc/apparmor.d/usr.bin.inbm-dispatcher index 1d4d7db6c..9a1617fb5 100644 --- a/inbm/dispatcher-agent/fpm-template/etc/apparmor.d/usr.bin.inbm-dispatcher +++ b/inbm/dispatcher-agent/fpm-template/etc/apparmor.d/usr.bin.inbm-dispatcher @@ -90,6 +90,7 @@ /etc/task_list.yaml r, /etc/trtl.conf w, /opt/afulnx/afulnx_64 rUx, + /opt/sota rw, /usr/bin/afulnx_64 rUx, /proc/1/comm r, /proc/device-tree/firmware/bios/** r, diff --git a/inbm/dispatcher-agent/tests/unit/sota/test_sota.py b/inbm/dispatcher-agent/tests/unit/sota/test_sota.py index fe7743805..ab84a87e5 100644 --- a/inbm/dispatcher-agent/tests/unit/sota/test_sota.py +++ b/inbm/dispatcher-agent/tests/unit/sota/test_sota.py @@ -145,7 +145,8 @@ def test_run_raises(self, mock_reboot, mock_rollback_and_delete_snap, mock_print @patch("dispatcher.sota.sota.print_execution_summary") @patch("dispatcher.sota.snapshot.DebianBasedSnapshot._rollback_and_delete_snap") @patch('inbm_common_lib.shell_runner.PseudoShellRunner.run', return_value=('200', "", 0)) - def test_run_pass(self, mock_run, mock_rollback_and_delete_snap, mock_print, + @patch("dispatcher.sota.sota.detect_os", return_value='Ubuntu') + def test_run_pass(self, mock_os, mock_run, mock_rollback_and_delete_snap, mock_print, mock_detect_os) -> None: mock_detect_os.return_value = 'Ubuntu' parsed_manifest = {'log_to_file': 'Y', 'sota_cmd': 'update',