From 3e522f6ef914130a91c7a6b90433bed04e302123 Mon Sep 17 00:00:00 2001 From: Jesse Pretorius Date: Wed, 28 Sep 2016 18:24:11 +0100 Subject: [PATCH] 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 --- defaults/main.yml | 12 ++++++----- handlers/main.yml | 28 ++++++++++++------------- tasks/glance_init.yml | 30 --------------------------- tasks/glance_init_common.yml | 8 +++---- tasks/glance_init_systemd.yml | 29 ++++++++++++++------------ tasks/glance_init_upstart.yml | 16 +++++--------- tasks/glance_post_install.yml | 3 +-- tasks/main.yml | 2 +- templates/glance-systemd-init.j2 | 8 +++---- templates/glance-systemd-tempfiles.j2 | 4 ++-- templates/glance-upstart-init.j2 | 24 +++++++++++---------- tests/inventory | 9 +++++++- 12 files changed, 75 insertions(+), 98 deletions(-) delete mode 100644 tasks/glance_init.yml diff --git a/defaults/main.yml b/defaults/main.yml index 4904d82d..4387760e 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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 diff --git a/handlers/main.yml b/handlers/main.yml index 0035c550..d49a143e 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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] diff --git a/tasks/glance_init.yml b/tasks/glance_init.yml deleted file mode 100644 index e157f1b7..00000000 --- a/tasks/glance_init.yml +++ /dev/null @@ -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 }}" diff --git a/tasks/glance_init_common.yml b/tasks/glance_init_common.yml index ddddfcb6..fc7d958c 100644 --- a/tasks/glance_init_common.yml +++ b/tasks/glance_init_common.yml @@ -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 diff --git a/tasks/glance_init_systemd.yml b/tasks/glance_init_systemd.yml index 4119fa58..07a85e94 100644 --- a/tasks/glance_init_systemd.yml +++ b/tasks/glance_init_systemd.yml @@ -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 diff --git a/tasks/glance_init_upstart.yml b/tasks/glance_init_upstart.yml index 9b78ca3e..18cc94e9 100644 --- a/tasks/glance_init_upstart.yml +++ b/tasks/glance_init_upstart.yml @@ -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 diff --git a/tasks/glance_post_install.yml b/tasks/glance_post_install.yml index 0c7bf6d6..f9dd0ff5 100644 --- a/tasks/glance_post_install.yml +++ b/tasks/glance_post_install.yml @@ -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: diff --git a/tasks/main.yml b/tasks/main.yml index 2f2546a1..89ac29ae 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -50,7 +50,7 @@ - glance-install - glance-config -- include: glance_init.yml +- include: glance_init_common.yml tags: - glance-install diff --git a/templates/glance-systemd-init.j2 b/templates/glance-systemd-init.j2 index 18ea2193..d94125d3 100644 --- a/templates/glance-systemd-init.j2 +++ b/templates/glance-systemd-init.j2 @@ -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 diff --git a/templates/glance-systemd-tempfiles.j2 b/templates/glance-systemd-tempfiles.j2 index b723d85d..bb92409e 100644 --- a/templates/glance-systemd-tempfiles.j2 +++ b/templates/glance-systemd-tempfiles.j2 @@ -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 }} diff --git a/templates/glance-upstart-init.j2 b/templates/glance-upstart-init.j2 index d03aa1f1..1b7dd694 100644 --- a/templates/glance-upstart-init.j2 +++ b/templates/glance-upstart-init.j2 @@ -1,6 +1,7 @@ # {{ ansible_managed }} -description "{{ program_name }}" + +description "{{ item.value.service_name }}" author "Kevin Carter " 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 + diff --git a/tests/inventory b/tests/inventory index 09149c6a..71fb3b9c 100644 --- a/tests/inventory +++ b/tests/inventory @@ -24,5 +24,12 @@ memcached_all [keystone_all] openstack1 -[glance_all] +[glance_api] openstack1 + +[glance_registry] +openstack1 + +[glance_all:children] +glance_api +glance_registry