From 465bc9ee1c9324ba95b223f7b8d11bc0cd376608 Mon Sep 17 00:00:00 2001
From: Alexandru Bogdan Pica <alexandru.pica@1and1.ro>
Date: Wed, 31 Jan 2018 20:27:38 +0200
Subject: [PATCH] Improve mariadb_recovery

The purpose of this change is to improve upon
https://review.openstack.org/#/c/531122/

- Moved vars inside the defaults/main.yml file
- Made the regex for the lineinfile safer

Change-Id: Id581c0b36f3d4bd61d3627b8364b79296b967387
Closes-Bug: 1746567
Related-Bug: 1682153
---
 ansible/roles/mariadb/defaults/main.yml       |  5 +++
 .../roles/mariadb/tasks/recover_cluster.yml   | 39 +++++++------------
 2 files changed, 20 insertions(+), 24 deletions(-)

diff --git a/ansible/roles/mariadb/defaults/main.yml b/ansible/roles/mariadb/defaults/main.yml
index 2162cba322..f0b885609f 100644
--- a/ansible/roles/mariadb/defaults/main.yml
+++ b/ansible/roles/mariadb/defaults/main.yml
@@ -26,3 +26,8 @@ database_max_timeout: 120
 mariadb_image: "{{ docker_registry ~ '/' if docker_registry else '' }}{{ docker_namespace }}/{{ kolla_base_distro }}-{{ kolla_install_type }}-mariadb"
 mariadb_tag: "{{ openstack_release }}"
 mariadb_image_full: "{{ mariadb_image }}:{{ mariadb_tag }}"
+
+########################################
+# Vars used within recover_cluster.yml
+########################################
+mariadb_service: "{{ mariadb_services['mariadb'] }}"
diff --git a/ansible/roles/mariadb/tasks/recover_cluster.yml b/ansible/roles/mariadb/tasks/recover_cluster.yml
index 7a92846287..5fda36221c 100644
--- a/ansible/roles/mariadb/tasks/recover_cluster.yml
+++ b/ansible/roles/mariadb/tasks/recover_cluster.yml
@@ -17,33 +17,30 @@
 - block:
   - name: Stop MariaDB containers
     kolla_docker:
-      name: "mariadb"
+      name: "{{ mariadb_service.container_name }}"
       action: "stop_container"
 
   - name: Run MariaDB wsrep recovery
-    vars:
-      service_name: "mariadb"
-      service: "{{ mariadb_services[service_name] }}"
     kolla_docker:
       action: "start_container"
       common_options: "{{ docker_common_options }}"
       environment:
         KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
         BOOTSTRAP_ARGS: "--wsrep-recover"
-      image: "{{ service.image }}"
+      image: "{{ mariadb_service.image }}"
       labels:
         BOOTSTRAP:
-      name: "{{ service.container_name }}"
+      name: "{{ mariadb_service.container_name }}"
       restart_policy: "never"
-      volumes: "{{ service.volumes }}"
+      volumes: "{{ mariadb_service.volumes }}"
 
   - name: Stop MariaDB containers
     kolla_docker:
-      name: "{{ service.container_name }}"
+      name: "{{ mariadb_service.container_name }}"
       action: "stop_container"
 
   - name: Copying MariaDB log file to /tmp
-    shell: "docker cp {{ service.container_name }}:/var/log/kolla/mariadb/mariadb.log /tmp/mariadb_tmp.log"
+    shell: "docker cp {{ mariadb_service.container_name }}:/var/log/kolla/mariadb/mariadb.log /tmp/mariadb_tmp.log"
 
   - name: Get MariaDB wsrep recovery seqno
     shell: "tail -n 200 /tmp/mariadb_tmp.log | grep  Recovered | tail -1 | awk '{print $7}' | awk -F'\n' '{print $1}' | awk -F':' '{print $2}'"
@@ -88,7 +85,7 @@
   changed_when: true
 
 - name: Copying grastate.dat file from MariaDB container in bootstrap host
-  command: docker cp mariadb:/var/lib/mysql/grastate.dat /tmp/kolla_mariadb_grastate.dat
+  command: "docker cp {{ mariadb_service.container_name }}:/var/lib/mysql/grastate.dat /tmp/kolla_mariadb_grastate.dat"
   changed_when: false
   when:
     - bootstrap_host is defined
@@ -97,7 +94,7 @@
 - name: Set grastate.dat file from MariaDB container in bootstrap host
   lineinfile:
     dest: /tmp/kolla_mariadb_grastate.dat
-    regexp: 'safe_to_bootstrap: 0'
+    regexp: 'safe_to_bootstrap:(.*)$'
     line: 'safe_to_bootstrap: 1'
     state: present
   when:
@@ -112,21 +109,18 @@
     - bootstrap_host == inventory_hostname
 
 - name: Starting first MariaDB container
-  vars:
-    service_name: "mariadb"
-    service: "{{ mariadb_services[service_name] }}"
   kolla_docker:
     action: "start_container"
     common_options: "{{ docker_common_options }}"
     environment:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
       BOOTSTRAP_ARGS: "--wsrep-new-cluster"
-    image: "{{ service.image }}"
+    image: "{{ mariadb_service.image }}"
     labels:
       BOOTSTRAP:
-    name: "{{ service.container_name }}"
+    name: "{{ mariadb_service.container_name }}"
     restart_policy: "never"
-    volumes: "{{ service.volumes }}"
+    volumes: "{{ mariadb_service.volumes }}"
   when:
     - bootstrap_host is defined
     - bootstrap_host == inventory_hostname
@@ -147,28 +141,25 @@
     - bootstrap_host == inventory_hostname
 
 - name: Set first MariaDB container as primary
-  shell: "docker exec mariadb mysql -uroot -p{{ database_password }} -e \"SET GLOBAL wsrep_provider_options='pc.bootstrap=yes';\""
+  shell: "docker exec {{ mariadb_service.container_name }} mysql -uroot -p{{ database_password }} -e \"SET GLOBAL wsrep_provider_options='pc.bootstrap=yes';\""
   no_log: True
   when:
     - bootstrap_host is defined
     - bootstrap_host == inventory_hostname
 
 - name: Restart slave MariaDB container
-  vars:
-    service_name: "mariadb"
-    service: "{{ mariadb_services[service_name] }}"
   kolla_docker:
     action: "start_container"
     common_options: "{{ docker_common_options }}"
     environment:
       KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
       BOOTSTRAP_ARGS: " "
-    image: "{{ service.image }}"
+    image: "{{ mariadb_service.image }}"
     labels:
       BOOTSTRAP:
-    name: "{{ service.container_name }}"
+    name: "{{ mariadb_service.container_name }}"
     restart_policy: "never"
-    volumes: "{{ service.volumes }}"
+    volumes: "{{ mariadb_service.volumes }}"
   when:
     - bootstrap_host is defined
     - bootstrap_host != inventory_hostname