From d3d3472326598b89a2d589e4446ac167d989521a Mon Sep 17 00:00:00 2001
From: zhubingbing <1392607554@qq.com>
Date: Wed, 31 Aug 2016 07:46:41 +0000
Subject: [PATCH] Implement Ansible rally role

Change-Id: I647c38adbfd00c70874cf51c0bfcb68d243e26cc
Partially-Implements: blueprint rally-role
---
 ansible/group_vars/all.yml                    |  1 +
 ansible/inventory/all-in-one                  |  3 +
 ansible/inventory/multinode                   |  3 +
 ansible/roles/rally/defaults/main.yml         | 18 ++++++
 ansible/roles/rally/meta/main.yml             |  3 +
 ansible/roles/rally/tasks/bootstrap.yml       | 41 ++++++++++++
 .../roles/rally/tasks/bootstrap_service.yml   | 20 ++++++
 ansible/roles/rally/tasks/config.yml          | 26 ++++++++
 ansible/roles/rally/tasks/deploy.yml          |  4 ++
 ansible/roles/rally/tasks/do_reconfigure.yml  | 64 +++++++++++++++++++
 ansible/roles/rally/tasks/main.yml            |  2 +
 ansible/roles/rally/tasks/pull.yml            |  6 ++
 ansible/roles/rally/tasks/reconfigure.yml     |  3 +
 ansible/roles/rally/tasks/start.yml           | 11 ++++
 ansible/roles/rally/tasks/upgrade.yml         |  4 ++
 ansible/roles/rally/templates/rally.conf.j2   |  9 +++
 ansible/roles/rally/templates/rally.json.j2   | 11 ++++
 ansible/site.yml                              |  6 ++
 docker/rally/extend_start.sh                  |  2 +-
 etc/kolla/globals.yml                         |  2 +-
 etc/kolla/passwords.yml                       |  2 +
 .../notes/add-rally-c6d1468accfb1da6.yaml     |  3 +
 22 files changed, 242 insertions(+), 2 deletions(-)
 create mode 100644 ansible/roles/rally/defaults/main.yml
 create mode 100644 ansible/roles/rally/meta/main.yml
 create mode 100644 ansible/roles/rally/tasks/bootstrap.yml
 create mode 100644 ansible/roles/rally/tasks/bootstrap_service.yml
 create mode 100644 ansible/roles/rally/tasks/config.yml
 create mode 100644 ansible/roles/rally/tasks/deploy.yml
 create mode 100644 ansible/roles/rally/tasks/do_reconfigure.yml
 create mode 100644 ansible/roles/rally/tasks/main.yml
 create mode 100644 ansible/roles/rally/tasks/pull.yml
 create mode 100644 ansible/roles/rally/tasks/reconfigure.yml
 create mode 100644 ansible/roles/rally/tasks/start.yml
 create mode 100644 ansible/roles/rally/tasks/upgrade.yml
 create mode 100644 ansible/roles/rally/templates/rally.conf.j2
 create mode 100644 ansible/roles/rally/templates/rally.json.j2
 create mode 100644 releasenotes/notes/add-rally-c6d1468accfb1da6.yaml

diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml
index 32d1ea79d3..f14b86b4ef 100644
--- a/ansible/group_vars/all.yml
+++ b/ansible/group_vars/all.yml
@@ -245,6 +245,7 @@ enable_murano: "no"
 enable_neutron_lbaas: "no"
 enable_neutron_qos: "no"
 enable_neutron_agent_ha: "no"
+enable_rally: "no"
 enable_swift: "no"
 enable_tempest: "no"
 enable_watcher: "no"
diff --git a/ansible/inventory/all-in-one b/ansible/inventory/all-in-one
index 8f03580079..ae7a684679 100644
--- a/ansible/inventory/all-in-one
+++ b/ansible/inventory/all-in-one
@@ -103,6 +103,9 @@ control
 [watcher:children]
 control
 
+[rally:children]
+control
+
 # Additional control implemented here. These groups allow you to control which
 # services run on which hosts at a per-service level.
 #
diff --git a/ansible/inventory/multinode b/ansible/inventory/multinode
index 7775448ae0..0e09ee8fb4 100644
--- a/ansible/inventory/multinode
+++ b/ansible/inventory/multinode
@@ -120,6 +120,9 @@ control
 [watcher:children]
 control
 
+[rally:children]
+control
+
 # Additional control implemented here. These groups allow you to control which
 # services run on which hosts at a per-service level.
 #
