Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wwaltersp committed Mar 11, 2019
2 parents dc5e90f + c1ee7a6 commit d7a5f73
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ An example of how to use the role
celery_user: celery
celery_services:
- name: celeryd
state: started
enabled: yes
- name: celery_beat
state: stopped
enabled: no
environment:
SINGLE_BEAT_IDENTIFIER: celery-beat
SINGLE_BEAT_LOCK_TIME: 300
Expand Down
13 changes: 11 additions & 2 deletions tasks/manage_configs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
---

- name: Configure Celery EnvironmentFile
template: src=celery_env.j2 dest="{{celery_env_dir}}/{{item.name}}"
notify: "restart celery services"
template:
src: celery_env.j2
dest: "{{celery_env_dir}}/{{item.name}}"
with_items: "{{celery_services}}"

- name: Configure Celery systemd service unit
template:
src: celery.service.j2
dest: "{{celery_env_dir}}/{{item.name}}.service"
register: "celery_states"
with_items: "{{celery_services}}"
58 changes: 55 additions & 3 deletions tasks/manage_units.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
---
- name: Configure Celery systemd service unit
template: src=celery.service.j2 dest="/etc/systemd/system/{{item.name}}.service"
notify: restart celery services

- name: Link Service Unit
file:
src: "{{celery_env_dir}}/{{item.name}}.service"
dest: "/etc/systemd/system/{{item.name}}.service"
owner: root
group: root
state: link
with_items: "{{celery_services}}"

- name: Get units configs
find:
path: "{{celery_env_dir}}/"
pattern: '^.*?\.service$'
use_regex: yes
register: service_configs
changed_when: false

- name: Set file list
set_fact:
unmanaged_configs: "{{(service_configs.files|map(attribute='path')|map('basename')|map('replace', '.service','')|list)|reject('in', celery_services|map(attribute='name')|list)|list}}"
changed_when: false

- name: Stop Unmanaged Service
systemd:
name: "{{item}}.service"
state: stopped
with_items: "{{unmanaged_configs}}"

- name: Remove Unmanaged Services
file:
dest: "/etc/systemd/system/{{item}}.service"
owner: root
group: root
state: absent
register: removed_services
with_items: "{{unmanaged_configs}}"

- name: Detect Changes
command: /bin/true
register: celery_services_state
when: item.changed
with_items: "{{celery_states.results + removed_services.results}}"

- name: Reload systemd daemon
systemd:
daemon_reload: yes
when: celery_services_state is changed

- name: Enable Service
systemd:
name: "{{item.name}}.service"
state: "{{item.state|default('stopped')}}"
enabled: yes
when: item.enabled|default(False)|bool
with_items: "{{celery_services}}"
3 changes: 3 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ printf ${green}"Testing Start Handlers and Service\n"${neutral}
docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook -e 'test_handlers=True test_service=True'
printf ${green}"Handlers and Service test passed\n"${neutra}

printf ${green}"Testing Service Removal\n"${neutral}
docker exec $container_id ansible-playbook /etc/ansible/roles/role_under_test/tests/$playbook -e 'service_test=True'
printf ${green}"Service Removal test passed\n"${neutra}

# Remove the Docker container (if configured).
if [ "$cleanup" = true ]; then
Expand Down
19 changes: 19 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,22 @@
command: echo stopping celery
notify: stop celery services
when: test_handlers is defined

- hosts: localhost
remote_user: root
roles:
- { role: role_under_test, when: service_test is defined, celery_working_dir: /srv, celery_app: tasks, celery_user: celery, celery_services: []}
tasks:
- name: Fail To start Celery
systemd:
name: celeryd
state: stopped
register: failed_start
failed_when: false
when: service_test is defined

- name: Assert failure
assert:
that:
- "'Could not find the requested service celeryd' in failed_start.msg"
when: service_test is defined

0 comments on commit d7a5f73

Please sign in to comment.