---
# 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.
# 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: Stop services
  service:
    name: "{{ item.service_name }}"
    enabled: yes
    state: "stopped"
    daemon_reload: "{{ (ansible_service_mgr == 'systemd') | ternary('yes', omit) }}"
  with_items: "{{ filtered_glance_services }}"
  register: _stop
  until: _stop is success
  retries: 5
  delay: 2
  listen:
    - "Restart glance services"
    - "venv changed"

# TODO(odyssey4me):
# Assuming glance-registry is removed in the S cycle as planned, this
# task can be removed in the T cycle.
# Note that this uses shell task because the unit may not exist. When it
# does not exist, the ansible task which stops/disables it fails and it
# is important that we see failures, but get failures in the right
# circumstances.
- name: Stop and disable glance-registry when it is no longer used
  shell: |
    return_code=0
    if [[ "$(systemctl is-active {{ glance_services['glance-registry']['service_name'] }})" == "active" ]]; then
      systemctl stop {{ glance_services['glance-registry']['service_name'] }}
      return_code=2
    fi
    if [[ "$(systemctl is-enabled {{ glance_services['glance-registry']['service_name'] }})" == "enabled" ]]; then
      systemctl disable {{ glance_services['glance-registry']['service_name'] }}
      return_code=2
    fi
    exit ${return_code}
  args:
    executable: /bin/bash
  when:
    - not glance_services['glance-registry']['condition']
  register: _remove_glance_service
  changed_when: _remove_glance_service.rc == 2
  failed_when: _remove_glance_service.rc not in [0, 2]
  listen:
    - "Restart glance services"
    - "venv changed"
  # This task causes ansible-lint to give a ANSIBLE0014
  # error, which does not make much sense given how the
  # environment variable is used in this case.
  # TODO(odyssey4me):
  # Try to understand the issue ansible-lint is trying
  # to highlight and address it.
  tags:
    - skip_ansible_lint

# TODO(odyssey4me):
# Assuming glance-registry is removed in the S cycle as planned, this
# task can be removed in the T cycle.
- name: Clean up glance-registry init files when they are no longer needed
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "/etc/tmpfiles.d/openstack-{{ glance_services['glance-registry']['service_name'] }}.conf"
    - "/etc/systemd/system/{{ glance_services['glance-registry']['service_name'] }}.service"
  when:
    - not glance_services['glance-registry']['condition']
  listen:
    - "Restart glance services"
    - "venv changed"

- name: Start services
  service:
    name: "{{ item.service_name }}"
    enabled: yes
    state: "started"
    daemon_reload: "{{ (ansible_service_mgr == 'systemd') | ternary('yes', omit) }}"
  with_items: "{{ filtered_glance_services }}"
  register: _start
  until: _start is success
  retries: 5
  delay: 2
  listen:
    - "Restart glance services"
    - "venv changed"

- meta: noop
  listen: Manage LB
  when: false