From 18fa72c901434c023ab68754a2e68d726484f608 Mon Sep 17 00:00:00 2001
From: Jeffrey Zhang <zhang.lei.fly@gmail.com>
Date: Thu, 29 Sep 2016 01:14:55 +0800
Subject: [PATCH] integrate gnocchi with ceilometer

Closes-Bug: #1626364
Change-Id: I9d586b950b7099a9b160f7b32c9ff00b189a0287
---
 ansible/group_vars/all.yml                    |  2 +-
 ansible/roles/ceilometer/defaults/main.yml    |  7 +++----
 ansible/roles/ceilometer/tasks/bootstrap.yml  |  3 ---
 .../ceilometer/tasks/bootstrap_service.yml    |  1 +
 .../ceilometer/templates/ceilometer.conf.j2   | 20 +++++++++++++++----
 .../ceilometer/ceilometer-api/extend_start.sh | 11 +++++++++-
 .../ceilometer/ceilometer-base/Dockerfile.j2  |  6 +++---
 etc/kolla/globals.yml                         |  2 +-
 8 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml
index 73e11047cc..9085a74ed3 100644
--- a/ansible/group_vars/all.yml
+++ b/ansible/group_vars/all.yml
@@ -344,7 +344,7 @@ glance_backend_ceph: "{{ enable_ceph }}"
 #######################
 # Ceilometer options
 #######################
-# Valid options are [ mongodb, mysql ]
+# Valid options are [ mongodb, mysql, gnocchi ]
 ceilometer_database_type: "mongodb"
 
 
diff --git a/ansible/roles/ceilometer/defaults/main.yml b/ansible/roles/ceilometer/defaults/main.yml
index fc30fc8dee..b914e62d95 100644
--- a/ansible/roles/ceilometer/defaults/main.yml
+++ b/ansible/roles/ceilometer/defaults/main.yml
@@ -7,10 +7,9 @@ project_name: "ceilometer"
 ####################
 ceilometer_database_name: "ceilometer"
 ceilometer_database_user: "ceilometer"
-ceilometer_database_address_mappings:
-  mongodb: "{% for host in groups['mongodb'] %}{{ hostvars[host]['ansible_' + hostvars[host]['api_interface']]['ipv4']['address'] }}:{{ mongodb_port }}{% if not loop.last %},{% endif %}{% endfor %}"
-  mysql: "{{ kolla_internal_fqdn }}:{{ database_port }}"
-ceilometer_database_address: "{{ ceilometer_database_address_mappings[ceilometer_database_type] }}"
+
+ceilometer_database_mongodb_address: "{% for host in groups['mongodb'] %}{{ hostvars[host]['ansible_' + hostvars[host]['api_interface']]['ipv4']['address'] }}:{{ mongodb_port }}{% if not loop.last %},{% endif %}{% endfor %}"
+ceilometer_database_mysql_address: "{{ kolla_internal_fqdn }}:{{ database_port }}"
 
 
 ####################
diff --git a/ansible/roles/ceilometer/tasks/bootstrap.yml b/ansible/roles/ceilometer/tasks/bootstrap.yml
index e6780394f5..0e6f77fc21 100644
--- a/ansible/roles/ceilometer/tasks/bootstrap.yml
+++ b/ansible/roles/ceilometer/tasks/bootstrap.yml
@@ -49,6 +49,3 @@
     - ceilometer_database_type == "mysql"
 
 - include: bootstrap_service.yml
-  when:
-    - ceilometer_database_type == "mysql"
-    - database.created
diff --git a/ansible/roles/ceilometer/tasks/bootstrap_service.yml b/ansible/roles/ceilometer/tasks/bootstrap_service.yml
index 904fa15bae..487b7999fd 100644
--- a/ansible/roles/ceilometer/tasks/bootstrap_service.yml
+++ b/ansible/roles/ceilometer/tasks/bootstrap_service.yml
@@ -7,6 +7,7 @@
     environment:
       KOLLA_BOOTSTRAP:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
+      CEILOMETER_DATABASE_TYPE: "{{ ceilometer_database_type }}"
     image: "{{ ceilometer_api_image_full }}"
     labels:
       BOOTSTRAP:
diff --git a/ansible/roles/ceilometer/templates/ceilometer.conf.j2 b/ansible/roles/ceilometer/templates/ceilometer.conf.j2
index cb27a26f8e..2bffb57e49 100644
--- a/ansible/roles/ceilometer/templates/ceilometer.conf.j2
+++ b/ansible/roles/ceilometer/templates/ceilometer.conf.j2
@@ -5,6 +5,11 @@ log_dir = /var/log/kolla/ceilometer
 
 transport_url = rabbit://{% for host in groups['rabbitmq'] %}{{ rabbitmq_user }}:{{ rabbitmq_password }}@{{ hostvars[host]['ansible_' + hostvars[host]['api_interface']]['ipv4']['address'] }}:{{ rabbitmq_port }}{% if not loop.last %},{% endif %}{% endfor %}
 
