From d3e07342a53facb87782aa98940136576a5bb98d Mon Sep 17 00:00:00 2001
From: guochao <guochao@winhong.com>
Date: Wed, 12 Jul 2017 09:42:34 +0800
Subject: [PATCH] Add Swift backend support to Glance

Support setting Swift as Glance storage backend.

Change-Id: Idddbf2ce741e0486d60e1de88c77a7f0332a5a2b
---
 ansible/group_vars/all.yml                     |  1 +
 ansible/roles/glance/defaults/main.yml         |  2 ++
 ansible/roles/glance/handlers/main.yml         |  1 +
 ansible/roles/glance/tasks/config.yml          | 18 ++++++++++++++++++
 .../roles/glance/templates/glance-api.conf.j2  | 13 +++++++++++++
 .../roles/glance/templates/glance-api.json.j2  |  9 +++++++--
 .../glance/templates/glance-swift.conf.j2      |  7 +++++++
 etc/kolla/globals.yml                          |  1 +
 ...swift-backend-support-6f48573601991b5b.yaml |  3 +++
 9 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 ansible/roles/glance/templates/glance-swift.conf.j2
 create mode 100644 releasenotes/notes/add-glance-swift-backend-support-6f48573601991b5b.yaml

diff --git a/ansible/group_vars/all.yml b/ansible/group_vars/all.yml
index 992eb4b2d9..dcea7b516f 100644
--- a/ansible/group_vars/all.yml
+++ b/ansible/group_vars/all.yml
@@ -484,6 +484,7 @@ keystone_default_user_role: "_member_"
 glance_backend_file: "{{ not enable_ceph | bool }}"
 glance_backend_ceph: "{{ enable_ceph }}"
 glance_backend_vmware: "no"
+glance_backend_swift: "no"
 glance_file_datadir_volume: "glance"
 
 #######################
diff --git a/ansible/roles/glance/defaults/main.yml b/ansible/roles/glance/defaults/main.yml
index 53ce07891c..6af594a56b 100644
--- a/ansible/roles/glance/defaults/main.yml
+++ b/ansible/roles/glance/defaults/main.yml
@@ -72,6 +72,8 @@ glance_backends:
     enabled: "{{ glance_backend_vmware | bool }}"
   - name: cinder
     enabled: "{{ enable_cinder | bool }}"
+  - name: "swift"
+    enabled: "{{ enable_swift | bool and glance_backend_swift | bool }}"
 
 glance_store_backends: "{{ glance_backends|selectattr('enabled', 'equalto', true)|list }}"
 
diff --git a/ansible/roles/glance/handlers/main.yml b/ansible/roles/glance/handlers/main.yml
index 328494e8e9..33909725d2 100644
--- a/ansible/roles/glance/handlers/main.yml
+++ b/ansible/roles/glance/handlers/main.yml
@@ -19,6 +19,7 @@
     - service.enabled | bool
     - config_json.changed | bool
       or glance_conf.changed | bool
+      or glance_swift_conf.changed | bool
       or policy_json.changed | bool
       or glance_api_container.changed | bool
 
diff --git a/ansible/roles/glance/tasks/config.yml b/ansible/roles/glance/tasks/config.yml
index cfcde6c1c7..612ebcabbd 100644
--- a/ansible/roles/glance/tasks/config.yml
+++ b/ansible/roles/glance/tasks/config.yml
@@ -49,6 +49,24 @@
   run_once: True
   register: glance_policy
 
+- name: Copying over glance-swift.conf for glance_api
+  vars:
+    glance_api: "{{ glance_services['glance-api'] }}"
+  merge_configs:
+    sources:
+      - "{{ role_path }}/templates/glance-swift.conf.j2"
+      - "{{ node_custom_config }}/glance-swift.conf"
+      - "{{ node_custom_config }}/glance/glance-swift.conf"
+      - "{{ node_custom_config }}/glance/{{ inventory_hostname }}/glance-swift.conf"
+    dest: "{{ node_config_directory }}/glance-api/glance-swift.conf"
+  register: glance_swift_conf
+  when:
+    - glance_api.enabled | bool
+    - inventory_hostname in groups[glance_api.group]
+    - glance_backend_swift | bool
+  notify:
+    - Restart glance-api container
+
 - name: Copying over existing policy.json
   template:
     src: "{{ node_custom_config }}/glance/policy.json"
