---

- name: Install python PIP
  apt:
    name: python-pip
    state: present
    update_cache: yes
    cache_valid_time: 3600

- name: get running containers
  shell: "docker ps -q 2> /dev/null"
  register: docker_ps
  changed_when: false
  failed_when: false

- name: get current storage driver
  shell: "docker info 2> /dev/null | grep 'Storage Driver' | cut -d ' ' -f 3"
  register: current_driver
  changed_when: false
  when: docker_ps is success

- name: get current docker dir
  shell: "docker info 2> /dev/null | grep 'Docker Root Dir' | cut -d ' ' -f 4"
  register: current_dir
  changed_when: false
  when: docker_ps is success

# If you encounter this failure, it is because you are trying to change
# either the docker storage driver, or the docker root lib
# but you still have running containers on the host. This would orphan
# those containers and generally be bad. You should first make sure to stop
# (and export if you'd like to re-import after this change) all containers
# and then you should be able to run this again.
- name: verify no running containers and not changing root dir or storage driver
  fail: msg="Containers are running yet {{ current_driver.stdout }} != {{ docker_storage_driver }} and {{ current_dir.stdout }} != /prestage/docker"
  when: docker_ps.rc == 0 and (docker_ps.stdout_lines and (current_driver.stdout != docker_storage_driver or current_dir.stdout != "/prestage/docker"))

- name: add docker gpg key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
    state: present

- name: remove old docker PPA
  apt_repository:
    repo: deb https://apt.dockerproject.org/repo ubuntu-trusty main
    state: absent

- name: add docker repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
    state: present

- name: install linux-image-extra for aufs support
  apt:
    name: linux-image-extra-{{ansible_kernel}}
    update_cache: yes
    cache_valid_time: 3600
  when: docker_storage_driver == 'aufs'
  tags: aufs

- name: install aufs-tools for aufs
  apt:
    name: aufs-tools
    update_cache: yes
    cache_valid_time: 600
  when: docker_storage_driver == 'aufs'

- name: remove ubuntu-packaged docker
  apt:
    name: docker.io
    state: absent

- name: remove old docker-engine apt package
  apt:
    name: "docker-engine=17.05.0~ce-0~ubuntu-trusty"
    state: absent

- name: install docker-engine apt package
  apt:
    name: "docker-ce=18.06.3~ce~3-0~ubuntu"
    state: present
    update_cache: yes
    cache_valid_time: 3600

- name: make docker user group
  group:
    name: docker
    state: present
    gid: 1102

- name: make docker user
  user:
    name: docker
    uid: 1102
    groups: docker,syslog
    state: present

- name: configure docker daemon
  template:
    src: docker
    dest: /etc/default/docker
  notify: restart docker
  when: ansible_distribution_version == '14.04'

- name: configure docker daemon
  template:
    src: daemon.json.j2
    dest: /etc/docker/daemon.json
    owner: root
    group: root
    mode: "0644"
  notify: reload docker
  when: ansible_distribution_version == '18.04'

- name: configure docker socket
  copy:
    src: docker.socket
    dest: /lib/systemd/system/docker.socket
    owner: root
    group: root
    mode: "0644"
  notify: reload docker.socket
  when: ansible_distribution_version == '18.04'

- name: Install docker-world-useable upstart task
  copy:
    src: zoox-docker-world-useable.conf
    dest: /etc/init/zoox-docker-world-useable.conf
    owner: root
    group: root
    mode: "0644"
  when: docker_world_useable and ansible_distribution_version == '14.04'

- name: Remove docker-world-useable upstart task
  file:
    path: /etc/init/zoox-docker-world-useable.conf
    state: absent
  when: not docker_world_useable and ansible_distribution_version == '14.04'

- name: start docker
  service:
    name: docker
    state: started
  become: yes

- name: remove python requests & chardet from dist-utils
  apt:
    name:
      - python-requests
      - python-chardet
    state: absent

- name: install pip modules
  pip:
    name: "{{item.name}}"
    version: "{{item.version}}"
    state: present
  with_items:
      # We need this for the ECR login.  ECR support wasn't added until 1.9.15,
      # which is much newer than what Ubuntu provides.
      - { name: awscli, version: 1.10.65 }
      - { name: docker-py, version: 1.10.6 }
      - { name: docker-compose, version: "{{ docker_compose_version }}" }

# Installing the pre-built binary of docker-compose as the one in pip is prone
# to breaking due to other dependencies changing underneath it
# This will overwrite the `docker-compose` command installed by pip above
# but we still need the compose pip *library* installed from above so
# Ansible can use it for its `docker_service` module
- name: install docker-compose
  get_url:
    url: "https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-Linux-x86_64"
    dest: /usr/local/bin/docker-compose
    checksum: "{{ docker_compose_checksum }}"
    mode: "0755"
    owner: root
    group: root
  tags: docker-compose

- name: allow managing docker without sudo
  user:
    name: "{{ansible_user_id}}"
    append: yes
    groups: docker
  register: add_group

# If ansible_user_id just got added to the `docker` group, Ansible needs to
# login again to see that new group. Otherwise, subsequent tasks/roles
# that rely on that group will fail.
# http://stackoverflow.com/questions/26677064/create-and-use-group-without-restart
- name: Force Ansible to re-login to refresh groups
  shell: sleep 1; pkill -u {{ ansible_ssh_user }} sshd
  async: 60
  poll: 5
  when: add_group.changed

# This provides a docker wrapper which allows for easy GPU use
# with docker by mounting the CUDA libs and nvidia devices
- name: Install dockergpu wrapper
  copy:
    src: dockergpu
    dest: /usr/bin/dockergpu
    mode: 0755
    owner: root
    group: root
  tags:
    - "dockergpu"

- name: Set docker.sock permissions
  file:
    path: /var/run/docker.sock
    owner: root
    group: docker
    mode: "{{ '0666' if docker_world_useable else '0660' }}"
