From a5b53f3a7006ee025dc65de99a3749035af79397 Mon Sep 17 00:00:00 2001
From: Hui Kang <kangh@us.ibm.com>
Date: Fri, 2 Sep 2016 00:48:01 -0400
Subject: [PATCH] Add etcd ansible role

Change-Id: If8351ab3000006323a05924e907f1e3745768304
Co-Authored-By: zhubingbing <zhubingbing10@gmail.com>>
Partially-implements: bp kuryr-docker-plugin
---
 ansible/group_vars/all.yml                    |  4 ++++
 ansible/inventory/all-in-one                  |  3 +++
 ansible/inventory/multinode                   |  3 +++
 ansible/roles/etcd/defaults/main.yml          | 10 ++++++++
 ansible/roles/etcd/meta/main.yml              |  3 +++
 ansible/roles/etcd/tasks/bootstrap.yml        | 18 ++++++++++++++
 ansible/roles/etcd/tasks/config.yml           | 15 ++++++++++++
 ansible/roles/etcd/tasks/deploy.yml           |  6 +++++
 ansible/roles/etcd/tasks/main.yml             |  2 ++
 ansible/roles/etcd/tasks/pull.yml             |  6 +++++
 ansible/roles/etcd/tasks/reconfigure.yml      |  1 +
 ansible/roles/etcd/tasks/start.yml            | 24 +++++++++++++++++++
 ansible/roles/etcd/tasks/upgrade.yml          |  4 ++++
 ansible/roles/etcd/templates/etcd.json.j2     |  3 +++
 ansible/site.yml                              |  7 ++++++
 docker/etcd/extend_start.sh                   |  8 +++++++
 etc/kolla/passwords.yml                       |  5 ++++
 .../etcd-docker-ansible-51baaa1322a0c5a8.yaml |  3 +++
 18 files changed, 125 insertions(+)
 create mode 100644 ansible/roles/etcd/defaults/main.yml
 create mode 100644 ansible/roles/etcd/meta/main.yml
 create mode 100644 ansible/roles/etcd/tasks/bootstrap.yml
 create mode 100644 ansible/roles/etcd/tasks/config.yml
 create mode 100644 ansible/roles/etcd/tasks/deploy.yml
 create mode 100644 ansible/roles/etcd/tasks/main.yml
 create mode 100644 ansible/roles/etcd/tasks/pull.yml
 create mode 100644 ansible/roles/etcd/tasks/reconfigure.yml
 create mode 100644 ansible/roles/etcd/tasks/start.yml
 create mode 100644 ansible/roles/etcd/tasks/upgrade.yml
 create mode 100644 ansible/roles/etcd/templates/etcd.json.j2
 create mode 100644 releasenotes/notes/etcd-docker-ansible-51baaa1322a0c5a8.yaml

diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml
index 8dccd00e44..b0e5485faf 100644
--- a/ansible/group_vars/all.yml
+++ b/ansible/group_vars/all.yml
@@ -195,6 +195,9 @@ influxdb_http_port: "8086"
 
 senlin_api_port: "8778"
 
+etcd_client_port: "2379"
+etcd_peer_port: "2380"
+
 public_protocol: "{{ 'https' if kolla_enable_tls_external | bool else 'http' }}"
 internal_protocol: "http"
 admin_protocol: "http"
@@ -245,6 +248,7 @@ enable_cinder: "no"
 enable_cinder_backend_lvm: "no"
 enable_cloudkitty: "no"
 enable_congress: "no"
+enable_etcd: "no"
 enable_gnocchi: "no"
 enable_grafana: "no"
 enable_heat: "yes"
diff --git a/ansible/inventory/all-in-one b/ansible/inventory/all-in-one
index d4c96d7da9..a3be4315be 100644
--- a/ansible/inventory/all-in-one
+++ b/ansible/inventory/all-in-one
@@ -23,6 +23,9 @@ compute
 [grafana:children]
 monitoring
 
+[etcd:children]
+control
+
 [kibana:children]
 control
 
diff --git a/ansible/inventory/multinode b/ansible/inventory/multinode
index f54341a151..542d8ecc33 100644
--- a/ansible/inventory/multinode
+++ b/ansible/inventory/multinode
@@ -41,6 +41,9 @@ compute
 [grafana:children]
 monitoring
 
+[etcd:children]
+control
+
 [influxdb:children]
 monitoring
 
