diff options
Diffstat (limited to 'cloudinit/sources/DataSourceLXD.py')
-rw-r--r-- | cloudinit/sources/DataSourceLXD.py | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/cloudinit/sources/DataSourceLXD.py b/cloudinit/sources/DataSourceLXD.py index 469707d2..071ea87c 100644 --- a/cloudinit/sources/DataSourceLXD.py +++ b/cloudinit/sources/DataSourceLXD.py @@ -1,4 +1,3 @@ - """Datasource for LXD, reads /dev/lxd/sock representaton of instance data. Notes: @@ -10,8 +9,10 @@ Notes: * TODO( Hotplug support using websockets API 1.0/events ) """ -from json.decoder import JSONDecodeError import os +import socket +import stat +from json.decoder import JSONDecodeError import requests from requests.adapters import HTTPAdapter @@ -29,9 +30,6 @@ from requests.adapters import HTTPAdapter from requests.packages.urllib3.connection import HTTPConnection from requests.packages.urllib3.connectionpool import HTTPConnectionPool -import socket -import stat - from cloudinit import log as logging from cloudinit import sources, subp, util @@ -47,7 +45,7 @@ CONFIG_KEY_ALIASES = { "cloud-init.vendor-data": "vendor-data", "user.user-data": "user-data", "user.network-config": "network-config", - "user.vendor-data": "vendor-data" + "user.vendor-data": "vendor-data", } @@ -57,18 +55,20 @@ def generate_fallback_network_config() -> dict: "version": 1, "config": [ { - "type": "physical", "name": "eth0", - "subnets": [{"type": "dhcp", "control": "auto"}] + "type": "physical", + "name": "eth0", + "subnets": [{"type": "dhcp", "control": "auto"}], } - ] + ], } if subp.which("systemd-detect-virt"): try: - virt_type, _ = subp.subp(['systemd-detect-virt']) + virt_type, _ = subp.subp(["systemd-detect-virt"]) except subp.ProcessExecutionError as err: LOG.warning( "Unable to run systemd-detect-virt: %s." - " Rendering default network config.", err + " Rendering default network config.", + err, ) return network_v1 if virt_type.strip() == "kvm": # instance.type VIRTUAL-MACHINE @@ -84,7 +84,7 @@ def generate_fallback_network_config() -> dict: class SocketHTTPConnection(HTTPConnection): def __init__(self, socket_path): - super().__init__('localhost') + super().__init__("localhost") self.socket_path = socket_path def connect(self): @@ -95,7 +95,7 @@ class SocketHTTPConnection(HTTPConnection): class SocketConnectionPool(HTTPConnectionPool): def __init__(self, socket_path): self.socket_path = socket_path - super().__init__('localhost') + super().__init__("localhost") def _new_conn(self): return SocketHTTPConnection(self.socket_path) @@ -118,16 +118,16 @@ def _maybe_remove_top_network(cfg): if "network" not in cfg: return cfg network_val = cfg["network"] - bmsg = 'Top level network key in network-config %s: %s' + bmsg = "Top level network key in network-config %s: %s" if not isinstance(network_val, dict): LOG.debug(bmsg, "was not a dict", cfg) return cfg if len(list(cfg.keys())) != 1: LOG.debug(bmsg, "had multiple top level keys", cfg) return cfg - if network_val.get('config') == "disabled": + if network_val.get("config") == "disabled": LOG.debug(bmsg, "was config/disabled", cfg) - elif not all(('config' in network_val, 'version' in network_val)): + elif not all(("config" in network_val, "version" in network_val)): LOG.debug(bmsg, "but missing 'config' or 'version'", cfg) return cfg LOG.debug(bmsg, "fixed by removing shifting network.", cfg) @@ -165,13 +165,16 @@ def _raw_instance_data_to_dict(metadata_type: str, metadata_value) -> dict: class DataSourceLXD(sources.DataSource): - dsname = 'LXD' + dsname = "LXD" _network_config = sources.UNSET _crawled_metadata = sources.UNSET sensitive_metadata_keys = ( - 'merged_cfg', 'user.meta-data', 'user.vendor-data', 'user.user-data', + "merged_cfg", + "user.meta-data", + "user.vendor-data", + "user.user-data", ) def _is_platform_viable(self) -> bool: @@ -185,8 +188,10 @@ class DataSourceLXD(sources.DataSource): return False self._crawled_metadata = util.log_time( - logfunc=LOG.debug, msg='Crawl of metadata service', - func=read_metadata) + logfunc=LOG.debug, + msg="Crawl of metadata service", + func=read_metadata, + ) self.metadata = _raw_instance_data_to_dict( "meta-data", self._crawled_metadata.get("meta-data") ) @@ -293,7 +298,7 @@ def read_metadata( "Invalid HTTP response [{code}] from {route}: {resp}".format( code=response.status_code, route=md_route, - resp=response.text + resp=response.text, ) ) @@ -304,7 +309,7 @@ def read_metadata( md = { "_metadata_api_version": api_version, # Document API version read "config": {}, - "meta-data": md["meta-data"] + "meta-data": md["meta-data"], } config_url = version_url + "config" @@ -317,7 +322,7 @@ def read_metadata( "Invalid HTTP response [{code}] from {route}: {resp}".format( code=response.status_code, route=config_url, - resp=response.text + resp=response.text, ) ) try: @@ -326,8 +331,7 @@ def read_metadata( raise sources.InvalidMetaDataException( "Unable to determine cloud-init config from {route}." " Expected JSON but found: {resp}".format( - route=config_url, - resp=response.text + route=config_url, resp=response.text ) ) from exc @@ -354,12 +358,15 @@ def read_metadata( else: LOG.warning( "Ignoring LXD config %s in favor of %s value.", - cfg_key, cfg_key.replace("user", "cloud-init", 1) + cfg_key, + cfg_key.replace("user", "cloud-init", 1), ) else: LOG.debug( "Skipping %s on [HTTP:%d]:%s", - url, response.status_code, response.text + url, + response.status_code, + response.text, ) return md |