Patch Remove

This commit enables patch remove in a Debian env. Patch remove
command operates on the feed directory. It resets the feed ostree
HEAD to the base commit of the patch being removed and then deletes
all commits that belong to the patch.

Test:
sw-patch remove works as expected in Debian

Story: 2009969
Task: 45327
Signed-off-by: Jessica Castelino <jessica.castelino@windriver.com>
Change-Id: If01ab1a1ab4e4e58ee1713cc622feb57481219b9
This commit is contained in:
Jessica Castelino 2022-05-10 15:21:04 +00:00
parent fe74de5def
commit 54fe1e2901
2 changed files with 37 additions and 19 deletions

View File

@ -32,6 +32,11 @@ class OSTreeTarFail(PatchError):
pass
class OSTreeCommandFail(PatchError):
"""OSTree Commands error."""
pass
class SemanticFail(PatchError):
"""Semantic check error."""
pass

View File

@ -35,7 +35,7 @@ from cgcs_patch.patch_functions import semantics_dir
from cgcs_patch.patch_functions import SW_VERSION
from cgcs_patch.patch_functions import root_package_dir
from cgcs_patch.exceptions import MetadataFail
from cgcs_patch.exceptions import ContentFail
from cgcs_patch.exceptions import OSTreeCommandFail
from cgcs_patch.exceptions import OSTreeTarFail
from cgcs_patch.exceptions import PatchError
from cgcs_patch.exceptions import PatchFail
@ -1152,7 +1152,14 @@ class PatchController(PatchService):
feed_ostree = "%s/rel-%s/ostree_repo" % (constants.FEED_OSTREE_BASE_DIR, patch_sw_version)
cmd = "ostree log %s --repo=%s" % (constants.OSTREE_REF, feed_ostree)
output = subprocess.run(cmd, shell=True, check=True, capture_output=True)
try:
output = subprocess.run(cmd, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
msg = "Failed to fetch ostree log of the feed ostree repo for %s." % patch_id
info_msg = "OSTree log Error: return code: %s , Output: %s" \
% (e.returncode, e.stderr.decode("utf-8"))
LOG.info(info_msg)
raise OSTreeCommandFail(msg)
# Store the output of the above command in a string
output_string = output.stdout.decode('utf-8')
@ -1340,25 +1347,31 @@ class PatchController(PatchService):
continue
patch_sw_version = self.patch_data.metadata[patch_id]["sw_version"]
for contentname in self.patch_data.contents[patch_id]:
contentfile = self.get_store_filename(patch_sw_version, contentname)
if not os.path.isfile(contentfile):
msg = "Could not find content: %s" % contentfile
LOG.error(msg)
raise ContentFail(msg)
repo_filename = self.get_repo_filename(patch_sw_version, contentname)
if repo_filename is None:
msg = "Failed to determine repo path for %s" % contentfile
LOG.exception(msg)
raise ContentFail(msg)
# Reset ostree HEAD to base commit of this patch
# Base commit is fetched from the patch metadata
base_commit = self.patch_data.contents[patch_id]["base"]["commit"]
feed_ostree = "%s/rel-%s/ostree_repo" % (constants.FEED_OSTREE_BASE_DIR, patch_sw_version)
cmd = "ostree reset %s %s --repo=%s" % (constants.OSTREE_REF, base_commit, feed_ostree)
try:
subprocess.run(cmd, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
msg = "Failed to reset the feed ostree repo head for %s." % patch_id
info_msg = "OSTree Reset Error: return code: %s , Output: %s" \
% (e.returncode, e.stderr.decode("utf-8"))
LOG.info(info_msg)
raise OSTreeCommandFail(msg)
for i in range(int(self.patch_data.contents[patch_id]["number_of_commits"])):
commit_to_delete = self.patch_data.contents[patch_id]["commit%s" % (i + 1)]["commit"]
cmd = "ostree prune --delete-commit %s --repo=%s" % (commit_to_delete, feed_ostree)
try:
os.remove(repo_filename)
except OSError:
msg = "Failed to remove content %s" % repo_filename
LOG.exception(msg)
raise ContentFail(msg)
subprocess.run(cmd, shell=True, check=True, capture_output=True)
except subprocess.CalledProcessError as e:
msg = "Failed to delete commit from feed ostree repo for %s." % patch_id
info_msg = "OSTree Prune Error: return code: %s , Output: %s" \
% (e.returncode, e.stderr.decode("utf-8"))
LOG.info(info_msg)
raise OSTreeCommandFail(msg)
try:
# Move the metadata to the available dir