From 32cdbd23d5dee8dbfe22570b61981f3e7000aa68 Mon Sep 17 00:00:00 2001 From: Ivan Halomi Date: Mon, 11 Mar 2024 15:29:17 +0100 Subject: Merge of container_facts modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merge container_facts and container_volume_facts into a single module for retrieving all information about containers and volumes. Change-Id: I5d321b8326edd7f3b7a11dbdc821e534f457f9d7 Signed-off-by: Ivan Halomi Signed-off-by: Roman KrĨek --- ansible/library/kolla_container_facts.py | 32 ++++++- ansible/library/kolla_container_volume_facts.py | 108 ------------------------ 2 files changed, 30 insertions(+), 110 deletions(-) delete mode 100644 ansible/library/kolla_container_volume_facts.py (limited to 'ansible/library') diff --git a/ansible/library/kolla_container_facts.py b/ansible/library/kolla_container_facts.py index 041aa3904..18c194c9f 100644 --- a/ansible/library/kolla_container_facts.py +++ b/ansible/library/kolla_container_facts.py @@ -1,4 +1,5 @@ # Copyright 2016 99cloud +# Copyright 2023 StackHPC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -45,7 +46,7 @@ options: - The action to perform required: True type: str -author: Jeffrey Zhang +author: Jeffrey Zhang, Michal Nasiadka ''' EXAMPLES = ''' @@ -76,6 +77,18 @@ EXAMPLES = ''' name: - glance_api action: get_containers_env + + - name: Gather glance volume facts + kolla_container_facts: + container_engine: docker + name: + - glance_api + action: get_volumes + + - name: Gather all volume facts + kolla_container_facts: + container_engine: docker + action: get_volumes ''' @@ -150,6 +163,20 @@ class ContainerFactsWorker(): envs = self._remap_envs(cont['Config']['Env']) self.result['envs'][name] = envs + def get_volumes(self): + """Handles when module is called with action get_volumes.""" + names = self.params.get('name') + self.result['volumes'] = dict() + + if isinstance(names, str): + names = [names] + + volumes = self.client.volumes.list() + for volume in volumes: + if names and volume.name not in names: + continue + self.result['volumes'][volume.name] = volume.attrs + class DockerFactsWorker(ContainerFactsWorker): def __init__(self, module): @@ -188,7 +215,8 @@ def main(): action=dict(required=True, type='str', choices=['get_containers', 'get_containers_env', - 'get_containers_state']), + 'get_containers_state', + 'get_volumes']), ) required_if = [ diff --git a/ansible/library/kolla_container_volume_facts.py b/ansible/library/kolla_container_volume_facts.py deleted file mode 100644 index a5aba0c9e..000000000 --- a/ansible/library/kolla_container_volume_facts.py +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright 2023 StackHPC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -from ansible.module_utils.basic import AnsibleModule - -DOCUMENTATION = ''' ---- -module: kolla_container_volume_facts -short_description: Module for collecting container volume facts -description: - - A module targeted at collecting container volume facts. It is used - for detecting whether the container volume exists on a host. -options: - container_engine: - description: - - Name of container engine to use - required: True - type: str - api_version: - description: - - The version of the api for docker-py to use when contacting docker - required: False - type: str - default: auto - name: - description: - - Name or names of the container volumes - required: False - type: str or list -author: Jeffrey Zhang / Michal Nasiadka -''' - -EXAMPLES = ''' -- hosts: all - tasks: - - name: Gather docker facts - kolla_container_volume_facts: - - - name: Gather glance container facts - kolla_container_volume_facts: - container_engine: docker - name: - - glance_api - - glance_registry -''' - - -def get_docker_client(): - import docker - return docker.APIClient - - -def get_docker_volumes(api_version): - client = get_docker_client()(version=api_version) - return client.volumes()['Volumes'] - - -def get_podman_volumes(): - from podman import PodmanClient - - client = PodmanClient(base_url="http+unix:/run/podman/podman.sock") - volumes = [] - for volume in client.volumes.list(): - volumes.append(volume.attrs) - return volumes - - -def main(): - argument_spec = dict( - name=dict(required=False, type='list', default=[]), - api_version=dict(required=False, type='str', default='auto'), - container_engine=dict(required=True, type='str') - ) - - module = AnsibleModule(argument_spec=argument_spec) - - results = dict(changed=False, _volumes=[]) - if module.params.get('container_engine') == 'docker': - volumes = get_docker_volumes(module.params.get('api_version')) - else: - volumes = get_podman_volumes() - - names = module.params.get('name') - if names and not isinstance(names, list): - names = [names] - for volume in volumes: - volume_name = volume['Name'] - if names and volume_name not in names: - continue - results['_volumes'].append(volume) - results[volume_name] = volume - module.exit_json(**results) - - -if __name__ == "__main__": - main() -- cgit v1.2.3