From 376ddeb48f09b01b06f8e7dad8adbc75f832bd6a Mon Sep 17 00:00:00 2001 From: Mohammed Naser Date: Fri, 28 Sep 2018 20:49:02 -0400 Subject: [PATCH] Convert role to use a common systemd service role This removes the systemd service templates and tasks from this role and leverages a common systemd service role instead. This changet 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. In addition, it re-orders some tasks for consistency with other roles. Change-Id: I124873a6ab96aa95f886ce146d28e7340c90d40d --- defaults/main.yml | 13 ++++++- handlers/main.yml | 18 +++------ tasks/main.yml | 29 +++++++++++++- tasks/octavia_init_common.yml | 26 ------------- tasks/octavia_init_systemd.yml | 54 --------------------------- tasks/octavia_uwsgi.yml | 8 ++-- templates/octavia-systemd-init.j2 | 39 ------------------- templates/octavia-systemd-tmpfiles.j2 | 4 -- templates/octavia-uwsgi.ini.j2 | 5 ++- templates/octavia.conf.j2 | 1 + vars/main.yml | 30 +++++++++++++++ 11 files changed, 83 insertions(+), 144 deletions(-) delete mode 100644 tasks/octavia_init_common.yml delete mode 100644 tasks/octavia_init_systemd.yml delete mode 100644 templates/octavia-systemd-init.j2 delete mode 100644 templates/octavia-systemd-tmpfiles.j2 create mode 100644 vars/main.yml diff --git a/defaults/main.yml b/defaults/main.yml index e8f9e626..cbbe0c21 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -181,10 +181,12 @@ octavia_services: octavia-api: group: octavia-api service_name: octavia-api + start_order: 4 init_config_overrides: "{{ octavia_api_init_overrides }}" + execstarts: "{{ octavia_bin }}/uwsgi --ini /etc/uwsgi/octavia-api.ini" + execreloads: "{{ octavia_bin }}/uwsgi --reload /var/run/octavia-api/octavia-api.pid" wsgi_overrides: "{{ octavia_api_uwsgi_ini_overrides }}" wsgi_app: True - log_string: "--logto " wsgi_name: octavia-wsgi uwsgi_port: "{{ octavia_service_port }}" uwsgi_bind_address: "{{ octavia_uwsgi_bind_address }}" @@ -192,15 +194,24 @@ octavia_services: octavia-worker: group: octavia-worker service_name: octavia-worker + start_order: 1 init_config_overrides: "{{ octavia_worker_init_overrides }}" + execstarts: "{{ octavia_bin }}/octavia-worker" + execreloads: "/bin/kill -HUP $MAINPID" octavia-housekeeping: group: octavia-housekeeping service_name: octavia-housekeeping + start_order: 3 init_config_overrides: "{{ octavia_housekeeping_init_overrides }}" + execstarts: "{{ octavia_bin }}/octavia-housekeeping" + execreloads: "/bin/kill -HUP $MAINPID" octavia-health-manager: group: octavia-health_manager service_name: octavia-health-manager + start_order: 2 init_config_overrides: "{{ octavia_health_manager_init_overrides }}" + execstarts: "{{ octavia_bin }}/octavia-health-manager" + execreloads: "/bin/kill -HUP $MAINPID" # Required secrets for the role octavia_required_secrets: diff --git a/handlers/main.yml b/handlers/main.yml index 34ea2f7c..79d96ed5 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -13,28 +13,22 @@ # See the License for the specific language governing permissions and # limitations under the License. -- name: Reload systemd daemon - systemd: - daemon_reload: yes - notify: - - Restart octavia services - - name: Restart octavia services service: - name: "{{ item.value.service_name }}" + name: "{{ item.service_name }}" state: "restarted" - with_dict: "{{ octavia_services }}" + with_items: "{{ filtered_octavia_services }}" listen: - "venv changed" - name: Start octavia services service: - name: "{{ item.value.service_name }}" + name: "{{ item.service_name }}" state: "started" - with_dict: "{{ octavia_services }}" + with_items: "{{ filtered_octavia_services }}" - name: Stop octavia services service: - name: "{{ item.value.service_name }}" + name: "{{ item.service_name }}" state: "stopped" - with_dict: "{{ octavia_services }}" + with_items: "{{ filtered_octavia_services }}" diff --git a/tasks/main.yml b/tasks/main.yml index 9adc68d3..f1fa9cdf 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -50,9 +50,34 @@ tags: - octavia-config -- include_tasks: octavia_init_common.yml +- name: Run the systemd service role + include_role: + name: systemd_service + private: true + vars: + systemd_after_targets: "{{ service_var.after_targets | default(['syslog.target', 'network.target']) }}" + systemd_user_name: "{{ octavia_system_user_name }}" + systemd_group_name: "{{ octavia_system_group_name }}" + systemd_tempd_prefix: openstack + systemd_slice_name: nova + systemd_lock_path: /var/lock/octavia + systemd_CPUAccounting: true + systemd_BlockIOAccounting: true + systemd_MemoryAccounting: true + systemd_TasksAccounting: true + systemd_services: + - service_name: "{{ service_var.service_name }}" + enabled: yes + state: started + execstarts: "{{ service_var.execstarts }}" + execreloads: "{{ service_var.execreloads | default([]) }}" + config_overrides: "{{ service_var.init_config_overrides }}" + with_items: "{{ filtered_octavia_services }}" + loop_control: + loop_var: service_var tags: - - octavia-install + - nova-config + - systemd-service - include_tasks: octavia_service_add.yml run_once: true diff --git a/tasks/octavia_init_common.yml b/tasks/octavia_init_common.yml deleted file mode 100644 index 797ffe7b..00000000 --- a/tasks/octavia_init_common.yml +++ /dev/null @@ -1,26 +0,0 @@ ---- -# Copyright 2017, 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_tasks: octavia_init_systemd.yml - when: - - ansible_service_mgr == 'systemd' - -- name: Load service - service: - name: "{{ item.value.service_name }}" - enabled: "yes" - with_dict: "{{ octavia_services }}" - notify: - - Restart octavia services diff --git a/tasks/octavia_init_systemd.yml b/tasks/octavia_init_systemd.yml deleted file mode 100644 index e9057e0e..00000000 --- a/tasks/octavia_init_systemd.yml +++ /dev/null @@ -1,54 +0,0 @@ ---- -# Copyright 2017, 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: "{{ octavia_system_user_name }}" - group: "{{ octavia_system_group_name }}" - mode: "02755" - with_dict: "{{ octavia_services }}" - -- name: Create TEMP lock dir - file: - path: "/var/lock/{{ item.value.service_name }}" - state: directory - owner: "{{ octavia_system_user_name }}" - group: "{{ octavia_system_group_name }}" - mode: "02755" - with_dict: "{{ octavia_services }}" - -- name: Create tmpfiles.d entry - template: - src: "octavia-systemd-tmpfiles.j2" - dest: "/etc/tmpfiles.d/openstack-{{ item.value.service_name }}.conf" - mode: "0644" - owner: "root" - group: "root" - with_dict: "{{ octavia_services }}" - -- name: Place the systemd init script - config_template: - src: "octavia-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: "{{ octavia_services }}" - notify: - - Reload systemd daemon diff --git a/tasks/octavia_uwsgi.yml b/tasks/octavia_uwsgi.yml index 0e838f65..2df5b09f 100644 --- a/tasks/octavia_uwsgi.yml +++ b/tasks/octavia_uwsgi.yml @@ -22,11 +22,11 @@ - name: Apply uWSGI configuration config_template: src: "octavia-uwsgi.ini.j2" - dest: "/etc/uwsgi/{{ item.value.service_name }}.ini" + dest: "/etc/uwsgi/{{ item.service_name }}.ini" mode: "0744" - config_overrides: "{{ item.value.wsgi_overrides }}" + config_overrides: "{{ item.wsgi_overrides }}" config_type: ini - with_dict: "{{ octavia_services }}" - when: item.value.wsgi_app | default(False) + with_items: "{{ filtered_octavia_services }}" + when: item.wsgi_app | default(False) notify: - Restart octavia services diff --git a/templates/octavia-systemd-init.j2 b/templates/octavia-systemd-init.j2 deleted file mode 100644 index ce0cdb68..00000000 --- a/templates/octavia-systemd-init.j2 +++ /dev/null @@ -1,39 +0,0 @@ -# {{ ansible_managed }} - -[Unit] -Description=octavia openstack service -After=syslog.target -After=network.target - -[Service] -Type=simple -User={{ octavia_system_user_name }} -Group={{ octavia_system_group_name }} - -{% if item.value.program_override is defined %} -ExecStart={{ item.value.program_override }} {{ item.value.program_config_options|default('') }} {{ item.value.log_string | default('--log-file=') }}/var/log/octavia/{{ item.value.service_name }}.log -{% else %} -ExecStart={{ octavia_bin }}/{{ item.value.service_name }} {{ item.value.program_config_options|default('') }} --log-file=/var/log/octavia/{{ item.value.service_name }}.log -{% endif %} - -# Give a reasonable amount of time for the server to start up/shut down -TimeoutStartSec=120 -# Give extra time for shutdown to allow flows to finish (failover, etc.) -# This timer expiring early can lead to load balancers stuck in PENDING_* -# states as the flows were interrupted before completing. -# This setting depends on the performance of your cloud. -TimeoutStopSec=300 -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=octavia.slice -CPUAccounting=true -BlockIOAccounting=true -MemoryAccounting=false -TasksAccounting=true - -[Install] -WantedBy=multi-user.target diff --git a/templates/octavia-systemd-tmpfiles.j2 b/templates/octavia-systemd-tmpfiles.j2 deleted file mode 100644 index 5c271a39..00000000 --- a/templates/octavia-systemd-tmpfiles.j2 +++ /dev/null @@ -1,4 +0,0 @@ -# {{ ansible_managed }} - -D /var/lock/{{ item.value.service_name }} 2755 {{ octavia_system_user_name }} {{ octavia_system_group_name }} -D /var/run/{{ item.value.service_name }} 2755 {{ octavia_system_user_name }} {{ octavia_system_group_name }} diff --git a/templates/octavia-uwsgi.ini.j2 b/templates/octavia-uwsgi.ini.j2 index 75ad6868..f0203b61 100644 --- a/templates/octavia-uwsgi.ini.j2 +++ b/templates/octavia-uwsgi.ini.j2 @@ -3,8 +3,8 @@ uid = {{ octavia_system_user_name }} gid = {{ octavia_system_group_name }} virtualenv = /openstack/venvs/octavia-{{ octavia_venv_tag }} -wsgi-file = {{ octavia_bin }}/{{ item.value.wsgi_name }} -http-socket = {{ item.value.uwsgi_bind_address }}:{{ item.value.uwsgi_port }} +wsgi-file = {{ octavia_bin }}/{{ item.wsgi_name }} +http-socket = {{ item.uwsgi_bind_address }}:{{ item.uwsgi_port }} master = true enable-threads = true @@ -17,6 +17,7 @@ add-header = Connection: close buffer-size = {{ octavia_wsgi_buffer_size }} thunder-lock = true logfile-chmod = 644 +pidfile = /var/run/{{ item.service_name }}/{{ item.service_name }}.pid # Avoid filling up the logs with health check requests from haproxy. route-user-agent = ^osa-haproxy-healthcheck$ donotlog: diff --git a/templates/octavia.conf.j2 b/templates/octavia.conf.j2 index c032a26d..664ef972 100644 --- a/templates/octavia.conf.j2 +++ b/templates/octavia.conf.j2 @@ -1,6 +1,7 @@ [DEFAULT] # Print debugging output (set logging level to DEBUG instead of default WARNING level). debug = {{ debug }} +log_dir = /var/log/octavia {% if not octavia_v2|bool %} bind_host = 0.0.0.0 diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 00000000..1f142b49 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,30 @@ +--- +# Copyright 2018, VEXXHOST, 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. + +# +# Compile a list of the services on a host based on whether +# the host is in the host group and the service is enabled. +# +filtered_octavia_services: |- + {% set services = [] %} + {% for key, value in octavia_services.items() %} + {% if (value['group'] in group_names) and + (('condition' not in value) or + ('condition' in value and value['condition'])) %} + {% set _ = value.update({'service_key': key}) %} + {% set _ = services.append(value) %} + {% endif %} + {% endfor %} + {{ services | sort(attribute='start_order') }}