diff options
author | d1r3ct0r <calvin.mwadime@canonical.com> | 2023-03-27 09:42:28 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-27 08:42:28 +0200 |
commit | baad0946758a406201b0b17acbd03b27d1d46f2c (patch) | |
tree | 4af26a8f021f5202859b88de5cd5684f1f107d67 /cloudinit | |
parent | 7803f9c64d55993d720d5b24459ea13a19781f62 (diff) | |
download | cloud-init-git-baad0946758a406201b0b17acbd03b27d1d46f2c.tar.gz |
refactor: stop passing log instances to cc_* handlers (#2016)
Use the module level Log instances instead of passing log instances
to the cc_* handlers
Diffstat (limited to 'cloudinit')
64 files changed, 443 insertions, 551 deletions
diff --git a/cloudinit/cmd/main.py b/cloudinit/cmd/main.py index 9c2307ac..70efa7b7 100755 --- a/cloudinit/cmd/main.py +++ b/cloudinit/cmd/main.py @@ -821,7 +821,7 @@ def _maybe_set_hostname(init, stage, retry_stage): ) if hostname: # meta-data or user-data hostname content try: - cc_set_hostname.handle("set-hostname", init.cfg, cloud, LOG, None) + cc_set_hostname.handle("set-hostname", init.cfg, cloud, None) except cc_set_hostname.SetHostnameError as e: LOG.debug( "Failed setting hostname in %s stage. Will" diff --git a/cloudinit/config/cc_ansible.py b/cloudinit/config/cc_ansible.py index 5392e605..f8ca29bd 100644 --- a/cloudinit/config/cc_ansible.py +++ b/cloudinit/config/cc_ansible.py @@ -4,7 +4,7 @@ import os import re import sys from copy import deepcopy -from logging import Logger, getLogger +from logging import getLogger from textwrap import dedent from typing import Optional @@ -156,9 +156,7 @@ class AnsiblePullDistro(AnsiblePull): return bool(which("ansible")) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: ansible_cfg: dict = cfg.get("ansible", {}) ansible_user = ansible_cfg.get("run_user") diff --git a/cloudinit/config/cc_apk_configure.py b/cloudinit/config/cc_apk_configure.py index 07a7fa85..080b1590 100644 --- a/cloudinit/config/cc_apk_configure.py +++ b/cloudinit/config/cc_apk_configure.py @@ -6,7 +6,6 @@ """Apk Configure: Configures apk repositories file.""" -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -109,9 +108,7 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """ Call to handle apk_repos sections in cloud-config file. diff --git a/cloudinit/config/cc_apt_configure.py b/cloudinit/config/cc_apt_configure.py index e8de000a..d70b7cb4 100644 --- a/cloudinit/config/cc_apt_configure.py +++ b/cloudinit/config/cc_apt_configure.py @@ -12,7 +12,6 @@ import glob import os import pathlib import re -from logging import Logger from textwrap import dedent from cloudinit import gpg @@ -170,17 +169,12 @@ def get_default_mirrors(arch=None, target=None): raise ValueError("No default mirror known for arch %s" % arch) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +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 - if log is not None: - global LOG - LOG = log # feed back converted config, but only work on the subset under 'apt' cfg = convert_to_v3_apt_format(cfg) apt_cfg = cfg.get("apt", {}) diff --git a/cloudinit/config/cc_apt_pipelining.py b/cloudinit/config/cc_apt_pipelining.py index 6a9ace9c..25cb63df 100644 --- a/cloudinit/config/cc_apt_pipelining.py +++ b/cloudinit/config/cc_apt_pipelining.py @@ -6,7 +6,7 @@ """Apt Pipelining: configure apt pipelining.""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import util @@ -15,6 +15,8 @@ from cloudinit.config import Config from cloudinit.config.schema import MetaSchema, get_meta_doc from cloudinit.settings import PER_INSTANCE +LOG = logging.getLogger(__name__) + frequency = PER_INSTANCE distros = ["ubuntu", "debian"] DEFAULT_FILE = "/etc/apt/apt.conf.d/90cloud-init-pipelining" @@ -59,20 +61,18 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: apt_pipe_value = cfg.get("apt_pipelining", "os") apt_pipe_value_s = str(apt_pipe_value).lower().strip() if apt_pipe_value_s == "false": - write_apt_snippet("0", log, DEFAULT_FILE) + write_apt_snippet("0", LOG, DEFAULT_FILE) elif apt_pipe_value_s in ("none", "unchanged", "os"): return elif apt_pipe_value_s in [str(b) for b in range(0, 6)]: - write_apt_snippet(apt_pipe_value_s, log, DEFAULT_FILE) + write_apt_snippet(apt_pipe_value_s, LOG, DEFAULT_FILE) else: - log.warning("Invalid option for apt_pipelining: %s", apt_pipe_value) + LOG.warning("Invalid option for apt_pipelining: %s", apt_pipe_value) def write_apt_snippet(setting, log, f_name): diff --git a/cloudinit/config/cc_bootcmd.py b/cloudinit/config/cc_bootcmd.py index 48cd21cc..45012c3a 100644 --- a/cloudinit/config/cc_bootcmd.py +++ b/cloudinit/config/cc_bootcmd.py @@ -9,8 +9,8 @@ """Bootcmd: run arbitrary commands early in the boot process.""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import subp, temp_utils, util @@ -19,6 +19,8 @@ from cloudinit.config import Config from cloudinit.config.schema import MetaSchema, get_meta_doc from cloudinit.settings import PER_ALWAYS +LOG = logging.getLogger(__name__) + frequency = PER_ALWAYS distros = ["all"] @@ -63,12 +65,10 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "bootcmd" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'bootcmd' key in configuration", name ) return @@ -79,7 +79,7 @@ def handle( tmpf.write(util.encode_text(content)) tmpf.flush() except Exception as e: - util.logexc(log, "Failed to shellify bootcmd: %s", str(e)) + util.logexc(LOG, "Failed to shellify bootcmd: %s", str(e)) raise try: @@ -90,7 +90,7 @@ def handle( cmd = ["/bin/sh", tmpf.name] subp.subp(cmd, env=env, capture=False) except Exception: - util.logexc(log, "Failed to run bootcmd module %s", name) + util.logexc(LOG, "Failed to run bootcmd module %s", name) raise diff --git a/cloudinit/config/cc_byobu.py b/cloudinit/config/cc_byobu.py index 681936b4..ef3d1b58 100644 --- a/cloudinit/config/cc_byobu.py +++ b/cloudinit/config/cc_byobu.py @@ -8,7 +8,7 @@ """Byobu: Enable/disable byobu system wide and for default user.""" -from logging import Logger +import logging from cloudinit import subp, util from cloudinit.cloud import Cloud @@ -36,6 +36,8 @@ Valid configuration options for this module are: """ distros = ["ubuntu", "debian"] +LOG = logging.getLogger(__name__) + meta: MetaSchema = { "id": "cc_byobu", "name": "Byobu", @@ -53,16 +55,14 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if len(args) != 0: value = args[0] else: value = util.get_cfg_option_str(cfg, "byobu_by_default", "") if not value: - log.debug("Skipping module named %s, no 'byobu' values found", name) + LOG.debug("Skipping module named %s, no 'byobu' values found", name) return if value == "user" or value == "system": @@ -77,7 +77,7 @@ def handle( "disable", ) if value not in valid: - log.warning("Unknown value %s for byobu_by_default", value) + LOG.warning("Unknown value %s for byobu_by_default", value) mod_user = value.endswith("-user") mod_sys = value.endswith("-system") @@ -97,7 +97,7 @@ def handle( (users, _groups) = ug_util.normalize_users_groups(cfg, cloud.distro) (user, _user_config) = ug_util.extract_default(users) if not user: - log.warning( + LOG.warning( "No default byobu user provided, " "can not launch %s for the default user", bl_inst, @@ -112,7 +112,7 @@ def handle( if len(shcmd): cmd = ["/bin/sh", "-c", "%s %s %s" % ("X=0;", shcmd, "exit $X")] - log.debug("Setting byobu to %s", value) + LOG.debug("Setting byobu to %s", value) subp.subp(cmd, capture=False) diff --git a/cloudinit/config/cc_ca_certs.py b/cloudinit/config/cc_ca_certs.py index 1c0ec0b1..c1cd42a4 100644 --- a/cloudinit/config/cc_ca_certs.py +++ b/cloudinit/config/cc_ca_certs.py @@ -5,7 +5,6 @@ """CA Certs: Add ca certificates.""" import os -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -231,9 +230,7 @@ def remove_default_ca_certs(distro_cfg): util.delete_dir_contents(distro_cfg["ca_cert_local_path"]) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """ Call to handle ca_cert sections in cloud-config file. diff --git a/cloudinit/config/cc_chef.py b/cloudinit/config/cc_chef.py index 5a809230..940aa5f7 100644 --- a/cloudinit/config/cc_chef.py +++ b/cloudinit/config/cc_chef.py @@ -10,8 +10,8 @@ import itertools import json +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import subp, temp_utils, templater, url_helper, util @@ -97,6 +97,8 @@ CHEF_EXEC_DEF_ARGS = tuple(["-d", "-i", "1800", "-s", "20"]) frequency = PER_ALWAYS distros = ["all"] +LOG = logging.getLogger(__name__) + meta: MetaSchema = { "id": "cc_chef", "name": "Chef", @@ -146,7 +148,7 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def post_run_chef(chef_cfg, log): +def post_run_chef(chef_cfg): delete_pem = util.get_cfg_option_bool( chef_cfg, "delete_validation_post_exec", default=False ) @@ -154,14 +156,14 @@ def post_run_chef(chef_cfg, log): os.unlink(CHEF_VALIDATION_PEM_PATH) -def get_template_params(iid, chef_cfg, log): +def get_template_params(iid, chef_cfg): params = CHEF_RB_TPL_DEFAULTS.copy() # Allow users to overwrite any of the keys they want (if they so choose), # when a value is None, then the value will be set to None and no boolean # or string version will be populated... for (k, v) in chef_cfg.items(): if k not in CHEF_RB_TPL_KEYS: - log.debug("Skipping unknown chef template key '%s'", k) + LOG.debug("Skipping unknown chef template key '%s'", k) continue if v is None: params[k] = None @@ -189,14 +191,12 @@ def get_template_params(iid, chef_cfg, log): return params -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """Handler method activated by cloud-init.""" # If there isn't a chef key in the configuration don't do anything if "chef" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'chef' key in configuration", name ) return @@ -218,7 +218,7 @@ def handle( if vcert != "system": util.write_file(vkey_path, vcert) elif not os.path.isfile(vkey_path): - log.warning( + LOG.warning( "chef validation_cert provided as 'system', but " "validation_key path '%s' does not exist.", vkey_path, @@ -228,7 +228,7 @@ def handle( template_fn = cloud.get_template_filename("chef_client.rb") if template_fn: iid = str(cloud.datasource.get_instance_id()) - params = get_template_params(iid, chef_cfg, log) + params = get_template_params(iid, chef_cfg) # Do a best effort attempt to ensure that the template values that # are associated with paths have their parent directory created # before they are used by the chef-client itself. @@ -239,14 +239,14 @@ def handle( util.ensure_dirs(param_paths) templater.render_to_file(template_fn, CHEF_RB_PATH, params) else: - log.warning("No template found, not rendering to %s", CHEF_RB_PATH) + LOG.warning("No template found, not rendering to %s", CHEF_RB_PATH) # Set the firstboot json fb_filename = util.get_cfg_option_str( chef_cfg, "firstboot_path", default=CHEF_FB_PATH ) if not fb_filename: - log.info("First boot path empty, not writing first boot json file") + LOG.info("First boot path empty, not writing first boot json file") else: initial_json = {} if "run_list" in chef_cfg: @@ -263,18 +263,18 @@ def handle( ) installed = subp.is_exe(CHEF_EXEC_PATH) if not installed or force_install: - run = install_chef(cloud, chef_cfg, log) + run = install_chef(cloud, chef_cfg) elif installed: run = util.get_cfg_option_bool(chef_cfg, "exec", default=False) else: run = False if run: - run_chef(chef_cfg, log) - post_run_chef(chef_cfg, log) + run_chef(chef_cfg) + post_run_chef(chef_cfg) -def run_chef(chef_cfg, log): - log.debug("Running chef-client") +def run_chef(chef_cfg): + LOG.debug("Running chef-client") cmd = [CHEF_EXEC_PATH] if "exec_arguments" in chef_cfg: cmd_args = chef_cfg["exec_arguments"] @@ -283,7 +283,7 @@ def run_chef(chef_cfg, log): elif isinstance(cmd_args, str): cmd.append(cmd_args) else: - log.warning( + LOG.warning( "Unknown type %s provided for chef" " 'exec_arguments' expected list, tuple," " or string", @@ -345,7 +345,7 @@ def install_chef_from_omnibus( ) -def install_chef(cloud: Cloud, chef_cfg, log): +def install_chef(cloud: Cloud, chef_cfg): # If chef is not installed, we install chef based on 'install_type' install_type = util.get_cfg_option_str( chef_cfg, "install_type", "packages" @@ -373,7 +373,7 @@ def install_chef(cloud: Cloud, chef_cfg, log): omnibus_version=omnibus_version, ) else: - log.warning("Unknown chef install type '%s'", install_type) + LOG.warning("Unknown chef install type '%s'", install_type) run = False return run diff --git a/cloudinit/config/cc_disable_ec2_metadata.py b/cloudinit/config/cc_disable_ec2_metadata.py index 7439b89b..2773e35c 100644 --- a/cloudinit/config/cc_disable_ec2_metadata.py +++ b/cloudinit/config/cc_disable_ec2_metadata.py @@ -8,7 +8,7 @@ """Disable EC2 Metadata: Disable AWS EC2 metadata.""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import subp, util @@ -21,6 +21,8 @@ from cloudinit.settings import PER_ALWAYS REJECT_CMD_IF = ["route", "add", "-host", "169.254.169.254", "reject"] REJECT_CMD_IP = ["ip", "route", "add", "prohibit", "169.254.169.254"] +LOG = logging.getLogger(__name__) + meta: MetaSchema = { "id": "cc_disable_ec2_metadata", "name": "Disable EC2 Metadata", @@ -40,9 +42,7 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: disabled = util.get_cfg_option_bool(cfg, "disable_ec2_metadata", False) if disabled: reject_cmd = None @@ -51,14 +51,14 @@ def handle( elif subp.which("ifconfig"): reject_cmd = REJECT_CMD_IF else: - log.error( + LOG.error( 'Neither "route" nor "ip" command found, unable to ' "manipulate routing table" ) return subp.subp(reject_cmd, capture=False) else: - log.debug( + LOG.debug( "Skipping module named %s, disabling the ec2 route not enabled", name, ) diff --git a/cloudinit/config/cc_disk_setup.py b/cloudinit/config/cc_disk_setup.py index ecaca079..3250efd8 100644 --- a/cloudinit/config/cc_disk_setup.py +++ b/cloudinit/config/cc_disk_setup.py @@ -10,7 +10,6 @@ import logging import os import shlex -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -112,9 +111,7 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """ See doc/examples/cloud-config-disk-setup.txt for documentation on the format. @@ -128,14 +125,14 @@ def handle( disk_setup = cfg.get("disk_setup") if isinstance(disk_setup, dict): update_disk_setup_devices(disk_setup, alias_to_device) - log.debug("Partitioning disks: %s", str(disk_setup)) + LOG.debug("Partitioning disks: %s", str(disk_setup)) for disk, definition in disk_setup.items(): if not isinstance(definition, dict): - log.warning("Invalid disk definition for %s" % disk) + LOG.warning("Invalid disk definition for %s", disk) continue try: - log.debug("Creating new partition table/disk") + LOG.debug("Creating new partition table/disk") util.log_time( logfunc=LOG.debug, msg="Creating partition on %s" % disk, @@ -147,15 +144,15 @@ def handle( fs_setup = cfg.get("fs_setup") if isinstance(fs_setup, list): - log.debug("setting up filesystems: %s", str(fs_setup)) + LOG.debug("setting up filesystems: %s", str(fs_setup)) update_fs_setup_devices(fs_setup, alias_to_device) for definition in fs_setup: if not isinstance(definition, dict): - log.warning("Invalid file system definition: %s" % definition) + LOG.warning("Invalid file system definition: %s", definition) continue try: - log.debug("Creating new filesystem.") + LOG.debug("Creating new filesystem.") device = definition.get("device") util.log_time( logfunc=LOG.debug, diff --git a/cloudinit/config/cc_fan.py b/cloudinit/config/cc_fan.py index ae211f31..e6ce1f19 100644 --- a/cloudinit/config/cc_fan.py +++ b/cloudinit/config/cc_fan.py @@ -5,7 +5,6 @@ # This file is part of cloud-init. See LICENSE file for license information. """Fan: Configure ubuntu fan networking""" -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -87,9 +86,7 @@ def stop_update_start(distro, service, config_file, content): distro.manage_service("enable", service) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: cfgin = cfg.get("fan") if not cfgin: cfgin = {} diff --git a/cloudinit/config/cc_final_message.py b/cloudinit/config/cc_final_message.py index d773afb1..efc234b9 100644 --- a/cloudinit/config/cc_final_message.py +++ b/cloudinit/config/cc_final_message.py @@ -7,7 +7,7 @@ # This file is part of cloud-init. See LICENSE file for license information. """Final Message: Output final message when cloud-init has finished""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import templater, util, version @@ -56,6 +56,7 @@ meta: MetaSchema = { "activate_by_schema_keys": [], } +LOG = logging.getLogger(__name__) __doc__ = get_meta_doc(meta) # Jinja formated default message @@ -66,9 +67,7 @@ FINAL_MESSAGE_DEF = ( ) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: msg_in = "" if len(args) != 0: @@ -95,20 +94,20 @@ def handle( "%s\n" % (templater.render_string(msg_in, subs)), console=False, stderr=True, - log=log, + log=LOG, ) except Exception: - util.logexc(log, "Failed to render final message template") + util.logexc(LOG, "Failed to render final message template") boot_fin_fn = cloud.paths.boot_finished try: contents = "%s - %s - v. %s\n" % (uptime, ts, cver) util.write_file(boot_fin_fn, contents, ensure_dir_exists=False) except Exception: - util.logexc(log, "Failed to write boot finished file %s", boot_fin_fn) + util.logexc(LOG, "Failed to write boot finished file %s", boot_fin_fn) if cloud.datasource.is_disconnected: - log.warning("Used fallback datasource") + LOG.warning("Used fallback datasource") # vi: ts=4 expandtab diff --git a/cloudinit/config/cc_growpart.py b/cloudinit/config/cc_growpart.py index 2a496820..cce2f686 100644 --- a/cloudinit/config/cc_growpart.py +++ b/cloudinit/config/cc_growpart.py @@ -16,7 +16,6 @@ import re import stat from abc import ABC, abstractmethod from contextlib import suppress -from logging import Logger from pathlib import Path from textwrap import dedent from typing import Tuple @@ -566,18 +565,16 @@ def resize_devices(resizer, devices): return info -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "growpart" not in cfg: - log.debug( - "No 'growpart' entry in cfg. Using default: %s" % DEFAULT_CONFIG + LOG.debug( + "No 'growpart' entry in cfg. Using default: %s", DEFAULT_CONFIG ) cfg["growpart"] = DEFAULT_CONFIG mycfg = cfg.get("growpart") if not isinstance(mycfg, dict): - log.warning("'growpart' in config was not a dict") + LOG.warning("'growpart' in config was not a dict") return mode = mycfg.get("mode", "auto") @@ -588,39 +585,39 @@ def handle( deprecated_version="22.2", extra_message="Use 'off' instead.", ) - log.debug("growpart disabled: mode=%s" % mode) + LOG.debug("growpart disabled: mode=%s", mode) return if util.is_false(mycfg.get("ignore_growroot_disabled", False)): if os.path.isfile("/etc/growroot-disabled"): - log.debug("growpart disabled: /etc/growroot-disabled exists") - log.debug("use ignore_growroot_disabled to ignore") + LOG.debug("growpart disabled: /etc/growroot-disabled exists") + LOG.debug("use ignore_growroot_disabled to ignore") return devices = util.get_cfg_option_list(mycfg, "devices", ["/"]) if not len(devices): - log.debug("growpart: empty device list") + LOG.debug("growpart: empty device list") return try: resizer = resizer_factory(mode, cloud.distro) except (ValueError, TypeError) as e: - log.debug("growpart unable to find resizer for '%s': %s" % (mode, e)) + LOG.debug("growpart unable to find resizer for '%s': %s", mode, e) if mode != "auto": raise e return resized = util.log_time( - logfunc=log.debug, + logfunc=LOG.debug, msg="resize_devices", func=resize_devices, args=(resizer, devices), ) for (entry, action, msg) in resized: if action == RESIZE.CHANGED: - log.info("'%s' resized: %s" % (entry, msg)) + LOG.info("'%s' resized: %s", entry, msg) else: - log.debug("'%s' %s: %s" % (entry, action, msg)) + LOG.debug("'%s' %s: %s", entry, action, msg) RESIZERS = (("growpart", ResizeGrowPart), ("gpart", ResizeGpart)) diff --git a/cloudinit/config/cc_grub_dpkg.py b/cloudinit/config/cc_grub_dpkg.py index 2e0e671e..91736146 100644 --- a/cloudinit/config/cc_grub_dpkg.py +++ b/cloudinit/config/cc_grub_dpkg.py @@ -8,8 +8,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Grub Dpkg: Configure grub debconf installation device""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -57,9 +57,10 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def fetch_idevs(log: Logger): +def fetch_idevs(): """ Fetches the /dev/disk/by-id device grub is installed to. Falls back to plain disk name if no by-id entry is present. @@ -71,7 +72,7 @@ def fetch_idevs(log: Logger): # EFI mode systems use /boot/efi and the partition path. probe_target = "disk" probe_mount = "/boot" - if is_efi_booted(log): + if is_efi_booted(): probe_target = "device" probe_mount = "/boot/efi" @@ -84,18 +85,18 @@ def fetch_idevs(log: Logger): # grub-common may not be installed, especially on containers # FileNotFoundError is a nested exception of ProcessExecutionError if isinstance(e.reason, FileNotFoundError): - log.debug("'grub-probe' not found in $PATH") + LOG.debug("'grub-probe' not found in $PATH") # disks from the container host are present in /proc and /sys # which is where grub-probe determines where /boot is. # it then checks for existence in /dev, which fails as host disks # are not exposed to the container. elif "failed to get canonical path" in e.stderr: - log.debug("grub-probe 'failed to get canonical path'") + LOG.debug("grub-probe 'failed to get canonical path'") else: # something bad has happened, continue to log the error raise except Exception: - util.logexc(log, "grub-probe failed to execute for grub-dpkg") + util.logexc(LOG, "grub-probe failed to execute for grub-dpkg") if not disk or not os.path.exists(disk): # If we failed to detect a disk, we can return early @@ -113,73 +114,71 @@ def fetch_idevs(log: Logger): ) except Exception: util.logexc( - log, "udevadm DEVLINKS symlink query failed for disk='%s'", disk + LOG, "udevadm DEVLINKS symlink query failed for disk='%s'", disk ) - log.debug("considering these device symlinks: %s", ",".join(devices)) + LOG.debug("considering these device symlinks: %s", ",".join(devices)) # filter symlinks for /dev/disk/by-id entries devices = [dev for dev in devices if "disk/by-id" in dev] - log.debug("filtered to these disk/by-id symlinks: %s", ",".join(devices)) + LOG.debug("filtered to these disk/by-id symlinks: %s", ",".join(devices)) # select first device if there is one, else fall back to plain name idevs = sorted(devices)[0] if devices else disk - log.debug("selected %s", idevs) + LOG.debug("selected %s", idevs) return idevs -def is_efi_booted(log: Logger) -> bool: +def is_efi_booted() -> bool: """ Check if the system is booted in EFI mode. """ try: return os.path.exists("/sys/firmware/efi") except OSError as e: - log.error("Failed to determine if system is booted in EFI mode: %s", e) + LOG.error("Failed to determine if system is booted in EFI mode: %s", e) # If we can't determine if we're booted in EFI mode, assume we're not. return False -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: mycfg = cfg.get("grub_dpkg", cfg.get("grub-dpkg", {})) if not mycfg: mycfg = {} enabled = mycfg.get("enabled", True) if util.is_false(enabled): - log.debug("%s disabled by config grub_dpkg/enabled=%s", name, enabled) + LOG.debug("%s disabled by config grub_dpkg/enabled=%s", name, enabled) return - dconf_sel = get_debconf_config(mycfg, log) - log.debug("Setting grub debconf-set-selections with '%s'" % dconf_sel) + dconf_sel = get_debconf_config(mycfg) + LOG.debug("Setting grub debconf-set-selections with '%s'", dconf_sel) try: subp.subp(["debconf-set-selections"], dconf_sel) except Exception as e: util.logexc( - log, "Failed to run debconf-set-selections for grub_dpkg: %s", e + LOG, "Failed to run debconf-set-selections for grub_dpkg: %s", e ) -def get_debconf_config(mycfg: Config, log: Logger) -> str: +def get_debconf_config(mycfg: Config) -> str: """ Returns the debconf config for grub-pc or grub-efi depending on the systems boot mode. """ - if is_efi_booted(log): + if is_efi_booted(): idevs = util.get_cfg_option_str( mycfg, "grub-efi/install_devices", None ) if idevs is None: - idevs = fetch_idevs(log) + idevs = fetch_idevs() return "grub-pc grub-efi/install_devices string %s\n" % idevs else: idevs = util.get_cfg_option_str(mycfg, "grub-pc/install_devices", None) if idevs is None: - idevs = fetch_idevs(log) + idevs = fetch_idevs() idevs_empty = mycfg.get("grub-pc/install_devices_empty") if idevs_empty is None: diff --git a/cloudinit/config/cc_install_hotplug.py b/cloudinit/config/cc_install_hotplug.py index b95b8a4c..621e3be5 100644 --- a/cloudinit/config/cc_install_hotplug.py +++ b/cloudinit/config/cc_install_hotplug.py @@ -1,7 +1,7 @@ # This file is part of cloud-init. See LICENSE file for license information. """Install hotplug udev rules if supported and enabled""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import stages, subp, util @@ -56,6 +56,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) HOTPLUG_UDEV_PATH = "/etc/udev/rules.d/10-cloud-init-hook-hotplug.rules" @@ -68,9 +69,7 @@ LABEL="cloudinit_end" """ -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: network_hotplug_enabled = ( "updates" in cfg and "network" in cfg["updates"] @@ -90,19 +89,19 @@ def handle( ) if not (hotplug_supported and hotplug_enabled): if os.path.exists(HOTPLUG_UDEV_PATH): - log.debug("Uninstalling hotplug, not enabled") + LOG.debug("Uninstalling hotplug, not enabled") util.del_file(HOTPLUG_UDEV_PATH) subp.subp(["udevadm", "control", "--reload-rules"]) elif network_hotplug_enabled: - log.warning( + LOG.warning( "Hotplug is unsupported by current datasource. " "Udev rules will NOT be installed." ) else: - log.debug("Skipping hotplug install, not enabled") + LOG.debug("Skipping hotplug install, not enabled") return if not subp.which("udevadm"): - log.debug("Skipping hotplug install, udevadm not found") + LOG.debug("Skipping hotplug install, udevadm not found") return # This may need to turn into a distro property at some point diff --git a/cloudinit/config/cc_keyboard.py b/cloudinit/config/cc_keyboard.py index f6075e63..5e61a0da 100644 --- a/cloudinit/config/cc_keyboard.py +++ b/cloudinit/config/cc_keyboard.py @@ -6,7 +6,6 @@ """keyboard: set keyboard layout""" -from logging import Logger from textwrap import dedent from cloudinit import distros @@ -60,9 +59,7 @@ __doc__ = get_meta_doc(meta) LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "keyboard" not in cfg: LOG.debug( "Skipping module named %s, no 'keyboard' section found", name diff --git a/cloudinit/config/cc_keys_to_console.py b/cloudinit/config/cc_keys_to_console.py index 649c0abb..657910af 100644 --- a/cloudinit/config/cc_keys_to_console.py +++ b/cloudinit/config/cc_keys_to_console.py @@ -8,8 +8,8 @@ """Keys to Console: Control which SSH host keys may be written to console""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -68,6 +68,8 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) + def _get_helper_tool_path(distro): try: @@ -77,18 +79,16 @@ def _get_helper_tool_path(distro): return HELPER_TOOL_TPL % base_lib -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if util.is_false(cfg.get("ssh", {}).get("emit_keys_to_console", True)): - log.debug( + LOG.debug( "Skipping module named %s, logging of SSH host keys disabled", name ) return helper_path = _get_helper_tool_path(cloud.distro) if not os.path.exists(helper_path): - log.warning( + LOG.warning( "Unable to activate module %s, helper tool not found at %s", name, helper_path, @@ -107,7 +107,7 @@ def handle( (stdout, _stderr) = subp.subp(cmd) util.multi_log("%s\n" % (stdout.strip()), stderr=False, console=True) except Exception: - log.warning("Writing keys to the system console failed!") + LOG.warning("Writing keys to the system console failed!") raise diff --git a/cloudinit/config/cc_landscape.py b/cloudinit/config/cc_landscape.py index 8abb4c5f..a34ea019 100644 --- a/cloudinit/config/cc_landscape.py +++ b/cloudinit/config/cc_landscape.py @@ -8,9 +8,9 @@ """install and configure landscape client""" +import logging import os from io import BytesIO -from logging import Logger from textwrap import dedent from configobj import ConfigObj @@ -98,11 +98,10 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """ Basically turn a top level 'landscape' entry with a 'client' dict and render it to ConfigObj format under '[client]' section in @@ -135,7 +134,7 @@ def handle( util.ensure_dir(os.path.dirname(LSC_CLIENT_CFG_FILE)) util.write_file(LSC_CLIENT_CFG_FILE, contents.getvalue()) - log.debug("Wrote landscape config file to %s", LSC_CLIENT_CFG_FILE) + LOG.debug("Wrote landscape config file to %s", LSC_CLIENT_CFG_FILE) util.write_file(LS_DEFAULT_FILE, "RUN=1\n") subp.subp(["service", "landscape-client", "restart"]) diff --git a/cloudinit/config/cc_locale.py b/cloudinit/config/cc_locale.py index 4a53e765..b69475a7 100644 --- a/cloudinit/config/cc_locale.py +++ b/cloudinit/config/cc_locale.py @@ -8,7 +8,7 @@ """Locale: set system locale""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import util @@ -49,23 +49,22 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if len(args) != 0: locale = args[0] else: locale = util.get_cfg_option_str(cfg, "locale", cloud.get_locale()) if util.is_false(locale): - log.debug( + LOG.debug( "Skipping module named %s, disabled by config: %s", name, locale ) return - log.debug("Setting locale to %s", locale) + LOG.debug("Setting locale to %s", locale) locale_cfgfile = util.get_cfg_option_str(cfg, "locale_configfile") cloud.distro.apply_locale(locale, locale_cfgfile) diff --git a/cloudinit/config/cc_lxd.py b/cloudinit/config/cc_lxd.py index 06c9f6a6..695d233e 100644 --- a/cloudinit/config/cc_lxd.py +++ b/cloudinit/config/cc_lxd.py @@ -7,7 +7,6 @@ """LXD: configure lxd with ``lxd init`` and optionally lxd-bridge""" import os -from logging import Logger from textwrap import dedent from typing import List, Tuple @@ -197,13 +196,11 @@ def supplemental_schema_validation( raise ValueError(". ".join(errors)) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # Get config lxd_cfg = cfg.get("lxd") if not lxd_cfg: - log.debug( + LOG.debug( "Skipping module named %s, not present or disabled by cfg", name ) return @@ -224,7 +221,7 @@ def handle( try: cloud.distro.install_packages(packages) except subp.ProcessExecutionError as exc: - log.warning("failed to install packages %s: %s", packages, exc) + LOG.warning("failed to install packages %s: %s", packages, exc) return subp.subp(["lxd", "waitready", "--timeout=300"]) @@ -251,7 +248,7 @@ def handle( if init_cfg["storage_backend"] == "lvm" and not os.path.exists( f"/lib/modules/{kernel}/kernel/drivers/md/dm-thin-pool.ko" ): - log.warning( + LOG.warning( "cloud-init doesn't use thinpool by default on Ubuntu due to " "LP #1982780. This behavior will change in the future.", ) @@ -294,7 +291,7 @@ def handle( # Update debconf database try: - log.debug("Setting lxd debconf via " + dconf_comm) + LOG.debug("Setting lxd debconf via %s", dconf_comm) data = ( "\n".join( ["set %s %s" % (k, v) for k, v in debconf.items()] @@ -304,14 +301,14 @@ def handle( subp.subp(["debconf-communicate"], data) except Exception: util.logexc( - log, "Failed to run '%s' for lxd with" % dconf_comm + LOG, "Failed to run '%s' for lxd with" % dconf_comm ) # Remove the existing configuration file (forces re-generation) util.del_file("/etc/default/lxd-bridge") # Run reconfigure - log.debug("Running dpkg-reconfigure for lxd") + LOG.debug("Running dpkg-reconfigure for lxd") subp.subp(["dpkg-reconfigure", "lxd", "--frontend=noninteractive"]) else: # Built-in LXD bridge support @@ -323,12 +320,12 @@ def handle( attach=bool(cmd_attach), ) if cmd_create: - log.debug("Creating lxd bridge: %s" % " ".join(cmd_create)) + LOG.debug("Creating lxd bridge: %s", " ".join(cmd_create)) _lxc(cmd_create) if cmd_attach: - log.debug( - "Setting up default lxd bridge: %s" % " ".join(cmd_attach) + LOG.debug( + "Setting up default lxd bridge: %s", " ".join(cmd_attach) ) _lxc(cmd_attach) diff --git a/cloudinit/config/cc_mcollective.py b/cloudinit/config/cc_mcollective.py index 7d75078d..967ca8f3 100644 --- a/cloudinit/config/cc_mcollective.py +++ b/cloudinit/config/cc_mcollective.py @@ -11,7 +11,6 @@ import errno import io -from logging import Logger from textwrap import dedent # Used since this can maintain comments @@ -89,6 +88,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) def configure( @@ -152,13 +152,11 @@ def configure( util.write_file(server_cfg, contents.getvalue(), mode=0o644) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # If there isn't a mcollective key in the configuration don't do anything if "mcollective" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'mcollective' key in configuration", name, ) diff --git a/cloudinit/config/cc_migrator.py b/cloudinit/config/cc_migrator.py index 956a9478..13a0f00a 100644 --- a/cloudinit/config/cc_migrator.py +++ b/cloudinit/config/cc_migrator.py @@ -6,9 +6,9 @@ """Migrator: Migrate old versions of cloud-init data to new""" +import logging import os import shutil -from logging import Logger from cloudinit import helpers, util from cloudinit.cloud import Cloud @@ -39,6 +39,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) def _migrate_canon_sems(cloud): @@ -59,7 +60,7 @@ def _migrate_canon_sems(cloud): return am_adjusted -def _migrate_legacy_sems(cloud, log): +def _migrate_legacy_sems(cloud): legacy_adjust = { "apt-update-upgrade": [ "apt-configure", @@ -82,25 +83,23 @@ def _migrate_legacy_sems(cloud, log): util.del_file(os.path.join(sem_path, p)) (_name, freq) = os.path.splitext(p) for m in migrate_to: - log.debug( + LOG.debug( "Migrating %s => %s with the same frequency", p, m ) with sem_helper.lock(m, freq): pass -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: do_migrate = util.get_cfg_option_str(cfg, "migrate", True) if not util.translate_bool(do_migrate): - log.debug("Skipping module named %s, migration disabled", name) + LOG.debug("Skipping module named %s, migration disabled", name) return sems_moved = _migrate_canon_sems(cloud) - log.debug( + LOG.debug( "Migrated %s semaphore files to there canonicalized names", sems_moved ) - _migrate_legacy_sems(cloud, log) + _migrate_legacy_sems(cloud) # vi: ts=4 expandtab diff --git a/cloudinit/config/cc_mounts.py b/cloudinit/config/cc_mounts.py index db7a7c26..cd26a4ee 100644 --- a/cloudinit/config/cc_mounts.py +++ b/cloudinit/config/cc_mounts.py @@ -12,7 +12,6 @@ import logging import math import os import re -from logging import Logger from string import whitespace from textwrap import dedent @@ -163,15 +162,15 @@ def _is_block_device(device_path, partition_path=None): return os.path.exists(sys_path) -def sanitize_devname(startname, transformer, log, aliases=None): - log.debug("Attempting to determine the real name of %s", startname) +def sanitize_devname(startname, transformer, aliases=None): + LOG.debug("Attempting to determine the real name of %s", startname) # workaround, allow user to specify 'ephemeral' # rather than more ec2 correct 'ephemeral0' devname = startname if devname == "ephemeral": devname = "ephemeral0" - log.debug("Adjusted mount option from ephemeral to ephemeral0") + LOG.debug("Adjusted mount option from ephemeral to ephemeral0") if is_network_device(startname): return startname @@ -182,7 +181,7 @@ def sanitize_devname(startname, transformer, log, aliases=None): if aliases: device_path = aliases.get(device_path, device_path) if orig != device_path: - log.debug("Mapped device alias %s to %s", orig, device_path) + LOG.debug("Mapped device alias %s to %s", orig, device_path) if is_meta_device_name(device_path): device_path = transformer(device_path) @@ -190,7 +189,7 @@ def sanitize_devname(startname, transformer, log, aliases=None): return None if not device_path.startswith("/"): device_path = "/dev/%s" % (device_path,) - log.debug("Mapped metadata name %s to %s", orig, device_path) + LOG.debug("Mapped metadata name %s to %s", orig, device_path) else: if DEVICE_NAME_RE.match(startname): device_path = "/dev/%s" % (device_path,) @@ -407,9 +406,7 @@ def handle_swapcfg(swapcfg): return None -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # fs_spec, fs_file, fs_vfstype, fs_mntops, fs-freq, fs_passno def_mnt_opts = "defaults,nobootwait" uses_systemd = cloud.distro.uses_systemd() @@ -455,7 +452,7 @@ def handle( for i in range(len(cfgmnt)): # skip something that wasn't a list if not isinstance(cfgmnt[i], list): - log.warning( + LOG.warning( "Mount option %s not a list, got a %s instead", (i + 1), type_utils.obj_name(cfgmnt[i]), @@ -464,16 +461,16 @@ def handle( start = str(cfgmnt[i][0]) sanitized = sanitize_devname( - start, cloud.device_name_to_device, log, aliases=device_aliases + start, cloud.device_name_to_device, aliases=device_aliases ) if sanitized != start: - log.debug("changed %s => %s" % (start, sanitized)) + LOG.debug("changed %s => %s", start, sanitized) if sanitized is None: - log.debug("Ignoring nonexistent named mount %s", start) + LOG.debug("Ignoring nonexistent named mount %s", start) continue elif sanitized in fstab_devs: - log.info( + LOG.info( "Device %s already defined in fstab: %s", sanitized, fstab_devs[sanitized], @@ -511,16 +508,16 @@ def handle( for defmnt in defmnts: start = defmnt[0] sanitized = sanitize_devname( - start, cloud.device_name_to_device, log, aliases=device_aliases + start, cloud.device_name_to_device, aliases=device_aliases ) if sanitized != start: - log.debug("changed default device %s => %s" % (start, sanitized)) + LOG.debug("changed default device %s => %s", start, sanitized) if sanitized is None: - log.debug("Ignoring nonexistent default named mount %s", start) + LOG.debug("Ignoring nonexistent default named mount %s", start) continue elif sanitized in fstab_devs: - log.debug( + LOG.debug( "Device %s already defined in fstab: %s", sanitized, fstab_devs[sanitized], @@ -536,7 +533,7 @@ def handle( break if cfgmnt_has: - log.debug("Not including %s, already previously included", start) + LOG.debug("Not including %s, already previously included", start) continue cfgmnt.append(defmnt) @@ -545,7 +542,7 @@ def handle( actlist = [] for x in cfgmnt: if x[1] is None: - log.debug("Skipping nonexistent device named %s", x[0]) + LOG.debug("Skipping nonexistent device named %s", x[0]) else: actlist.append(x) @@ -554,7 +551,7 @@ def handle( actlist.append([swapret, "none", "swap", "sw", "0", "0"]) if len(actlist) == 0: - log.debug("No modifications to fstab needed") + LOG.debug("No modifications to fstab needed") return cc_lines = [] @@ -577,7 +574,7 @@ def handle( try: util.ensure_dir(d) except Exception: - util.logexc(log, "Failed to make '%s' config-mount", d) + util.logexc(LOG, "Failed to make '%s' config-mount", d) # dirs is list of directories on which a volume should be mounted. # If any of them does not already show up in the list of current # mount points, we will definitely need to do mount -a. @@ -600,9 +597,9 @@ def handle( activate_cmds.append(["swapon", "-a"]) if len(sops) == 0: - log.debug("No changes to /etc/fstab made.") + LOG.debug("No changes to /etc/fstab made.") else: - log.debug("Changes to fstab: %s", sops) + LOG.debug("Changes to fstab: %s", sops) need_mount_all = True if need_mount_all: @@ -615,10 +612,10 @@ def handle( fmt = "Activate mounts: %s:" + " ".join(cmd) try: subp.subp(cmd) - log.debug(fmt, "PASS") + LOG.debug(fmt, "PASS") except subp.ProcessExecutionError: - log.warning(fmt, "FAIL") - util.logexc(log, fmt, "FAIL") + LOG.warning(fmt, "FAIL") + util.logexc(LOG, fmt, "FAIL") # vi: ts=4 expandtab diff --git a/cloudinit/config/cc_ntp.py b/cloudinit/config/cc_ntp.py index b5620f37..47659af7 100644 --- a/cloudinit/config/cc_ntp.py +++ b/cloudinit/config/cc_ntp.py @@ -8,7 +8,6 @@ import copy import os -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -548,9 +547,7 @@ def supplemental_schema_validation(ntp_config): ) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """Enable and configure ntp.""" if "ntp" not in cfg: LOG.debug( diff --git a/cloudinit/config/cc_package_update_upgrade_install.py b/cloudinit/config/cc_package_update_upgrade_install.py index 7d346a19..7935c33a 100644 --- a/cloudinit/config/cc_package_update_upgrade_install.py +++ b/cloudinit/config/cc_package_update_upgrade_install.py @@ -8,7 +8,6 @@ import os import time -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -60,6 +59,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) def _multi_cfg_bool_get(cfg, *keys): @@ -69,7 +69,7 @@ def _multi_cfg_bool_get(cfg, *keys): return False -def _fire_reboot(log, wait_attempts=6, initial_sleep=1, backoff=2): +def _fire_reboot(wait_attempts=6, initial_sleep=1, backoff=2): subp.subp(REBOOT_CMD) start = time.time() wait_time = initial_sleep @@ -77,7 +77,7 @@ def _fire_reboot(log, wait_attempts=6, initial_sleep=1, backoff=2): time.sleep(wait_time) wait_time *= backoff elapsed = time.time() - start - log.debug("Rebooted, but still running after %s seconds", int(elapsed)) + LOG.debug("Rebooted, but still running after %s seconds", int(elapsed)) # If we got here, not good elapsed = time.time() - start raise RuntimeError( @@ -85,9 +85,7 @@ def _fire_reboot(log, wait_attempts=6, initial_sleep=1, backoff=2): ) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # Handle the old style + new config names update = _multi_cfg_bool_get(cfg, "apt_update", "package_update") upgrade = _multi_cfg_bool_get(cfg, "package_upgrade", "apt_upgrade") @@ -101,21 +99,21 @@ def handle( try: cloud.distro.update_package_sources() except Exception as e: - util.logexc(log, "Package update failed") + util.logexc(LOG, "Package update failed") errors.append(e) if upgrade: try: cloud.distro.package_command("upgrade") except Exception as e: - util.logexc(log, "Package upgrade failed") + util.logexc(LOG, "Package upgrade failed") errors.append(e) if len(pkglist): try: cloud.distro.install_packages(pkglist) except Exception as e: - util.logexc(log, "Failed to install packages: %s", pkglist) + util.logexc(LOG, "Failed to install packages: %s", pkglist) errors.append(e) # TODO(smoser): handle this less violently @@ -125,18 +123,18 @@ def handle( reboot_fn_exists = os.path.isfile(REBOOT_FILE) if (upgrade or pkglist) and reboot_if_required and reboot_fn_exists: try: - log.warning( + LOG.warning( "Rebooting after upgrade or install per %s", REBOOT_FILE ) # Flush the above warning + anything else out... - logging.flushLoggers(log) - _fire_reboot(log) + logging.flushLoggers(LOG) + _fire_reboot() except Exception as e: - util.logexc(log, "Requested reboot did not happen!") + util.logexc(LOG, "Requested reboot did not happen!") errors.append(e) if len(errors): - log.warning( + LOG.warning( "%s failed with exceptions, re-raising the last one", len(errors) ) raise errors[-1] diff --git a/cloudinit/config/cc_phone_home.py b/cloudinit/config/cc_phone_home.py index 7bbee5af..a0589404 100644 --- a/cloudinit/config/cc_phone_home.py +++ b/cloudinit/config/cc_phone_home.py @@ -8,7 +8,7 @@ """Phone Home: Post data to url""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import templater, url_helper, util @@ -95,7 +95,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) - +LOG = logging.getLogger(__name__) # phone_home: # url: http://my.foo.bar/$INSTANCE/ # post: all @@ -108,14 +108,12 @@ __doc__ = get_meta_doc(meta) # -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if len(args) != 0: ph_cfg = util.read_conf(args[0]) else: if "phone_home" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, " "no 'phone_home' configuration found", name, @@ -124,7 +122,7 @@ def handle( ph_cfg = cfg["phone_home"] if "url" not in ph_cfg: - log.warning( + LOG.warning( "Skipping module named %s, " "no 'url' found in 'phone_home' configuration", name, @@ -139,7 +137,7 @@ def handle( except (ValueError, TypeError): tries = 10 util.logexc( - log, + LOG, "Configuration entry 'tries' is not an integer, using %s instead", tries, ) @@ -165,7 +163,7 @@ def handle( all_keys[n] = util.load_file(path) except Exception: util.logexc( - log, "%s: failed to open, can not phone home that data!", path + LOG, "%s: failed to open, can not phone home that data!", path ) submit_keys = {} @@ -174,7 +172,7 @@ def handle( submit_keys[k] = all_keys[k] else: submit_keys[k] = None - log.warning( + LOG.warning( "Requested key %s from 'post'" " configuration list not available", k, @@ -203,7 +201,7 @@ def handle( ) except Exception: util.logexc( - log, "Failed to post phone home data to %s in %s tries", url, tries + LOG, "Failed to post phone home data to %s in %s tries", url, tries ) diff --git a/cloudinit/config/cc_power_state_change.py b/cloudinit/config/cc_power_state_change.py index 1eb63d78..1c2df860 100644 --- a/cloudinit/config/cc_power_state_change.py +++ b/cloudinit/config/cc_power_state_change.py @@ -7,11 +7,11 @@ """Power State Change: Change power state""" import errno +import logging import os import re import subprocess import time -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -79,6 +79,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) def givecmdline(pid): @@ -99,10 +100,9 @@ def givecmdline(pid): return None -def check_condition(cond, log=None): +def check_condition(cond): if isinstance(cond, bool): - if log: - log.debug("Static Condition: %s" % cond) + LOG.debug("Static Condition: %s", cond) return cond pre = "check_condition command (%s): " % cond @@ -111,58 +111,49 @@ def check_condition(cond, log=None): proc.communicate() ret = proc.returncode if ret == 0: - if log: - log.debug(pre + "exited 0. condition met.") + LOG.debug("%sexited 0. condition met.", pre) return True elif ret == 1: - if log: - log.debug(pre + "exited 1. condition not met.") + LOG.debug("%sexited 1. condition not met.", pre) return False else: - if log: - log.warning( - pre + "unexpected exit %s. " % ret + "do not apply change." - ) + LOG.warning("%sunexpected exit %s. do not apply change.", pre, ret) return False except Exception as e: - if log: - log.warning(pre + "Unexpected error: %s" % e) + LOG.warning("%sUnexpected error: %s", pre, e) return False -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: try: (args, timeout, condition) = load_power_state(cfg, cloud.distro) if args is None: - log.debug("no power_state provided. doing nothing") + LOG.debug("no power_state provided. doing nothing") return except Exception as e: - log.warning("%s Not performing power state change!" % str(e)) + LOG.warning("%s Not performing power state change!", str(e)) return if condition is False: - log.debug("Condition was false. Will not perform state change.") + LOG.debug("Condition was false. Will not perform state change.") return mypid = os.getpid() cmdline = givecmdline(mypid) if not cmdline: - log.warning("power_state: failed to get cmdline of current process") + LOG.warning("power_state: failed to get cmdline of current process") return devnull_fp = open(os.devnull, "w") - log.debug("After pid %s ends, will execute: %s" % (mypid, " ".join(args))) + LOG.debug("After pid %s ends, will execute: %s", mypid, " ".join(args)) util.fork_cb( run_after_pid_gone, mypid, cmdline, timeout, - log, condition, execmd, [args, devnull_fp], @@ -227,7 +218,7 @@ def execmd(exe_args, output=None, data_in=None): doexit(ret) -def run_after_pid_gone(pid, pidcmdline, timeout, log, condition, func, args): +def run_after_pid_gone(pid, pidcmdline, timeout, condition, func, args): # wait until pid, with /proc/pid/cmdline contents of pidcmdline # is no longer alive. After it is gone, or timeout has passed # execute func(args) @@ -235,8 +226,7 @@ def run_after_pid_gone(pid, pidcmdline, timeout, log, condition, func, args): end_time = time.time() + timeout def fatal(msg): - if log: - log.warning(msg) + LOG.warning(msg) doexit(EXIT_FAIL) known_errnos = (errno.ENOENT, errno.ESRCH) @@ -267,11 +257,10 @@ def run_after_pid_gone(pid, pidcmdline, timeout, log, condition, func, args): if not msg: fatal("Unexpected error in run_after_pid_gone") - if log: - log.debug(msg) + LOG.debug(msg) try: - if not check_condition(condition, log): + if not check_condition(condition): return except Exception as e: fatal("Unexpected Exception when checking condition: %s" % e) diff --git a/cloudinit/config/cc_puppet.py b/cloudinit/config/cc_puppet.py index 38c2cc99..a3a2a4cf 100644 --- a/cloudinit/config/cc_puppet.py +++ b/cloudinit/config/cc_puppet.py @@ -8,10 +8,10 @@ """Puppet: Install, configure and start puppet""" +import logging import os import socket from io import StringIO -from logging import Logger from textwrap import dedent import yaml @@ -107,10 +107,15 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) + class PuppetConstants: def __init__( - self, puppet_conf_file, puppet_ssl_dir, csr_attributes_path, log + self, + puppet_conf_file, + puppet_ssl_dir, + csr_attributes_path, ): self.conf_path = puppet_conf_file self.ssl_dir = puppet_ssl_dir @@ -119,7 +124,7 @@ class PuppetConstants: self.csr_attributes_path = csr_attributes_path -def _manage_puppet_services(log, cloud: Cloud, action: str): +def _manage_puppet_services(cloud: Cloud, action: str): """Attempts to perform action on one of the puppet services""" service_managed: str = "" for puppet_name in PUPPET_PACKAGE_NAMES: @@ -130,7 +135,7 @@ def _manage_puppet_services(log, cloud: Cloud, action: str): except subp.ProcessExecutionError: pass if not service_managed: - log.warning( + LOG.warning( "Could not '%s' any of the following services: %s", action, ", ".join(PUPPET_PACKAGE_NAMES), @@ -182,12 +187,10 @@ def install_puppet_aio( return subp.subp([tmpf] + args, capture=False) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # If there isn't a puppet key in the configuration don't do anything if "puppet" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'puppet' configuration found", name ) return @@ -223,11 +226,11 @@ def handle( puppet_cfg, "package_name", puppet_package ) if not install and version: - log.warning( + LOG.warning( "Puppet install set to false but version supplied, doing nothing." ) elif install: - log.debug( + LOG.debug( "Attempting to install puppet %s from %s", version if version else "latest", install_type, @@ -243,7 +246,7 @@ def handle( except subp.ProcessExecutionError: pass if not package_name: - log.warning( + LOG.warning( "No installable puppet package in any of: %s", ", ".join(PUPPET_PACKAGE_NAMES), ) @@ -255,7 +258,7 @@ def handle( cloud.distro, aio_install_url, version, collection, cleanup ) else: - log.warning("Unknown puppet install type '%s'", install_type) + LOG.warning("Unknown puppet install type '%s'", install_type) run = False conf_file = util.get_cfg_option_str( @@ -270,7 +273,7 @@ def handle( get_config_value(puppet_bin, "csr_attributes"), ) - p_constants = PuppetConstants(conf_file, ssl_dir, csr_attributes_path, log) + p_constants = PuppetConstants(conf_file, ssl_dir, csr_attributes_path) # ... and then update the puppet configuration if "conf" in puppet_cfg: @@ -329,11 +332,11 @@ def handle( if start_puppetd: # Enables the services - _manage_puppet_services(log, cloud, "enable") + _manage_puppet_services(cloud, "enable") # Run the agent if needed if run: - log.debug("Running puppet-agent") + LOG.debug("Running puppet-agent") cmd = [puppet_bin, "agent"] if "exec_args" in puppet_cfg: cmd_args = puppet_cfg["exec_args"] @@ -342,7 +345,7 @@ def handle( elif isinstance(cmd_args, str): cmd.extend(cmd_args.split()) else: - log.warning( + LOG.warning( "Unknown type %s provided for puppet" " 'exec_args' expected list, tuple," " or string", @@ -355,7 +358,7 @@ def handle( if start_puppetd: # Start puppetd - _manage_puppet_services(log, cloud, "start") + _manage_puppet_services(cloud, "start") # vi: ts=4 expandtab diff --git a/cloudinit/config/cc_refresh_rmc_and_interface.py b/cloudinit/config/cc_refresh_rmc_and_interface.py index 87923f0b..28cf343d 100644 --- a/cloudinit/config/cc_refresh_rmc_and_interface.py +++ b/cloudinit/config/cc_refresh_rmc_and_interface.py @@ -8,7 +8,6 @@ Ensure Network Manager is not managing IPv6 interface""" import errno -from logging import Logger from cloudinit import log as logging from cloudinit import netinfo, subp, util @@ -57,9 +56,7 @@ LOG = logging.getLogger(__name__) RMCCTRL = "rmcctrl" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if not subp.which(RMCCTRL): LOG.debug("No '%s' in path, disabled", RMCCTRL) return diff --git a/cloudinit/config/cc_reset_rmc.py b/cloudinit/config/cc_reset_rmc.py index d687c482..10f4b7ea 100644 --- a/cloudinit/config/cc_reset_rmc.py +++ b/cloudinit/config/cc_reset_rmc.py @@ -7,7 +7,6 @@ import os -from logging import Logger from cloudinit import log as logging from cloudinit import subp, util @@ -65,9 +64,7 @@ LOG = logging.getLogger(__name__) NODE_ID_FILE = "/etc/ct_node_id" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # Ensuring node id has to be generated only once during first boot if cloud.datasource.platform_type == "none": LOG.debug("Skipping creation of new ct_node_id node") diff --git a/cloudinit/config/cc_resizefs.py b/cloudinit/config/cc_resizefs.py index 0e6197a2..1ef0f475 100644 --- a/cloudinit/config/cc_resizefs.py +++ b/cloudinit/config/cc_resizefs.py @@ -9,9 +9,9 @@ """Resizefs: cloud-config module which resizes the filesystem""" import errno +import logging import os import stat -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -50,6 +50,8 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) + def _resize_btrfs(mount_point, devpth): # If "/" is ro resize will fail. However it should be allowed since resize @@ -146,7 +148,7 @@ def can_skip_resize(fs_type, resize_what, devpth): return False -def maybe_get_writable_device_path(devpath, info, log): +def maybe_get_writable_device_path(devpath, info): """Return updated devpath if the devpath is a writable block device. @param devpath: Requested path to the root device we want to resize. @@ -166,25 +168,25 @@ def maybe_get_writable_device_path(devpath, info, log): ): devpath = util.rootdev_from_cmdline(util.get_cmdline()) if devpath is None: - log.warning("Unable to find device '/dev/root'") + LOG.warning("Unable to find device '/dev/root'") return None - log.debug("Converted /dev/root to '%s' per kernel cmdline", devpath) + LOG.debug("Converted /dev/root to '%s' per kernel cmdline", devpath) if devpath == "overlayroot": - log.debug("Not attempting to resize devpath '%s': %s", devpath, info) + LOG.debug("Not attempting to resize devpath '%s': %s", devpath, info) return None # FreeBSD zpool can also just use gpt/<label> # with that in mind we can not do an os.stat on "gpt/whatever" # therefore return the devpath already here. if devpath.startswith("gpt/"): - log.debug("We have a gpt label - just go ahead") + LOG.debug("We have a gpt label - just go ahead") return devpath # Alternatively, our device could simply be a name as returned by gpart, # such as da0p3 if not devpath.startswith("/dev/") and not os.path.exists(devpath): fulldevpath = "/dev/" + devpath.lstrip("/") - log.debug( + LOG.debug( "'%s' doesn't appear to be a valid device path. Trying '%s'", devpath, fulldevpath, @@ -195,13 +197,13 @@ def maybe_get_writable_device_path(devpath, info, log): statret = os.stat(devpath) except OSError as exc: if container and exc.errno == errno.ENOENT: - log.debug( + LOG.debug( "Device '%s' did not exist in container. cannot resize: %s", devpath, info, ) elif exc.errno == errno.ENOENT: - log.warning( + LOG.warning( "Device '%s' did not exist. cannot resize: %s", devpath, info ) else: @@ -210,35 +212,36 @@ def maybe_get_writable_device_path(devpath, info, log): if not stat.S_ISBLK(statret.st_mode) and not stat.S_ISCHR(statret.st_mode): if container: - log.debug( + LOG.debug( "device '%s' not a block device in container." - " cannot resize: %s" % (devpath, info) + " cannot resize: %s", + devpath, + info, ) else: - log.warning( - "device '%s' not a block device. cannot resize: %s" - % (devpath, info) + LOG.warning( + "device '%s' not a block device. cannot resize: %s", + devpath, + info, ) return None return devpath # The writable block devpath -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if len(args) != 0: resize_root = args[0] else: resize_root = util.get_cfg_option_str(cfg, "resize_rootfs", True) if not util.translate_bool(resize_root, addons=[NOBLOCK]): - log.debug("Skipping module named %s, resizing disabled", name) + LOG.debug("Skipping module named %s, resizing disabled", name) return # TODO(harlowja): allow what is to be resized to be configurable?? resize_what = "/" - result = util.get_mount_info(resize_what, log) + result = util.get_mount_info(resize_what, LOG) if not result: - log.warning("Could not determine filesystem type of %s", resize_what) + LOG.warning("Could not determine filesystem type of %s", resize_what) return (devpth, fs_type, mount_point) = result @@ -256,15 +259,15 @@ def handle( resize_what = zpool info = "dev=%s mnt_point=%s path=%s" % (devpth, mount_point, resize_what) - log.debug("resize_info: %s" % info) + LOG.debug("resize_info: %s", info) - devpth = maybe_get_writable_device_path(devpth, info, log) + devpth = maybe_get_writable_device_path(devpth, info) if not devpth: return # devpath was not a writable block device resizer = None if can_skip_resize(fs_type, resize_what, devpth): - log.debug( + LOG.debug( "Skip resize filesystem type %s for %s", fs_type, resize_what ) return @@ -276,7 +279,7 @@ def handle( break if not resizer: - log.warning( + LOG.warning( "Not resizing unknown filesystem type %s for %s", fs_type, resize_what, @@ -284,7 +287,7 @@ def handle( return resize_cmd = resizer(resize_what, devpth) - log.debug( + LOG.debug( "Resizing %s (%s) using %s", resize_what, fs_type, " ".join(resize_cmd) ) @@ -293,32 +296,32 @@ def handle( # the resize command util.fork_cb( util.log_time, - logfunc=log.debug, + logfunc=LOG.debug, msg="backgrounded Resizing", func=do_resize, - args=(resize_cmd, log), + args=(resize_cmd), ) else: util.log_time( - logfunc=log.debug, + logfunc=LOG.debug, msg="Resizing", func=do_resize, - args=(resize_cmd, log), + args=(resize_cmd), ) action = "Resized" if resize_root == NOBLOCK: action = "Resizing (via forking)" - log.debug( + LOG.debug( "%s root filesystem (type=%s, val=%s)", action, fs_type, resize_root ) -def do_resize(resize_cmd, log): +def do_resize(resize_cmd): try: subp.subp(resize_cmd) except subp.ProcessExecutionError: - util.logexc(log, "Failed to resize filesystem (cmd=%s)", resize_cmd) + util.logexc(LOG, "Failed to resize filesystem (cmd=%s)", resize_cmd) raise # TODO(harlowja): Should we add a fsck check after this to make # sure we didn't corrupt anything? diff --git a/cloudinit/config/cc_resolv_conf.py b/cloudinit/config/cc_resolv_conf.py index 4629ca7d..d8a7bfa0 100644 --- a/cloudinit/config/cc_resolv_conf.py +++ b/cloudinit/config/cc_resolv_conf.py @@ -8,7 +8,6 @@ """Resolv Conf: configure resolv.conf""" -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -122,9 +121,7 @@ def generate_resolv_conf(template_fn, params, target_fname): templater.render_to_file(template_fn, target_fname, params) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: """ Handler for resolv.conf @@ -135,7 +132,7 @@ def handle( @param args: Any module arguments from cloud.cfg """ if "manage_resolv_conf" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s," " no 'manage_resolv_conf' key in configuration", name, @@ -143,7 +140,7 @@ def handle( return if not util.get_cfg_option_bool(cfg, "manage_resolv_conf", False): - log.debug( + LOG.debug( "Skipping module named %s," " 'manage_resolv_conf' present but set to False", name, @@ -151,7 +148,7 @@ def handle( return if "resolv_conf" not in cfg: - log.warning("manage_resolv_conf True but no parameters provided!") + LOG.warning("manage_resolv_conf True but no parameters provided!") return try: @@ -159,7 +156,7 @@ def handle( RESOLVE_CONFIG_TEMPLATE_MAP[cloud.distro.resolve_conf_fn] ) except KeyError: - log.warning("No template found, not rendering resolve configs") + LOG.warning("No template found, not rendering resolve configs") return generate_resolv_conf( diff --git a/cloudinit/config/cc_rh_subscription.py b/cloudinit/config/cc_rh_subscription.py index ce88ec65..533862a9 100644 --- a/cloudinit/config/cc_rh_subscription.py +++ b/cloudinit/config/cc_rh_subscription.py @@ -5,7 +5,6 @@ # This file is part of cloud-init. See LICENSE file for license information. """Red Hat Subscription: Register Red Hat Enterprise Linux based system""" -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -80,12 +79,10 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: - sm = SubscriptionManager(cfg, log=log) +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: + sm = SubscriptionManager(cfg, log=LOG) if not sm.is_configured(): - log.debug("%s: module not configured.", name) + LOG.debug("%s: module not configured.", name) return None if not sm.is_registered(): diff --git a/cloudinit/config/cc_rightscale_userdata.py b/cloudinit/config/cc_rightscale_userdata.py index 9e84032a..729b30af 100644 --- a/cloudinit/config/cc_rightscale_userdata.py +++ b/cloudinit/config/cc_rightscale_userdata.py @@ -6,8 +6,8 @@ # # This file is part of cloud-init. See LICENSE file for license information. +import logging import os -from logging import Logger from urllib.parse import parse_qs from cloudinit import url_helper as uhelp @@ -51,6 +51,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) # # The purpose of this script is to allow cloud-init to consume @@ -70,19 +71,17 @@ __doc__ = get_meta_doc(meta) # -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: get_userdata_raw = getattr(cloud, "get_userdata_raw", None) if not get_userdata_raw or not callable(get_userdata_raw): - log.debug("Failed to get raw userdata in module %s", name) + LOG.debug("Failed to get raw userdata in module %s", name) return ud = get_userdata_raw() try: mdict = parse_qs(ud) if not mdict or MY_HOOKNAME not in mdict: - log.debug( + LOG.debug( "Skipping module %s, did not find %s in parsed raw userdata", name, MY_HOOKNAME, @@ -90,7 +89,7 @@ def handle( return except Exception: util.logexc( - log, "Failed to parse query string %s into a dictionary", ud + LOG, "Failed to parse query string %s into a dictionary", ud ) raise @@ -113,18 +112,18 @@ def handle( except Exception as e: captured_excps.append(e) util.logexc( - log, "%s failed to read %s and write %s", MY_NAME, url, fname + LOG, "%s failed to read %s and write %s", MY_NAME, url, fname ) if wrote_fns: - log.debug("Wrote out rightscale userdata to %s files", len(wrote_fns)) + LOG.debug("Wrote out rightscale userdata to %s files", len(wrote_fns)) if len(wrote_fns) != len(urls): skipped = len(urls) - len(wrote_fns) - log.debug("%s urls were skipped or failed", skipped) + LOG.debug("%s urls were skipped or failed", skipped) if captured_excps: - log.warning( + LOG.warning( "%s failed with exceptions, re-raising the last one", len(captured_excps), ) diff --git a/cloudinit/config/cc_rsyslog.py b/cloudinit/config/cc_rsyslog.py index 47ade927..9ecefa05 100644 --- a/cloudinit/config/cc_rsyslog.py +++ b/cloudinit/config/cc_rsyslog.py @@ -10,7 +10,6 @@ import os import re -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -297,11 +296,9 @@ def remotes_to_rsyslog_cfg(remotes, header=None, footer=None): return "\n".join(lines) + "\n" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "rsyslog" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'rsyslog' key in configuration", name ) return @@ -319,7 +316,7 @@ def handle( ) if not mycfg["configs"]: - log.debug("Empty config rsyslog['configs'], nothing to do") + LOG.debug("Empty config rsyslog['configs'], nothing to do") return changes = apply_rsyslog_changes( @@ -329,14 +326,14 @@ def handle( ) if not changes: - log.debug("restart of syslog not necessary, no changes made") + LOG.debug("restart of syslog not necessary, no changes made") return try: restarted = reload_syslog(cloud.distro, command=mycfg[KEYNAME_RELOAD]) except subp.ProcessExecutionError as e: restarted = False - log.warning("Failed to reload syslog", e) + LOG.warning("Failed to reload syslog %s", str(e)) if restarted: # This only needs to run if we *actually* restarted @@ -344,7 +341,7 @@ def handle( cloud.cycle_logging() # This should now use rsyslog if # the logging was setup to use it... - log.debug("%s configured %s files", name, changes) + LOG.debug("%s configured %s files", name, changes) # vi: ts=4 expandtab syntax=python diff --git a/cloudinit/config/cc_runcmd.py b/cloudinit/config/cc_runcmd.py index 27c0429b..aacbfd12 100644 --- a/cloudinit/config/cc_runcmd.py +++ b/cloudinit/config/cc_runcmd.py @@ -8,8 +8,8 @@ """Runcmd: run arbitrary commands at rc.local with output to the console""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import util @@ -75,12 +75,12 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: + +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "runcmd" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'runcmd' key in configuration", name ) return diff --git a/cloudinit/config/cc_salt_minion.py b/cloudinit/config/cc_salt_minion.py index f3a8c16c..27fddbbc 100644 --- a/cloudinit/config/cc_salt_minion.py +++ b/cloudinit/config/cc_salt_minion.py @@ -4,8 +4,8 @@ """Salt Minion: Setup and run salt minion""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import safeyaml, subp, util @@ -65,6 +65,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) # Note: see https://docs.saltstack.com/en/latest/topics/installation/ # Note: see https://docs.saltstack.com/en/latest/ref/configuration/ @@ -98,12 +99,10 @@ class SaltConstants: ) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # If there isn't a salt key in the configuration don't do anything if "salt_minion" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'salt_minion' key in configuration", name, ) diff --git a/cloudinit/config/cc_scripts_per_boot.py b/cloudinit/config/cc_scripts_per_boot.py index 3e093d0e..30bd69d9 100644 --- a/cloudinit/config/cc_scripts_per_boot.py +++ b/cloudinit/config/cc_scripts_per_boot.py @@ -7,8 +7,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Scripts Per Boot: Run per boot scripts""" +import logging import os -from logging import Logger from cloudinit import subp from cloudinit.cloud import Cloud @@ -37,20 +37,19 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) SCRIPT_SUBDIR = "per-boot" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # Comes from the following: # https://forums.aws.amazon.com/thread.jspa?threadID=96918 runparts_path = os.path.join(cloud.get_cpath(), "scripts", SCRIPT_SUBDIR) try: subp.runparts(runparts_path) except Exception: - log.warning( + LOG.warning( "Failed to run module %s (%s in %s)", name, SCRIPT_SUBDIR, diff --git a/cloudinit/config/cc_scripts_per_instance.py b/cloudinit/config/cc_scripts_per_instance.py index 719b8a2a..583a0671 100644 --- a/cloudinit/config/cc_scripts_per_instance.py +++ b/cloudinit/config/cc_scripts_per_instance.py @@ -7,8 +7,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Scripts Per Instance: Run per instance scripts""" +import logging import os -from logging import Logger from cloudinit import subp from cloudinit.cloud import Cloud @@ -38,21 +38,19 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) - +LOG = logging.getLogger(__name__) SCRIPT_SUBDIR = "per-instance" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # Comes from the following: # https://forums.aws.amazon.com/thread.jspa?threadID=96918 runparts_path = os.path.join(cloud.get_cpath(), "scripts", SCRIPT_SUBDIR) try: subp.runparts(runparts_path) except Exception: - log.warning( + LOG.warning( "Failed to run module %s (%s in %s)", name, SCRIPT_SUBDIR, diff --git a/cloudinit/config/cc_scripts_per_once.py b/cloudinit/config/cc_scripts_per_once.py index 42aa89b3..9a6b86fa 100644 --- a/cloudinit/config/cc_scripts_per_once.py +++ b/cloudinit/config/cc_scripts_per_once.py @@ -7,8 +7,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Scripts Per Once: Run one time scripts""" +import logging import os -from logging import Logger from cloudinit import subp from cloudinit.cloud import Cloud @@ -37,20 +37,19 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) SCRIPT_SUBDIR = "per-once" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # Comes from the following: # https://forums.aws.amazon.com/thread.jspa?threadID=96918 runparts_path = os.path.join(cloud.get_cpath(), "scripts", SCRIPT_SUBDIR) try: subp.runparts(runparts_path) except Exception: - log.warning( + LOG.warning( "Failed to run module %s (%s in %s)", name, SCRIPT_SUBDIR, diff --git a/cloudinit/config/cc_scripts_user.py b/cloudinit/config/cc_scripts_user.py index b6ae37f5..aaef65fe 100644 --- a/cloudinit/config/cc_scripts_user.py +++ b/cloudinit/config/cc_scripts_user.py @@ -7,8 +7,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Scripts User: Run user scripts""" +import logging import os -from logging import Logger from cloudinit import subp from cloudinit.cloud import Cloud @@ -38,14 +38,13 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) SCRIPT_SUBDIR = "scripts" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # This is written to by the user data handlers # Ie, any custom shell scripts that come down # go here... @@ -53,7 +52,7 @@ def handle( try: subp.runparts(runparts_path) except Exception: - log.warning( + LOG.warning( "Failed to run module %s (%s in %s)", name, SCRIPT_SUBDIR, diff --git a/cloudinit/config/cc_scripts_vendor.py b/cloudinit/config/cc_scripts_vendor.py index b3ee9df1..674a3ded 100644 --- a/cloudinit/config/cc_scripts_vendor.py +++ b/cloudinit/config/cc_scripts_vendor.py @@ -5,8 +5,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Scripts Vendor: Run vendor scripts""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -59,14 +59,13 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) SCRIPT_SUBDIR = "vendor" -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # This is written to by the vendor data handlers # any vendor data shell scripts get placed in runparts_path runparts_path = os.path.join( @@ -78,7 +77,7 @@ def handle( try: subp.runparts(runparts_path, exe_prefix=prefix) except Exception: - log.warning( + LOG.warning( "Failed to run module %s (%s in %s)", name, SCRIPT_SUBDIR, diff --git a/cloudinit/config/cc_seed_random.py b/cloudinit/config/cc_seed_random.py index 1c1b81d8..0abf7957 100644 --- a/cloudinit/config/cc_seed_random.py +++ b/cloudinit/config/cc_seed_random.py @@ -11,7 +11,6 @@ import base64 import os from io import BytesIO -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -110,9 +109,7 @@ def handle_random_seed_command(command, required, env=None): subp.subp(command, env=env, capture=False) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: mycfg = cfg.get("random_seed", {}) seed_path = mycfg.get("file", "/dev/urandom") seed_data = mycfg.get("data", b"") @@ -129,7 +126,7 @@ def handle( seed_data = seed_buf.getvalue() if len(seed_data): - log.debug( + LOG.debug( "%s: adding %s bytes of random seed entropy to %s", name, len(seed_data), @@ -144,7 +141,7 @@ def handle( env["RANDOM_SEED_FILE"] = seed_path handle_random_seed_command(command=command, required=req, env=env) except ValueError as e: - log.warning("handling random command [%s] failed: %s", command, e) + LOG.warning("handling random command [%s] failed: %s", command, e) raise e diff --git a/cloudinit/config/cc_set_hostname.py b/cloudinit/config/cc_set_hostname.py index fa5c023c..106d0851 100644 --- a/cloudinit/config/cc_set_hostname.py +++ b/cloudinit/config/cc_set_hostname.py @@ -7,8 +7,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """Set Hostname: Set hostname and FQDN""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import util @@ -69,6 +69,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) class SetHostnameError(Exception): @@ -79,11 +80,9 @@ class SetHostnameError(Exception): """ -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if util.get_cfg_option_bool(cfg, "preserve_hostname", False): - log.debug( + LOG.debug( "Configuration option 'preserve_hostname' is set," " not setting the hostname in module %s", name, @@ -113,18 +112,18 @@ def handle( "hostname" ) or fqdn != prev_hostname.get("fqdn") if not hostname_changed: - log.debug("No hostname changes. Skipping set-hostname") + LOG.debug("No hostname changes. Skipping set-hostname") return if is_default and hostname == "localhost": # https://github.com/systemd/systemd/commit/d39079fcaa05e23540d2b1f0270fa31c22a7e9f1 - log.debug("Hostname is localhost. Let other services handle this.") + LOG.debug("Hostname is localhost. Let other services handle this.") return - log.debug("Setting the hostname to %s (%s)", fqdn, hostname) + LOG.debug("Setting the hostname to %s (%s)", fqdn, hostname) try: cloud.distro.set_hostname(hostname, fqdn) except Exception as e: msg = "Failed to set the hostname to %s (%s)" % (fqdn, hostname) - util.logexc(log, msg) + util.logexc(LOG, msg) raise SetHostnameError("%s: %s" % (msg, e)) from e write_json(prev_fn, {"hostname": hostname, "fqdn": fqdn}) diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py index bd27d533..d3707bfe 100644 --- a/cloudinit/config/cc_set_passwords.py +++ b/cloudinit/config/cc_set_passwords.py @@ -8,7 +8,6 @@ """Set Passwords: Set user passwords and enable/disable SSH password auth""" import re -from logging import Logger from string import ascii_letters, digits from textwrap import dedent from typing import List @@ -172,9 +171,7 @@ def handle_ssh_pwauth(pw_auth, distro: Distro): _restart_ssh_daemon(distro, service) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: distro: Distro = cloud.distro if args: # if run from command line, and give args, wipe the chpasswd['list'] @@ -198,7 +195,7 @@ def handle( extra_message="Use 'users' instead.", ) if isinstance(chfg["list"], list): - log.debug("Handling input for chpasswd as list.") + LOG.debug("Handling input for chpasswd as list.") plist = util.get_cfg_option_list(chfg, "list", plist) else: util.deprecate( @@ -206,7 +203,7 @@ def handle( deprecated_version="22.2", extra_message="Use string type instead.", ) - log.debug("Handling input for chpasswd as multiline string.") + LOG.debug("Handling input for chpasswd as multiline string.") multiline = util.get_cfg_option_str(chfg, "list") if multiline: plist = multiline.splitlines() @@ -219,7 +216,7 @@ def handle( if user: plist = ["%s:%s" % (user, password)] else: - log.warning("No default or defined user to change password for.") + LOG.warning("No default or defined user to change password for.") errors = [] if plist or users_list: @@ -259,22 +256,22 @@ def handle( users.append(u) if users: try: - log.debug("Changing password for %s:", users) + LOG.debug("Changing password for %s:", users) distro.chpasswd(plist_in, hashed=False) except Exception as e: errors.append(e) util.logexc( - log, "Failed to set passwords with chpasswd for %s", users + LOG, "Failed to set passwords with chpasswd for %s", users ) if hashed_users: try: - log.debug("Setting hashed password for %s:", hashed_users) + LOG.debug("Setting hashed password for %s:", hashed_users) distro.chpasswd(hashed_plist_in, hashed=True) except Exception as e: errors.append(e) util.logexc( - log, + LOG, "Failed to set hashed passwords with chpasswd for %s", hashed_users, ) @@ -299,14 +296,14 @@ def handle( expired_users.append(u) except Exception as e: errors.append(e) - util.logexc(log, "Failed to set 'expire' for %s", u) + util.logexc(LOG, "Failed to set 'expire' for %s", u) if expired_users: - log.debug("Expired passwords for: %s users", expired_users) + LOG.debug("Expired passwords for: %s users", expired_users) handle_ssh_pwauth(cfg.get("ssh_pwauth"), distro) if len(errors): - log.debug("%s errors occurred, re-raising the last one", len(errors)) + LOG.debug("%s errors occurred, re-raising the last one", len(errors)) raise errors[-1] diff --git a/cloudinit/config/cc_snap.py b/cloudinit/config/cc_snap.py index 3bf25f1e..841dc06b 100644 --- a/cloudinit/config/cc_snap.py +++ b/cloudinit/config/cc_snap.py @@ -6,7 +6,6 @@ import os import sys -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -186,9 +185,7 @@ def run_commands(commands): raise RuntimeError(msg) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: cfgin = cfg.get("snap", {}) if not cfgin: LOG.debug( diff --git a/cloudinit/config/cc_spacewalk.py b/cloudinit/config/cc_spacewalk.py index add40c1c..c8249e61 100644 --- a/cloudinit/config/cc_spacewalk.py +++ b/cloudinit/config/cc_spacewalk.py @@ -1,7 +1,7 @@ # This file is part of cloud-init. See LICENSE file for license information. """Spacewalk: Install and configure spacewalk""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import subp @@ -41,6 +41,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) distros = ["redhat", "fedora"] @@ -67,15 +68,13 @@ def do_register( profile_name, ca_cert_path=def_ca_cert_path, proxy=None, - log=None, activation_key=None, ): - if log is not None: - log.info( - "Registering using `rhnreg_ks` profile '%s' into server '%s'", - profile_name, - server, - ) + LOG.info( + "Registering using `rhnreg_ks` profile '%s' into server '%s'", + profile_name, + server, + ) cmd = ["rhnreg_ks"] cmd.extend(["--serverUrl", "https://%s/XMLRPC" % server]) cmd.extend(["--profilename", str(profile_name)]) @@ -88,11 +87,9 @@ def do_register( subp.subp(cmd, capture=False) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "spacewalk" not in cfg: - log.debug( + LOG.debug( "Skipping module named %s, no 'spacewalk' key in configuration", name, ) @@ -107,11 +104,10 @@ def handle( spacewalk_server, cloud.datasource.get_hostname(fqdn=True).hostname, proxy=cfg.get("proxy"), - log=log, activation_key=cfg.get("activation_key"), ) else: - log.debug( + LOG.debug( "Skipping module named %s, 'spacewalk/server' key" " was not found in configuration", name, diff --git a/cloudinit/config/cc_ssh.py b/cloudinit/config/cc_ssh.py index 1ec889f3..57129776 100644 --- a/cloudinit/config/cc_ssh.py +++ b/cloudinit/config/cc_ssh.py @@ -8,10 +8,10 @@ """SSH: Configure SSH and SSH keys""" import glob +import logging import os import re import sys -from logging import Logger from textwrap import dedent from typing import List, Optional, Sequence @@ -170,6 +170,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) GENERATE_KEY_NAMES = ["rsa", "dsa", "ecdsa", "ed25519"] pattern_unsupported_config_keys = re.compile( @@ -196,9 +197,7 @@ for k in GENERATE_KEY_NAMES: KEY_GEN_TPL = 'o=$(ssh-keygen -yf "%s") && echo "$o" root@localhost > "%s"' -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # remove the static keys from the pristine image if cfg.get("ssh_deletekeys", True): @@ -207,7 +206,7 @@ def handle( try: util.del_file(f) except Exception: - util.logexc(log, "Failed deleting key file %s", f) + util.logexc(LOG, "Failed deleting key file %s", f) if "ssh_keys" in cfg: # if there are keys and/or certificates in cloud-config, use them @@ -218,7 +217,7 @@ def handle( reason = "unsupported" else: reason = "unrecognized" - log.warning('Skipping %s ssh_keys entry: "%s"', reason, key) + LOG.warning('Skipping %s ssh_keys entry: "%s"', reason, key) continue tgt_fn = CONFIG_KEY_TO_FILE[key][0] tgt_perms = CONFIG_KEY_TO_FILE[key][1] @@ -245,12 +244,12 @@ def handle( # TODO(harlowja): Is this guard needed? with util.SeLinuxGuard("/etc/ssh", recursive=True): subp.subp(cmd, capture=False) - log.debug( - f"Generated a key for {public_file} from {private_file}" + LOG.debug( + "Generated a key for %s from %s", public_file, private_file ) except Exception: util.logexc( - log, + LOG, "Failed generating a key for " f"{public_file} from {private_file}", ) @@ -288,10 +287,10 @@ def handle( if e.exit_code == 1 and err.lower().startswith( "unknown key" ): - log.debug("ssh-keygen: unknown key type '%s'", keytype) + LOG.debug("ssh-keygen: unknown key type '%s'", keytype) else: util.logexc( - log, + LOG, "Failed generating key type %s to file %s", keytype, keyfile, @@ -315,7 +314,7 @@ def handle( try: cloud.datasource.publish_host_keys(hostkeys) except Exception: - util.logexc(log, "Publishing host keys failed!") + util.logexc(LOG, "Publishing host keys failed!") try: (users, _groups) = ug_util.normalize_users_groups(cfg, cloud.distro) @@ -329,7 +328,7 @@ def handle( if util.get_cfg_option_bool(cfg, "allow_public_ssh_keys", True): keys = cloud.get_public_ssh_keys() or [] else: - log.debug( + LOG.debug( "Skipping import of publish SSH keys per " "config setting: allow_public_ssh_keys=False" ) @@ -340,7 +339,7 @@ def handle( apply_credentials(keys, user, disable_root, disable_root_opts) except Exception: - util.logexc(log, "Applying SSH credentials failed!") + util.logexc(LOG, "Applying SSH credentials failed!") def apply_credentials(keys, user, disable_root, disable_root_opts): diff --git a/cloudinit/config/cc_ssh_authkey_fingerprints.py b/cloudinit/config/cc_ssh_authkey_fingerprints.py index 4b4c3d60..0134b15a 100644 --- a/cloudinit/config/cc_ssh_authkey_fingerprints.py +++ b/cloudinit/config/cc_ssh_authkey_fingerprints.py @@ -7,7 +7,7 @@ import base64 import hashlib -from logging import Logger +import logging from cloudinit import ssh_util, util from cloudinit.cloud import Cloud @@ -38,6 +38,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) def _split_hash(bin_hash): @@ -115,11 +116,9 @@ def _pprint_key_entries( ) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if util.is_true(cfg.get("no_ssh_fingerprints", False)): - log.debug( + LOG.debug( "Skipping module named %s, logging of SSH fingerprints disabled", name, ) @@ -129,7 +128,7 @@ def handle( (users, _groups) = ug_util.normalize_users_groups(cfg, cloud.distro) for (user_name, _cfg) in users.items(): if _cfg.get("no_create_home") or _cfg.get("system"): - log.debug( + LOG.debug( "Skipping printing of ssh fingerprints for user '%s' because " "no home directory is created", user_name, diff --git a/cloudinit/config/cc_ssh_import_id.py b/cloudinit/config/cc_ssh_import_id.py index ed5ac492..6c9a28e4 100644 --- a/cloudinit/config/cc_ssh_import_id.py +++ b/cloudinit/config/cc_ssh_import_id.py @@ -7,8 +7,8 @@ # This file is part of cloud-init. See LICENSE file for license information. """SSH Import ID: Import SSH id""" +import logging import pwd -from logging import Logger from textwrap import dedent from cloudinit import subp, util @@ -50,20 +50,19 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if not is_key_in_nested_dict(cfg, "ssh_import_id"): - log.debug( + LOG.debug( "Skipping module named ssh-import-id, no 'ssh_import_id'" " directives found." ) return elif not subp.which(SSH_IMPORT_ID_BINARY): - log.warning( + LOG.warning( "ssh-import-id is not installed, but module ssh_import_id is " "configured. Skipping module." ) @@ -76,7 +75,7 @@ def handle( if len(args) > 1: ids = args[1:] - import_ssh_ids(ids, user, log) + import_ssh_ids(ids, user) return # import for cloudinit created users @@ -90,14 +89,14 @@ def handle( try: import_ids = user_cfg["ssh_import_id"] except Exception: - log.debug("User %s is not configured for ssh_import_id", user) + LOG.debug("User %s is not configured for ssh_import_id", user) continue try: import_ids = util.uniq_merge(import_ids) import_ids = [str(i) for i in import_ids] except Exception: - log.debug( + LOG.debug( "User %s is not correctly configured for ssh_import_id", user ) continue @@ -106,10 +105,10 @@ def handle( continue try: - import_ssh_ids(import_ids, user, log) + import_ssh_ids(import_ids, user) except Exception as exc: util.logexc( - log, "ssh-import-id failed for: %s %s", user, import_ids + LOG, "ssh-import-id failed for: %s %s", user, import_ids ) elist.append(exc) @@ -117,10 +116,10 @@ def handle( raise elist[0] -def import_ssh_ids(ids, user, log): +def import_ssh_ids(ids, user): if not (user and ids): - log.debug("empty user(%s) or ids(%s). not importing", user, ids) + LOG.debug("empty user(%s) or ids(%s). not importing", user, ids) return try: @@ -160,12 +159,12 @@ def import_ssh_ids(ids, user, log): user, SSH_IMPORT_ID_BINARY, ] + ids - log.debug("Importing SSH ids for user %s.", user) + LOG.debug("Importing SSH ids for user %s.", user) try: subp.subp(cmd, capture=False) except subp.ProcessExecutionError as exc: - util.logexc(log, "Failed to run command to import %s SSH ids", user) + util.logexc(LOG, "Failed to run command to import %s SSH ids", user) raise exc diff --git a/cloudinit/config/cc_timezone.py b/cloudinit/config/cc_timezone.py index 7436adf3..726bad73 100644 --- a/cloudinit/config/cc_timezone.py +++ b/cloudinit/config/cc_timezone.py @@ -7,7 +7,7 @@ # This file is part of cloud-init. See LICENSE file for license information. """Timezone: Set the system timezone""" -from logging import Logger +import logging from cloudinit import util from cloudinit.cloud import Cloud @@ -34,18 +34,17 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if len(args) != 0: timezone = args[0] else: timezone = util.get_cfg_option_str(cfg, "timezone", False) if not timezone: - log.debug("Skipping module named %s, no 'timezone' specified", name) + LOG.debug("Skipping module named %s, no 'timezone' specified", name) return # Let the distro handle settings its timezone diff --git a/cloudinit/config/cc_ubuntu_advantage.py b/cloudinit/config/cc_ubuntu_advantage.py index e4840a73..b85db6a7 100644 --- a/cloudinit/config/cc_ubuntu_advantage.py +++ b/cloudinit/config/cc_ubuntu_advantage.py @@ -4,7 +4,6 @@ import json import re -from logging import Logger from textwrap import dedent from typing import Any, List from urllib.parse import urlparse @@ -464,9 +463,7 @@ def _auto_attach(ua_section: dict): raise RuntimeError(msg) from ex -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: ua_section = None if "ubuntu-advantage" in cfg: LOG.warning( diff --git a/cloudinit/config/cc_ubuntu_autoinstall.py b/cloudinit/config/cc_ubuntu_autoinstall.py index 3870cf59..0eab281b 100644 --- a/cloudinit/config/cc_ubuntu_autoinstall.py +++ b/cloudinit/config/cc_ubuntu_autoinstall.py @@ -3,7 +3,6 @@ """Autoinstall: Support ubuntu live-server autoinstall syntax.""" import re -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -75,9 +74,7 @@ __doc__ = get_meta_doc(meta) LIVE_INSTALLER_SNAPS = ("subiquity", "ubuntu-desktop-installer") -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "autoinstall" not in cfg: LOG.debug( diff --git a/cloudinit/config/cc_ubuntu_drivers.py b/cloudinit/config/cc_ubuntu_drivers.py index 59347e25..fb340e79 100644 --- a/cloudinit/config/cc_ubuntu_drivers.py +++ b/cloudinit/config/cc_ubuntu_drivers.py @@ -16,7 +16,6 @@ except ImportError: debconf = None HAS_DEBCONF = False -from logging import Logger from cloudinit import log as logging from cloudinit import subp, temp_utils, type_utils, util @@ -140,14 +139,12 @@ def install_drivers(cfg, pkg_install_func, distro: Distro): raise -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if "drivers" not in cfg: - log.debug("Skipping module named %s, no 'drivers' key in config", name) + LOG.debug("Skipping module named %s, no 'drivers' key in config", name) return if not HAS_DEBCONF: - log.warning( + LOG.warning( "Skipping module named %s, 'python3-debconf' is not installed", name, ) diff --git a/cloudinit/config/cc_update_etc_hosts.py b/cloudinit/config/cc_update_etc_hosts.py index 695bc019..c3e5b71e 100644 --- a/cloudinit/config/cc_update_etc_hosts.py +++ b/cloudinit/config/cc_update_etc_hosts.py @@ -8,7 +8,7 @@ """Update Etc Hosts: Update the hosts file (usually ``/etc/hosts``)""" -from logging import Logger +import logging from textwrap import dedent from cloudinit import templater, util @@ -95,11 +95,10 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: manage_hosts = util.get_cfg_option_str(cfg, "manage_etc_hosts", False) hosts_fn = cloud.distro.hosts_fn @@ -113,7 +112,7 @@ def handle( ) (hostname, fqdn, _) = util.get_hostname_fqdn(cfg, cloud) if not hostname: - log.warning( + LOG.warning( "Option 'manage_etc_hosts' was set, but no hostname was found" ) return @@ -135,15 +134,15 @@ def handle( elif manage_hosts == "localhost": (hostname, fqdn, _) = util.get_hostname_fqdn(cfg, cloud) if not hostname: - log.warning( + LOG.warning( "Option 'manage_etc_hosts' was set, but no hostname was found" ) return - log.debug("Managing localhost in %s", hosts_fn) + LOG.debug("Managing localhost in %s", hosts_fn) cloud.distro.update_etc_hosts(hostname, fqdn) else: - log.debug( + LOG.debug( "Configuration option 'manage_etc_hosts' is not set," " not managing %s in module %s", hosts_fn, diff --git a/cloudinit/config/cc_update_hostname.py b/cloudinit/config/cc_update_hostname.py index 8a99297f..8de51147 100644 --- a/cloudinit/config/cc_update_hostname.py +++ b/cloudinit/config/cc_update_hostname.py @@ -8,8 +8,8 @@ """Update Hostname: Update hostname and fqdn""" +import logging import os -from logging import Logger from textwrap import dedent from cloudinit import util @@ -80,13 +80,12 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if util.get_cfg_option_bool(cfg, "preserve_hostname", False): - log.debug( + LOG.debug( "Configuration option 'preserve_hostname' is set," " not updating the hostname in module %s", name, @@ -103,16 +102,16 @@ def handle( (hostname, fqdn, is_default) = util.get_hostname_fqdn(cfg, cloud) if is_default and hostname == "localhost": # https://github.com/systemd/systemd/commit/d39079fcaa05e23540d2b1f0270fa31c22a7e9f1 - log.debug("Hostname is localhost. Let other services handle this.") + LOG.debug("Hostname is localhost. Let other services handle this.") return try: prev_fn = os.path.join(cloud.get_cpath("data"), "previous-hostname") - log.debug("Updating hostname to %s (%s)", fqdn, hostname) + LOG.debug("Updating hostname to %s (%s)", fqdn, hostname) cloud.distro.update_hostname(hostname, fqdn, prev_fn) except Exception: util.logexc( - log, "Failed to update the hostname to %s (%s)", fqdn, hostname + LOG, "Failed to update the hostname to %s (%s)", fqdn, hostname ) raise diff --git a/cloudinit/config/cc_users_groups.py b/cloudinit/config/cc_users_groups.py index c654270e..52f0b844 100644 --- a/cloudinit/config/cc_users_groups.py +++ b/cloudinit/config/cc_users_groups.py @@ -6,7 +6,6 @@ "Users and Groups: Configure users and groups" -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -161,9 +160,7 @@ NO_HOME = ("no_create_home", "system") NEED_HOME = ("ssh_authorized_keys", "ssh_import_id", "ssh_redirect_user") -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: (users, groups) = ug_util.normalize_users_groups(cfg, cloud.distro) (default_user, _user_config) = ug_util.extract_default(users) cloud_keys = cloud.get_public_ssh_keys() or [] diff --git a/cloudinit/config/cc_wireguard.py b/cloudinit/config/cc_wireguard.py index 732440f0..1e8ad890 100644 --- a/cloudinit/config/cc_wireguard.py +++ b/cloudinit/config/cc_wireguard.py @@ -4,7 +4,6 @@ """Wireguard""" import re -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -258,9 +257,7 @@ def load_wireguard_kernel_module(): raise -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: wg_section = None if "wireguard" in cfg: diff --git a/cloudinit/config/cc_write_files.py b/cloudinit/config/cc_write_files.py index a517d044..f928dcc8 100644 --- a/cloudinit/config/cc_write_files.py +++ b/cloudinit/config/cc_write_files.py @@ -8,7 +8,6 @@ import base64 import os -from logging import Logger from textwrap import dedent from cloudinit import log as logging @@ -119,9 +118,7 @@ meta: MetaSchema = { __doc__ = get_meta_doc(meta) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: file_list = cfg.get("write_files", []) filtered_files = [ f @@ -129,7 +126,7 @@ def handle( if not util.get_cfg_option_bool(f, "defer", DEFAULT_DEFER) ] if not filtered_files: - log.debug( + LOG.debug( "Skipping module named %s," " no/empty 'write_files' key in configuration", name, diff --git a/cloudinit/config/cc_write_files_deferred.py b/cloudinit/config/cc_write_files_deferred.py index a196ffb9..3c90f5c3 100644 --- a/cloudinit/config/cc_write_files_deferred.py +++ b/cloudinit/config/cc_write_files_deferred.py @@ -4,7 +4,7 @@ """Write Files Deferred: Defer writing certain files""" -from logging import Logger +import logging from cloudinit import util from cloudinit.cloud import Cloud @@ -36,11 +36,10 @@ meta: MetaSchema = { # This module is undocumented in our schema docs __doc__ = "" +LOG = logging.getLogger(__name__) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: file_list = cfg.get("write_files", []) filtered_files = [ f @@ -48,7 +47,7 @@ def handle( if util.get_cfg_option_bool(f, "defer", DEFAULT_DEFER) ] if not filtered_files: - log.debug( + LOG.debug( "Skipping module named %s," " no deferred file defined in configuration", name, diff --git a/cloudinit/config/cc_yum_add_repo.py b/cloudinit/config/cc_yum_add_repo.py index 3087b22c..097493ba 100644 --- a/cloudinit/config/cc_yum_add_repo.py +++ b/cloudinit/config/cc_yum_add_repo.py @@ -7,9 +7,9 @@ "Yum Add Repo: Add yum repository configuration to the system" import io +import logging import os from configparser import ConfigParser -from logging import Logger from textwrap import dedent from cloudinit import util @@ -123,6 +123,7 @@ meta: MetaSchema = { } __doc__ = get_meta_doc(meta) +LOG = logging.getLogger(__name__) def _canonicalize_id(repo_id: str) -> str: @@ -169,12 +170,10 @@ def _format_repository_config(repo_id, repo_config): return "".join(lines) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: repos = cfg.get("yum_repos") if not repos: - log.debug( + LOG.debug( "Skipping module named %s, no 'yum_repos' configuration found", name, ) @@ -188,14 +187,14 @@ def handle( canon_repo_id = _canonicalize_id(repo_id) repo_fn_pth = os.path.join(repo_base_path, "%s.repo" % (canon_repo_id)) if os.path.exists(repo_fn_pth): - log.info( + LOG.info( "Skipping repo %s, file %s already exists!", repo_id, repo_fn_pth, ) continue elif canon_repo_id in repo_locations: - log.info( + LOG.info( "Skipping repo %s, file %s already pending!", repo_id, repo_fn_pth, @@ -213,7 +212,7 @@ def handle( missing_required = 0 for req_field in ["baseurl"]: if req_field not in repo_config: - log.warning( + LOG.warning( "Repository %s does not contain a %s" " configuration 'required' entry", repo_id, @@ -224,7 +223,7 @@ def handle( repo_configs[canon_repo_id] = repo_config repo_locations[canon_repo_id] = repo_fn_pth else: - log.warning( + LOG.warning( "Repository %s is missing %s required fields, skipping!", repo_id, missing_required, diff --git a/cloudinit/config/cc_zypper_add_repo.py b/cloudinit/config/cc_zypper_add_repo.py index 958e4f94..9bbc4c33 100644 --- a/cloudinit/config/cc_zypper_add_repo.py +++ b/cloudinit/config/cc_zypper_add_repo.py @@ -6,7 +6,6 @@ """zypper_add_repo: Add zypper repositories to the system""" import os -from logging import Logger from textwrap import dedent import configobj @@ -190,9 +189,7 @@ def _write_zypp_config(zypper_config): util.write_file(zypp_config, new_config) -def handle( - name: str, cfg: Config, cloud: Cloud, log: Logger, args: list -) -> None: +def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: zypper_section = cfg.get("zypper") if not zypper_section: LOG.debug( diff --git a/cloudinit/config/modules.py b/cloudinit/config/modules.py index 6716fc32..def6b795 100644 --- a/cloudinit/config/modules.py +++ b/cloudinit/config/modules.py @@ -7,6 +7,7 @@ # This file is part of cloud-init. See LICENSE file for license information. import copy +from inspect import signature from types import ModuleType from typing import Dict, List, NamedTuple, Optional @@ -221,17 +222,12 @@ class Modules: # and which ones failed + the exception of why it failed failures = [] which_ran = [] - for (mod, name, freq, args) in mostly_mods: + for mod, name, freq, args in mostly_mods: try: LOG.debug( "Running module %s (%s) with frequency %s", name, mod, freq ) - # Use the configs logger and not our own - # TODO(harlowja): possibly check the module - # for having a LOG attr and just give it back - # its own logger? - func_args = [name, self.cfg, cc, LOG, args] # Mark it as having started running which_ran.append(name) # This name will affect the semaphore name created @@ -241,8 +237,22 @@ class Modules: myrep = ReportEventStack( name=run_name, description=desc, parent=self.reporter ) + func_args = { + "name": name, + "cfg": self.cfg, + "cloud": cc, + "args": args, + } with myrep: + func_signature = signature(mod.handle) + func_params = func_signature.parameters + if len(func_params) == 5: + util.deprecate( + deprecated="Config modules with a `log` parameter", + deprecated_version="23.2", + ) + func_args.update({"log": LOG}) ran, _r = cc.run( run_name, mod.handle, func_args, freq=freq ) |