diff --git a/ansible/roles/etcd/defaults/main.yml b/ansible/roles/etcd/defaults/main.yml
new file mode 100644
index 0000000000..c8f73263a7
--- /dev/null
+++ b/ansible/roles/etcd/defaults/main.yml
@@ -0,0 +1,10 @@
+---
+project_name: "etcd"
+
+
+####################
+# Docker
+####################
+etcd_image: "{{ docker_registry ~ '/' if docker_registry else '' }}{{ docker_namespace }}/{{ kolla_base_distro }}-{{ kolla_install_type }}-etcd"
+etcd_tag: "{{ openstack_release }}"
+etcd_image_full: "{{ etcd_image }}:{{ etcd_tag }}"
diff --git a/ansible/roles/etcd/meta/main.yml b/ansible/roles/etcd/meta/main.yml
new file mode 100644
index 0000000000..6b4fff8fef
--- /dev/null
+++ b/ansible/roles/etcd/meta/main.yml
@@ -0,0 +1,3 @@
+---
+dependencies:
+  - { role: common }
diff --git a/ansible/roles/etcd/tasks/bootstrap.yml b/ansible/roles/etcd/tasks/bootstrap.yml
new file mode 100644
index 0000000000..bea1cb7ec7
--- /dev/null
+++ b/ansible/roles/etcd/tasks/bootstrap.yml
@@ -0,0 +1,18 @@
+---
+- name: Running etcd bootstrap container
+  kolla_docker:
+    action: "start_container"
+    common_options: "{{ docker_common_options }}"
+    detach: False
+    environment:
+      KOLLA_BOOTSTRAP:
+      KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
+    image: "{{ etcd_image_full }}"
+    labels:
+      BOOTSTRAP:
+    name: "bootstrap_etcd"
+    restart_policy: "never"
+    volumes:
+      - "{{ node_config_directory }}/etcd/:{{ container_config_directory }}/:ro"
+      - "kolla_etcd:/var/lib/etcd/"
+      - "kolla_logs:/var/log/kolla/"
diff --git a/ansible/roles/etcd/tasks/config.yml b/ansible/roles/etcd/tasks/config.yml
new file mode 100644
index 0000000000..2ff3ac1f0e
--- /dev/null
+++ b/ansible/roles/etcd/tasks/config.yml
@@ -0,0 +1,15 @@
+---
+- name: Ensuring config directories exist
+  file:
+    path: "{{ node_config_directory }}/{{ item }}"
+    state: "directory"
+    recurse: yes
+  with_items:
+    - "etcd"
+
+- name: Copying over config.json files for services
+  template:
+    src: "{{ item }}.json.j2"
+    dest: "{{ node_config_directory }}/{{ item }}/config.json"
+  with_items:
+    - "etcd"
diff --git a/ansible/roles/etcd/tasks/deploy.yml b/ansible/roles/etcd/tasks/deploy.yml
new file mode 100644
index 0000000000..98daa4021c
--- /dev/null
+++ b/ansible/roles/etcd/tasks/deploy.yml
@@ -0,0 +1,6 @@
+---
+- include: config.yml
+
+- include: bootstrap.yml
+
+- include: start.yml
diff --git a/ansible/roles/etcd/tasks/main.yml b/ansible/roles/etcd/tasks/main.yml
new file mode 100644
index 0000000000..b017e8b4ad
--- /dev/null
+++ b/ansible/roles/etcd/tasks/main.yml
@@ -0,0 +1,2 @@
+---
+- include: "{{ action }}.yml"
diff --git a/ansible/roles/etcd/tasks/pull.yml b/ansible/roles/etcd/tasks/pull.yml
new file mode 100644
index 0000000000..57d5bdae51
--- /dev/null
+++ b/ansible/roles/etcd/tasks/pull.yml
@@ -0,0 +1,6 @@
+---
+- name: Pulling etcd image
+  kolla_docker:
+    action: "pull_image"
+    common_options: "{{ docker_common_options }}"
+    image: "{{ etcd_image_full }}"
diff --git a/ansible/roles/etcd/tasks/reconfigure.yml b/ansible/roles/etcd/tasks/reconfigure.yml
new file mode 100644
index 0000000000..ed97d539c0
--- /dev/null
+++ b/ansible/roles/etcd/tasks/reconfigure.yml
@@ -0,0 +1 @@
+---
diff --git a/ansible/roles/etcd/tasks/start.yml b/ansible/roles/etcd/tasks/start.yml
new file mode 100644
index 0000000000..7546f8841d
--- /dev/null
+++ b/ansible/roles/etcd/tasks/start.yml
@@ -0,0 +1,24 @@
+---
+- name: Starting etcd container
+  kolla_docker:
+    action: "start_container"
+    common_options: "{{ docker_common_options }}"
+    environment:
+      ETCD_DATA_DIR: "/var/lib/etcd"
+      ETCD_NAME: "{{ ansible_hostname }}"
+      ETCD_ADVERTISE_CLIENT_URLS: "{{ internal_protocol }}://{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}:{{ etcd_client_port }}"
+      ETCD_LISTEN_CLIENT_URLS: "{{ internal_protocol }}://{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}:{{ etcd_client_port }}"
+      ETCD_INITIAL_ADVERTISE_PEER_URLS: "{{ internal_protocol }}://{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}:{{ etcd_peer_port }}"
+      ETCD_LISTEN_PEER_URLS: "{{ internal_protocol }}://{{ hostvars[inventory_hostname]['ansible_' + api_interface]['ipv4']['address'] }}:{{ etcd_peer_port }}"
+      ETCD_INITIAL_CLUSTER_TOKEN: "{{ etcd_cluster_token }}"
+      ETCD_INITIAL_CLUSTER: "{% for host in groups['etcd'] %}{{ hostvars[host]['ansible_hostname'] }}={{ internal_protocol }}://{{ hostvars[host]['ansible_' + api_interface]['ipv4']['address'] }}:{{ etcd_peer_port }}{% if not loop.last %},{% endif %}{% endfor %}"
+      ETCD_INITIAL_CLUSTER_STATE: "new"
+      ETCD_OUT_FILE: "/var/log/kolla/etcd/etcd.log"
+      KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
+    image: "{{ etcd_image_full }}"
+    name: "etcd"
+    volumes:
+      - "{{ node_config_directory }}/etcd/:{{ container_config_directory }}/:ro"
+      - "/etc/localtime:/etc/localtime:ro"
+      - "kolla_etcd:/var/lib/etcd/"
+      - "kolla_logs:/var/log/kolla/"
diff --git a/ansible/roles/etcd/tasks/upgrade.yml b/ansible/roles/etcd/tasks/upgrade.yml
new file mode 100644
index 0000000000..1f16915ad9
--- /dev/null
+++ b/ansible/roles/etcd/tasks/upgrade.yml
@@ -0,0 +1,4 @@
+---
+- include: config.yml
+
+- include: start.yml
diff --git a/ansible/roles/etcd/templates/etcd.json.j2 b/ansible/roles/etcd/templates/etcd.json.j2
new file mode 100644
index 0000000000..3ea11fd909
--- /dev/null
+++ b/ansible/roles/etcd/templates/etcd.json.j2
@@ -0,0 +1,3 @@
+{
+    "command": "etcd"
+}
diff --git a/ansible/site.yml b/ansible/site.yml
index 9134db55ce..9aee3ca330 100644
--- a/ansible/site.yml
+++ b/ansible/site.yml
@@ -94,6 +94,13 @@
         tags: rabbitmq,
         when: enable_rabbitmq | bool }
 
