Use dictionary for service group mappings

Change the 'glance_service_names' from a list to a dictionary mapping
of services, groups that install those services. This brings the
method into line with that used in the os_neutron role in order to
implement a more standardised method.

The init tasks have been updated to run once and loop through this
mapping rather than being included multiple times and re-run against
each host. This may potentially reduce role run times.

Currently the reload of upstart/systemd scripts may not happen if
only one script changes as the task uses a loop with only one result
register. This patch implements handlers to reload upstart/systemd
scripts to ensure that this happens when any one of the scripts
change.

The handler to reload the services now only tries to restart the
service if the host is in the group for the service according to the
service group mapping. This allows us to ensure that handler
failures are no longer ignored and that no execution time is wasted
trying to restart services which do not exist on the host.

Finally:
- Common variables shared by each service's template files have
  been updated to use the service namespaced variables.
- Unused handlers have been removed.
- Unused variables have been removed.

Change-Id: Ia74bbcac35c27928f7e96056b9449932253b75de
This commit is contained in:
Jesse Pretorius 2016-09-28 18:24:11 +01:00 committed by Andy McCrae
parent a30fb6326c
commit 3e522f6ef9
12 changed files with 75 additions and 98 deletions

View File

@ -93,11 +93,9 @@ glance_galera_user: glance
glance_role_name: admin
glance_api_bind_address: 0.0.0.0
glance_api_service_port: 9292
glance_api_program_name: glance-api
glance_registry_bind_address: 0.0.0.0
glance_registry_service_port: 9191
glance_registry_program_name: glance-registry
## Service Type and Data
glance_service_region: RegionOne
@ -211,9 +209,13 @@ glance_pip_packages:
- warlock
## Service Names
glance_service_names:
- "glance-api"
- "glance-registry"
glance_services:
glance-api:
group: glance_api
service_name: glance-api
glance-registry:
group: glance_registry
service_name: glance-registry
# This variable is used by the repo_build process to determine
# which host group to check for members of before building the

View File

@ -1,5 +1,5 @@
---
# Copyright 2014, Rackspace US, Inc.
# Copyright 2015, 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,21 +13,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Restart glance api
service:
name: "glance-api"
state: "restarted"
pattern: "glance-api"
- name: Reload systemd daemon
command: "systemctl daemon-reload"
notify:
- Restart glance services
- name: Restart glance registry
service:
name: "glance-registry"
state: "restarted"
pattern: "glance-registry"
- name: Reload upstart init scripts
shell: |
initctl reload-configuration
notify:
- Restart glance services
- name: Restart glance services
service:
name: "{{ item }}"
name: "{{ item.value.service_name }}"
state: "restarted"
pattern: "{{ item }}"
with_items: "{{ glance_service_names }}"
pattern: "{{ item.value.service_name }}"
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]

View File

@ -1,30 +0,0 @@
---
# 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_common.yml
vars:
program_name: "{{ glance_api_program_name }}"
service_name: "{{ glance_service_name }}"
system_user: "{{ glance_system_user_name }}"
system_group: "{{ glance_system_group_name }}"
service_home: "{{ glance_system_user_home }}"
- include: glance_init_common.yml
vars:
program_name: "{{ glance_registry_program_name }}"
service_name: "{{ glance_service_name }}"
system_user: "{{ glance_system_user_name }}"
system_group: "{{ glance_system_group_name }}"
service_home: "{{ glance_system_user_home }}"

View File

@ -5,7 +5,7 @@
# 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
# 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,
@ -14,16 +14,16 @@
# limitations under the License.
- include: glance_init_upstart.yml
static: no
when: pid1_name == "init"
- include: glance_init_systemd.yml
static: no
when: pid1_name == "systemd"
- name: Load service
service:
name: "{{ program_name }}"
name: "{{ item.value.service_name }}"
enabled: "yes"
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]
notify:
- Restart glance services

View File

@ -15,19 +15,23 @@
- name: Create TEMP run dir
file:
path: "/var/run/{{ program_name }}"
path: "/var/run/{{ item.value.service_name }}"
state: directory
owner: "{{ system_user }}"
group: "{{ system_group }}"
owner: "{{ glance_system_user_name }}"
group: "{{ glance_system_group_name }}"
mode: "02755"
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]
- name: Create TEMP lock dir
file:
path: "/var/lock/{{ program_name }}"
path: "/var/lock/{{ item.value.service_name }}"
state: directory
owner: "{{ system_user }}"
group: "{{ system_group }}"
owner: "{{ glance_system_user_name }}"
group: "{{ glance_system_group_name }}"
mode: "02755"
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]
- name: Create tempfile.d entry
template:
@ -36,18 +40,17 @@
mode: "0644"
owner: "root"
group: "root"
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]
- name: Place the systemd init script
template:
src: "glance-systemd-init.j2"
dest: "/etc/systemd/system/{{ program_name }}.service"
dest: "/etc/systemd/system/{{ item.value.service_name }}.service"
mode: "0644"
owner: "root"
group: "root"
register: systemd_init
- name: Reload the systemd daemon
command: "systemctl daemon-reload"
when: systemd_init | changed
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]
notify:
- Restart glance services
- Reload systemd daemon

