1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-15 08:21:31 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -426,32 +426,30 @@ from ansible_collections.community.general.plugins.module_utils.lxd import LXDCl
# LXD_ANSIBLE_STATES is a map of states that contain values of methods used
# when a particular state is evoked.
LXD_ANSIBLE_STATES = {
'started': '_started',
'stopped': '_stopped',
'restarted': '_restarted',
'absent': '_destroyed',
'frozen': '_frozen',
"started": "_started",
"stopped": "_stopped",
"restarted": "_restarted",
"absent": "_destroyed",
"frozen": "_frozen",
}
# ANSIBLE_LXD_STATES is a map of states of lxd containers to the Ansible
# lxc_container module state parameter value.
ANSIBLE_LXD_STATES = {
'Running': 'started',
'Stopped': 'stopped',
'Frozen': 'frozen',
"Running": "started",
"Stopped": "stopped",
"Frozen": "frozen",
}
# ANSIBLE_LXD_DEFAULT_URL is a default value of the lxd endpoint
ANSIBLE_LXD_DEFAULT_URL = 'unix:/var/lib/lxd/unix.socket'
ANSIBLE_LXD_DEFAULT_URL = "unix:/var/lib/lxd/unix.socket"
# CONFIG_PARAMS is a list of config attribute names.
CONFIG_PARAMS = [
'architecture', 'config', 'devices', 'ephemeral', 'profiles', 'source', 'type'
]
CONFIG_PARAMS = ["architecture", "config", "devices", "ephemeral", "profiles", "source", "type"]
# CONFIG_CREATION_PARAMS is a list of attribute names that are only applied
# on instance creation.
CONFIG_CREATION_PARAMS = ['source', 'type']
CONFIG_CREATION_PARAMS = ["source", "type"]
class LXDContainerManagement:
@ -462,61 +460,58 @@ class LXDContainerManagement:
:type module: ``object``
"""
self.module = module
self.name = self.module.params['name']
self.project = self.module.params['project']
self.name = self.module.params["name"]
self.project = self.module.params["project"]
self._build_config()
self.state = self.module.params['state']
self.state = self.module.params["state"]
self.timeout = self.module.params['timeout']
self.wait_for_ipv4_addresses = self.module.params['wait_for_ipv4_addresses']
self.force_stop = self.module.params['force_stop']
self.timeout = self.module.params["timeout"]
self.wait_for_ipv4_addresses = self.module.params["wait_for_ipv4_addresses"]
self.force_stop = self.module.params["force_stop"]
self.addresses = None
self.target = self.module.params['target']
self.wait_for_container = self.module.params['wait_for_container']
self.target = self.module.params["target"]
self.wait_for_container = self.module.params["wait_for_container"]
self.type = self.module.params['type']
self.type = self.module.params["type"]
self.key_file = self.module.params.get('client_key')
self.key_file = self.module.params.get("client_key")
if self.key_file is None:
self.key_file = f"{os.environ['HOME']}/.config/lxc/client.key"
self.cert_file = self.module.params.get('client_cert')
self.cert_file = self.module.params.get("client_cert")
if self.cert_file is None:
self.cert_file = f"{os.environ['HOME']}/.config/lxc/client.crt"
self.debug = self.module._verbosity >= 4
try:
if self.module.params['url'] != ANSIBLE_LXD_DEFAULT_URL:
self.url = self.module.params['url']
elif os.path.exists(self.module.params['snap_url'].replace('unix:', '')):
self.url = self.module.params['snap_url']
if self.module.params["url"] != ANSIBLE_LXD_DEFAULT_URL:
self.url = self.module.params["url"]
elif os.path.exists(self.module.params["snap_url"].replace("unix:", "")):
self.url = self.module.params["snap_url"]
else:
self.url = self.module.params['url']
self.url = self.module.params["url"]
except Exception as e:
self.module.fail_json(msg=e.msg)
try:
self.client = LXDClient(
self.url, key_file=self.key_file, cert_file=self.cert_file,
debug=self.debug
)
self.client = LXDClient(self.url, key_file=self.key_file, cert_file=self.cert_file, debug=self.debug)
except LXDClientException as e:
self.module.fail_json(msg=e.msg)
# LXD (3.19) Rest API provides instances endpoint, failback to containers and virtual-machines
# https://documentation.ubuntu.com/lxd/en/latest/rest-api/#instances-containers-and-virtual-machines
self.api_endpoint = '/1.0/instances'
check_api_endpoint = self.client.do('GET', f'{self.api_endpoint}?project=', ok_error_codes=[404])
self.api_endpoint = "/1.0/instances"
check_api_endpoint = self.client.do("GET", f"{self.api_endpoint}?project=", ok_error_codes=[404])
if check_api_endpoint['error_code'] == 404:
if self.type == 'container':
self.api_endpoint = '/1.0/containers'
elif self.type == 'virtual-machine':
self.api_endpoint = '/1.0/virtual-machines'
if check_api_endpoint["error_code"] == 404:
if self.type == "container":
self.api_endpoint = "/1.0/containers"
elif self.type == "virtual-machine":
self.api_endpoint = "/1.0/virtual-machines"
self.trust_password = self.module.params.get('trust_password', None)
self.trust_password = self.module.params.get("trust_password", None)
self.actions = []
self.diff = {'before': {}, 'after': {}}
self.diff = {"before": {}, "after": {}}
self.old_instance_json = {}
self.old_sections = {}
@ -528,90 +523,83 @@ class LXDContainerManagement:
self.config[attr] = param_val
def _get_instance_json(self):
url = f'{self.api_endpoint}/{self.name}'
url = f"{self.api_endpoint}/{self.name}"
if self.project:
url = f'{url}?{urlencode(dict(project=self.project))}'
return self.client.do('GET', url, ok_error_codes=[404])
url = f"{url}?{urlencode(dict(project=self.project))}"
return self.client.do("GET", url, ok_error_codes=[404])
def _get_instance_state_json(self):
url = f'{self.api_endpoint}/{self.name}/state'
url = f"{self.api_endpoint}/{self.name}/state"
if self.project:
url = f'{url}?{urlencode(dict(project=self.project))}'
return self.client.do('GET', url, ok_error_codes=[404])
url = f"{url}?{urlencode(dict(project=self.project))}"
return self.client.do("GET", url, ok_error_codes=[404])
@staticmethod
def _instance_json_to_module_state(resp_json):
if resp_json['type'] == 'error':
return 'absent'
return ANSIBLE_LXD_STATES[resp_json['metadata']['status']]
if resp_json["type"] == "error":
return "absent"
return ANSIBLE_LXD_STATES[resp_json["metadata"]["status"]]
def _change_state(self, action, force_stop=False):
url = f'{self.api_endpoint}/{self.name}/state'
url = f"{self.api_endpoint}/{self.name}/state"
if self.project:
url = f'{url}?{urlencode(dict(project=self.project))}'
body_json = {'action': action, 'timeout': self.timeout}
url = f"{url}?{urlencode(dict(project=self.project))}"
body_json = {"action": action, "timeout": self.timeout}
if force_stop:
body_json['force'] = True
body_json["force"] = True
if not self.module.check_mode:
return self.client.do('PUT', url, body_json=body_json)
return self.client.do("PUT", url, body_json=body_json)
def _create_instance(self):
url = self.api_endpoint
url_params = dict()
if self.target:
url_params['target'] = self.target
url_params["target"] = self.target
if self.project:
url_params['project'] = self.project
url_params["project"] = self.project
if url_params:
url = f'{url}?{urlencode(url_params)}'
url = f"{url}?{urlencode(url_params)}"
config = self.config.copy()
config['name'] = self.name
config["name"] = self.name
if self.type not in self.api_endpoint:
config['type'] = self.type
config["type"] = self.type
if not self.module.check_mode:
self.client.do('POST', url, config, wait_for_container=self.wait_for_container)
self.actions.append('create')
self.client.do("POST", url, config, wait_for_container=self.wait_for_container)
self.actions.append("create")
def _start_instance(self):
self._change_state('start')
self.actions.append('start')
self._change_state("start")
self.actions.append("start")
def _stop_instance(self):
self._change_state('stop', self.force_stop)
self.actions.append('stop')
self._change_state("stop", self.force_stop)
self.actions.append("stop")
def _restart_instance(self):
self._change_state('restart', self.force_stop)
self.actions.append('restart')
self._change_state("restart", self.force_stop)
self.actions.append("restart")
def _delete_instance(self):
url = f'{self.api_endpoint}/{self.name}'
url = f"{self.api_endpoint}/{self.name}"
if self.project:
url = f'{url}?{urlencode(dict(project=self.project))}'
url = f"{url}?{urlencode(dict(project=self.project))}"
if not self.module.check_mode:
self.client.do('DELETE', url)
self.actions.append('delete')
self.client.do("DELETE", url)
self.actions.append("delete")
def _freeze_instance(self):
self._change_state('freeze')
self.actions.append('freeze')
self._change_state("freeze")
self.actions.append("freeze")
def _unfreeze_instance(self):
self._change_state('unfreeze')
self.actions.append('unfreeze')
self._change_state("unfreeze")
self.actions.append("unfreeze")
def _instance_ipv4_addresses(self, ignore_devices=None):
ignore_devices = ['lo'] if ignore_devices is None else ignore_devices
data = (self._get_instance_state_json() or {}).get('metadata', None) or {}
network = {
k: v
for k, v in (data.get('network') or {}).items()
if k not in ignore_devices
}
addresses = {
k: [a['address'] for a in v['addresses'] if a['family'] == 'inet']
for k, v in network.items()
}
ignore_devices = ["lo"] if ignore_devices is None else ignore_devices
data = (self._get_instance_state_json() or {}).get("metadata", None) or {}
network = {k: v for k, v in (data.get("network") or {}).items() if k not in ignore_devices}
addresses = {k: [a["address"] for a in v["addresses"] if a["family"] == "inet"] for k, v in network.items()}
return addresses
@staticmethod
@ -628,17 +616,17 @@ class LXDContainerManagement:
self.addresses = addresses
return
except LXDClientException as e:
e.msg = 'timeout for getting IPv4 addresses'
e.msg = "timeout for getting IPv4 addresses"
raise
def _started(self):
if self.old_state == 'absent':
if self.old_state == "absent":
self._create_instance()
self._start_instance()
else:
if self.old_state == 'frozen':
if self.old_state == "frozen":
self._unfreeze_instance()
elif self.old_state == 'stopped':
elif self.old_state == "stopped":
self._start_instance()
if self._needs_to_apply_instance_configs():
self._apply_instance_configs()
@ -646,27 +634,27 @@ class LXDContainerManagement:
self._get_addresses()
def _stopped(self):
if self.old_state == 'absent':
if self.old_state == "absent":
self._create_instance()
else:
if self.old_state == 'stopped':
if self.old_state == "stopped":
if self._needs_to_apply_instance_configs():
self._start_instance()
self._apply_instance_configs()
self._stop_instance()
else:
if self.old_state == 'frozen':
if self.old_state == "frozen":
self._unfreeze_instance()
if self._needs_to_apply_instance_configs():
self._apply_instance_configs()
self._stop_instance()
def _restarted(self):
if self.old_state == 'absent':
if self.old_state == "absent":
self._create_instance()
self._start_instance()
else:
if self.old_state == 'frozen':
if self.old_state == "frozen":
self._unfreeze_instance()
if self._needs_to_apply_instance_configs():
self._apply_instance_configs()
@ -675,20 +663,20 @@ class LXDContainerManagement:
self._get_addresses()
def _destroyed(self):
if self.old_state != 'absent':
if self.old_state == 'frozen':
if self.old_state != "absent":
if self.old_state == "frozen":
self._unfreeze_instance()
if self.old_state != 'stopped':
if self.old_state != "stopped":
self._stop_instance()
self._delete_instance()
def _frozen(self):
if self.old_state == 'absent':
if self.old_state == "absent":
self._create_instance()
self._start_instance()
self._freeze_instance()
else:
if self.old_state == 'stopped':
if self.old_state == "stopped":
self._start_instance()
if self._needs_to_apply_instance_configs():
self._apply_instance_configs()
@ -698,10 +686,10 @@ class LXDContainerManagement:
if key not in self.config:
return False
if key == 'config':
if key == "config":
# self.old_sections is already filtered for volatile keys if necessary
old_configs = dict(self.old_sections.get(key, None) or {})
for k, v in self.config['config'].items():
for k, v in self.config["config"].items():
if k not in old_configs:
return True
if old_configs[k] != v:
@ -718,81 +706,85 @@ class LXDContainerManagement:
return False
def _apply_instance_configs(self):
old_metadata = copy.deepcopy(self.old_instance_json).get('metadata', None) or {}
old_metadata = copy.deepcopy(self.old_instance_json).get("metadata", None) or {}
body_json = {}
for param in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS):
if param in old_metadata:
body_json[param] = old_metadata[param]
if self._needs_to_change_instance_config(param):
if param == 'config':
body_json['config'] = body_json.get('config', None) or {}
for k, v in self.config['config'].items():
body_json['config'][k] = v
if param == "config":
body_json["config"] = body_json.get("config", None) or {}
for k, v in self.config["config"].items():
body_json["config"][k] = v
else:
body_json[param] = self.config[param]
self.diff['after']['instance'] = body_json
url = f'{self.api_endpoint}/{self.name}'
self.diff["after"]["instance"] = body_json
url = f"{self.api_endpoint}/{self.name}"
if self.project:
url = f'{url}?{urlencode(dict(project=self.project))}'
url = f"{url}?{urlencode(dict(project=self.project))}"
if not self.module.check_mode:
self.client.do('PUT', url, body_json=body_json)
self.actions.append('apply_instance_configs')
self.client.do("PUT", url, body_json=body_json)
self.actions.append("apply_instance_configs")
def run(self):
"""Run the main method."""
def adjust_content(content):
return content if not isinstance(content, dict) else {
k: v for k, v in content.items() if not (self.ignore_volatile_options and k.startswith('volatile.'))
}
return (
content
if not isinstance(content, dict)
else {
k: v for k, v in content.items() if not (self.ignore_volatile_options and k.startswith("volatile."))
}
)
try:
if self.trust_password is not None:
self.client.authenticate(self.trust_password)
self.ignore_volatile_options = self.module.params.get('ignore_volatile_options')
self.ignore_volatile_options = self.module.params.get("ignore_volatile_options")
self.old_instance_json = self._get_instance_json()
self.old_sections = {
section: adjust_content(content)
for section, content in (self.old_instance_json.get('metadata') or {}).items()
for section, content in (self.old_instance_json.get("metadata") or {}).items()
if section in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS)
}
self.diff['before']['instance'] = self.old_sections
self.diff["before"]["instance"] = self.old_sections
# preliminary, will be overwritten in _apply_instance_configs() if called
self.diff['after']['instance'] = self.config
self.diff["after"]["instance"] = self.config
self.old_state = self._instance_json_to_module_state(self.old_instance_json)
self.diff['before']['state'] = self.old_state
self.diff['after']['state'] = self.state
self.diff["before"]["state"] = self.old_state
self.diff["after"]["state"] = self.state
action = getattr(self, LXD_ANSIBLE_STATES[self.state])
action()
state_changed = len(self.actions) > 0
result_json = {
'log_verbosity': self.module._verbosity,
'changed': state_changed,
'old_state': self.old_state,
'actions': self.actions,
'diff': self.diff,
"log_verbosity": self.module._verbosity,
"changed": state_changed,
"old_state": self.old_state,
"actions": self.actions,
"diff": self.diff,
}
if self.client.debug:
result_json['logs'] = self.client.logs
result_json["logs"] = self.client.logs
if self.addresses is not None:
result_json['addresses'] = self.addresses
result_json["addresses"] = self.addresses
self.module.exit_json(**result_json)
except LXDClientException as e:
state_changed = len(self.actions) > 0
fail_params = {
'msg': e.msg,
'changed': state_changed,
'actions': self.actions,
'diff': self.diff,
"msg": e.msg,
"changed": state_changed,
"actions": self.actions,
"diff": self.diff,
}
if self.client.debug:
fail_params['logs'] = e.kwargs['logs']
fail_params["logs"] = e.kwargs["logs"]
self.module.fail_json(**fail_params)
@ -802,80 +794,77 @@ def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(
type='str',
type="str",
required=True,
),
project=dict(
type='str',
type="str",
),
architecture=dict(
type='str',
type="str",
),
config=dict(
type='dict',
type="dict",
),
ignore_volatile_options=dict(
type='bool',
type="bool",
default=False,
),
devices=dict(
type='dict',
type="dict",
),
ephemeral=dict(
type='bool',
type="bool",
),
profiles=dict(
type='list',
elements='str',
type="list",
elements="str",
),
source=dict(
type='dict',
type="dict",
),
state=dict(
choices=list(LXD_ANSIBLE_STATES.keys()),
default='started',
default="started",
),
target=dict(
type='str',
),
timeout=dict(
type='int',
default=30
type="str",
),
timeout=dict(type="int", default=30),
type=dict(
type='str',
default='container',
choices=['container', 'virtual-machine'],
type="str",
default="container",
choices=["container", "virtual-machine"],
),
wait_for_container=dict(
type='bool',
type="bool",
default=False,
),
wait_for_ipv4_addresses=dict(
type='bool',
type="bool",
default=False,
),
force_stop=dict(
type='bool',
type="bool",
default=False,
),
url=dict(
type='str',
type="str",
default=ANSIBLE_LXD_DEFAULT_URL,
),
snap_url=dict(
type='str',
default='unix:/var/snap/lxd/common/lxd/unix.socket',
type="str",
default="unix:/var/snap/lxd/common/lxd/unix.socket",
),
client_key=dict(
type='path',
aliases=['key_file'],
type="path",
aliases=["key_file"],
),
client_cert=dict(
type='path',
aliases=['cert_file'],
type="path",
aliases=["cert_file"],
),
trust_password=dict(type='str', no_log=True),
trust_password=dict(type="str", no_log=True),
),
supports_check_mode=True,
)
@ -884,5 +873,5 @@ def main():
lxd_manage.run()
if __name__ == '__main__':
if __name__ == "__main__":
main()