From 784e1628742764e2417ab42f9d1802ce6fd1049d Mon Sep 17 00:00:00 2001 From: Dostoievski Batista Date: Thu, 25 Apr 2024 19:10:52 -0300 Subject: [PATCH] Automatically add deploy-precheck script to patch When building a patch that includes the software package, the deploy-precheck script will be automatically fetch from .deb file and included inside the patch. With this there is no need to have element 'deploy_precheck' in XML recipe schema. Test Plan: PASS: Create patch successfully with software package included and install it in a AIO-SX system. PASS: Create patch without software package included and install it in a AIO-SX system. Story: 2010676 Task: 49978 Change-Id: I9d470ed049d42880bb515320324de1e3a0e05680 Signed-off-by: Dostoievski Batista --- .../EXAMPLES/patch-recipe-sample-binary.xml | 1 - .../EXAMPLES/patch-recipe-sample-insvc.xml | 1 - .../EXAMPLES/patch-recipe-sample-large.xml | 1 - .../patch/EXAMPLES/patch-recipe-sample.xml | 1 - .../stx/patch/config/patch-recipe-schema.xsd | 1 - build-tools/stx/patch/metadata.py | 4 -- build-tools/stx/patch/patch_builder.py | 62 +++++++++++++++++-- .../stx/patch/scripts/deploy-precheck.sh | 24 ------- 8 files changed, 57 insertions(+), 38 deletions(-) delete mode 100644 build-tools/stx/patch/scripts/deploy-precheck.sh diff --git a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-binary.xml b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-binary.xml index cca63a11..3c5ce4e2 100644 --- a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-binary.xml +++ b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-binary.xml @@ -20,7 +20,6 @@ scripts/pre-install.sh scripts/post-install.sh - diff --git a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-insvc.xml b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-insvc.xml index cbb8e52c..334dc372 100644 --- a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-insvc.xml +++ b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-insvc.xml @@ -15,7 +15,6 @@ - diff --git a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-large.xml b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-large.xml index 949742f0..bf6db8e4 100644 --- a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-large.xml +++ b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample-large.xml @@ -20,7 +20,6 @@ scripts/pre-install.sh scripts/post-install.sh - scripts/deploy-precheck.sh diff --git a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample.xml b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample.xml index dac6c229..d3461d6c 100644 --- a/build-tools/stx/patch/EXAMPLES/patch-recipe-sample.xml +++ b/build-tools/stx/patch/EXAMPLES/patch-recipe-sample.xml @@ -20,7 +20,6 @@ scripts/pre-install.sh scripts/post-install.sh - scripts/deploy-precheck.sh diff --git a/build-tools/stx/patch/config/patch-recipe-schema.xsd b/build-tools/stx/patch/config/patch-recipe-schema.xsd index 2f130749..753942e4 100644 --- a/build-tools/stx/patch/config/patch-recipe-schema.xsd +++ b/build-tools/stx/patch/config/patch-recipe-schema.xsd @@ -22,7 +22,6 @@ - diff --git a/build-tools/stx/patch/metadata.py b/build-tools/stx/patch/metadata.py index 082cd432..54b335d3 100644 --- a/build-tools/stx/patch/metadata.py +++ b/build-tools/stx/patch/metadata.py @@ -35,7 +35,6 @@ WARNINGS = 'warnings' REBOOT_REQUIRED = 'reboot_required' PRE_INSTALL = 'pre_install' POST_INSTALL = 'post_install' -DEPLOY_PRECHECK = 'deploy_precheck' UNREMOVABLE = 'unremovable' REQUIRES = 'requires' REQUIRES_PATCH_ID = 'req_patch_id' @@ -97,7 +96,6 @@ class PatchMetadata(object): # strip path from pre_install and post_install scripts self.pre_install = self.pre_install.split('/')[-1] self.post_install = self.post_install.split('/')[-1] - self.deploy_precheck = self.deploy_precheck.split('/')[-1] top_tag = ET.Element(PATCH_ROOT_TAG) self.__add_text_tag_to_xml(top_tag, PATCH_ID, self.patch_id) @@ -127,7 +125,6 @@ class PatchMetadata(object): self.__add_text_tag_to_xml(top_tag, PRE_INSTALL, self.pre_install) self.__add_text_tag_to_xml(top_tag, POST_INSTALL, self.post_install) - self.__add_text_tag_to_xml(top_tag, DEPLOY_PRECHECK, self.deploy_precheck) packages_tag = ET.SubElement(top_tag, PACKAGES) for package in sorted(self.debs): @@ -158,7 +155,6 @@ class PatchMetadata(object): self.reboot_required = patch_recipe[REBOOT_REQUIRED] self.pre_install = self.check_script_path(patch_recipe[PRE_INSTALL]) self.post_install = self.check_script_path(patch_recipe[POST_INSTALL]) - self.deploy_precheck = self.check_script_path(patch_recipe[DEPLOY_PRECHECK]) self.unremovable = patch_recipe[UNREMOVABLE] self.status = patch_recipe[STATUS] if 'id' in patch_recipe[REQUIRES]: diff --git a/build-tools/stx/patch/patch_builder.py b/build-tools/stx/patch/patch_builder.py index 203023f8..82a03200 100755 --- a/build-tools/stx/patch/patch_builder.py +++ b/build-tools/stx/patch/patch_builder.py @@ -40,7 +40,7 @@ PATCH_OUTPUT = os.path.join(BUILD_ROOT, "patch_output") PATCH_SCRIPTS = { "PRE_INSTALL": "pre-install.sh", "POST_INSTALL": "post-install.sh", - "DEPLOY_PRECHECK": "deploy-precheck.sh" + "DEPLOY_PRECHECK": "deploy-precheck" } class PatchBuilder(object): @@ -92,7 +92,7 @@ class PatchBuilder(object): pre_install = self.metadata.pre_install post_install = self.metadata.post_install - deploy_precheck = self.metadata.deploy_precheck + # pre/post install scripts if pre_install: logger.debug(f"Copying pre-install script: {pre_install}") @@ -102,9 +102,13 @@ class PatchBuilder(object): logger.debug(f"Copying post-install script: {post_install}") self.copy_script("POST_INSTALL", post_install) - if deploy_precheck: - logger.debug(f"Copying deploy pre-check script: {deploy_precheck}") - self.copy_script("DEPLOY_PRECHECK", deploy_precheck) + # if the patch includes the 'software' package we need to make deploy-precheck + # script from .deb file accessible directly from patch file + if 'software' in self.metadata.stx_packages: + logger.info(f"Patch includes software package, getting script from deb file...") + path_deployprecheck = self.get_file_from_deb(dl_dir, 'software', 'deploy-precheck') + logger.debug(f"Copying deploy pre-check script: {path_deployprecheck}") + self.copy_script("DEPLOY_PRECHECK", path_deployprecheck) if not pre_install and not post_install and self.metadata.reboot_required == 'N': logger.warn("In service patch without restart scripts provided") @@ -135,6 +139,54 @@ class PatchBuilder(object): else: raise ValueError(f"Script type provided is not valid one: {script_type}") + def get_file_from_deb(self, download_dir, package_name ,script_name): + '''Get specific file from .deb + + :param download_dir: Full path of directory where the deb is downloaded + :param package_name: Name of the package + :param script_name: Name of file that should be searched + + :returns string: full path for the script file + ''' + # from download dir, search for {package_name}_*.deb package + pkg_name = None + for file in os.listdir(download_dir): + if file.startswith(f'{package_name}_') and file.endswith('.deb'): + pkg_name = file + + if not pkg_name: + erro_msg = f'Unable to find {package_name} package inside download folder' + logger.error(erro_msg) + raise FileNotFoundError(erro_msg) + + deb_path = os.path.join(download_dir, pkg_name) + + # we create a temp folder and copy deb there + tmpdir_deb = tempfile.mkdtemp(prefix='deb_') + shutil.copy(deb_path, tmpdir_deb) + + # We first unpack deb file and get data.tar.xz from there + cmd = ['ar', '-x', os.path.join(tmpdir_deb, pkg_name)] + subprocess.check_call(cmd, cwd=tmpdir_deb) + + # With data.tar.xz, we try to find script file + data_tar = tarfile.open(os.path.join(tmpdir_deb, 'data.tar.xz')) + script_tarpath = None + for member in data_tar.getnames(): + if member.endswith(script_name): + script_tarpath = member + + if not script_tarpath: + erro_msg = f"Unable to find {script_name} inside data tar." + logger.error(erro_msg) + raise FileNotFoundError(erro_msg) + + # We extract said file to the temporary folder + data_tar.extract(script_tarpath, path=tmpdir_deb) + data_tar.close() + + return os.path.join(tmpdir_deb, script_tarpath) + def __sign_and_pack(self, patch_file): """ Generates the patch signatures and pack the .patch file diff --git a/build-tools/stx/patch/scripts/deploy-precheck.sh b/build-tools/stx/patch/scripts/deploy-precheck.sh deleted file mode 100644 index 21eef534..00000000 --- a/build-tools/stx/patch/scripts/deploy-precheck.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -# -# -# Copyright (c) 2023 Wind River Systems, Inc. -# -# SPDX-License-Identifier: Apache-2.0 -# -# -# The patching subsystem provides a patch-functions bash source file -# with useful function and variable definitions. -# -. /etc/patching/patch-functions - -# -# Declare an overall script return code -# -declare -i GLOBAL_RC=$PATCH_STATUS_OK - -echo "Pre deploy check script" - -# -# Exit the script with the overall return code -# -exit $GLOBAL_RC