diff --git a/ansible/roles/rally/defaults/main.yml b/ansible/roles/rally/defaults/main.yml
new file mode 100644
index 0000000000..ced0999cb0
--- /dev/null
+++ b/ansible/roles/rally/defaults/main.yml
@@ -0,0 +1,18 @@
+---
+project_name: "rally"
+
+
+########
+# Docker
+########
+rally_image: "{{ docker_registry ~ '/' if docker_registry else '' }}{{ docker_namespace }}/{{ kolla_base_distro }}-{{ kolla_install_type }}-rally"
+rally_tag: "{{ openstack_release }}"
+rally_image_full: "{{ rally_image }}:{{ rally_tag }}"
+
+
+####################
+# Database
+####################
+rally_database_name: "rally"
+rally_database_user: "rally"
+rally_database_address: "{{ kolla_internal_fqdn }}:{{ database_port }}"
diff --git a/ansible/roles/rally/meta/main.yml b/ansible/roles/rally/meta/main.yml
new file mode 100644
index 0000000000..6b4fff8fef
--- /dev/null
+++ b/ansible/roles/rally/meta/main.yml
@@ -0,0 +1,3 @@
+---
+dependencies:
+  - { role: common }
diff --git a/ansible/roles/rally/tasks/bootstrap.yml b/ansible/roles/rally/tasks/bootstrap.yml
new file mode 100644
index 0000000000..9875c3e57b
--- /dev/null
+++ b/ansible/roles/rally/tasks/bootstrap.yml
@@ -0,0 +1,41 @@
+---
+- name: Creating rally database
+  command: docker exec -t kolla_toolbox /usr/bin/ansible localhost
+    -m mysql_db
+    -a "login_host='{{ database_address }}'
+        login_port='{{ database_port }}'
+        login_user='{{ database_user }}'
+        login_password='{{ database_password }}'
+        name='{{ rally_database_name }}'"
+  register: database
+  changed_when: "{{ database.stdout.find('localhost | SUCCESS => ') != -1 and
+                    (database.stdout.split('localhost | SUCCESS => ')[1]|from_json).changed }}"
+  failed_when: database.stdout.split()[2] != 'SUCCESS'
+  run_once: True
+  delegate_to: "{{ groups['rally'][0] }}"
+
+- name: Reading json from variable
+  set_fact:
+    database_created: "{{ (database.stdout.split('localhost | SUCCESS => ')[1]|from_json).changed }}"
+
+- name: Creating rally database user and setting permissions
+  command: docker exec -t kolla_toolbox /usr/bin/ansible localhost
+    -m mysql_user
+    -a "login_host='{{ database_address }}'
+        login_port='{{ database_port }}'
+        login_user='{{ database_user }}'
+        login_password='{{ database_password }}'
+        name='{{ rally_database_name }}'
+        password='{{ rally_database_password }}'
+        host='%'
+        priv='{{ rally_database_name }}.*:ALL'
+        append_privs='yes'"
+  register: database_user_create
+  changed_when: "{{ database_user_create.stdout.find('localhost | SUCCESS => ') != -1 and
+                    (database_user_create.stdout.split('localhost | SUCCESS => ')[1]|from_json).changed }}"
+  failed_when: database_user_create.stdout.split()[2] != 'SUCCESS'
+  run_once: True
+  delegate_to: "{{ groups['rally'][0] }}"
+
+- include: bootstrap_service.yml
+  when: database_created
diff --git a/ansible/roles/rally/tasks/bootstrap_service.yml b/ansible/roles/rally/tasks/bootstrap_service.yml
new file mode 100644
index 0000000000..6d5f1f0863
--- /dev/null
+++ b/ansible/roles/rally/tasks/bootstrap_service.yml
@@ -0,0 +1,20 @@
+---
+- name: Running rally bootstrap container
+  kolla_docker:
+    action: "start_container"
+    common_options: "{{ docker_common_options }}"
+    detach: False
+    environment:
+      KOLLA_BOOTSTRAP:
+      KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
+    image: "{{ rally_image_full }}"
+    labels:
+      BOOTSTRAP:
+    name: "bootstrap_rally"
+    restart_policy: "never"
+    volumes:
+      - "{{ node_config_directory }}/rally/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
+  run_once: True
+  delegate_to: "{{ groups['rally'][0] }}"
\ No newline at end of file
diff --git a/ansible/roles/rally/tasks/config.yml b/ansible/roles/rally/tasks/config.yml
new file mode 100644
index 0000000000..c210dc338f
--- /dev/null
+++ b/ansible/roles/rally/tasks/config.yml
@@ -0,0 +1,26 @@
+---
+- name: Ensuring config directories exist
+  file:
+    path: "{{ node_config_directory }}/{{ item }}"
+    state: "directory"
+    recurse: yes
+  with_items:
+    - "rally"
+
+- name: Copying over config.json files for services
+  template:
+    src: "{{ item }}.json.j2"
+    dest: "{{ node_config_directory }}/{{ item }}/config.json"
+  with_items:
+    - "rally"
+
+- name: Copying over rally.conf
+  merge_configs:
+    vars:
+      project_name: "rally"
+    sources:
+      - "{{ role_path }}/templates/rally.conf.j2"
+      - "{{ node_custom_config }}/rally.conf"
+    dest: "{{ node_config_directory }}/{{ item }}/rally.conf"
+  with_items:
+    - "rally"
diff --git a/ansible/roles/rally/tasks/deploy.yml b/ansible/roles/rally/tasks/deploy.yml
new file mode 100644
index 0000000000..1dc8c04e30
--- /dev/null
+++ b/ansible/roles/rally/tasks/deploy.yml
@@ -0,0 +1,4 @@
+---
+- include: config.yml
+- include: bootstrap.yml
+- include: start.yml
diff --git a/ansible/roles/rally/tasks/do_reconfigure.yml b/ansible/roles/rally/tasks/do_reconfigure.yml
new file mode 100644
index 0000000000..79c3dde5ac
--- /dev/null
+++ b/ansible/roles/rally/tasks/do_reconfigure.yml
@@ -0,0 +1,64 @@
+---
+- name: Ensuring the containers up
+  kolla_docker:
+    name: "{{ item.name }}"
+    action: "get_container_state"
+  register: container_state
+  failed_when: container_state.Running == false
+  when: inventory_hostname in groups[item.group]
+  with_items:
+    - { name: rally, group: rally }
+
+- include: config.yml
+
+- name: Check the configs
+  command: docker exec {{ item.name }} /usr/local/bin/kolla_set_configs --check
+  changed_when: false
+  failed_when: false
+  register: check_results
+  when: inventory_hostname in groups[item.group]
+  with_items:
+    - { name: rally, group: rally }
+
+# NOTE(jeffrey4l): when config_strategy == 'COPY_ALWAYS'
+# and container env['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE',
+# just remove the container and start again
+- name: Containers config strategy
+  kolla_docker:
+    name: "{{ item.name }}"
+    action: "get_container_env"
+  register: container_envs
+  when: inventory_hostname in groups[item.group]
+  with_items:
+    - { name: rally, group: rally }
+
+- name: Remove the containers
+  kolla_docker:
+    name: "{{ item[0]['name'] }}"
+    action: "remove_container"
+  register: remove_containers
+  when:
+    - config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE'
+    - item[2]['rc'] == 1
+    - inventory_hostname in groups[item[0]['group']]
+  with_together:
+    - [{ name: rally, group: rally }]
+    - "{{ container_envs.results }}"
+    - "{{ check_results.results }}"
+
+- include: start.yml
+  when: remove_containers.changed
+
+- name: Restart containers
+  kolla_docker:
+    name: "{{ item[0]['name'] }}"
+    action: "restart_container"
+  when:
+    - config_strategy == 'COPY_ALWAYS'
+    - item[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
+    - item[2]['rc'] == 1
+    - inventory_hostname in groups[item[0]['group']]
+  with_together:
+    - [{ name: rally, group: rally }]
+    - "{{ container_envs.results }}"
+    - "{{ check_results.results }}"
diff --git a/ansible/roles/rally/tasks/main.yml b/ansible/roles/rally/tasks/main.yml
new file mode 100644
index 0000000000..b017e8b4ad
--- /dev/null
+++ b/ansible/roles/rally/tasks/main.yml
@@ -0,0 +1,2 @@
+---
+- include: "{{ action }}.yml"
diff --git a/ansible/roles/rally/tasks/pull.yml b/ansible/roles/rally/tasks/pull.yml
new file mode 100644
index 0000000000..bcada160d8
--- /dev/null
+++ b/ansible/roles/rally/tasks/pull.yml
@@ -0,0 +1,6 @@
+---
+- name: Pulling rally image
+  kolla_docker:
+    action: "pull_image"
+    common_options: "{{ docker_common_options }}"
+    image: "{{ rally_image_full }}"
diff --git a/ansible/roles/rally/tasks/reconfigure.yml b/ansible/roles/rally/tasks/reconfigure.yml
new file mode 100644
index 0000000000..66933249bb
--- /dev/null
+++ b/ansible/roles/rally/tasks/reconfigure.yml
@@ -0,0 +1,3 @@
+---
+- include: do_reconfigure.yml
+  serial: "30%"
diff --git a/ansible/roles/rally/tasks/start.yml b/ansible/roles/rally/tasks/start.yml
new file mode 100644
index 0000000000..f95000fa17
--- /dev/null
+++ b/ansible/roles/rally/tasks/start.yml
@@ -0,0 +1,11 @@
+---
+- name: Starting rally container
+  kolla_docker:
+    action: "start_container"
+    common_options: "{{ docker_common_options }}"
+    image: "{{ rally_image_full }}"
+    name: "rally"
+    volumes:
+      - "{{ node_config_directory }}/rally/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_logs:/var/log/kolla/"
diff --git a/ansible/roles/rally/tasks/upgrade.yml b/ansible/roles/rally/tasks/upgrade.yml
new file mode 100644
index 0000000000..1f16915ad9
--- /dev/null
+++ b/ansible/roles/rally/tasks/upgrade.yml
@@ -0,0 +1,4 @@
+---
+- include: config.yml
+
+- include: start.yml
diff --git a/ansible/roles/rally/templates/rally.conf.j2 b/ansible/roles/rally/templates/rally.conf.j2
new file mode 100644
index 0000000000..853751871b
--- /dev/null
+++ b/ansible/roles/rally/templates/rally.conf.j2
@@ -0,0 +1,9 @@
+[DEFAULT]
+debug = {{ openstack_logging_debug }}
+log_file = rally.log
+use_stderr = False
+log_dir = /var/log/kolla/rally/
+
+[database]
+connection = mysql+pymysql://{{ rally_database_user }}:{{ rally_database_password }}@{{ rally_database_address }}/{{ rally_database_name }}
+max_retries = -1
diff --git a/ansible/roles/rally/templates/rally.json.j2 b/ansible/roles/rally/templates/rally.json.j2
new file mode 100644
index 0000000000..c7c2d0217c
--- /dev/null
+++ b/ansible/roles/rally/templates/rally.json.j2
@@ -0,0 +1,11 @@
+{
+    "command": "sleep infinity",
+    "config_files":[
+        {
+            "source": "{{ container_config_directory }}/rally.conf",
+            "dest": "/etc/rally/rally.conf",
+            "owner": "root",
+            "perm": "0600"
+        }
+    ]
+}
diff --git a/ansible/site.yml b/ansible/site.yml
index 7bec545b87..b696956429 100644
--- a/ansible/site.yml
+++ b/ansible/site.yml
@@ -260,6 +260,12 @@
         tags: tempest,
         when: enable_tempest | bool }
 
