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 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.

Change-Id: Id817631699d711993bbdad08660d8067550430e6
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
This commit is contained in:
Kevin Carter 2018-07-30 09:00:10 -05:00 committed by Kevin Carter (cloudnull)
parent be6bbec459
commit eac3740711
11 changed files with 119 additions and 133 deletions

View File

@ -18,16 +18,30 @@ almanach_version: 4.0.9
almanach_collector_program_name: almanach-collector
almanach_api_program_name: almanach-api
almanach_service_names:
- "almanach-api"
- "almanach-collector"
almanach_venv_tag: untagged
almanach_bin: "/openstack/venvs/almanach-{{ almanach_venv_tag }}/bin"
almanach_requires_apt_packages:
- python3-pip
almanach_services:
almanach-api:
group: almanach_all
service_name: almanach-api
init_config_overrides: "{{ almanach_api_init_config_overrides }}"
start_order: 1
execstarts: "{{ almanach_bin }}/almanach-api"
almanach-collector:
group: almanach_all
service_name: almanach-collector
init_config_overrides: "{{ almanach_collector_init_config_overrides }}"
start_order: 2
execstarts: "{{ almanach_bin }}/almanach-collector"
almanach_requires_pip_packages:
- virtualenv
almanach_pip_packages:
- "almanach=={{ almanach_version }}"
- systemd-python
almanach_system_user_name: almanach
almanach_system_group_name: almanach
almanach_system_shell: /bin/false
@ -36,8 +50,6 @@ almanach_system_comment: almanach system user
almanach_log_dir: /var/log/almanach
almanach_log_file: /var/log/almanach/almanach.log
almanach_app_dir: /opt/almanach
almanach_config_dir: /etc/almanach
almanach_config_file: /etc/almanach/almanach.conf
almanach_logrotate_config_file: /etc/logrotate.d/almanach

View File

@ -26,7 +26,7 @@
- name: Restart almanach services
systemd:
name: "{{ item }}"
name: "{{ item.service_name }}"
state: "restarted"
with_items: "{{ almanach_service_names }}"
with_items: "{{ filtered_almanach_services }}"
failed_when: false

View File

@ -1,30 +0,0 @@
---
# Copyright 2016, Internap 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: almanach_init_systemd.yml
vars:
program_name: "{{ almanach_api_program_name }}"
service_name: "almanach-api"
system_user: "{{ almanach_system_user_name }}"
system_group: "{{ almanach_system_group_name }}"
init_config_overrides: "{{ almanach_api_init_config_overrides }}"
- include: almanach_init_systemd.yml
vars:
program_name: "{{ almanach_collector_program_name }}"
service_name: "almanach-collector"
system_user: "{{ almanach_system_user_name }}"
system_group: "{{ almanach_system_group_name }}"
init_config_overrides: "{{ almanach_collector_init_config_overrides }}"

View File

@ -1,24 +0,0 @@
---
# Copyright 2016, Internap 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: Place the systemd init script
template:
src: "almanach-systemd-init.j2"
dest: "/etc/systemd/system/{{ program_name }}.service"
mode: "0644"
owner: "root"
group: "root"
notify:
- Restart almanach services

View File

@ -13,20 +13,45 @@
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Update APT index
apt: update_cache=yes
become: True
- name: Install apt packages
apt:
package:
name: "{{ almanach_requires_apt_packages | join(' ') }}"
state: present
update_cache: yes
register: install_packages
until: install_packages is success
retries: 5
delay: 2
# NOTE(cloudnull): This role has no "developer_mode" as such the developer
# packages are simply installed.
- name: Install developer apt packages
package:
name: "{{ almanach_developer_mode_distro_packages }}"
state: present
update_cache: yes
register: install_packages
until: install_packages is success
retries: 5
delay: 2
- name: Install pip packages
pip:
name: "{{ almanach_requires_pip_packages }}"
state: present
executable: pip3
register: install_packages
until: install_packages is success
retries: 5
delay: 2
- name: Install Almanach
pip: name=almanach version={{ almanach_version }} virtualenv={{ almanach_app_dir }} virtualenv_python=python3
- name: Install pip packages
pip:
name: "{{ almanach_pip_packages }}"
state: present
virtualenv: "/openstack/venvs/almanach-{{ almanach_venv_tag }}"
virtualenv_python: "python3"
register: install_packages
until: install_packages is success
retries: 5
delay: 2

View File

@ -24,19 +24,6 @@
tags:
- always
- name: Check init system
command: cat /proc/1/comm
changed_when: false
register: _pid1_name
tags:
- always
- name: Set the name of pid1
set_fact:
pid1_name: "{{ _pid1_name.stdout }}"
tags:
- always
- include: almanach_pre_install.yml
tags:
- almanach-install
@ -50,6 +37,30 @@
- almanach-install
- almanach-config
- include: almanach_init.yml
- name: Run the systemd service role
include_role:
name: systemd_service
private: true
vars:
systemd_user_name: "{{ almanach_system_user_name }}"
systemd_group_name: "{{ almanach_system_group_name }}"
systemd_tempd_prefix: openstack
systemd_slice_name: almanach
systemd_lock_path: /var/lock/almanach
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_almanach_services }}"
loop_control:
loop_var: service_var
tags:
- almanach-install
- almanach-config
- systemd-service

View File

@ -1,31 +0,0 @@
# {{ ansible_managed }}
[Unit]
Description=Almanach api openstack service
After=syslog.target
After=network.target
[Service]
Type=simple
EnvironmentFile=/etc/environment
User={{ system_user }}
Group={{ system_group }}
ExecStart={{ almanach_app_dir }}/bin/{{ program_name }} --config-file={{ almanach_config_file }} --log-file={{ almanach_log_file }}
# 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=almanach.slice
CPUAccounting=true
BlockIOAccounting=true
MemoryAccounting=false
TasksAccounting=true
[Install]
WantedBy=multi-user.target

View File

@ -1,4 +1,5 @@
[DEFAULT]
use_journal = True
[api]
bind_ip = {{ almanach_bind_ip }}

31
vars/main.yml Normal file
View File

@ -0,0 +1,31 @@
---
# Copyright 2018, 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.
#
# Compile a list of the services on a host based on whether
# the host is in the host group and the service is enabled.
# The service list is provided in the defined start order.
#
filtered_almanach_services: |-
{% set services = [] %}
{% for key, value in almanach_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') }}

View File

@ -1,17 +0,0 @@
---
# Copyright 2016, Internap 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.
## APT Cache options
cache_timeout: 600

View File

@ -15,3 +15,11 @@
## APT Cache options
cache_timeout: 600
almanach_requires_apt_packages:
- python3-pip
almanach_developer_mode_distro_packages:
- build-essential
- libsystemd-dev
- pkg-config