From 24c3d7053e03083b0963faa6fd808045ecc596fc Mon Sep 17 00:00:00 2001
From: Kevin Carter <kevin.carter@rackspace.com>
Date: Tue, 24 May 2016 10:12:26 -0500
Subject: [PATCH] Implement Ubuntu 16.04 support with SystemD

This change updates the glance role to support Ubuntu 14.04 with
upstart init and 16.04 with a systemd init.

Implements: blueprint support-ubuntu-1604
Depends-On: Ib6d7e68133de8d10b81d9116b74dca1de7568897
Change-Id: I39214199e2bbc2bbc5c1b30f8a04aa4e74ed967b
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
---
 meta/main.yml                                 |  1 +
 .../glance-1604-support-e65870170a925bfe.yaml |  3 ++
 ...lance_upstart_init.yml => glance_init.yml} |  6 +--
 tasks/glance_init_common.yml                  | 31 ++++++++++++
 tasks/glance_init_systemd.yml                 | 48 +++++++++++++++++++
 ...ommon_init.yml => glance_init_upstart.yml} | 20 ++------
 tasks/main.yml                                | 14 +++++-
 templates/glance-systemd-init.j2              | 25 ++++++++++
 templates/glance-systemd-tempfiles.j2         |  4 ++
 vars/ubuntu-16.04.yml                         | 23 +++++++++
 10 files changed, 154 insertions(+), 21 deletions(-)
 create mode 100644 releasenotes/notes/glance-1604-support-e65870170a925bfe.yaml
 rename tasks/{glance_upstart_init.yml => glance_init.yml} (89%)
 create mode 100644 tasks/glance_init_common.yml
 create mode 100644 tasks/glance_init_systemd.yml
 rename tasks/{glance_upstart_common_init.yml => glance_init_upstart.yml} (75%)
 create mode 100644 templates/glance-systemd-init.j2
 create mode 100644 templates/glance-systemd-tempfiles.j2
 create mode 100644 vars/ubuntu-16.04.yml

diff --git a/meta/main.yml b/meta/main.yml
index a5fea52d..39ed4bc4 100644
--- a/meta/main.yml
+++ b/meta/main.yml
@@ -23,6 +23,7 @@ galaxy_info:
     - name: Ubuntu
       versions:
         - trusty
+        - xenial
   categories:
     - cloud
     - python
diff --git a/releasenotes/notes/glance-1604-support-e65870170a925bfe.yaml b/releasenotes/notes/glance-1604-support-e65870170a925bfe.yaml
new file mode 100644
index 00000000..79f5253b
--- /dev/null
+++ b/releasenotes/notes/glance-1604-support-e65870170a925bfe.yaml
@@ -0,0 +1,3 @@
+---
+features:
+  - The ``os_glance`` role now supports Ubuntu 16.04 and SystemD.
diff --git a/tasks/glance_upstart_init.yml b/tasks/glance_init.yml
similarity index 89%
rename from tasks/glance_upstart_init.yml
rename to tasks/glance_init.yml
index f54100b9..e157f1b7 100644
--- a/tasks/glance_upstart_init.yml
+++ b/tasks/glance_init.yml
@@ -1,5 +1,5 @@
 ---
-# Copyright 2014, Rackspace US, Inc.
+# Copyright 2016, Rackspace US, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-- include: glance_upstart_common_init.yml
+- include: glance_init_common.yml
   vars:
     program_name: "{{ glance_api_program_name }}"
     service_name: "{{ glance_service_name }}"
@@ -21,7 +21,7 @@
     system_group: "{{ glance_system_group_name }}"
     service_home: "{{ glance_system_user_home }}"
 
-- include: glance_upstart_common_init.yml
+- include: glance_init_common.yml
   vars:
     program_name: "{{ glance_registry_program_name }}"
     service_name: "{{ glance_service_name }}"