+- hosts: rally
+  roles:
+    - { role: rally,
+        tags: rally,
+        when: enable_rally | bool }
+
 - hosts:
     - watcher-api
     - watcher-engine
diff --git a/docker/rally/extend_start.sh b/docker/rally/extend_start.sh
index a3af2c96ef..06d4d60861 100644
--- a/docker/rally/extend_start.sh
+++ b/docker/rally/extend_start.sh
@@ -3,7 +3,7 @@
 # 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
-    rally-manage db recreate
+    rally-manage db create || rally-manage db upgrade
     exit 0
 fi
 
diff --git a/etc/kolla/globals.yml b/etc/kolla/globals.yml
index bef77a1e25..06b328c8ee 100644
--- a/etc/kolla/globals.yml
+++ b/etc/kolla/globals.yml
@@ -140,11 +140,11 @@ kolla_internal_vip_address: "10.10.10.254"
 #enable_neutron_lbaas: "no"
 #enable_neutron_qos: "no"
 #enable_neutron_agent_ha: "no"
+#enable_rally: "no"
 #enable_swift: "no"
 #enable_tempest: "no"
 #enable_watcher: "no"
 
-
 ###################
 # Ceph options
 ###################
diff --git a/etc/kolla/passwords.yml b/etc/kolla/passwords.yml
index b635c6851c..b54a33c02d 100644
--- a/etc/kolla/passwords.yml
+++ b/etc/kolla/passwords.yml
@@ -73,6 +73,8 @@ watcher_keystone_password:
 congress_database_password:
 congress_keystone_password:
 
+rally_database_password:
+
 horizon_secret_key:
 
 telemetry_secret_key:
diff --git a/releasenotes/notes/add-rally-c6d1468accfb1da6.yaml b/releasenotes/notes/add-rally-c6d1468accfb1da6.yaml
new file mode 100644
index 0000000000..3874b6fad7
--- /dev/null
+++ b/releasenotes/notes/add-rally-c6d1468accfb1da6.yaml
@@ -0,0 +1,3 @@
+---
+features:
+  - Implement rally ansible role