Skip to content

Commit

Permalink
WIP: remove target
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Nov 15, 2023
1 parent 41235cd commit 95cb638
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 106 deletions.
69 changes: 29 additions & 40 deletions cloudinit/config/cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@
PORTS_ARCHES = ["s390x", "arm64", "armhf", "powerpc", "ppc64el", "riscv64"]


def get_default_mirrors(arch=None, target=None):
def get_default_mirrors(arch=None, ):
"""returns the default mirrors for the target. These depend on the
architecture, for more see:
https://wiki.ubuntu.com/UbuntuDevelopment/PackageArchive#Ports"""
if arch is None:
arch = util.get_dpkg_architecture(target)
arch = util.get_dpkg_architecture()
if arch in PRIMARY_ARCHES:
return PRIMARY_ARCH_MIRRORS.copy()
if arch in PORTS_ARCHES:
Expand All @@ -202,8 +202,6 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
"""process the config for apt_config. This can be called from
curthooks if a global apt config was provided or via the "apt"
standalone command."""
# keeping code close to curtin codebase via entry handler
target = None
# feed back converted config, but only work on the subset under 'apt'
cfg = convert_to_v3_apt_format(cfg)
apt_cfg = cfg.get("apt", {})
Expand All @@ -215,8 +213,8 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
)
)

apply_debconf_selections(apt_cfg, target)
apply_apt(apt_cfg, cloud, target)
apply_debconf_selections(apt_cfg)
apply_apt(apt_cfg, cloud)


def _should_configure_on_empty_apt():
Expand All @@ -228,7 +226,7 @@ def _should_configure_on_empty_apt():
return True, "Apt is available."


def apply_apt(cfg, cloud, target):
def apply_apt(cfg, cloud):
# cfg is the 'apt' top level dictionary already in 'v3' format.
if not cfg:
should_config, msg = _should_configure_on_empty_apt()
Expand All @@ -238,8 +236,8 @@ def apply_apt(cfg, cloud, target):

LOG.debug("handling apt config: %s", cfg)

release = util.lsb_release(target=target)["codename"]
arch = util.get_dpkg_architecture(target)
release = util.lsb_release()["codename"]
arch = util.get_dpkg_architecture()
mirrors = find_apt_mirror_info(cfg, cloud, arch=arch)
LOG.debug("Apt Mirror info: %s", mirrors)

Expand All @@ -250,9 +248,9 @@ def apply_apt(cfg, cloud, target):
_ensure_dependencies(cfg, matcher, cloud)

if util.is_false(cfg.get("preserve_sources_list", False)):
add_mirror_keys(cfg, cloud, target)
add_mirror_keys(cfg, cloud)
generate_sources_list(cfg, release, mirrors, cloud)
rename_apt_lists(mirrors, target, arch)
rename_apt_lists(mirrors, arch)

try:
apply_apt_config(cfg, APT_PROXY_FN, APT_CONFIG_FN)
Expand All @@ -268,15 +266,13 @@ def apply_apt(cfg, cloud, target):
add_apt_sources(
cfg["sources"],
cloud,
target=target,
template_params=params,
aa_repo_match=matcher,
)
# GH: 4344 - stop gpg-agent/dirmgr daemons spawned by gpg key imports.
# Daemons spawned by cloud-config.service on systemd v253 report (running)
gpg_process_out, _err = subp.subp(
["ps", "-o", "ppid,pid", "-C", "dirmngr", "-C", "gpg-agent"],
target=target,
capture=True,
rcs=[0, 1],
)
Expand All @@ -288,18 +284,17 @@ def apply_apt(cfg, cloud, target):
os.kill(gpg_pid, signal.SIGKILL)


def debconf_set_selections(selections, target=None):
def debconf_set_selections(selections):
if not selections.endswith(b"\n"):
selections += b"\n"
subp.subp(
["debconf-set-selections"],
data=selections,
target=target,
capture=True,
)


def dpkg_reconfigure(packages, target=None):
def dpkg_reconfigure(packages):
# For any packages that are already installed, but have preseed data
# we populate the debconf database, but the filesystem configuration
# would be preferred on a subsequent dpkg-reconfigure.
Expand All @@ -310,7 +305,7 @@ def dpkg_reconfigure(packages, target=None):
for pkg in packages:
if pkg in CONFIG_CLEANERS:
LOG.debug("unconfiguring %s", pkg)
CONFIG_CLEANERS[pkg](target)
CONFIG_CLEANERS[pkg]()
to_config.append(pkg)
else:
unhandled.append(pkg)
Expand All @@ -327,12 +322,11 @@ def dpkg_reconfigure(packages, target=None):
["dpkg-reconfigure", "--frontend=noninteractive"]
+ list(to_config),
data=None,
target=target,
capture=True,
)


