---
- name: Include OS family-specific variables
  include_vars: "{{ ansible_facts.os_family }}.yml"

- name: Ensure EPEL repo is installed
  package:
    name: epel-release
    state: present
  become: True
  when:
    - ansible_facts.os_family == 'RedHat'
    - kolla_install_epel | bool

- name: Ensure required packages are installed
  package:
    name: "{{ kolla_package_dependencies }}"
    state: present
    cache_valid_time: "{{ apt_cache_valid_time if ansible_facts.os_family == 'Debian' else omit }}"
    update_cache: "{{ True if ansible_facts.os_family == 'Debian' else omit }}"
  become: True

- name: Ensure source code checkout path exists
  file:
    path: "{{ kolla_source_path | dirname }}"
    state: directory
    owner: "{{ ansible_facts.user_uid }}"
    group: "{{ ansible_facts.user_gid }}"
  become: True
  when: kolla_ctl_install_type == 'source'

- name: Ensure Kolla source code checkout exists
  git:
    repo: "{{ kolla_source_url }}"
    dest: "{{ kolla_source_path }}"
    version: "{{ kolla_source_version }}"
  when: kolla_ctl_install_type == 'source'

- name: Ensure virtualenv parent directory exists
  file:
    path: "{{ kolla_venv | dirname }}"
    state: directory
    owner: "{{ ansible_facts.user_uid }}"
    group: "{{ ansible_facts.user_gid }}"
  become: True
  when: kolla_venv is not none

- name: Ensure the latest version of pip is installed
  pip:
    name: "{{ item.name }}"
    state: latest
    virtualenv: "{{ kolla_venv }}"
    virtualenv_command: "python3.{{ ansible_facts.python.version.minor }} -m venv"
  with_items:
    - { name: pip }

- name: Ensure Python package docker-py is absent
  # In version 2.0.0, docker renamed the docker-py python package to docker.
  # Kolla requires the docker package rather than the docker-py package.
  pip:
    name: docker-py
    state: absent
    virtualenv: "{{ kolla_venv }}"

- name: Ensure required Python packages are installed
  pip:
    name: "{{ item.name }}"
    version: "{{ item.version | default(omit) }}"
    state: "{{ item.state | default('present') }}"
    virtualenv: "{{ kolla_venv }}"
    extra_args: "{% if kolla_upper_constraints_file %}-c {{ kolla_upper_constraints_file }}{% endif %}"
  with_items:
    # Intall Kolla from source.
    - name: "{{ kolla_source_path }}"
      install: "{{ kolla_ctl_install_type == 'source' }}"
    # Intall Kolla from PyPI.
    - name: "kolla"
      version: "{{ kolla_openstack_release }}"
      install: "{{ kolla_ctl_install_type == 'binary' }}"
  when: item.install | default(True) | bool
