Skip to content

Commit

Permalink
chore: Add helper, refactor utilities into separate module (#5573)
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Aug 6, 2024
1 parent 315c146 commit bb475c6
Show file tree
Hide file tree
Showing 39 changed files with 380 additions and 317 deletions.
9 changes: 5 additions & 4 deletions cloudinit/cmd/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from cloudinit import warnings
from cloudinit import reporting
from cloudinit import atomic_helper
from cloudinit import lifecycle
from cloudinit.cmd.devel import read_cfg_paths
from cloudinit.config import cc_set_hostname
from cloudinit.config.modules import Modules
Expand Down Expand Up @@ -94,7 +95,7 @@ def log_ppid(distro, bootstage_name):
if distro.is_linux:
ppid = os.getppid()
if 1 != ppid and distro.uses_systemd():
util.deprecate(
lifecycle.deprecate(
deprecated=(
"Unsupported configuration: boot stage called "
f"by PID [{ppid}] outside of systemd"
Expand Down Expand Up @@ -255,7 +256,7 @@ def attempt_cmdline_url(path, network=True, cmdline=None) -> Tuple[int, str]:
is_cloud_cfg = False
if is_cloud_cfg:
if cmdline_name == "url":
return util.deprecate(
return lifecycle.deprecate(
deprecated="The kernel command line key `url`",
deprecated_version="22.3",
extra_message=" Please use `cloud-config-url` "
Expand Down Expand Up @@ -650,7 +651,7 @@ def main_modules(action_name, args):
log_ppid(init.distro, bootstage_name)

if name == "init":
util.deprecate(
lifecycle.deprecate(
deprecated="`--mode init`",
deprecated_version="24.1",
extra_message="Use `cloud-init init` instead.",
Expand Down Expand Up @@ -983,7 +984,7 @@ def main(sysv_args=None):
parser_mod = subparsers.add_parser(
"modules", help="Activate modules using a given configuration key."
)
extra_help = util.deprecate(
extra_help = lifecycle.deprecate(
deprecated="`init`",
deprecated_version="24.1",
extra_message="Use `cloud-init init` instead.",
Expand Down
10 changes: 5 additions & 5 deletions cloudinit/config/cc_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from copy import deepcopy
from typing import Optional

from cloudinit import subp
from cloudinit import lifecycle, subp
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
from cloudinit.distros import ALL_DISTROS, Distro
from cloudinit.settings import PER_INSTANCE
from cloudinit.util import Version, get_cfg_by_path
from cloudinit.util import get_cfg_by_path

meta: MetaSchema = {
"id": "cc_ansible",
Expand All @@ -39,13 +39,13 @@ def __init__(self, distro: Distro):
# and cloud-init might not have that set, default: /root
self.env["HOME"] = os.environ.get("HOME", "/root")

def get_version(self) -> Optional[Version]:
def get_version(self) -> Optional[lifecycle.Version]:
stdout, _ = self.do_as(self.cmd_version)
first_line = stdout.splitlines().pop(0)
matches = re.search(r"([\d\.]+)", first_line)
if matches:
version = matches.group(0)
return Version.from_str(version)
return lifecycle.Version.from_str(version)
return None

def pull(self, *args) -> str:
Expand Down Expand Up @@ -210,7 +210,7 @@ def run_ansible_pull(pull: AnsiblePull, cfg: dict):
v = pull.get_version()
if not v:
LOG.warning("Cannot parse ansible version")
elif v < Version(2, 7, 0):
elif v < lifecycle.Version(2, 7, 0):
# diff was added in commit edaa0b52450ade9b86b5f63097ce18ebb147f46f
if cfg.get("diff"):
raise ValueError(
Expand Down
8 changes: 4 additions & 4 deletions cloudinit/config/cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from textwrap import indent
from typing import Dict, Iterable, List, Mapping

from cloudinit import features, subp, templater, util
from cloudinit import features, lifecycle, subp, templater, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -745,7 +745,7 @@ def add_apt_sources(
def convert_v1_to_v2_apt_format(srclist):
"""convert v1 apt format to v2 (dict in apt_sources)"""
srcdict = {}
util.deprecate(
lifecycle.deprecate(
deprecated="Config key 'apt_sources'",
deprecated_version="22.1",
extra_message="Use 'apt' instead",
Expand Down Expand Up @@ -824,15 +824,15 @@ def convert_v2_to_v3_apt_format(oldcfg):
# no old config, so no new one to be created
if not needtoconvert:
return oldcfg
util.deprecate(
lifecycle.deprecate(
deprecated=f"The following config key(s): {needtoconvert}",
deprecated_version="22.1",
)

# if old AND new config are provided, prefer the new one (LP #1616831)
newaptcfg = oldcfg.get("apt", None)
if newaptcfg is not None:
util.deprecate(
lifecycle.deprecate(
deprecated="Support for combined old and new apt module keys",
deprecated_version="22.1",
)
Expand Down
6 changes: 3 additions & 3 deletions cloudinit/config/cc_ca_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
import os

from cloudinit import subp, util
from cloudinit import lifecycle, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -231,7 +231,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
@param args: Any module arguments from cloud.cfg
"""
if "ca-certs" in cfg:
util.deprecate(
lifecycle.deprecate(
deprecated="Key 'ca-certs'",
deprecated_version="22.1",
extra_message="Use 'ca_certs' instead.",
Expand All @@ -254,7 +254,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
# If there is a remove_defaults option set to true, disable the system
# default trusted CA certs first.
if "remove-defaults" in ca_cert_cfg:
util.deprecate(
lifecycle.deprecate(
deprecated="Key 'remove-defaults'",
deprecated_version="22.1",
extra_message="Use 'remove_defaults' instead.",
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_growpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pathlib import Path
from typing import Optional, Tuple

from cloudinit import subp, temp_utils, util
from cloudinit import lifecycle, subp, temp_utils, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -542,7 +542,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
mode = mycfg.get("mode", "auto")
if util.is_false(mode):
if mode != "off":
util.deprecate(
lifecycle.deprecate(
deprecated=f"Growpart's 'mode' key with value '{mode}'",
deprecated_version="22.2",
extra_message="Use 'off' instead.",
Expand Down
6 changes: 3 additions & 3 deletions cloudinit/config/cc_resizefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import stat
from typing import Optional

from cloudinit import subp, util
from cloudinit import lifecycle, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -56,8 +56,8 @@ def _resize_btrfs(mount_point, devpth):
# btrfs has exclusive operations and resize may fail if btrfs is busy
# doing one of the operations that prevents resize. As of btrfs 5.10
# the resize operation can be queued
btrfs_with_queue = util.Version.from_str("5.10")
system_btrfs_ver = util.Version.from_str(
btrfs_with_queue = lifecycle.Version.from_str("5.10")
system_btrfs_ver = lifecycle.Version.from_str(
subp.subp(["btrfs", "--version"])[0].split("v")[-1].strip()
)
if system_btrfs_ver >= btrfs_with_queue:
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_rsyslog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import re
from textwrap import dedent

from cloudinit import log, subp, util
from cloudinit import lifecycle, log, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -153,7 +153,7 @@ def load_config(cfg: dict, distro: Distro) -> dict:
distro_config = distro_default_rsyslog_config(distro)

if isinstance(cfg.get("rsyslog"), list):
util.deprecate(
lifecycle.deprecate(
deprecated="The rsyslog key with value of type 'list'",
deprecated_version="22.2",
)
Expand Down
8 changes: 4 additions & 4 deletions cloudinit/config/cc_set_passwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from string import ascii_letters, digits
from typing import List

from cloudinit import features, subp, util
from cloudinit import features, lifecycle, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -71,7 +71,7 @@ def handle_ssh_pwauth(pw_auth, distro: Distro):
cfg_name = "PasswordAuthentication"

if isinstance(pw_auth, str):
util.deprecate(
lifecycle.deprecate(
deprecated="Using a string value for the 'ssh_pwauth' key",
deprecated_version="22.2",
extra_message="Use a boolean value with 'ssh_pwauth'.",
Expand Down Expand Up @@ -128,7 +128,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
chfg = cfg["chpasswd"]
users_list = util.get_cfg_option_list(chfg, "users", default=[])
if "list" in chfg and chfg["list"]:
util.deprecate(
lifecycle.deprecate(
deprecated="Config key 'lists'",
deprecated_version="22.3",
extra_message="Use 'users' instead.",
Expand All @@ -137,7 +137,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:
LOG.debug("Handling input for chpasswd as list.")
plist = util.get_cfg_option_list(chfg, "list", plist)
else:
util.deprecate(
lifecycle.deprecate(
deprecated="The chpasswd multiline string",
deprecated_version="22.2",
extra_message="Use string type instead.",
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import sys
from typing import List, Optional, Sequence

from cloudinit import ssh_util, subp, util
from cloudinit import lifecycle, ssh_util, subp, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand Down Expand Up @@ -75,7 +75,7 @@ def set_redhat_keyfile_perms(keyfile: str) -> None:
"""
permissions_public = 0o644
ssh_version = ssh_util.get_opensshd_upstream_version()
if ssh_version and ssh_version < util.Version(9, 0):
if ssh_version and ssh_version < lifecycle.Version(9, 0):
# fedora 37, centos 9 stream and below has sshd
# versions less than 9 and private key permissions are
# set to 0o640 from sshd-keygen.
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/config/cc_update_etc_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import logging

from cloudinit import templater, util
from cloudinit import lifecycle, templater, util
from cloudinit.cloud import Cloud
from cloudinit.config import Config
from cloudinit.config.schema import MetaSchema
Expand All @@ -33,7 +33,7 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None:

if util.translate_bool(manage_hosts, addons=["template"]):
if manage_hosts == "template":
util.deprecate(
lifecycle.deprecate(
deprecated="Value 'template' for key 'manage_etc_hosts'",
deprecated_version="22.2",
extra_message="Use 'true' instead.",
Expand Down
8 changes: 4 additions & 4 deletions cloudinit/config/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from types import ModuleType
from typing import Dict, List, NamedTuple, Optional

from cloudinit import config, importer, type_utils, util
from cloudinit import config, importer, lifecycle, type_utils, util
from cloudinit.distros import ALL_DISTROS
from cloudinit.helpers import ConfigMerger
from cloudinit.reporting.events import ReportEventStack
Expand Down Expand Up @@ -194,7 +194,7 @@ def _fixup_modules(self, raw_mods) -> List[ModuleDetails]:
if not mod_name:
continue
if freq and freq not in FREQUENCIES:
util.deprecate(
lifecycle.deprecate(
deprecated=(
f"Config specified module {raw_name} has an unknown"
f" frequency {freq}"
Expand All @@ -205,7 +205,7 @@ def _fixup_modules(self, raw_mods) -> List[ModuleDetails]:
# default meta attribute "frequency" value is used.
freq = None
if mod_name in RENAMED_MODULES:
util.deprecate(
lifecycle.deprecate(
deprecated=(
f"Module has been renamed from {mod_name} to "
f"{RENAMED_MODULES[mod_name]}. Update any"
Expand Down Expand Up @@ -278,7 +278,7 @@ def _run_modules(self, mostly_mods: List[ModuleDetails]):
func_signature = signature(mod.handle)
func_params = func_signature.parameters
if len(func_params) == 5:
util.deprecate(
lifecycle.deprecate(
deprecated="Config modules with a `log` parameter",
deprecated_version="23.2",
)
Expand Down
12 changes: 7 additions & 5 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import yaml

from cloudinit import features, importer, safeyaml
from cloudinit import features, importer, lifecycle, safeyaml
from cloudinit.cmd.devel import read_cfg_paths
from cloudinit.handlers import INCLUSION_TYPES_MAP, type_from_starts_with
from cloudinit.helpers import Paths
Expand All @@ -42,7 +42,6 @@
get_modules_from_dir,
load_text_file,
load_yaml,
should_log_deprecation,
write_file,
)

Expand Down Expand Up @@ -795,8 +794,11 @@ def validate_cloudconfig_schema(
if isinstance(
schema_error, SchemaDeprecationError
): # pylint: disable=W1116
if schema_error.version == "devel" or should_log_deprecation(
schema_error.version, features.DEPRECATION_INFO_BOUNDARY
if (
schema_error.version == "devel"
or lifecycle.should_log_deprecation(
schema_error.version, features.DEPRECATION_INFO_BOUNDARY
)
):
deprecations.append(SchemaProblem(path, schema_error.message))
else:
Expand All @@ -818,7 +820,7 @@ def validate_cloudconfig_schema(
deprecations,
prefix="Deprecated cloud-config provided: ",
)
# This warning doesn't fit the standardized util.deprecated()
# This warning doesn't fit the standardized lifecycle.deprecated()
# utility format, but it is a deprecation log, so log it directly.
LOG.deprecated(message) # type: ignore
if strict and (errors or deprecations or info_deprecations):
Expand Down
5 changes: 3 additions & 2 deletions cloudinit/distros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from cloudinit import (
helpers,
importer,
lifecycle,
net,
persistence,
ssh_util,
Expand Down Expand Up @@ -710,7 +711,7 @@ def add_user(self, name, **kwargs):
groups = groups.split(",")

if isinstance(groups, dict):
util.deprecate(
lifecycle.deprecate(
deprecated=f"The user {name} has a 'groups' config value "
"of type dict",
deprecated_version="22.3",
Expand Down Expand Up @@ -848,7 +849,7 @@ def create_user(self, name, **kwargs):
if kwargs["sudo"]:
self.write_sudo_rules(name, kwargs["sudo"])
elif kwargs["sudo"] is False:
util.deprecate(
lifecycle.deprecate(
deprecated=f"The value of 'false' in user {name}'s "
"'sudo' config",
deprecated_version="22.2",
Expand Down
4 changes: 2 additions & 2 deletions cloudinit/distros/alpine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from datetime import datetime
from typing import Any, Dict, Optional

from cloudinit import distros, helpers, subp, util
from cloudinit import distros, helpers, lifecycle, subp, util
from cloudinit.distros.parsers.hostname import HostnameConf
from cloudinit.settings import PER_ALWAYS, PER_INSTANCE

Expand Down Expand Up @@ -248,7 +248,7 @@ def add_user(self, name, **kwargs):
if isinstance(groups, str):
groups = groups.split(",")
elif isinstance(groups, dict):
util.deprecate(
lifecycle.deprecate(
deprecated=f"The user {name} has a 'groups' config value "
"of type dict",
deprecated_version="22.3",
Expand Down
Loading

0 comments on commit bb475c6

Please sign in to comment.