---

- name: add https support
  apt:
    name:
      - apt-transport-https
      - gnupg
      - cron
      - ca-certificates
    cache_valid_time: 3600
    update_cache: yes

- name: add docker gpg key to apt keyring
  apt_key:
    id: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
    url: https://download.docker.com/linux/debian/gpg

- apt_repository:
    repo: "deb https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"
    state: present
    update_cache: yes
  when: ansible_distribution == "Debian"

- name: install docker.io from Debian
  apt:
    name: docker.io
  when: ansible_architecture != 'x86_64'

- name: install docker-ce
  apt:
    name: docker-ce
  when: ansible_architecture == 'x86_64'

- name: add jenkins to the docker group
  user:
    name: "{{ jenkins_user }}"
    groups: docker
    append: yes

- name: cleanup old docker images
  cron:
    name: cleanup-docker-images
    disabled: false
    minute: 0
    hour: '*/3'
    user: "{{ jenkins_user }}"
    job: "test -x /home/{{ jenkins_user }}/osmo-ci/scripts/docker-cleanup.sh && /home/{{ jenkins_user }}/osmo-ci/scripts/docker-cleanup.sh >/dev/null"

- name: copy daemon.json to support ipv6
  copy:
    src: daemon.json
    dest: /etc/docker/daemon.json
  notify: restart docker

# daemon.json configures overlay2 storage driver, clean up vfs leftovers
- name: cleanup vfs storage dir
  shell: "docker info | grep -q 'Storage Driver: overlay2' || rm -rf /var/lib/docker/vfs"

# After docker is set up, add docuum to clean old docker images
- import_tasks: docuum.yml
