Lindley Vieira 6ce41230c5 Remove apt-ostree component
If some Debian package is corrupted inside apt-ostree repo, it can
not be deleted by name.
To fix it this commit adds the option to remove the component with
all packages inside it.

Test-Plan:
PASS: run 'sudo apt-ostree repo remove --feed=<pkgs_feed> \
      --release=bullseye --component=<component>' to remove the
      component
PASS: see in the logs 'Component removed successfully'
PASS: Check in <pkgs_feed>/conf/distributions if the component is
      not there.

Story: 2010676
Task: 51067

Change-Id: I77ae8047a757ead1f96f72b0935dea878a9bc434
Signed-off-by: Lindley Vieira <lindley.vieira@windriver.com>
2024-09-24 09:22:29 -03:00

139 lines
3.5 KiB
Python

"""
Copyright (c) 2023-2024 Wind River Systems, Inc.
SPDX-License-Identifier: Apache-2.0
"""
import logging
import os
import subprocess
from apt_ostree import exceptions
LOG = logging.getLogger(__name__)
def run_command(cmd,
debug=False,
stdin=None,
stdout=None,
stderr=None,
check=True,
env=None,
cwd=None):
"""Run a command in a shell."""
_env = os.environ.copy()
if env:
_env.update(env)
try:
return subprocess.run(
cmd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
env=_env,
cwd=cwd,
check=check,
)
except FileNotFoundError:
msg = "%s is not found in $PATH" % cmd[0]
LOG.error(msg)
raise exceptions.CommandError(msg)
except subprocess.CalledProcessError as e:
msg = "Shell execution error: %s, Output: %s" \
% (e.returncode, e.stderr.decode("utf-8"))
LOG.error(msg)
raise exceptions.CommandError(msg)
def run_sandbox_command(
args,
rootfs,
stdin=None,
stdout=None,
stderr=None,
check=True,
env=None
):
"""Run a shell wrapped with bwrap."""
cmd = [
"bwrap",
"--proc", "/proc",
"--dev", "/dev",
"--dir", "/run",
"--bind", "/tmp", "/tmp",
"--bind", f"{rootfs}/boot", "/boot",
"--bind", f"{rootfs}/usr", "/usr",
"--bind", f"{rootfs}/etc", "/etc",
"--bind", f"{rootfs}/var", "/var",
"--symlink", "/usr/lib", "/lib",
"--symlink", "/usr/lib64", "/lib64",
"--symlink", "/usr/bin", "/bin",
"--symlink", "/usr/sbin", "/sbin",
"--share-net",
"--die-with-parent",
"--chdir", "/",
]
cmd += args
return run_command(
cmd,
stdin=stdin,
stdout=stdout,
stderr=stderr,
check=check,
env=env,
)
def check_and_append_component(config_path, component):
with open(config_path, 'r') as file:
lines = file.readlines()
for i, line in enumerate(lines):
if line.startswith("Components:"):
components = line.split()[1:]
if component not in components:
lines[i] = line.strip() + f" {component}\n"
break
with open(config_path, 'w') as file:
file.writelines(lines)
def remove_component_from_config(config_path, component):
try:
with open(config_path, 'r') as file:
lines = file.readlines()
except FileNotFoundError:
msg = "The file %s does not exist." % config_path
LOG.error(msg)
return False
updated_lines = []
component_found = False
for line in lines:
if line.startswith("Components:"):
components = line.split()[1:]
if component in components:
components.remove(component)
component_found = True
updated_line = "Components: " + " ".join(components) + "\n"
updated_lines.append(updated_line)
else:
updated_lines.append(line)
if not component_found:
msg = "Component %s not found in the configuration." % component
LOG.error(msg)
return False
with open(config_path, 'w') as file:
file.writelines(updated_lines)
LOG.info("Component %s removed from %s successfully"
% (component, config_path))
return True