+{% if ceilometer_database_type == 'gnocchi' %}
+meter_dispatchers = gnocchi
+event_dispatchers = gnocchi
+{% endif %}
+
 [api]
 port = {{ ceilometer_api_port }}
 host = {{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}
@@ -13,11 +18,11 @@ host = {{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['addr
 
 [database]
 {% if ceilometer_database_type == "mongodb" %}
-event_connection = mongodb://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_address }}/{{ ceilometer_database_name }}
-metering_connection = mongodb://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_address }}/{{ ceilometer_database_name }}
+event_connection = mongodb://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_mongodb_address }}/{{ ceilometer_database_name }}
+metering_connection = mongodb://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_mongodb_address }}/{{ ceilometer_database_name }}
 {% elif ceilometer_database_type == "mysql" %}
-event_connection = mysql+pymysql://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_address }}/{{ ceilometer_database_name }}
-metering_connection = mysql+pymysql://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_address }}/{{ ceilometer_database_name }}
+event_connection = mysql+pymysql://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_mysql_address }}/{{ ceilometer_database_name }}
+metering_connection = mysql+pymysql://{{ ceilometer_database_user }}:{{ ceilometer_database_password }}@{{ ceilometer_database_mysql_address }}/{{ ceilometer_database_name }}
 {% endif %}
 
 [keystone_authtoken]
@@ -43,3 +48,10 @@ project_name = service
 project_domain_id = default
 user_domain_id = default
 auth_type = password
+
+{% if ceilometer_database_type == 'gnocchi' %}
+[dispatcher_gnocchi]
+filter_service_activity = False
+url = {{ internal_protocol }}://{{ kolla_internal_fqdn }}:{{ gnocchi_api_port }}
+archive_policy = low
+{% endif %}
diff --git a/docker/ceilometer/ceilometer-api/extend_start.sh b/docker/ceilometer/ceilometer-api/extend_start.sh
index 7918a86053..1780742d04 100644
--- a/docker/ceilometer/ceilometer-api/extend_start.sh
+++ b/docker/ceilometer/ceilometer-api/extend_start.sh
@@ -3,7 +3,16 @@
 # Bootstrap and exit if KOLLA_BOOTSTRAP variable is set. This catches all cases
 # of the KOLLA_BOOTSTRAP variable being set, including empty.
 if [[ "${!KOLLA_BOOTSTRAP[@]}" ]]; then
-    sudo -H -u ceilometer ceilometer-upgrade --skip-gnocchi-resource-types
+    if [[ "${CEILOMETER_DATABASE_TYPE}" == "mysql" ]]; then
+        sudo -H -u ceilometer ceilometer-upgrade --skip-gnocchi-resource-types
+    elif [[ "${CEILOMETER_DATABASE_TYPE}" == "gnocchi" ]]; then
+        sudo -H -u ceilometer ceilometer-upgrade --skip-metering-database --skip-event-database
+    elif [[ "${CEILOMETER_DATABASE_TYPE}" == "mongodb" ]]; then
+        echo "Ceilometer doesn't need to initialize a database when MongoDB is configured as the database backend."
+    else
+        echo "Unsupported database type: ${CEILOMETER_DATABASE_TYPE}"
+        exit 1
+    fi
     sudo chown -R ceilometer: /var/lib/ceilometer/
     exit 0
 fi
diff --git a/docker/ceilometer/ceilometer-base/Dockerfile.j2 b/docker/ceilometer/ceilometer-base/Dockerfile.j2
index bc39a8fcf6..861ea018d1 100644
--- a/docker/ceilometer/ceilometer-base/Dockerfile.j2
+++ b/docker/ceilometer/ceilometer-base/Dockerfile.j2
@@ -26,10 +26,10 @@ MAINTAINER {{ maintainer }}
 ADD ceilometer-base-archive /ceilometer-base-source
 RUN ln -s ceilometer-base-source/* ceilometer \
     && useradd --user-group ceilometer \
-    && /var/lib/kolla/venv/bin/pip --no-cache-dir install --upgrade -c requirements/upper-constraints.txt /ceilometer pymongo \
-    && mkdir -p /etc/ceilometer /var/log/ceilometer /home/ceilometer \
+    && /var/lib/kolla/venv/bin/pip --no-cache-dir install --upgrade -c requirements/upper-constraints.txt /ceilometer[gnocchi,mongo,mysql] \
+    && mkdir -p /etc/ceilometer /var/lib/ceilometer /home/ceilometer \
     && cp -r /ceilometer/etc/ceilometer/* /etc/ceilometer/ \
-    && chown -R ceilometer: /etc/ceilometer /var/log/ceilometer /home/ceilometer \
+    && chown -R ceilometer: /etc/ceilometer /var/lib/ceilometer /home/ceilometer \
     && sed -i 's|^exec_dirs.*|exec_dirs=/var/lib/kolla/venv/bin,/sbin,/usr/sbin,/bin,/usr/bin,/usr/local/bin,/usr/local/sbin|g' /etc/ceilometer/rootwrap.conf
 
 {% endif %}
diff --git a/etc/kolla/globals.yml b/etc/kolla/globals.yml
index be588e7e0d..a4744777f8 100644
--- a/etc/kolla/globals.yml
+++ b/etc/kolla/globals.yml
@@ -189,7 +189,7 @@ kolla_internal_vip_address: "10.10.10.254"
 #######################
 # Ceilometer options
 #######################
-# Valid options are [ mongodb, mysql ]
+# Valid options are [ mongodb, mysql, gnocchi ]
 #ceilometer_database_type: "mongodb"