---
# 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: Destroy lxc containers except galera
  hosts: "{{ destroy_hosts | default('all_containers:!galera_all:!neutron_agent:!ceph_all:!rsyslog_all')}}"
  gather_facts: false
  user: root
  tasks:
    - name: Destroy a container
      command: lxc-destroy --force --name "{{ container_name }}"
      delegate_to: "{{ physical_host }}"
      register: destroy
      failed_when: destroy.rc not in [0, 1]
      changed_when: destroy.rc == 0

    - name: Destroy container data
      file:
        path: "{{ item }}"
        state: "absent"
      with_items:
        - "/var/lib/lxc/{{ container_name }}"
        - "/openstack/{{ container_name }}"
        - "/openstack/backup/{{ container_name }}"
        - "/openstack/log/{{ container_name }}"
        - "/var/log/lxc/lxc-{{ container_name }}.log"
      delegate_to: "{{ physical_host }}"

- name: Destroy lxc container cache
  hosts: "hosts"
  gather_facts: false
  user: root
  tasks:
    - name: Destroy container cache data
      file:
        path: "{{ item }}"
        state: "absent"
      with_items:
        - "/var/cache/lxc/download"
        - "/var/cache/lxc/trusty"
        - "/var/lib/lxc/LXC_NAME"
      delegate_to: "{{ physical_host }}"

- name: Simple destroy the Galera and Logging Containers
  hosts: "galera_all:rsyslog_all"
  gather_facts: false
  tasks:
    - name: Destroy a container
      command: lxc-destroy --force --name "{{ container_name }}"
      delegate_to: "{{ physical_host }}"
      register: destroy
      failed_when: destroy.rc not in [0, 1]
      changed_when: destroy.rc == 0

- name: Host prep for redeployment
  hosts: "hosts"
  gather_facts: false
  user: root
  tasks:
    - name: Stop LXC dnsmasq
      command: "lxc-system-manage dnsmasq-stop"
      failed_when: false
    - name: Bring the LXC bridged interface down
      command: "ifdown {{ lxc_net_bridge | default('lxcbr0') }}"
      failed_when: false
    - name: Update apt-cache
      command: apt-get update