Invert order of undeploying inactive deployments

There is an issue on the logic of the undeploy_inactive_deployments
function when there are more than 2 deployments in the sysroot ostree.
For example, when there are 3 deployments:

0 -> ostree-commit-a
1 -> ostree-commit-b
2 -> ostree-commit-c

When deployment 1 is deleted, deployment 2 becomes the 1, and when
the function tries to remove 2 it fails, since there is no such
deployment, similar to an index out-of-bounds failure.

This commit fixes the issue by changing the logic to undeploy
in the inverse order, i.e. from the oldest to the newest. In the
example above it will undeploy in the order 2 -> 1. This commit
also fixes an issues with wrong usage of lstrip(), that would
remove characters incorrectly from the commit-id if it started
with characters of the string 'debian'.

Test Plan
PASS: stx-8 -> stx-10 major release deploy delete successfully
      and verify the inactive deployments were removed
PASS: force a scenario where more than 2 deployments exists and
      validate the function behaves as expected without errors

Story: 2010676
Task: 51321

Change-Id: I6e78ce199e6904096cec4c1f9d212d1448a9852b
Signed-off-by: Heitor Matsui <heitorvieira.matsui@windriver.com>
This commit is contained in:
Heitor Matsui
2024-11-11 15:46:01 -03:00
parent 6cb3454dda
commit 7f900f5da1

View File

@@ -503,7 +503,8 @@ def delete_older_deployments():
def undeploy_inactive_deployments():
"""
Remove deployments other than the current deployment,
i.e. deployments from index 1 to len(deployments) - 1
i.e. deployments from index 1 to len(deployments) - 1,
in the reverse order, from the oldest to the newest
"""
cmd = ["ostree", "admin", "status"]
try:
@@ -516,8 +517,8 @@ def undeploy_inactive_deployments():
pattern = r"debian [a-z0-9]+.[0-9]+"
deployments = re.findall(pattern, output.stdout)
# skip the first (active) deployment
for index, deployment in enumerate(deployments[1:], 1):
commit_id = deployment.lstrip("debian ").split(".")[0]
for index, deployment in reversed(list(enumerate(deployments[1:], 1))):
commit_id = deployment.replace("debian ", "").split(".")[0]
cmd = ["ostree", "admin", "undeploy", str(index)]
try:
subprocess.run(cmd, check=True)