---
- name: Prepare Swift containers list
  set_fact:
    swift_containers:
      - { name: swift_account_server, group: swift-account-server }
      - { name: swift_account_auditor, group: swift-account-server }
      - { name: swift_account_replicator, group: swift-account-server }
      - { name: swift_account_reaper, group: swift-account-server }
      - { name: swift_rsyncd, group: swift-account-server }
      - { name: swift_container_server, group: swift-container-server }
      - { name: swift_container_auditor, group: swift-container-server }
      - { name: swift_container_replicator, group: swift-container-server }
      - { name: swift_container_updater, group: swift-container-server }
      - { name: swift_rsyncd, group: swift-container-server }
      - { name: swift_object_server, group: swift-object-server }
      - { name: swift_object_auditor, group: swift-object-server }
      - { name: swift_object_replicator, group: swift-object-server }
      - { name: swift_object_updater, group: swift-object-server }
      - { name: swift_object_expirer, group: swift-object-server }
      - { name: swift_rsyncd, group: swift-object-server }
      - { name: swift_proxy_server, group: swift-proxy-server }
    swift_replication_server_containers:
      - { name: swift_account_replication_server, group: swift-account-server }
      - { name: swift_container_replication_server, group: swift-container-server }
      - { name: swift_object_replication_server, group: swift-object-server }

- name: Add _replication_server containers to the list
  set_fact:
    swift_containers: "{{ swift_containers + swift_replication_server_containers }}"
  when: swift_has_replication_network

- name: Ensuring the containers up
  become: true
  kolla_docker:
    name: "{{ item.name }}"
    action: "get_container_state"
  register: container_state
  failed_when: not container_state.Running
  when: inventory_hostname in groups[item.group]
  with_items:
    - "{{ swift_containers }}"

- import_tasks: config.yml

- name: Check the configs
  become: true
  command: "{{ kolla_container_engine }} exec -u root {{ item.name }} /usr/local/bin/kolla_set_configs --check"
  changed_when: false
  failed_when: false
  register: check_results
  when: inventory_hostname in groups[item.group]
  with_items:
    - "{{ swift_containers }}"

# NOTE(jeffrey4l): when config_strategy == 'COPY_ALWAYS'
# and container env['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE',
# just remove the container and start again
- name: Containers config strategy
  become: true
  kolla_docker:
    name: "{{ item.name }}"
    action: "get_container_env"
  register: container_envs
  when: inventory_hostname in groups[item.group]
  with_items:
    - "{{ swift_containers }}"

- name: Remove the containers
  become: true
  kolla_docker:
    name: "{{ item[0]['name'] }}"
    action: "remove_container"
  register: remove_containers
  when:
    - inventory_hostname in groups[item[0]['group']]
    - config_strategy == "COPY_ONCE" or item[1]['KOLLA_CONFIG_STRATEGY'] == 'COPY_ONCE'
    - item[2]['rc'] == 1
  with_together:
    - "{{ swift_containers }}"
    - "{{ container_envs.results }}"
    - "{{ check_results.results }}"

- include_tasks: start.yml
  vars:
    run_start_swift_proxy_server: "yes"
  when: remove_containers.changed

- name: Restart containers
  become: true
  kolla_docker:
    name: "{{ item[0]['name'] }}"
    action: "restart_container"
  when:
    - inventory_hostname in groups[item[0]['group']]
    - config_strategy == 'COPY_ALWAYS'
    - item[1]['KOLLA_CONFIG_STRATEGY'] != 'COPY_ONCE'
    - item[2]['rc'] == 1
  with_together:
    - "{{ swift_containers }}"
    - "{{ container_envs.results }}"
    - "{{ check_results.results }}"
