---
# Copyright 2015, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Unify pip utils
  hosts: "hosts:!localhost"
  gather_facts: false
  user: root
  vars:
    pip_upstream_url: "https://raw.githubusercontent.com/pypa/get-pip/430ba37776ae2ad89f794c7a43b90dc23bac334c/get-pip.py"
    pip_validate_certs: true
    python_apt_packages:
      - libssl-dev
      - libffi-dev
      - python-dev
  tasks:
    - name: "Copy global requirements"
      copy:
        src: "/opt/leap42/openstack-ansible-{{ release_version }}/global-requirement-pins.txt"
        dest: "/tmp/global-requirement-pins.txt"

    - name: "Copy upgrade requirements"
      copy:
        src: "{{ playbook_dir }}/../lib/upgrade-requirements.txt"
        dest: "/tmp/upgrade-requirements.txt"

    - name: Get Modern PIP
      get_url:
        url: "{{ pip_upstream_url }}"
        dest: "/opt/get-pip.py"
        force: "yes"
        validate_certs: "{{ pip_validate_certs }}"
      register: get_pip
      until: get_pip is success
      failed_when: false
      retries: 5
      delay: 2

    - name: Get Modern PIP using fallback URL
      get_url:
        url: "{{ pip_fallback_url }}"
        dest: "/opt/get-pip.py"
        force: "yes"
        validate_certs: "{{ pip_validate_certs }}"
      when:
        - get_pip  is failed
        - pip_fallback_url is defined
      register: get_pip_fallback
      until: get_pip_fallback is success
      retries: 5
      delay: 2

    - name: purge old pip utils
      raw: "rm -rf /usr/local/lib/python2.7/dist-packages/{{ item }}*"
      with_items:
        - setuptools
        - wheel
        - pip
        - distutils
        - packaging
      args:
        executable: /bin/bash

    - name: Re-install pip
      raw: |
        /usr/bin/python /opt/get-pip.py \
            --force-reinstall \
            --upgrade \
            --isolated \
            --constraint /tmp/global-requirement-pins.txt
      args:
        executable: /bin/bash

    - name: apt-get install libssl-dev
      apt:
        name: "{{ python_apt_packages }}"
        update_cache: yes

    - name: Re-install pip packaging requirements
      raw: |
        /usr/bin/python /opt/get-pip.py \
            --upgrade \
            --isolated \
            --requirement /tmp/upgrade-requirements.txt \
            --constraint /tmp/global-requirement-pins.txt
      args:
        executable: /bin/bash