+- hosts: etcd
+  serial: '{{ "30%" if action == "upgrade" else "0" }}'
+  roles:
+    - { role: etcd,
+        tags: etcd,
+        when: enable_etcd | bool }
+
 - hosts:
     - keystone
   serial: '{{ "30%" if action == "upgrade" else "0" }}'
diff --git a/docker/etcd/extend_start.sh b/docker/etcd/extend_start.sh
index 485ee8e2de..718de9ebbd 100644
--- a/docker/etcd/extend_start.sh
+++ b/docker/etcd/extend_start.sh
@@ -1,5 +1,13 @@
 #!/bin/bash
 
+# Create log directory, with appropriate permissions
+if [[ ! -d "/var/log/kolla/etcd" ]]; then
+    mkdir -p /var/log/kolla/etcd
+fi
+if [[ $(stat -c %a /var/log/kolla/etcd) != "755" ]]; then
+    chmod 755 /var/log/kolla/etcd
+fi
+
 # 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
diff --git a/etc/kolla/passwords.yml b/etc/kolla/passwords.yml
index 84b1b77865..0291a79d79 100644
--- a/etc/kolla/passwords.yml
+++ b/etc/kolla/passwords.yml
@@ -138,3 +138,8 @@ keepalived_password:
 # Kibana options
 ####################
 kibana_password:
+
+####################
+# etcd options
+####################
+etcd_cluster_token:
diff --git a/releasenotes/notes/etcd-docker-ansible-51baaa1322a0c5a8.yaml b/releasenotes/notes/etcd-docker-ansible-51baaa1322a0c5a8.yaml
new file mode 100644
index 0000000000..d16085c225
--- /dev/null
+++ b/releasenotes/notes/etcd-docker-ansible-51baaa1322a0c5a8.yaml
@@ -0,0 +1,3 @@
+---
+features:
+  - Add etcd ansible role