From c475d72b7dc3f6c6b4d7c2106d7f0f5acf879c0d Mon Sep 17 00:00:00 2001 From: paulober <44974737+paulober@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:20:54 +0100 Subject: [PATCH] Fix formatting Signed-off-by: paulober <44974737+paulober@users.noreply.github.com> --- cloudinit/config/cc_ca_certs.py | 8 +++- cloudinit/config/cc_rpi_connect.py | 6 +-- cloudinit/config/cc_rpi_interfaces.py | 33 ++++++++----- cloudinit/config/cc_rpi_userdata.py | 47 ++++++++++--------- tests/unittests/config/test_cc_rpi_connect.py | 7 ++- .../config/test_cc_rpi_interfaces.py | 7 ++- .../unittests/config/test_cc_rpi_userdata.py | 2 +- tests/unittests/test_render_template.py | 2 +- 8 files changed, 66 insertions(+), 46 deletions(-) diff --git a/cloudinit/config/cc_ca_certs.py b/cloudinit/config/cc_ca_certs.py index 0edcbdc5988..8b3f7762d59 100644 --- a/cloudinit/config/cc_ca_certs.py +++ b/cloudinit/config/cc_ca_certs.py @@ -158,7 +158,13 @@ def disable_default_ca_certs(distro_name, distro_cfg): """ if distro_name in ["rhel", "photon"]: remove_default_ca_certs(distro_cfg) - elif distro_name in ["alpine", "aosc", "debian", "raspberry-pi-os", "ubuntu"]: + elif distro_name in [ + "alpine", + "aosc", + "debian", + "raspberry-pi-os", + "ubuntu", + ]: disable_system_ca_certs(distro_cfg) if distro_name in ["debian", "raspberry-pi-os", "ubuntu"]: diff --git a/cloudinit/config/cc_rpi_connect.py b/cloudinit/config/cc_rpi_connect.py index d084bbe3eb2..09e6aa0c301 100644 --- a/cloudinit/config/cc_rpi_connect.py +++ b/cloudinit/config/cc_rpi_connect.py @@ -4,12 +4,12 @@ # # This file is part of cloud-init. See LICENSE file for license information. -import logging from cloudinit import subp from cloudinit.cloud import Cloud from cloudinit.config import Config from cloudinit.config.schema import MetaSchema from cloudinit.settings import PER_INSTANCE +import logging LOG = logging.getLogger(__name__) @@ -31,7 +31,7 @@ def configure_rpi_connect(enable: bool) -> None: try: subp.subp(["/usr/bin/raspi-config", "do_rpi_connect", str(num)]) except subp.ProcessExecutionError as e: - LOG.error(f"Failed to configure rpi-connect: {e}") + LOG.error("Failed to configure rpi-connect: %s", e) def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: @@ -43,5 +43,5 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: configure_rpi_connect(enable) else: LOG.warning( - f"Invalid value for {ENABLE_RPI_CONNECT_KEY}: " + str(enable) + "Invalid value for %s: %s", ENABLE_RPI_CONNECT_KEY, enable ) diff --git a/cloudinit/config/cc_rpi_interfaces.py b/cloudinit/config/cc_rpi_interfaces.py index 7f6c78a7d5a..d8fecf014b7 100644 --- a/cloudinit/config/cc_rpi_interfaces.py +++ b/cloudinit/config/cc_rpi_interfaces.py @@ -4,12 +4,12 @@ # # This file is part of cloud-init. See LICENSE file for license information. -import logging from cloudinit import subp from cloudinit.cloud import Cloud from cloudinit.config import Config from cloudinit.config.schema import MetaSchema from cloudinit.settings import PER_INSTANCE +import logging LOG = logging.getLogger(__name__) @@ -86,17 +86,17 @@ def configure_serial_interface(cfg: dict | bool, instCfg: Config) -> None: ] ) except subp.ProcessExecutionError as e: - LOG.error(f"Failed to configure serial hardware: {e}") + LOG.error("Failed to configure serial hardware: %s", e) require_reboot(instCfg) except subp.ProcessExecutionError as e: - LOG.error(f"Failed to configure serial console: {e}") + LOG.error("Failed to configure serial console: %s", e) def enable_ssh(cfg: Config, enable: bool) -> None: if not enable: return - + try: subp.subp( [ @@ -106,7 +106,7 @@ def enable_ssh(cfg: Config, enable: bool) -> None: ) require_reboot(cfg) except subp.ProcessExecutionError as e: - LOG.error(f"Failed to enable ssh: {e}") + LOG.error("Failed to enable ssh: %s", e) def configure_interface(iface: str, enable: bool) -> None: @@ -124,7 +124,7 @@ def configure_interface(iface: str, enable: bool) -> None: ] ) except subp.ProcessExecutionError as e: - LOG.error(f"Failed to configure {iface}: {e}") + LOG.error("Failed to configure %s: %s", iface, e) def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: @@ -132,18 +132,19 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: return elif not isinstance(cfg[RPI_INTERFACES_KEY], dict): LOG.warning( - f"Invalid value for {RPI_INTERFACES_KEY}: " - + cfg[RPI_INTERFACES_KEY] + "Invalid value for %s: %s", + RPI_INTERFACES_KEY, + cfg[RPI_INTERFACES_KEY], ) return elif not cfg[RPI_INTERFACES_KEY]: - LOG.debug(f"Empty value for {RPI_INTERFACES_KEY}. Skipping...") + LOG.debug("Empty value for %s. Skipping...", RPI_INTERFACES_KEY) return # check for supported ARM interfaces for key in cfg[RPI_INTERFACES_KEY]: if key not in SUPPORTED_INTERFACES.keys(): - LOG.warning(f"Invalid key for {RPI_INTERFACES_KEY}: {key}") + LOG.warning("Invalid key for %s: %s", RPI_INTERFACES_KEY, key) continue enable = cfg[RPI_INTERFACES_KEY][key] @@ -151,7 +152,10 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: if key == "serial": if not isinstance(enable, dict) and not isinstance(enable, bool): LOG.warning( - f"Invalid value for {RPI_INTERFACES_KEY}.{key}: {enable}" + "Invalid value for %s.%s: %s", + RPI_INTERFACES_KEY, + key, + enable, ) else: configure_serial_interface(enable, cfg) @@ -159,7 +163,10 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: elif key == "ssh": if not isinstance(enable, bool): LOG.warning( - f"Invalid value for {RPI_INTERFACES_KEY}.{key}: {enable}" + "Invalid value for %s.%s: %s", + RPI_INTERFACES_KEY, + key, + enable, ) else: enable_ssh(cfg, enable) @@ -169,5 +176,5 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: configure_interface(key, enable) else: LOG.warning( - f"Invalid value for {RPI_INTERFACES_KEY}.{key}: {enable}" + "Invalid value for %s.%s: %s", RPI_INTERFACES_KEY, key, enable ) diff --git a/cloudinit/config/cc_rpi_userdata.py b/cloudinit/config/cc_rpi_userdata.py index 5593e075f6a..b3d052148f2 100644 --- a/cloudinit/config/cc_rpi_userdata.py +++ b/cloudinit/config/cc_rpi_userdata.py @@ -4,16 +4,15 @@ # # This file is part of cloud-init. See LICENSE file for license information. -import logging -import os -import time -import subprocess from cloudinit import subp from cloudinit.cloud import Cloud from cloudinit.config import Config from cloudinit.config.schema import MetaSchema from cloudinit.settings import PER_ALWAYS - +import logging +import os +import subprocess +import time LOG = logging.getLogger(__name__) DISABLE_PIWIZ_KEY = "disable_piwiz" @@ -101,7 +100,7 @@ def run_userconf_service( LOG.error("Userconf service timed out.") return False except Exception as e: - LOG.error(f"Error running service: {e}") + LOG.warning("Error running service: %s", e) if base: try: os.remove(f"{base}/userconf.txt") @@ -116,17 +115,17 @@ def run_service( # Ensure the TTY exists before trying to open it if not os.path.exists(USERCONF_SERVICE_TTY): if not passwd_override: - LOG.error(f"TTY device {USERCONF_SERVICE_TTY} does not exist.") - return + LOG.error("TTY device %s does not exist.", USERCONF_SERVICE_TTY) + return False else: - LOG.debug(f"TTY device {USERCONF_SERVICE_TTY} does not exist.") + LOG.debug("TTY device %s does not exist.", USERCONF_SERVICE_TTY) # should never happen and not solvable by the user assert (passwd_override is None and user_override is None) or ( passwd_override is not None and user_override is not None ), ( - "Internal error: User override is required when password " - + "override is provided." + "Internal error: User override is required when password " \ + "override is provided." ) base: str | None = None @@ -138,7 +137,7 @@ def run_service( assert base, "Internal error: Failed to get firmware location." with open(f"{base}/userconf.txt", "w") as f: f.write(f"{user_override}:{passwd_override}") - LOG.debug(f"Userconf override file written to {base}/userconf.txt") + LOG.debug("Userconf override file written to %s/userconf.txt", base) LOG.debug("Start running userconf-pi service loop...") while True: @@ -158,11 +157,11 @@ def configure_pizwiz( user_override: str | None = None, ) -> None: LOG.debug( - "Configuring piwiz with disable_piwiz=%s, passwd_override=%s, " - + "user_override=%s", + "Configuring piwiz with disable_piwiz=%s, passwd_override=%s, " \ + "user_override=%s", bool_to_str(disable), - bool_to_str(passwd_override != None), - bool_to_str(user_override != None), + bool_to_str(passwd_override is not None), + bool_to_str(user_override is not None), ) if disable: @@ -192,8 +191,8 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: "/usr/lib/userconf-pi" ): LOG.debug( - "Userconf-Pi: deactivation file detected or userconf-pi " - + "not installed. Skipping..." + "Userconf-Pi: deactivation file detected or userconf-pi " \ + "not installed. Skipping..." ) return @@ -207,19 +206,21 @@ def handle(name: str, cfg: Config, cloud: Cloud, args: list) -> None: # user key is optional with default to pi user_override = userconf.get("user", "pi") LOG.debug( - f"Userconf override: user={user_override}, " - + "password=" + "Userconf override: user=%s, " \ + "password=", + user_override ) else: - LOG.error(f"Invalid userconf-pi configuration: {userconf}") + LOG.error("Invalid userconf-pi configuration: %s", userconf) if not password_override and DISABLE_PIWIZ_KEY in cfg: if isinstance(cfg[DISABLE_PIWIZ_KEY], bool): disable_piwiz = cfg[DISABLE_PIWIZ_KEY] else: LOG.error( - f"Invalid {DISABLE_PIWIZ_KEY} configuration: " - + cfg[DISABLE_PIWIZ_KEY] + "Invalid %s configuration: %s", + str(cfg[DISABLE_PIWIZ_KEY]), + DISABLE_PIWIZ_KEY ) configure_pizwiz(cfg, disable_piwiz, password_override, user_override) diff --git a/tests/unittests/config/test_cc_rpi_connect.py b/tests/unittests/config/test_cc_rpi_connect.py index 3368c060ce7..19284e49864 100644 --- a/tests/unittests/config/test_cc_rpi_connect.py +++ b/tests/unittests/config/test_cc_rpi_connect.py @@ -1,8 +1,11 @@ # This file is part of cloud-init. See LICENSE file for license information. from cloudinit.config.cc_rpi_connect import ENABLE_RPI_CONNECT_KEY -from cloudinit.config.schema import validate_cloudconfig_schema, get_schema -from cloudinit.config.schema import SchemaValidationError +from cloudinit.config.schema import ( + SchemaValidationError, + get_schema, + validate_cloudconfig_schema, +) from tests.unittests.helpers import skipUnlessJsonSchema import pytest diff --git a/tests/unittests/config/test_cc_rpi_interfaces.py b/tests/unittests/config/test_cc_rpi_interfaces.py index ffd9a8ab9ef..c9d55db2de6 100644 --- a/tests/unittests/config/test_cc_rpi_interfaces.py +++ b/tests/unittests/config/test_cc_rpi_interfaces.py @@ -1,8 +1,11 @@ # This file is part of cloud-init. See LICENSE file for license information. from cloudinit.config.cc_rpi_interfaces import RPI_INTERFACES_KEY -from cloudinit.config.schema import validate_cloudconfig_schema, get_schema -from cloudinit.config.schema import SchemaValidationError +from cloudinit.config.schema import ( + SchemaValidationError, + get_schema, + validate_cloudconfig_schema, +) from tests.unittests.helpers import skipUnlessJsonSchema import pytest diff --git a/tests/unittests/config/test_cc_rpi_userdata.py b/tests/unittests/config/test_cc_rpi_userdata.py index f26fbcba7c0..75d78b05018 100644 --- a/tests/unittests/config/test_cc_rpi_userdata.py +++ b/tests/unittests/config/test_cc_rpi_userdata.py @@ -1,6 +1,5 @@ # This file is part of cloud-init. See LICENSE file for license information. -import pytest from cloudinit.config.cc_rpi_userdata import ( DISABLE_PIWIZ_KEY, RPI_USERCONF_KEY, @@ -10,6 +9,7 @@ get_schema, validate_cloudconfig_schema, ) +import pytest from tests.unittests.helpers import skipUnlessJsonSchema """ diff --git a/tests/unittests/test_render_template.py b/tests/unittests/test_render_template.py index 611392d50d1..dde51a69904 100644 --- a/tests/unittests/test_render_template.py +++ b/tests/unittests/test_render_template.py @@ -108,7 +108,7 @@ def test_variant_sets_default_user_in_cloud_cfg(self, variant, tmpdir): ("openbsd", ["openbsd"]), ("ubuntu", ["netplan", "eni", "sysconfig"]), ( - "raspberry-pi-os", + "raspberry-pi-os", ["netplan", "network-manager", "networkd", "eni"] ) ),