View File

@ -1,5 +1,5 @@
---
# Copyright 2016, Rackspace US, Inc.
# Copyright 2015, 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.
@ -16,17 +16,11 @@
- name: Place the init script
template:
src: "glance-upstart-init.j2"
dest: "/etc/init/{{ program_name }}.conf"
dest: "/etc/init/{{ item.value.service_name }}.conf"
mode: "0644"
owner: "root"
group: "root"
register: upstart_init
with_dict: "{{ glance_services }}"
when: inventory_hostname in groups[item.value.group]
notify:
- Restart glance services
- name: Reload init scripts
shell: |
initctl reload-configuration
when: upstart_init | changed
notify:
- Restart glance services
- Reload upstart init scripts

View File

@ -68,8 +68,7 @@
config_overrides: "{{ glance_glance_scheme_json_overrides }}"
config_type: "json"
notify:
- Restart glance api
- Restart glance registry
- Restart glance services
- name: Create nfs shares local path
file:

View File

@ -50,7 +50,7 @@
- glance-install
- glance-config
- include: glance_init.yml
- include: glance_init_common.yml
tags:
- glance-install

View File

@ -7,13 +7,13 @@ After=network.target
[Service]
Type=simple
User={{ system_user }}
Group={{ system_group }}
User={{ glance_system_user_name }}
Group={{ glance_system_group_name }}
{% if program_override is defined %}
ExecStart={{ program_override }} {{ program_config_options|default('') }} --log-file=/var/log/glance/{{ program_name }}.log
ExecStart={{ program_override }} {{ program_config_options|default('') }} --log-file=/var/log/glance/{{ item.value.service_name }}.log
{% else %}
ExecStart={{ glance_bin }}/{{ program_name }} {{ program_config_options|default('') }} --log-file=/var/log/glance/{{ program_name }}.log
ExecStart={{ glance_bin }}/{{ item.value.service_name }} {{ program_config_options|default('') }} --log-file=/var/log/glance/{{ item.value.service_name }}.log
{% endif %}
# Give a reasonable amount of time for the server to start up/shut down

View File

@ -1,4 +1,4 @@
# {{ ansible_managed }}
D /var/lock/{{ program_name }} 2755 {{ system_user }} {{ system_group }}
D /var/run/{{ program_name }} 2755 {{ system_user }} {{ system_group }}
D /var/lock/{{ item.value.service_name }} 2755 {{ glance_system_user_name }} {{ glance_system_group_name }}
D /var/run/{{ item.value.service_name }} 2755 {{ glance_system_user_name }} {{ glance_system_group_name }}

View File

@ -1,6 +1,7 @@
# {{ ansible_managed }}
description "{{ program_name }}"
description "{{ item.value.service_name }}"
author "Kevin Carter <kevin.carter@rackspace.com>"
start on runlevel [2345]
@ -10,18 +11,18 @@ respawn
respawn limit 10 5
# Set the RUNBIN environment variable
env RUNBIN="{{ glance_bin }}/{{ program_name }}"
env RUNBIN="{{ glance_bin }}/{{ item.value.service_name }}"
# Change directory to service users home
chdir "{{ service_home }}"
chdir "{{ glance_system_user_home }}"
# Pre start actions
pre-start script
mkdir -p "/var/run/{{ program_name }}"
chown {{ system_user }}:{{ system_group }} "/var/run/{{ program_name }}"
mkdir -p "/var/run/{{ item.value.service_name }}"
chown {{ glance_system_user_name }}:{{ glance_system_group_name }} "/var/run/{{ item.value.service_name }}"
mkdir -p "/var/lock/{{ program_name }}"
chown {{ system_user }}:{{ system_group }} "/var/lock/{{ program_name }}"
mkdir -p "/var/lock/{{ item.value.service_name }}"
chown {{ glance_system_user_name }}:{{ glance_system_group_name }} "/var/lock/{{ item.value.service_name }}"
. {{ glance_bin }}/activate
@ -29,14 +30,15 @@ end script
# Post stop actions
post-stop script
rm "/var/run/{{ program_name }}/{{ program_name }}.pid"
rm "/var/run/{{ item.value.service_name }}/{{ item.value.service_name }}.pid"
end script
# Run the start up job
exec start-stop-daemon --start \
--chuid {{ system_user }} \
--chuid {{ glance_system_user_name }} \
--make-pidfile \
--pidfile /var/run/{{ program_name }}/{{ program_name }}.pid \
--pidfile /var/run/{{ item.value.service_name }}/{{ item.value.service_name }}.pid \
--exec "{{ program_override|default('$RUNBIN') }}" \
-- {{ program_config_options|default('') }} \
--log-file=/var/log/glance/{{ program_name }}.log
--log-file=/var/log/{{ glance_service_name }}/{{ item.value.service_name }}.log

View File

@ -24,5 +24,12 @@ memcached_all
[keystone_all]
openstack1
[glance_all]
[glance_api]
openstack1
[glance_registry]
openstack1
[glance_all:children]
glance_api
glance_registry