Refactor of kolla_container_facts

Refactor that prepares kolla_container_facts
module for introducing more actions that will be moved
from kolla_container module and kolla_container_volume_facts.

This change is based on a discussion about adding a new action
to kolla_container module that retrieves all names of the running
containers. It was agreed that kolla-ansible should follow Ansible's
direction of splitting modules between action modules and facts
modules. Because of this, kolla_container_facts needs to be able
to handle different requests for data about containers or volumes.

Change-Id: Ieaec8f64922e4e5a2199db2d6983518b124cb4aa
Signed-off-by: Ivan Halomi <ivan.halomi@tietoevry.com>
This commit is contained in:
Ivan Halomi 2024-03-06 10:53:21 +01:00
parent 416574c8b4
commit 4ce47e2250
54 changed files with 138 additions and 49 deletions

View File

@ -12,8 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from abc import ABC
from abc import abstractmethod
from ansible.module_utils.basic import AnsibleModule
from traceback import format_exc
DOCUMENTATION = '''
@ -40,9 +42,9 @@ options:
- Name or names of the containers
required: False
type: str or list
container_engine:
action:
description:
- Name of container engine to use
- The action to perform
required: True
type: str
author: Jeffrey Zhang
@ -54,6 +56,7 @@ EXAMPLES = '''
- name: Gather docker facts
kolla_container_facts:
container_engine: docker
action: get_containers
- name: Gather glance container facts
kolla_container_facts:
@ -62,19 +65,35 @@ EXAMPLES = '''
- glance_api
- glance_registry
container_engine: podman
action: get_containers
'''
def get_docker_client():
class ContainerFactsWorker(ABC):
def __init__(self, module):
self.module = module
self.results = dict(changed=False, _containers=[])
self.params = module.params
@abstractmethod
def get_containers(self):
pass
class DockerFactsWorker(ContainerFactsWorker):
def __init__(self, module):
super().__init__(module)
try:
import docker
return docker.APIClient
except ImportError:
self.module.fail_json(
msg="The docker library could not be imported")
self.client = docker.APIClient(version=module.params.get(
'api_version'))
def use_docker(module, results):
client = get_docker_client()(version=module.params.get('api_version'))
containers = client.containers()
names = module.params.get('name')
def get_containers(self):
containers = self.client.containers()
names = self.params.get('name')
if names and not isinstance(names, list):
names = [names]
for container in containers:
@ -83,49 +102,66 @@ def use_docker(module, results):
container_name = container_name[1:]
if names and container_name not in names:
continue
results['_containers'].append(container)
results[container_name] = container
self.results['_containers'].append(container)
self.results[container_name] = container
def use_podman(module, results):
import podman.errors as pe
from podman import PodmanClient
client = PodmanClient(base_url="http+unix:/run/podman/podman.sock")
class PodmanFactsWorker(ContainerFactsWorker):
def __init__(self, module):
try:
containers = client.containers.list(all=True, ignore_removed=True)
except pe.APIError as e:
module.fail_json(failed=True, msg=f"Internal error: {e.explanation}")
names = module.params.get('name')
import podman.errors as podmanError
from podman import PodmanClient
except ImportError:
self.module.fail_json(
msg="The podman library could not be imported")
self.podmanError = podmanError
super().__init__(module)
self.client = PodmanClient(
base_url="http+unix:/run/podman/podman.sock")
def get_containers(self):
try:
containers = self.client.containers.list(
all=True, ignore_removed=True)
except self.podmanError.APIError as e:
self.module.fail_json(failed=True,
msg=f"Internal error: {e.explanation}")
names = self.params.get('name')
if names and not isinstance(names, list):
names = [names]
for container in containers:
container.reload()
container_name = container.attrs['Name']
if names and container_name not in names:
if container_name not in names:
continue
results['_containers'].append(container.attrs)
results[container_name] = container.attrs
self.results['_containers'].append(container.attrs)
self.results[container_name] = container.attrs
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')
container_engine=dict(required=True, type='str'),
action=dict(required=True, type='str',
choices=['get_containers']),
)
module = AnsibleModule(argument_spec=argument_spec)
results = dict(changed=False, _containers=[])
if module.params['container_engine'] == 'podman':
use_podman(module, results)
cw: ContainerFactsWorker = None
try:
if module.params.get('container_engine') == 'docker':
cw = DockerFactsWorker(module)
else:
use_docker(module, results)
cw = PodmanFactsWorker(module)
module.exit_json(**results)
result = bool(getattr(cw, module.params.get('action'))())
module.exit_json(result=result, **cw.results)
except Exception:
module.fail_json(changed=True, msg=repr(format_exc()),
**getattr(cw, 'result', {}))
if __name__ == "__main__":

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- aodh_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- barbican_api

View File

@ -3,6 +3,7 @@
- name: Check if bifrost_deploy container is running
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- bifrost_deploy

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- blazar_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- cinder_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- cloudkitty_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- cyborg_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- designate_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- etcd

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name: "{{ glance_services.values() | map(attribute='container_name') | list }}"
check_mode: false

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- gnocchi_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- grafana

View File

@ -2,6 +2,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- hacluster_pacemaker_remote

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- heat_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- horizon

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- influxdb

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- ironic_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- iscsid

View File

@ -2,6 +2,7 @@
- name: Checking for any running keystone_fernet containers
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- keystone_fernet

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- keystone

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- kuryr

View File

@ -2,6 +2,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- letsencrypt_webserver

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- haproxy

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- magnum_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- manila_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- mariadb

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- masakari_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- memcached

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- mistral_api

View File

@ -90,6 +90,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- neutron_l3_agent

View File

@ -2,6 +2,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- neutron_openvswitch_agent

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- neutron_server

View File

@ -9,6 +9,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- nova_libvirt

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- nova_libvirt

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- nova_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- octavia_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- opensearch

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- openvswitch_db

View File

@ -2,6 +2,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- ovn_nb_db

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- placement_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- prometheus_server

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- rabbitmq

View File

@ -3,6 +3,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- "{{ service.container_name }}"

View File

@ -2,6 +2,7 @@
- name: Get info on RabbitMQ container
become: True
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name: "rabbitmq"
register: container_info

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- redis

View File

@ -2,6 +2,7 @@
- name: "{{ project_name }} : {{ service.container_name }} | Get info on container"
become: True
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- "{{ service.container_name }}"

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- skyline_apiserver

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- swift_account_server

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- tacker_server

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- trove_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- venus_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- watcher_api

View File

@ -8,6 +8,7 @@
- name: Get container facts
become: true
kolla_container_facts:
action: get_containers
container_engine: "{{ kolla_container_engine }}"
name:
- zun_api