def apply_debconf_selections(cfg, target=None):
def apply_debconf_selections(cfg):
"""apply_debconf_selections - push content to debconf"""
# debconf_selections:
# set1: |
Expand All @@ -344,7 +338,7 @@ def apply_debconf_selections(cfg, target=None):
return

selections = "\n".join([selsets[key] for key in sorted(selsets.keys())])
debconf_set_selections(selections.encode(), target=target)
debconf_set_selections(selections.encode())

# get a complete list of packages listed in input
pkgs_cfgd = set()
Expand All @@ -355,7 +349,7 @@ def apply_debconf_selections(cfg, target=None):
pkg = re.sub(r"[:\s].*", "", line)
pkgs_cfgd.add(pkg)

pkgs_installed = util.get_installed_packages(target)
pkgs_installed = util.get_installed_packages()

LOG.debug("pkgs_cfgd: %s", pkgs_cfgd)
need_reconfig = pkgs_cfgd.intersection(pkgs_installed)
Expand All @@ -364,13 +358,13 @@ def apply_debconf_selections(cfg, target=None):
LOG.debug("no need for reconfig")
return

dpkg_reconfigure(need_reconfig, target=target)
dpkg_reconfigure(need_reconfig)


def clean_cloud_init(target):
def clean_cloud_init():
"""clean out any local cloud-init config"""
flist = glob.glob(
subp.target_path(target, "/etc/cloud/cloud.cfg.d/*dpkg*")
subp.target_path(path="/etc/cloud/cloud.cfg.d/*dpkg*")
)

LOG.debug("cleaning cloud-init config from: %s", flist)
Expand All @@ -396,11 +390,11 @@ def mirrorurl_to_apt_fileprefix(mirror):
return string


def rename_apt_lists(new_mirrors, target, arch):
def rename_apt_lists(new_mirrors, arch):
"""rename_apt_lists - rename apt lists to preserve old cache data"""
default_mirrors = get_default_mirrors(arch)

pre = subp.target_path(target, APT_LISTS)
pre = subp.target_path(APT_LISTS)
for name, omirror in default_mirrors.items():
nmirror = new_mirrors.get(name)
if not nmirror:
Expand Down Expand Up @@ -542,11 +536,11 @@ def disable_suites(disabled, src, release) -> str:
return retsrc


def add_mirror_keys(cfg, cloud, target):
def add_mirror_keys(cfg, cloud):
"""Adds any keys included in the primary/security mirror clauses"""
for key in ("primary", "security"):
for mirror in cfg.get(key, []):
add_apt_key(mirror, cloud, target, file_name=key)
add_apt_key(mirror, cloud, file_name=key)


def is_deb822_sources_format(apt_src_content: str) -> bool:
Expand Down Expand Up @@ -689,7 +683,7 @@ def generate_sources_list(cfg, release, mirrors, cloud):
util.write_file(aptsrc_file, disabled, mode=0o644)


def add_apt_key_raw(key, file_name, hardened=False, target=None):
def add_apt_key_raw(key, file_name, hardened=False):
"""
actual adding of a key as defined in key argument
to the system
Expand Down Expand Up @@ -737,7 +731,7 @@ def _ensure_dependencies(cfg, aa_repo_match, cloud):
cloud.distro.install_packages(sorted(missing_packages))


def add_apt_key(ent, cloud, target=None, hardened=False, file_name=None):
def add_apt_key(ent, cloud, hardened=False, file_name=None):
"""
Add key to the system as defined in ent (if any).
Supports raw keys or keyid's
Expand All @@ -761,7 +755,7 @@ def update_packages(cloud):


