diff options
author | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2019-04-26 13:26:45 +0200 |
---|---|---|
committer | Bartek Grzybowski <b.grzybowski@partner.samsung.com> | 2019-04-26 15:43:11 +0200 |
commit | ee157b53be7c176081aed96a74fe8ff09b208cb8 (patch) | |
tree | 12af04e01a5753136ce0cff60fd5cedfa009a63a /ansible/library | |
parent | cb94aefb4ef22fdf435578e676823a6b9600cbb9 (diff) |
Cleanup Rancher containers after Molecule converge
Rancher role test do remove rancher/agent and rancher/server
containers at 'cleanup' stage but Rancher Agent spawned
containers are left orphaned. This patch adds tasks to
remove them.
This patch also adds custom ansible module for listing
docker containers (as of ansible 2.7.10 no upstream module
provides that feature, even new docker_container_info info
module in ansible's devel branch doesn't have it)
Change-Id: I6325dc81063b55b70136280273f8f6138c7a0375
Issue-ID: OOM-1811
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
Diffstat (limited to 'ansible/library')
-rw-r--r-- | ansible/library/docker_list_containers.py | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/ansible/library/docker_list_containers.py b/ansible/library/docker_list_containers.py new file mode 100644 index 00000000..d8a789c6 --- /dev/null +++ b/ansible/library/docker_list_containers.py @@ -0,0 +1,99 @@ +#!/usr/bin/python + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = ''' +--- +module: docker_list_containers + +short_description: "List running docker containers" + +description: + - "Lists all running containers or those with matching label" + +options: + label_name: + description: + - container label name to match + required: false + label_value: + description: + - container label value to match + required: false + +author: + - Bartek Grzybowski (b.grzybowski@partner.samsung.com) +''' + +EXAMPLES = ''' +# List all running containers +- name: List containers + docker_list_containers: + +# List all running containers matching label +- name: List containers + docker_list_containers: + label_name: 'io.rancher.project.name' + label_value: 'kubernetes' +''' + +RETURN = ''' +containers: + description: List of running containers matching module criteria + type: list + returned: always + sample: [ + "rancher-agent", + "rancher-server", + "kubernetes-node-1", + "infrastructure-server" + ] +''' + +from ansible.module_utils.docker_common import AnsibleDockerClient + +class DockerListContainers: + + def __init__(self): + self.docker_client = AnsibleDockerClient( + argument_spec=dict( + label_name=dict(type='str', required=False), + label_value=dict(type='str', required=False) + ) + ) + + self.containers = self.docker_client.containers() + self.label_name=self.docker_client.module.params.get('label_name') + self.label_value=self.docker_client.module.params.get('label_value') + + if self.label_name: + self.containers_names=self._get_containers_names_by_label() + else: + self.containers_names=self._get_containers_names() + + self.result=dict( + containers=self.containers_names, + changed=False + ) + + def _get_containers_names(self): + return [str(container_meta.get('Names')[0][1:]) for container_meta in self.containers if 'Names' in container_meta] + + def _get_containers_names_by_label(self): + names=[] + for container_meta in self.containers: + if container_meta.get('Labels',{}).get(self.label_name) == self.label_value: + names.append(str(container_meta['Names'][0][1:])) # strip leading '/' in container name and convert to str from unicode + + return names + +def main(): + cont=DockerListContainers() + cont.docker_client.module.exit_json(**cont.result) + +if __name__ == '__main__': + main() |