---
#
# Control Plane Upgrade Playbook
#
# Upgrades masters and Docker (only on standalone etcd hosts)
#
# This upgrade does not include:
# - node service running on masters
# - docker running on masters
# - node service running on dedicated nodes
#
# You can run the upgrade_nodes.yml playbook after this to upgrade these components separately.
#
- import_playbook: ../init.yml
  vars:
    l_upgrade_no_switch_firewall_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"
    l_init_fact_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"
    l_base_packages_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"

- name: Configure the initial upgrade target for the common upgrade tasks
  hosts: oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config
  tasks:
  - set_fact:
      # We use 3.9 here so when we run openshift_version we can get
      # correct values for 3.9, 3.8 we will hard-code the values in
      # ../pre/version_override.yml, if necessary.
      openshift_upgrade_target: '3.9'
      openshift_upgrade_min: '3.7'

## Check to see if we need to double upgrade (3.7 -> 3.8 -> 3.9)
- name: Configure variables for double upgrade
  hosts: oo_masters_to_config:oo_etcd_to_config
  tasks:
  - set_fact:
      l_double_upgrade_cp: True
      l_version_override_hosts: "oo_masters_to_config:oo_etcd_to_config"
      l_double_upgrade_first_version: "3.8"
      l_double_upgrade_first_release: "3.8"
    when: hostvars[groups.oo_first_master.0].openshift_currently_installed_version | version_compare('3.8','<')

- import_playbook: ../pre/config.yml
  # These vars a meant to exclude oo_nodes from plays that would otherwise include
  # them by default.
  vars:
    l_version_override_hosts: "oo_masters_to_config:oo_etcd_to_config"
    l_openshift_version_set_hosts: "oo_etcd_to_config:oo_masters_to_config:!oo_first_master"
    l_openshift_version_check_hosts: "oo_masters_to_config:!oo_first_master"
    l_upgrade_repo_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"
    l_upgrade_no_proxy_hosts: "oo_masters_to_config"
    l_upgrade_health_check_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"
    l_upgrade_verify_targets_hosts: "oo_masters_to_config"
    l_upgrade_docker_target_hosts: "oo_masters_to_config:oo_etcd_to_config"
    l_upgrade_excluder_hosts: "oo_masters_to_config"
    openshift_protect_installed_version: False
  when: l_double_upgrade_cp | default(False)

- name: Flag pre-upgrade checks complete for hosts without errors 3.8
  hosts: oo_masters_to_config:oo_etcd_to_config
  tasks:
  - set_fact:
      pre_upgrade_complete: True
    when: l_double_upgrade_cp | default(False)

# Pre-upgrade completed

- name: Intermediate 3.8 Upgrade
  import_playbook: ../upgrade_control_plane.yml
  when: l_double_upgrade_cp | default(False)

- name: Restore 3.9 version variables
  hosts: oo_masters_to_config:oo_etcd_to_config
  tasks:
  - set_fact:
      # all:!all == 0 hosts
      l_version_override_hosts: "all:!all"
      openshift_version: "{{ l_double_upgrade_saved_version }}"
      openshift_release: "{{ l_double_upgrade_saved_release }}"
      openshift_image_tag: "{{ l_double_upgrade_saved_tag }}"
      openshift_pkg_version: "{{ l_double_upgrade_saved_pkgv }}"
    when: l_double_upgrade_cp | default(False)

## 3.8 upgrade complete we should now be able to upgrade to 3.9
- name: Clear some values now that we're done with double upgrades.
  hosts: oo_masters_to_config:oo_etcd_to_config
  tasks:
  - set_fact:
      l_double_upgrade_cp: False
      l_double_upgrade_cp_reset_version: False

# We should be on 3.8 at this point, need to set upgrade_target to 3.9
- name: Configure the upgrade target for second upgrade
  hosts: oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config
  tasks:
  - set_fact:
      openshift_upgrade_target: '3.9'
      openshift_upgrade_min: '3.8'

- import_playbook: ../pre/config.yml
  # These vars a meant to exclude oo_nodes from plays that would otherwise include
  # them by default.
  vars:
    l_openshift_version_set_hosts: "oo_etcd_to_config:oo_masters_to_config:!oo_first_master"
    l_openshift_version_check_hosts: "oo_masters_to_config:!oo_first_master"
    l_upgrade_repo_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"
    l_upgrade_no_proxy_hosts: "oo_masters_to_config"
    l_upgrade_health_check_hosts: "oo_masters_to_config:oo_etcd_to_config:oo_lb_to_config"
    l_upgrade_verify_targets_hosts: "oo_masters_to_config"
    l_upgrade_docker_target_hosts: "oo_masters_to_config:oo_etcd_to_config"
    l_upgrade_excluder_hosts: "oo_masters_to_config"
    openshift_protect_installed_version: False

- name: Flag pre-upgrade checks complete for hosts without errors
  hosts: oo_masters_to_config:oo_etcd_to_config
  tasks:
  - set_fact:
      pre_upgrade_complete: True

- import_playbook: label_nodes.yml

- import_playbook: ../upgrade_control_plane.yml

# All controllers must be stopped at the same time then restarted
- name: Cycle all controller services to force new leader election mode
  hosts: oo_masters_to_config
  gather_facts: no
  roles:
  - role: openshift_facts
  tasks:
  - name: Restart master controllers to force new leader election mode
    service:
      name: "{{ openshift_service_type }}-master-controllers"
      state: restarted
    when: openshift.common.rolling_restart_mode == 'services'
  - name: Re-enable master controllers to force new leader election mode
    service:
      name: "{{ openshift_service_type }}-master-controllers"
      enabled: true
    when: openshift.common.rolling_restart_mode == 'system'


- import_playbook: ../post_control_plane.yml

- hosts: oo_masters
  tasks:
  - import_role:
      name: openshift_web_console
      tasks_from: remove_old_asset_config

# This is for a migration for a dc/docker-registry env var
# https://bugzilla.redhat.com/show_bug.cgi?id=1540467
# OPENSHIFT_DEFAULT_REGISTRY to REGISTRY_OPENSHIFT_SERVER_ADDR
- hosts: oo_first_master
  tasks:
  - import_role:
      name: openshift_hosted
      tasks_from: migrate_default_registry_var.yml
    when: openshift_hosted_manage_registry | default(True) | bool
  - import_role:
      name: openshift_hosted
      tasks_from: registry_service_account.yml
    when: openshift_hosted_manage_registry | default(True) | bool
  - import_role:
      name: openshift_hosted
      tasks_from: remove_legacy_env_variables.yml
    when: openshift_hosted_manage_registry | default(True) | bool