diff --git a/tasks/glance_init_common.yml b/tasks/glance_init_common.yml
new file mode 100644
index 00000000..71adc5ee
--- /dev/null
+++ b/tasks/glance_init_common.yml
@@ -0,0 +1,31 @@
+---
+# Copyright 2016, Rackspace US, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+- include: glance_init_upstart.yml
+  when: pid1_name == "init"
+  tags:
+    - glance-init
+
+- include: glance_init_systemd.yml
+  when: pid1_name == "systemd"
+  tags:
+    - glance-init
+
+- name: Load service
+  service:
+   name: "{{ program_name }}"
+   enabled: "yes"
+  notify:
+    - Restart glance services
diff --git a/tasks/glance_init_systemd.yml b/tasks/glance_init_systemd.yml
new file mode 100644
index 00000000..a2ed37ba
--- /dev/null
+++ b/tasks/glance_init_systemd.yml
@@ -0,0 +1,48 @@
+---
+# Copyright 2016, Rackspace US, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+- name: Create glance TEMP dirs
+  file:
+    path: "{{ item.path }}/{{ program_name }}"
+    state: directory
+    owner: "{{ system_user }}"
+    group: "{{ system_group }}"
+    mode: "2755"
+  with_items:
+    - { path: "/var/run" }
+    - { path: "/var/lock" }
+
+- name: Create tempfile.d entry
+  template:
+    src: "glance-systemd-tempfiles.j2"
+    dest: "/etc/tmpfiles.d/glance.conf"
+    mode: "0644"
+    owner: "root"
+    group: "root"
+
+- name: Place the systemd init script
+  template:
+    src: "glance-systemd-init.j2"
+    dest: "/etc/systemd/system/{{ program_name }}.service"
+    mode: "0644"
+    owner: "root"
+    group: "root"
+  register: systemd_init
+
+- name: Reload the systemd daemon
+  command: "systemctl daemon-reload"
+  when: systemd_init | changed
+  notify:
+    - Restart glance services
diff --git a/tasks/glance_upstart_common_init.yml b/tasks/glance_init_upstart.yml
similarity index 75%
rename from tasks/glance_upstart_common_init.yml
rename to tasks/glance_init_upstart.yml
index 02c923ef..9b78ca3e 100644
--- a/tasks/glance_upstart_common_init.yml
+++ b/tasks/glance_init_upstart.yml
@@ -1,5 +1,5 @@
 ---
-# Copyright 2014, Rackspace US, Inc.
+# Copyright 2016, Rackspace US, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -20,27 +20,13 @@
     mode: "0644"
     owner: "root"
     group: "root"
+  register: upstart_init
   notify:
     - Restart glance services
-  tags:
-    - upstart-init
-    - glance-init
 
 - name: Reload init scripts
   shell: |
     initctl reload-configuration
+  when: upstart_init | changed
   notify:
     - Restart glance services
-  tags:
-    - upstart-init
-    - glance-init
-
-- name: Load service
-  service:
-   name: "{{ program_name }}"
-   enabled: "yes"
-  notify:
-    - Restart glance services
-  tags:
-    - upstart-init
-    - glance-init
diff --git a/tasks/main.yml b/tasks/main.yml
index 8ee1546d..e842d5db 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -22,10 +22,22 @@
   tags:
     - always
 
+- name: Check init system
+  command: cat /proc/1/comm
+  register: _pid1_name
+  tags:
+    - always
+
+- name: Set the name of pid1
+  set_fact:
+    pid1_name: "{{ _pid1_name.stdout }}"
+  tags:
+    - always
+
 - include: glance_pre_install.yml
 - include: glance_install.yml
 - include: glance_post_install.yml
-- include: glance_upstart_init.yml
+- include: glance_init.yml
 
 - include: glance_db_setup.yml
   when: >
diff --git a/templates/glance-systemd-init.j2 b/templates/glance-systemd-init.j2
new file mode 100644
index 00000000..18ea2193
--- /dev/null
+++ b/templates/glance-systemd-init.j2
@@ -0,0 +1,25 @@
+# {{ ansible_managed }}
+
+[Unit]
+Description=glance openstack service
+After=syslog.target
+After=network.target
+
+[Service]
+Type=simple
+User={{ system_user }}
+Group={{ system_group }}
+
+{% if program_override is defined %}
+ExecStart={{ program_override }} {{ program_config_options|default('') }} --log-file=/var/log/glance/{{ program_name }}.log
+{% else %}
+ExecStart={{ glance_bin }}/{{ program_name }} {{ program_config_options|default('') }} --log-file=/var/log/glance/{{ program_name }}.log
+{% endif %}
+
+# Give a reasonable amount of time for the server to start up/shut down
+TimeoutSec=300
+Restart=on-failure
+RestartSec=150
+
+[Install]
+WantedBy=multi-user.target
diff --git a/templates/glance-systemd-tempfiles.j2 b/templates/glance-systemd-tempfiles.j2
new file mode 100644
index 00000000..b723d85d
--- /dev/null
+++ b/templates/glance-systemd-tempfiles.j2
@@ -0,0 +1,4 @@
+# {{ ansible_managed }}
+
+D /var/lock/{{ program_name }} 2755 {{ system_user }} {{ system_group }}
+D /var/run/{{ program_name }} 2755 {{ system_user }} {{ system_group }}
diff --git a/vars/ubuntu-16.04.yml b/vars/ubuntu-16.04.yml
new file mode 100644
index 00000000..fe31302a
--- /dev/null
+++ b/vars/ubuntu-16.04.yml
@@ -0,0 +1,23 @@
+# Copyright 2016, Intel Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+## APT Cache options
+cache_timeout: 600
+
+# Common apt packages
+glance_apt_packages:
+  - rpcbind
+  - rsync
+  - git
+  - nfs-common
\ No newline at end of file