---
- name: Pre-migrate checks
  hosts: localhost
  tasks:
  # Check there is only one etcd host
  - assert:
      that: groups.oo_etcd_to_config | default([]) | length == 1
      msg: "[etcd] group must contain only one host"
  # Check there is only one master
  - assert:
      that: groups.oo_masters_to_config | default([]) | length == 1
      msg: "[master] group must contain only one host"

# 1. stop a master
- name: Prepare masters for etcd data migration
  hosts: oo_first_master
  roles:
  - role: openshift_facts
  tasks:
  - name: Check the master API is ready
    import_role:
      name: openshift_master
      tasks_from: check_master_api_is_ready.yml
  - set_fact:
      master_service: "{{ openshift_service_type + '-master' }}"
      embedded_etcd_backup_suffix: "{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}"
  - debug:
      msg: "master service name: {{ master_service }}"
  - name: Stop master
    service:
      name: "{{ master_service }}"
      state: stopped
  # 2. backup embedded etcd
  # Can't use with_items with import_role: https://github.com/ansible/ansible/issues/21285
  - import_role:
      name: etcd
      tasks_from: backup.yml
    vars:
      r_etcd_common_backup_tag: pre-migrate
      r_etcd_common_embedded_etcd: "{{ true }}"
      r_etcd_common_backup_sufix_name: "{{ embedded_etcd_backup_suffix }}"

  - import_role:
      name: etcd
      tasks_from: backup.archive.yml
    vars:
      r_etcd_common_backup_tag: pre-migrate
      r_etcd_common_embedded_etcd: "{{ true }}"
      r_etcd_common_backup_sufix_name: "{{ embedded_etcd_backup_suffix }}"

# 3. deploy certificates (for etcd and master)
- import_playbook: ca.yml

- import_playbook: server_certificates.yml

- name: Backup etcd client certificates for master host
  hosts: oo_first_master
  tasks:
  - import_role:
      name: etcd
      tasks_from: backup_master_etcd_certificates.yml

- name: Redeploy master etcd certificates
  import_playbook: master_etcd_certificates.yml
  vars:
    etcd_certificates_redeploy: "{{ true }}"

# 4. deploy external etcd
- import_playbook: config.yml

# 5. stop external etcd
- name: Cleanse etcd
  hosts: oo_etcd_to_config[0]
  gather_facts: no
  pre_tasks:
  - import_role:
      name: etcd
      tasks_from: disable_etcd.yml
  - import_role:
      name: etcd
      tasks_from: clean_data.yml

# 6. copy the embedded etcd backup to the external host
# TODO(jchaloup): if the etcd and first master are on the same host, just copy the directory
- name: Copy embedded etcd backup to the external host
  hosts: localhost
  tasks:
  - name: Create local temp directory for syncing etcd backup
    local_action: command mktemp -d /tmp/etcd_backup-XXXXXXX
    register: g_etcd_client_mktemp
    changed_when: False

  - name: Chmod local temp directory for syncing etcd backup
    local_action: command chmod 777 "{{ g_etcd_client_mktemp.stdout }}"
    changed_when: False

  - import_role:
      name: etcd
      tasks_from: backup.fetch.yml
    vars:
      etcd_backup_sync_directory: "{{ g_etcd_client_mktemp.stdout }}"
      r_etcd_common_backup_tag: pre-migrate
      r_etcd_common_embedded_etcd: "{{ true }}"
      r_etcd_common_backup_sufix_name: "{{ hostvars[groups.oo_first_master.0].embedded_etcd_backup_suffix }}"
    delegate_to: "{{ groups.oo_first_master[0] }}"

  - import_role:
      name: etcd
      tasks_from: backup.copy.yml
    vars:
      etcd_backup_sync_directory: "{{ g_etcd_client_mktemp.stdout }}"
      r_etcd_common_backup_tag: pre-migrate
      r_etcd_common_backup_sufix_name: "{{ hostvars[groups.oo_first_master.0].embedded_etcd_backup_suffix }}"
    delegate_to: "{{ groups.oo_etcd_to_config[0] }}"

  - debug:
      msg: "etcd_backup_dest_directory: {{ g_etcd_client_mktemp.stdout }}"

  - name: Delete temporary directory
    local_action: file path="{{ g_etcd_client_mktemp.stdout }}" state=absent
    changed_when: False

# 7. force new cluster from the backup
- name: Force new etcd cluster
  hosts: oo_etcd_to_config[0]
  tasks:
  - import_role:
      name: etcd
      tasks_from: backup.unarchive.yml
    vars:
      r_etcd_common_backup_tag: pre-migrate
      r_etcd_common_backup_sufix_name: "{{ hostvars[groups.oo_first_master.0].embedded_etcd_backup_suffix }}"

  - import_role:
      name: etcd
      tasks_from: backup.force_new_cluster.yml
    vars:
      r_etcd_common_backup_tag: pre-migrate
      r_etcd_common_backup_sufix_name: "{{ hostvars[groups.oo_first_master.0].embedded_etcd_backup_suffix }}"
      etcd_peer: "{{ openshift.common.ip }}"
      etcd_url_scheme: "https"
      etcd_peer_url_scheme: "https"

# 8. re-configure master to use the external etcd
- name: Configure master to use external etcd
  hosts: oo_first_master
  tasks:
  - import_role:
      name: openshift_master
      tasks_from: configure_external_etcd.yml
    vars:
      etcd_peer_url_scheme: "https"
      etcd_ip: "{{ hostvars[groups.oo_etcd_to_config.0].openshift.common.ip }}"
      etcd_peer_port: 2379

  # 9. start the master
  - name: Start master
    service:
      name: "{{ master_service }}"
      state: started
    register: service_status
    until: service_status.state is defined and service_status.state == "started"
    retries: 5
    delay: 10