From f3eddb3d513a5738f21a5088e5eb6c73730bd80f Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotjagov Date: Fri, 15 Feb 2019 11:42:06 +0200 Subject: [PATCH] Convert systemd services to common role(s) This removes the systemd service templates and tasks from this role and leverages a common systemd service role instead. This change removes a lot of code duplication across all roles all without sacrificing features or functionality. The intention of this change is to ensure uniformity and reduce the maintenance burden on the community when sweeping changes are needed. The exterior role is built to be OSA compatible and may be pulled into tree should we deem it necessary. Change-Id: I54e3063d6e641a785377f9039641072f8001cf24 --- defaults/main.yml | 2 + handlers/main.yml | 6 +-- tasks/gnocchi_init_systemd.yml | 60 --------------------------- tasks/main.yml | 17 +++++++- templates/gnocchi-systemd-init.j2 | 30 -------------- templates/gnocchi-systemd-tmpfiles.j2 | 4 -- vars/main.yml | 25 +++++++++++ 7 files changed, 46 insertions(+), 98 deletions(-) delete mode 100644 tasks/gnocchi_init_systemd.yml delete mode 100644 templates/gnocchi-systemd-init.j2 delete mode 100644 templates/gnocchi-systemd-tmpfiles.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 6509297..e55e6e7 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -169,11 +169,13 @@ gnocchi_services: service_name: "gnocchi-api" service_enabled: "{{ gnocchi_use_mod_wsgi | ternary(false, true) }}" init_config_overrides: "{{ gnocchi_api_init_overrides }}" + execstarts: "{{ gnocchi_bin }}/gnocchi-api" gnocchi-metricd: group: "gnocchi_metricd" service_name: "gnocchi-metricd" service_enabled: true init_config_overrides: "{{ gnocchi_metricd_init_overrides }}" + execstarts: "{{ gnocchi_bin }}/gnocchi-metricd" #: Common pip packages gnocchi_pip_packages: diff --git a/handlers/main.yml b/handlers/main.yml index 324488d..8899f9b 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -18,7 +18,7 @@ name: "{{ item.value.service_name }}" enabled: yes state: "stopped" - daemon_reload: "{{ (ansible_service_mgr == 'systemd') | ternary('yes', omit) }}" + daemon_reload: yes with_dict: "{{ gnocchi_services }}" when: - "item.value.group in group_names" @@ -56,7 +56,7 @@ name: "{{ item.value.service_name }}" enabled: yes state: "started" - daemon_reload: "{{ (ansible_service_mgr == 'systemd') | ternary('yes', omit) }}" + daemon_reload: yes with_dict: "{{ gnocchi_services }}" when: - "item.value.group in group_names" @@ -74,7 +74,7 @@ name: "{{ gnocchi_system_service_name }}" enabled: yes state: "restarted" - daemon_reload: "{{ (ansible_service_mgr == 'systemd') | ternary('yes', omit) }}" + daemon_reload: yes when: gnocchi_use_mod_wsgi | bool register: _restart until: _restart is success diff --git a/tasks/gnocchi_init_systemd.yml b/tasks/gnocchi_init_systemd.yml deleted file mode 100644 index 2ee8159..0000000 --- a/tasks/gnocchi_init_systemd.yml +++ /dev/null @@ -1,60 +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. - -- name: Create TEMP run dir - file: - path: "/var/run/{{ item.value.service_name }}" - state: directory - owner: "{{ gnocchi_system_user_name }}" - group: "{{ gnocchi_system_group_name }}" - mode: "02755" - with_dict: "{{ gnocchi_services }}" - when: "item.value.group in group_names" - -- name: Create TEMP lock dir - file: - path: "/var/lock/{{ item.value.service_name }}" - state: directory - owner: "{{ gnocchi_system_user_name }}" - group: "{{ gnocchi_system_group_name }}" - mode: "02755" - with_dict: "{{ gnocchi_services }}" - when: "item.value.group in group_names" - -- name: Create tmpfiles.d entry - template: - src: "gnocchi-systemd-tmpfiles.j2" - dest: "/etc/tmpfiles.d//{{ item.value.service_name }}.conf" - mode: "0644" - owner: "root" - group: "root" - with_dict: "{{ gnocchi_services }}" - when: "item.value.group in group_names" - notify: - - Restart gnocchi services - -- name: Place the systemd init script - config_template: - src: "gnocchi-systemd-init.j2" - dest: "/etc/systemd/system/{{ item.value.service_name }}.service" - mode: "0644" - owner: "root" - group: "root" - config_overrides: "{{ item.value.init_config_overrides }}" - config_type: "ini" - with_dict: "{{ gnocchi_services }}" - when: "item.value.group in group_names" - notify: - - Restart gnocchi services diff --git a/tasks/main.yml b/tasks/main.yml index 3b06315..0804fb1 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -38,10 +38,25 @@ tags: - gnocchi-config -- include_tasks: "gnocchi_init_{{ ansible_service_mgr }}.yml" +- name: Run the systemd service role + include_role: + name: systemd_service + private: true + vars: + systemd_user_name: "{{ gnocchi_system_user_name }}" + systemd_group_name: "{{ gnocchi_system_group_name }}" + systemd_tempd_prefix: openstack + systemd_slice_name: gnocchi + systemd_lock_path: /var/lock/gnocchi + systemd_CPUAccounting: true + systemd_BlockIOAccounting: true + systemd_MemoryAccounting: true + systemd_TasksAccounting: true + systemd_services: "{{ filtered_gnocchi_services }}" when: not gnocchi_identity_only | bool tags: - gnocchi-config + - systemd-service - include_tasks: gnocchi_service_setup.yml when: diff --git a/templates/gnocchi-systemd-init.j2 b/templates/gnocchi-systemd-init.j2 deleted file mode 100644 index c643b94..0000000 --- a/templates/gnocchi-systemd-init.j2 +++ /dev/null @@ -1,30 +0,0 @@ -# {{ ansible_managed }} - -[Unit] -Description=gnocchi openstack service -After=syslog.target -After=network.target - -[Service] -Type=simple -User={{ gnocchi_system_user_name }} -Group={{ gnocchi_system_group_name }} - -ExecStart={{ gnocchi_bin }}/{{ item.value.service_name }} {{ program_config_options|default('') }} --log-file=/var/log/gnocchi/{{ item.value.service_name }}.log - -# Give a reasonable amount of time for the server to start up/shut down -TimeoutSec=120 -Restart=on-failure -RestartSec=2 - -# This creates a specific slice which all services will operate from -# The accounting options give us the ability to see resource usage through -# the `systemd-cgtop` command. -Slice=gnocchi.slice -CPUAccounting=true -BlockIOAccounting=true -MemoryAccounting=false -TasksAccounting=true - -[Install] -WantedBy=multi-user.target diff --git a/templates/gnocchi-systemd-tmpfiles.j2 b/templates/gnocchi-systemd-tmpfiles.j2 deleted file mode 100644 index a2b67b1..0000000 --- a/templates/gnocchi-systemd-tmpfiles.j2 +++ /dev/null @@ -1,4 +0,0 @@ -# {{ ansible_managed }} - -D /var/lock/{{ item.value.service_name }} 2755 {{ gnocchi_system_user_name }} {{ gnocchi_system_group_name }} -D /var/run/{{ item.value.service_name }} 2755 {{ gnocchi_system_user_name }} {{ gnocchi_system_group_name }} diff --git a/vars/main.yml b/vars/main.yml index a3cdfbb..70af97a 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -23,3 +23,28 @@ gnocchi_api_paste_default_content: | gnocchi_policy_default_content: | {{ _git_file_fetch.results | selectattr('item', 'equalto', gnocchi_git_config_lookup_location ~ '/' ~ gnocchi_policy_git_file_path) | map(attribute='content') | first }} +filtered_gnocchi_services: |- + {% set services = [] %} + {% for name, service in gnocchi_services.items() %} + {% if (service['group'] in group_names) and + (('service_enabled' not in service) or + ('service_enabled' in service and service['service_enabled'])) %} + {% set _ = service.update( + { + 'service_key': name, + 'enabled': 'yes', + 'state': 'started', + 'config_overrides': service.init_config_overrides + } + ) + %} + {% set _ = service.pop('init_config_overrides') -%} + {# Note (noonedeadpunk): The following if statement is added for backwards compatability #} + {# As up to stein release gnocchi role didn't have 'execstarts' in gnocchi_services keys #} + {% if ('execstarts' not in service) %} + {% set _ = service.update({'execstarts': gnocchi_bin ~ '/' ~ service.service_name}) %} + {% endif %} + {% set _ = services.append(service) %} + {% endif %} + {% endfor %} + {{ services }}