From baad0946758a406201b0b17acbd03b27d1d46f2c Mon Sep 17 00:00:00 2001 From: d1r3ct0r Date: Mon, 27 Mar 2023 09:42:28 +0300 Subject: refactor: stop passing log instances to cc_* handlers (#2016) Use the module level Log instances instead of passing log instances to the cc_* handlers --- cloudinit/cmd/main.py | 2 +- cloudinit/config/cc_ansible.py | 6 +- cloudinit/config/cc_apk_configure.py | 5 +- cloudinit/config/cc_apt_configure.py | 8 +-- cloudinit/config/cc_apt_pipelining.py | 14 ++--- cloudinit/config/cc_bootcmd.py | 14 ++--- cloudinit/config/cc_byobu.py | 16 +++--- cloudinit/config/cc_ca_certs.py | 5 +- cloudinit/config/cc_chef.py | 40 ++++++------- cloudinit/config/cc_disable_ec2_metadata.py | 12 ++-- cloudinit/config/cc_disk_setup.py | 17 +++--- cloudinit/config/cc_fan.py | 5 +- cloudinit/config/cc_final_message.py | 15 +++-- cloudinit/config/cc_growpart.py | 27 ++++----- cloudinit/config/cc_grub_dpkg.py | 45 +++++++-------- cloudinit/config/cc_install_hotplug.py | 15 +++-- cloudinit/config/cc_keyboard.py | 5 +- cloudinit/config/cc_keys_to_console.py | 14 ++--- cloudinit/config/cc_landscape.py | 9 ++- cloudinit/config/cc_locale.py | 11 ++-- cloudinit/config/cc_lxd.py | 23 ++++---- cloudinit/config/cc_mcollective.py | 8 +-- cloudinit/config/cc_migrator.py | 17 +++--- cloudinit/config/cc_mounts.py | 51 ++++++++-------- cloudinit/config/cc_ntp.py | 5 +- .../config/cc_package_update_upgrade_install.py | 26 ++++----- cloudinit/config/cc_phone_home.py | 20 +++---- cloudinit/config/cc_power_state_change.py | 47 ++++++--------- cloudinit/config/cc_puppet.py | 37 ++++++------ cloudinit/config/cc_refresh_rmc_and_interface.py | 5 +- cloudinit/config/cc_reset_rmc.py | 5 +- cloudinit/config/cc_resizefs.py | 67 +++++++++++----------- cloudinit/config/cc_resolv_conf.py | 13 ++--- cloudinit/config/cc_rh_subscription.py | 9 +-- cloudinit/config/cc_rightscale_userdata.py | 21 ++++--- cloudinit/config/cc_rsyslog.py | 15 ++--- cloudinit/config/cc_runcmd.py | 10 ++-- cloudinit/config/cc_salt_minion.py | 9 ++- cloudinit/config/cc_scripts_per_boot.py | 9 ++- cloudinit/config/cc_scripts_per_instance.py | 10 ++-- cloudinit/config/cc_scripts_per_once.py | 9 ++- cloudinit/config/cc_scripts_user.py | 9 ++- cloudinit/config/cc_scripts_vendor.py | 9 ++- cloudinit/config/cc_seed_random.py | 9 +-- cloudinit/config/cc_set_hostname.py | 17 +++--- cloudinit/config/cc_set_passwords.py | 25 ++++---- cloudinit/config/cc_snap.py | 5 +- cloudinit/config/cc_spacewalk.py | 24 ++++---- cloudinit/config/cc_ssh.py | 27 +++++---- cloudinit/config/cc_ssh_authkey_fingerprints.py | 11 ++-- cloudinit/config/cc_ssh_import_id.py | 29 +++++----- cloudinit/config/cc_timezone.py | 9 ++- cloudinit/config/cc_ubuntu_advantage.py | 5 +- cloudinit/config/cc_ubuntu_autoinstall.py | 5 +- cloudinit/config/cc_ubuntu_drivers.py | 9 +-- cloudinit/config/cc_update_etc_hosts.py | 15 +++-- cloudinit/config/cc_update_hostname.py | 15 +++-- cloudinit/config/cc_users_groups.py | 5 +- cloudinit/config/cc_wireguard.py | 5 +- cloudinit/config/cc_write_files.py | 7 +-- cloudinit/config/cc_write_files_deferred.py | 9 ++- cloudinit/config/cc_yum_add_repo.py | 17 +++--- cloudinit/config/cc_zypper_add_repo.py | 5 +- cloudinit/config/modules.py | 22 +++++-- doc/rtd/development/module_creation.rst | 10 ++-- tests/unittests/cmd/test_main.py | 3 +- .../config/test_apt_configure_sources_list_v1.py | 11 +--- .../config/test_apt_configure_sources_list_v3.py | 11 +--- tests/unittests/config/test_apt_source_v1.py | 18 +++--- tests/unittests/config/test_cc_ansible.py | 8 +-- tests/unittests/config/test_cc_apk_configure.py | 39 +++---------- tests/unittests/config/test_cc_apt_pipelining.py | 4 +- tests/unittests/config/test_cc_bootcmd.py | 13 ++--- tests/unittests/config/test_cc_ca_certs.py | 24 +++----- tests/unittests/config/test_cc_chef.py | 15 ++--- .../config/test_cc_disable_ec2_metadata.py | 9 +-- tests/unittests/config/test_cc_final_message.py | 3 +- tests/unittests/config/test_cc_growpart.py | 8 +-- tests/unittests/config/test_cc_grub_dpkg.py | 52 +++++++++-------- tests/unittests/config/test_cc_install_hotplug.py | 10 ++-- tests/unittests/config/test_cc_keys_to_console.py | 2 +- tests/unittests/config/test_cc_landscape.py | 8 +-- tests/unittests/config/test_cc_locale.py | 10 ++-- tests/unittests/config/test_cc_lxd.py | 11 ++-- tests/unittests/config/test_cc_mcollective.py | 2 +- tests/unittests/config/test_cc_mounts.py | 63 ++++++++++++-------- tests/unittests/config/test_cc_ntp.py | 18 +++--- tests/unittests/config/test_cc_phone_home.py | 4 +- .../unittests/config/test_cc_power_state_change.py | 8 +-- tests/unittests/config/test_cc_puppet.py | 65 ++++++++++----------- .../config/test_cc_refresh_rmc_and_interface.py | 2 +- tests/unittests/config/test_cc_resizefs.py | 46 +++++++-------- tests/unittests/config/test_cc_resolv_conf.py | 2 +- tests/unittests/config/test_cc_rh_subscription.py | 24 ++------ tests/unittests/config/test_cc_runcmd.py | 8 +-- tests/unittests/config/test_cc_seed_random.py | 23 ++++---- tests/unittests/config/test_cc_set_hostname.py | 26 ++++----- tests/unittests/config/test_cc_set_passwords.py | 15 +++-- tests/unittests/config/test_cc_snap.py | 2 +- tests/unittests/config/test_cc_ssh.py | 12 ++-- tests/unittests/config/test_cc_ssh_import_id.py | 2 +- tests/unittests/config/test_cc_timezone.py | 2 +- tests/unittests/config/test_cc_ubuntu_advantage.py | 11 ++-- .../unittests/config/test_cc_ubuntu_autoinstall.py | 2 +- tests/unittests/config/test_cc_ubuntu_drivers.py | 36 ++++++------ tests/unittests/config/test_cc_update_etc_hosts.py | 4 +- tests/unittests/config/test_cc_users_groups.py | 18 +++--- tests/unittests/config/test_cc_wireguard.py | 4 +- tests/unittests/config/test_cc_write_files.py | 4 +- .../config/test_cc_write_files_deferred.py | 2 +- tests/unittests/config/test_cc_yum_add_repo.py | 6 +- tests/unittests/config/test_cc_zypper_add_repo.py | 2 +- tests/unittests/config/test_modules.py | 50 +++++++++++++++- 113 files changed, 795 insertions(+), 931 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/