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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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