diff --git a/ansible/roles/glance/templates/glance-api.conf.j2 b/ansible/roles/glance/templates/glance-api.conf.j2
index f4a803128c..a68e1d9cd0 100644
--- a/ansible/roles/glance/templates/glance-api.conf.j2
+++ b/ansible/roles/glance/templates/glance-api.conf.j2
@@ -45,6 +45,8 @@ flavor = keystone
 default_store = vsphere
 {% elif enable_ceph | bool and glance_backend_ceph | bool %}
 default_store = rbd
+{% elif enable_swift | bool and glance_backend_swift | bool %}
+default_store = swift+http
 {% else %}
 default_store = file
 filesystem_store_datadir = /var/lib/glance/images/
@@ -60,6 +62,17 @@ rbd_store_pool = {{ ceph_glance_pool_name }}
 rbd_store_chunk_size = 8
 {% endif %}
 
+{% if enable_swift | bool and glance_backend_swift | bool %}
+swift_store_container = glance
+swift_store_multiple_containers_seed = 0
+swift_store_multi_tenant = False
+swift_store_create_container_on_put = True
+swift_store_region = {{ openstack_region_name }}
+default_swift_reference = swift
+swift_store_config_file = /etc/glance/glance-swift.conf
+swift_store_auth_insecure = True
+{% endif %}
+
 {% if glance_backend_vmware | bool %}
 vmware_server_host = {{ vmware_vcenter_host_ip }}
 vmware_server_username = {{ vmware_vcenter_host_username }}
diff --git a/ansible/roles/glance/templates/glance-api.json.j2 b/ansible/roles/glance/templates/glance-api.json.j2
index fd15198c0e..a491931395 100644
--- a/ansible/roles/glance/templates/glance-api.json.j2
+++ b/ansible/roles/glance/templates/glance-api.json.j2
@@ -19,8 +19,13 @@
             "dest": "/etc/ceph/",
             "owner": "glance",
             "perm": "0700"
-        }
-        {% endif %}
+        }{% endif %}{% if glance_backend_swift | bool %},
+        {
+            "source": "{{ container_config_directory }}/glance-swift.conf",
+            "dest": "/etc/glance/glance-swift.conf",
+            "owner": "glance",
+            "perm": "0600"
+        }{% endif %}
     ],
     "permissions": [
         {
diff --git a/ansible/roles/glance/templates/glance-swift.conf.j2 b/ansible/roles/glance/templates/glance-swift.conf.j2
new file mode 100644
index 0000000000..9cec0ab69e
--- /dev/null
+++ b/ansible/roles/glance/templates/glance-swift.conf.j2
@@ -0,0 +1,7 @@
+[swift]
+auth_version = 3
+auth_address = {{ openstack_auth.auth_url }}/v3
+user = service:{{ glance_keystone_user }}
+key = {{ glance_keystone_password }}
+project_domain_id = default
+user_domain_id = default
diff --git a/etc/kolla/globals.yml b/etc/kolla/globals.yml
index f0a4b65963..255b1db449 100644
--- a/etc/kolla/globals.yml
+++ b/etc/kolla/globals.yml
@@ -258,6 +258,7 @@ kolla_internal_vip_address: "10.10.10.254"
 #glance_backend_file: "yes"
 #glance_backend_ceph: "no"
 #glance_backend_vmware: "no"
+#glance_backend_swift: "no"
 
 
 #######################
diff --git a/releasenotes/notes/add-glance-swift-backend-support-6f48573601991b5b.yaml b/releasenotes/notes/add-glance-swift-backend-support-6f48573601991b5b.yaml
new file mode 100644
index 0000000000..52d6ecef24
--- /dev/null
+++ b/releasenotes/notes/add-glance-swift-backend-support-6f48573601991b5b.yaml
@@ -0,0 +1,3 @@
+---
+features:
+  - Add Glance Swift backend support