---
- name: Gather facts
  openshift_facts:
    role: common

- block:
  - name: Clear yum cache
    command: "yum clean all"
    ignore_errors: true

  - name: Install the required rpms
    package:
      name: "{{ item }}"
      state: latest
    with_items: "{{ openshift_required_packages }}"

  - name: Start NetworkManager and network
    service:
      name: "{{ item }}"
      state: restarted
      enabled: true
    with_items:
    - NetworkManager
    - network

  - name: Determine if firewalld is installed
    rpm_q:
      name: "firewalld"
      state: present
    register: firewalld_installed
    failed_when: false

  - name: Stop firewalld
    service:
      name: firewalld
      state: stopped
      enabled: false
    when:
    - "{{ firewalld_installed.installed_versions | default([]) | length > 0 }}"

  - name: Start iptables
    service:
      name: iptables
      state: started
      enabled: true

  - name: Start docker
    service:
      name: docker
      state: started
      enabled: true

  when: not openshift.common.is_atomic | bool

# Fail as early as possible if Atomic and old version of Docker
- block:
  - name: Determine Atomic Host Docker Version
    shell: 'CURLY="{"; docker version --format "$CURLY{json .Server.Version}}"'
    register: l_atomic_docker_version

  - assert:
      msg: Installation on Atomic Host requires Docker 1.12 or later. Attempting to patch.
      that:
      - l_atomic_docker_version.stdout | replace('"', '') | version_compare('1.12','>=')

  rescue:
  - name: Patching Atomic instances
    shell: atomic host upgrade
    register: patched

  - name: Reboot when patched
    shell: sleep 5 && shutdown -r now "Reboot due to Atomic Patching"
    async: 1
    poll: 0
    ignore_errors: true
    when: patched.changed

  - name: Wait for hosts to be back
    pause:
      seconds: 60
    delegate_to: 127.0.0.1
    when: patched.changed

  when: openshift.common.is_atomic | bool