def add_apt_sources(
srcdict, cloud, target=None, template_params=None, aa_repo_match=None
srcdict, cloud, template_params=None, aa_repo_match=None
):
"""
install keys and repo source .list files defined in 'sources'
Expand Down Expand Up @@ -800,17 +794,13 @@ def add_apt_sources(
ent = srcdict[filename]
LOG.debug("adding source/key '%s'", ent)
if "filename" not in ent:
if target and filename.startswith(target):
# Strip target path prefix from filename
ent["filename"] = filename[len(target) :]
else:
ent["filename"] = filename
ent["filename"] = filename

if "source" in ent and "$KEY_FILE" in ent["source"]:
key_file = add_apt_key(ent, cloud, target, hardened=True)
key_file = add_apt_key(ent, cloud, hardened=True)
template_params["KEY_FILE"] = key_file
else:
add_apt_key(ent, cloud, target)
add_apt_key(ent, cloud)

if "source" not in ent:
continue
Expand All @@ -828,14 +818,13 @@ def add_apt_sources(
try:
subp.subp(
["add-apt-repository", "--no-update", source],
target=target,
)
except subp.ProcessExecutionError:
LOG.exception("add-apt-repository failed.")
raise
continue

sourcefn = subp.target_path(target, ent["filename"])
sourcefn = subp.target_path(path=ent["filename"])
try:
contents = "%s\n" % (source)
omode = "a"
Expand Down
2 changes: 1 addition & 1 deletion cloudinit/subp.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def ldecode(data, m="utf-8"):
return SubpResult(out, err)


def target_path(target, path=None):
def target_path(target=None, path=None):
# return 'path' inside target, accepting target as None
if target in (None, ""):
target = "/"
Expand Down
14 changes: 6 additions & 8 deletions cloudinit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,18 @@ def kernel_version():


@lru_cache()
def get_dpkg_architecture(target=None):
def get_dpkg_architecture():
"""Return the sanitized string output by `dpkg --print-architecture`.
N.B. This function is wrapped in functools.lru_cache, so repeated calls
won't shell out every time.
"""
out = subp.subp(
["dpkg", "--print-architecture"], capture=True, target=target
)
out = subp.subp(["dpkg", "--print-architecture"], capture=True)
return out.stdout.strip()


@lru_cache()
def lsb_release(target=None):
def lsb_release():
fmap = {
"Codename": "codename",
"Description": "description",
Expand All @@ -107,7 +105,7 @@ def lsb_release(target=None):

data = {}
try:
out = subp.subp(["lsb_release", "--all"], capture=True, target=target)
out = subp.subp(["lsb_release", "--all"], capture=True)
for line in out.stdout.splitlines():
fname, _, val = line.partition(":")
if fname in fmap:
Expand Down Expand Up @@ -2972,8 +2970,8 @@ def message_from_string(string):
return email.message_from_string(string)


def get_installed_packages(target=None):
out = subp.subp(["dpkg-query", "--list"], target=target, capture=True)
def get_installed_packages():
out = subp.subp(["dpkg-query", "--list"], capture=True)

pkgs_inst = set()
for line in out.stdout.splitlines():
Expand Down
3 changes: 0 additions & 3 deletions tests/unittests/config/test_apt_configure_sources_list_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def test_apt_v1_source_list_by_distro(self, distro, mirror, tmpdir):
self.subp.assert_called_once_with(
["ps", "-o", "ppid,pid", "-C", "dirmngr", "-C", "gpg-agent"],
capture=True,
target=None,
rcs=[0, 1],
)

Expand Down Expand Up @@ -223,7 +222,6 @@ def test_apt_v1_srcl_distro_mirrorfail(
self.subp.assert_called_once_with(
["ps", "-o", "ppid,pid", "-C", "dirmngr", "-C", "gpg-agent"],
capture=True,
target=None,
rcs=[0, 1],
)

Expand Down Expand Up @@ -286,6 +284,5 @@ def test_apt_v1_srcl_custom(
self.subp.assert_called_once_with(
["ps", "-o", "ppid,pid", "-C", "dirmngr", "-C", "gpg-agent"],
capture=True,
target=None,
rcs=[0, 1],
)
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,5 @@ def test_apt_v3_srcl_custom_deb822_feature_aware(
self.subp.assert_called_once_with(
["ps", "-o", "ppid,pid", "-C", "dirmngr", "-C", "gpg-agent"],
capture=True,
target=None,
rcs=[0, 1],
)
9 changes: 2 additions & 7 deletions tests/unittests/config/test_apt_source_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def apt_src_keyid(self, filename, cfg, keynum):
sources = cfg["apt"]["sources"]
for src in sources:
print(sources[src])
calls.append(call(sources[src], cloud, None))
calls.append(call(sources[src], cloud))

mockobj.assert_has_calls(calls, any_order=True)

Expand Down Expand Up @@ -485,7 +485,7 @@ def apt_src_key(self, filename, cfg):
calls = []
for src in sources:
print(sources[src])
calls.append(call(sources[src], cloud, None))
calls.append(call(sources[src], cloud))

mockobj.assert_has_calls(calls, any_order=True)

Expand Down Expand Up @@ -665,7 +665,6 @@ def test_apt_src_ppa(self, apt_lists, mocker):
"--no-update",
"ppa:smoser/cloud-init-test",
],
target=None,
),
mock.call(
[
Expand All @@ -678,7 +677,6 @@ def test_apt_src_ppa(self, apt_lists, mocker):
"gpg-agent",
],
capture=True,
target=None,
rcs=[0, 1],
),
]
Expand Down Expand Up @@ -712,23 +710,20 @@ def test_apt_src_ppa_tri(self, apt_lists):
"--no-update",
"ppa:smoser/cloud-init-test",
],
target=None,
),
call(
[
"add-apt-repository",
"--no-update",
"ppa:smoser/cloud-init-test2",
],
target=None,
),
call(
[
"add-apt-repository",
"--no-update",
"ppa:smoser/cloud-init-test3",
],
target=None,
),
]
mockobj.assert_has_calls(calls, any_order=True)
Expand Down
Loading

0 comments on commit 95cb638

Please sign in to comment.