From eb4313ca497b380229916f33f8ef54657188a288 Mon Sep 17 00:00:00 2001
From: Jeffrey Zhang <zhang.lei.fly@gmail.com>
Date: Mon, 7 Nov 2016 19:23:33 +0800
Subject: [PATCH] Move glance precheck into its own role

Depends-On: I0773851b61d341117ab214382856a9036aca51bb
Change-Id: I060e21ada928577e833de2782be5ea570be32730
Partially-implements: blueprint condition-pre-check
---
 ansible/library/kolla_container_facts.py      | 82 +++++++++++++++++++
 ansible/roles/glance/tasks/precheck.yml       | 26 ++++++
 ansible/roles/haproxy/tasks/precheck.yml      | 36 ++++++++
 ansible/roles/prechecks/tasks/port_checks.yml | 40 ---------
 4 files changed, 144 insertions(+), 40 deletions(-)
 create mode 100644 ansible/library/kolla_container_facts.py

diff --git a/ansible/library/kolla_container_facts.py b/ansible/library/kolla_container_facts.py
new file mode 100644
index 0000000000..a2c73009d7
--- /dev/null
+++ b/ansible/library/kolla_container_facts.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python
+
+# Copyright 2016 99cloud
+#
+# 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.
+
+DOCUMENTATION = '''
+---
+module: kolla_container_facts
+short_description: Module for collecting Docker container facts
+description:
+  - A module targeting at collecting Docker container facts. It is used for
+    detecting whether the container is running on host in Kolla.
+options:
+  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 containers
+    required: False
+    type: str or list
+author: Jeffrey Zhang
+'''
+
+EXAMPLES = '''
+- hosts: all
+  tasks:
+    - name: Gather docker facts
+      kolla_container_facts:
+
+    - name: Gather glance container facts
+      kolla_container_facts:
+        name:
+          - glance_api
+          - glance_registry
+'''
+
+import docker
+
+
+def main():
+    argument_spec = dict(
+        name=dict(required=False, type='list', default=[]),
+        api_version=dict(required=False, type='str', default='auto')
+    )
+
+    module = AnsibleModule(argument_spec=argument_spec)
+
+    results = dict(changed=False, _containers=[])
+    client = docker.Client(version=module.params.get('api_version'))
+    containers = client.containers()
+    names = module.params.get('name')
+    if names and not isinstance(names, list):
+        names = [names]
+    for container in containers:
+        for container_name in container['Names']:
+            # remove '/' prefix character
+            container_name = container_name[1:]
+            if names and container_name not in names:
+                continue
+            results['_containers'].append(container)
+            results[container_name] = container
+    module.exit_json(**results)
+
+
+from ansible.module_utils.basic import *  # noqa
+if __name__ == "__main__":
+    main()
diff --git a/ansible/roles/glance/tasks/precheck.yml b/ansible/roles/glance/tasks/precheck.yml
index ed97d539c0..37876d4f41 100644
--- a/ansible/roles/glance/tasks/precheck.yml
+++ b/ansible/roles/glance/tasks/precheck.yml
@@ -1 +1,27 @@
 ---
+- name: Get container facts
+  kolla_container_facts:
+    name:
+      - glance_api
+      - glance_registry
+  register: container_facts
+
+- name: Checking free port for Glance API
+  wait_for:
+    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
+    port: "{{ glance_api_port }}"
+    connect_timeout: 1
+    state: stopped
+  when:
+    - inventory_hostname in groups['glance-api']
+    - container_facts['glance_api'] is not defined
+
+- name: Checking free port for Glance Registry
+  wait_for:
+    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
+    port: "{{ glance_registry_port }}"
+    connect_timeout: 1
+    state: stopped
+  when:
+    - inventory_hostname in groups['glance-registry']
+    - container_facts['glance_registry'] is not defined
diff --git a/ansible/roles/haproxy/tasks/precheck.yml b/ansible/roles/haproxy/tasks/precheck.yml
index ed97d539c0..0dc35c9802 100644
--- a/ansible/roles/haproxy/tasks/precheck.yml
+++ b/ansible/roles/haproxy/tasks/precheck.yml
@@ -1 +1,37 @@
 ---
+- name: Get container facts
+  kolla_container_facts:
+    name: haproxy
+  register: container_facts
+
+- name: Getting haproxy stat
+  shell: echo "show stat" | docker exec -i haproxy socat unix-connect:/var/lib/kolla/haproxy/haproxy.sock stdio
+  register: haproxy_stat_shell
+  changed_when: false
+  failed_when: false
+  when: container_facts['haproxy'] is defined
+
+- set_fact:
+    haproxy_stat: "{{ haproxy_stat_shell.stdout|default('') }}"
+
+- name: Checking free port for Glance API HAProxy
+  wait_for:
+    host: "{{ kolla_internal_vip_address }}"
+    port: "{{ glance_api_port }}"
+    connect_timeout: 1
+    state: stopped
+  when:
+    - enable_glance | bool
+    - inventory_hostname in groups['haproxy']
+    - "{{ 'glance_api' not in haproxy_stat }}"
+
+- name: Checking free port for Glance Registry HAProxy
+  wait_for:
+    host: "{{ kolla_internal_vip_address }}"
+    port: "{{ glance_registry_port }}"
+    connect_timeout: 1
+    state: stopped
+  when:
+    - enable_glance | bool
+    - inventory_hostname in groups['haproxy']
+    - "{{ 'glance_registry' not in haproxy_stat }}"
diff --git a/ansible/roles/prechecks/tasks/port_checks.yml b/ansible/roles/prechecks/tasks/port_checks.yml
index 7aa1e0d673..1968b6153c 100644
--- a/ansible/roles/prechecks/tasks/port_checks.yml
+++ b/ansible/roles/prechecks/tasks/port_checks.yml
@@ -173,46 +173,6 @@
     - inventory_hostname in groups['etcd']
     - enable_etcd | bool
 
-- name: Checking free port for Glance API
-  wait_for:
-    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
-    port: "{{ glance_api_port }}"
-    connect_timeout: 1
-    state: stopped
-  when:
-    - inventory_hostname in groups['glance-api']
-    - enable_glance | bool
-
-- name: Checking free port for Glance API HAProxy
-  wait_for:
-    host: "{{ kolla_internal_vip_address }}"
-    port: "{{ glance_api_port }}"
-    connect_timeout: 1
-    state: stopped
-  when:
-    - inventory_hostname in groups['haproxy']
-    - enable_glance | bool
-
-- name: Checking free port for Glance Registry
-  wait_for:
-    host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"
-    port: "{{ glance_registry_port }}"
-    connect_timeout: 1
-    state: stopped
-  when:
-    - inventory_hostname in groups['glance-registry']
-    - enable_glance | bool
-
-- name: Checking free port for Glance Registry HAProxy
-  wait_for:
-    host: "{{ kolla_internal_vip_address }}"
-    port: "{{ glance_registry_port }}"
-    connect_timeout: 1
-    state: stopped
-  when:
-    - inventory_hostname in groups['haproxy']
-    - enable_glance | bool
-
 - name: Checking free port for Gnocchi API
   wait_for:
     host: "{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}"