From 7e7654ef5448d1c24ddf33adee4d4f9d66ca481e Mon Sep 17 00:00:00 2001 From: Jessica Castelino Date: Mon, 28 Aug 2023 19:52:49 +0000 Subject: [PATCH] software commit-patch implementation This commit enables the command "software commit-patch " in the Debian Env. Test Plan: [PASS] software commit-patch Story: 2010676 Task: 48789 Change-Id: I5f8392ac71dc964f54fa02ac8f4086feab4858ee Signed-off-by: Jessica Castelino --- software/software/api/controllers/root.py | 20 ++++++++ software/software/constants.py | 5 -- software/software/software_client.py | 58 ++++++++++++++--------- software/software/software_controller.py | 3 +- 4 files changed, 58 insertions(+), 28 deletions(-) diff --git a/software/software/api/controllers/root.py b/software/software/api/controllers/root.py index 1fde635c..13473e15 100644 --- a/software/software/api/controllers/root.py +++ b/software/software/api/controllers/root.py @@ -24,6 +24,26 @@ LOG = log.getLogger(__name__) class SoftwareAPIController(object): + @expose('json') + def commit_patch(self, *args): + try: + result = sc.patch_commit(list(args)) + except SoftwareError as e: + return dict(error=str(e)) + + sc.software_sync() + + return result + + @expose('json') + def commit_dry_run(self, *args): + try: + result = sc.patch_commit(list(args), dry_run=True) + except SoftwareError as e: + return dict(error=str(e)) + + return result + @expose('json') @expose('query.xml', content_type='application/xml') def delete(self, *args): diff --git a/software/software/constants.py b/software/software/constants.py index c3ae2cf1..8c8e03d2 100644 --- a/software/software/constants.py +++ b/software/software/constants.py @@ -15,11 +15,6 @@ try: except Exception: pass -CLI_OPT_ALL = '--all' -CLI_OPT_DRY_RUN = '--dry-run' -CLI_OPT_RECURSIVE = '--recursive' -CLI_OPT_RELEASE = '--release' - ADDRESS_VERSION_IPV4 = 4 ADDRESS_VERSION_IPV6 = 6 CONTROLLER_FLOATING_HOSTNAME = "controller" diff --git a/software/software/software_client.py b/software/software/software_client.py index 0adb9636..684d0a26 100644 --- a/software/software/software_client.py +++ b/software/software/software_client.py @@ -349,37 +349,25 @@ def release_delete_req(args): return check_rc(req) -def patch_commit_req(args): - print("patch_commit_req UNDER CONSTRUCTION") +def commit_patch_req(args): # Ignore interrupts during this function signal.signal(signal.SIGINT, signal.SIG_IGN) - dry_run = False - if constants.CLI_OPT_DRY_RUN in args: - dry_run = True - args.remove(constants.CLI_OPT_DRY_RUN) - - all_patches = False - if constants.CLI_OPT_ALL in args: - all_patches = True - args.remove(constants.CLI_OPT_ALL) - # Default to running release # this all needs to be changed relopt = RUNNING_SW_VERSION - release = args.release headers = {} append_auth_token_if_required(headers) - if release and not all_patches: + if args.sw_version and not args.all: # Disallow - print("Use of --release option requires --all") + print("Use of --sw-version option requires --all") return 1 - elif all_patches: + elif args.all: # Get a list of all patches extra_opts = "&release=%s" % relopt - url = "http://%s/software/query?show=all%s" % (api_addr, extra_opts) + url = "http://%s/software/query?show=patch%s" % (api_addr, extra_opts) req = requests.get(url, headers=headers) @@ -398,13 +386,14 @@ def patch_commit_req(args): return 0 print("The following patches will be committed:") - for release_id in patch_list: - print(" %s" % release_id) + for patch_id in patch_list: + print(" %s" % patch_id) print() patches = "/".join(patch_list) else: - patches = "/".join(args) + # args.patch is a list + patches = "/".join(args.patch) # First, get a list of dependencies and ask for confirmation url = "http://%s/software/query_dependencies/%s?recursive=yes" % (api_addr, patches) @@ -437,7 +426,7 @@ def patch_commit_req(args): print("Aborting...") return 1 - if dry_run: + if args.dry_run: return 0 print() @@ -451,7 +440,7 @@ def patch_commit_req(args): print("Aborting...") return 1 - url = "http://%s/software/commit/%s" % (api_addr, patches) + url = "http://%s/software/commit_patch/%s" % (api_addr, patches) req = requests.post(url, headers=headers) if args.debug: @@ -1254,6 +1243,31 @@ def setup_argparse(): commands = parser.add_subparsers(title='Commands', metavar='') commands.required = True + # -- software commit-patch --------------- + cmd = commands.add_parser( + 'commit-patch', + help='Commit patches to free disk space. WARNING: This action is irreversible!' + ) + cmd.set_defaults(cmd='commit-patch') + cmd.set_defaults(func=commit_patch_req) + cmd.add_argument('patch', + nargs="+", # accepts a list + help='Patch ID/s to commit') + # --dry-run is an optional argument + cmd.add_argument('--dry-run', + action='store_true', + required=False, + help='Check the space savings without committing the patch') + # --all is an optional argument + cmd.add_argument('--all', + action='store_true', + required=False, + help='Commit all the applied patches') + # --sw-version is an optional argument + cmd.add_argument('--sw-version', + required=False, + help='Software release version') + # -- software delete --------------- cmd = commands.add_parser( 'delete', diff --git a/software/software/software_controller.py b/software/software/software_controller.py index 5bfed438..7991f32d 100644 --- a/software/software/software_controller.py +++ b/software/software/software_controller.py @@ -1595,7 +1595,8 @@ class PatchController(PatchService): self.release_data.metadata[patch_id]["restart_script"]) if os.path.exists(restart_script_path): cleanup_files.add(restart_script_path) - patch_sw_version = self.release_data.metadata[patch_id]["sw_version"] + patch_sw_version = utils.get_major_release_version( + self.release_data.metadata[patch_id]["sw_version"]) abs_ostree_tar_dir = package_dir[patch_sw_version] software_tar_path = "%s/%s-software.tar" % (abs_ostree_tar_dir, patch_id) if os.path.exists(software_tar_path):