---

- hosts: localhost
  tasks:
    - name: CephFS Provisioner | Install pip packages
      pip:
        name: "{{ item.name }}"
        version: "{{ item.version }}"
        state: "{{ item.state }}"
        extra_args: "{{ pip_extra_args | default(omit) }}"
      with_items:
        - { state: "present", name: "docker", version: "3.4.1" }
        - { state: "present", name: "docker-compose", version: "1.21.2" }

    - name: CephFS Provisioner | Check Go version
      shell: |
        go version
      ignore_errors: yes
      register: go_version_result

    - name: CephFS Provisioner | Install Go 1.9
      shell: |
        add-apt-repository -y ppa:gophers/archive
        apt-get update
        apt-get install -y golang-1.9
        ln -fs /usr/lib/go-1.9/bin/* /usr/local/bin/
      when: 'go_version_result.rc != 0 or "go version go1.9" not in go_version_result.stdout'

    - name: CephFS Provisioner | Check if image exists
      shell: |
        docker image list | grep 'cephfs-provisioner'
      ignore_errors: yes
      register: check_image_result

    - block:
        - name: CephFS Provisioner | Clone repo
          git:
            repo: https://github.com/kubernetes-incubator/external-storage.git
            dest: "~/go/src/github.com/kubernetes-incubator/external-storage"
            version: 06fddbe2
            clone: yes
            update: yes

        - name: CephFS Provisioner | Build image
          shell: |
            cd ~/go/src/github.com/kubernetes-incubator/external-storage
            REGISTRY=quay.io/kubespray/ VERSION=06fddbe2 make ceph/cephfs

        - name: CephFS Provisioner | Push image
          docker_image:
            name: quay.io/kubespray/cephfs-provisioner:06fddbe2
            push: yes
          register: docker_image
          retries: 10
          until: docker_image is succeeded

      when: check_image_result.rc != 0