AETHER-2698 Docker role updates

- Add an ability to configure daemon option
- Add an option to install docker-compose
- Separate task for updating apt repository, and make it conditional

Change-Id: Iafff1635f9af07a9712124f82c2f3b27fb6e557c
diff --git a/README.md b/README.md
index f81cda1..4535fa3 100644
--- a/README.md
+++ b/README.md
@@ -9,8 +9,8 @@
 The version of Docker installed currently is 19.03. This version is supported
 by:
 
-- Rancher 2.5.x:
-  https://rancher.com/support-maintenance-terms/all-supported-versions/rancher-v2.5.2/
+- Rancher 2.5.8:
+  https://www.suse.com/suse-rancher/support-matrix/all-supported-versions/rancher-v2-5-8/
 - Kubernetes 1.14 and later:
   https://github.com/kubernetes/kubernetes/issues/82326#issuecomment-546464495
 
@@ -18,10 +18,9 @@
 https://download.docker.com/linux/ubuntu/gpg and is identical for
 Debian/Ubuntu.
 
-TODO:
+Docker daemon options can be configured through `docker_daemon_options` dictionary representing the json file `/etc/docker/daemon.json`.
 
-- Hooks to configure the docker daemon to use a proxy/passthrough cache
-
+It also can install docker-compose when `docker_install_compose` is set to true.
 
 ## Example Playbook
 
diff --git a/defaults/main.yml b/defaults/main.yml
index af54b70..663c6e8 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -4,4 +4,16 @@
 # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
 # SPDX-License-Identifier: Apache-2.0
 
+docker_add_package_repo: true
+
 docker_version: "19.03"
+
+docker_daemon_options: {}
+
+# docker compose options
+docker_install_compose: false
+docker_compose_version: "1.29.2"
+docker_compose_url: "https://github.com/docker/compose/releases/\
+  download/{{ docker_compose_version }}/docker-compose-Linux-x86_64"
+docker_compose_checksum: "sha256:f3f10cf3dbb8107e9ba2ea5f23c1d2159ff7321d16f0a23051d68d8e2547b323"
+docker_compose_path: /usr/local/bin/docker-compose
diff --git a/handlers/main.yml b/handlers/main.yml
index fa26d2f..819b140 100644
--- a/handlers/main.yml
+++ b/handlers/main.yml
@@ -3,3 +3,9 @@
 #
 # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
 # SPDX-License-Identifier: Apache-2.0
+
+- name: restart docker
+  service:
+    name: docker
+    state: restarted
+  ignore_errors: "{{ ansible_check_mode }}"
diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml
index b35d80b..b9fac05 100644
--- a/molecule/default/converge.yml
+++ b/molecule/default/converge.yml
@@ -6,6 +6,11 @@
 
 - name: Converge
   hosts: all
+  vars:
+    docker_daemon_options:
+      registry-mirrors:
+        - http://registry.aetherproject.net
+    docker_install_compose: true
   tasks:
     - name: "Include docker"
       include_role:
diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml
index 557092e..8e218ca 100644
--- a/molecule/default/verify.yml
+++ b/molecule/default/verify.yml
@@ -22,3 +22,33 @@
       assert:
         that:
           - "'Version:          19.03' in docker_version.stdout"
+
+    - name: Check docker daemon option exists
+      command:
+        cmd: "docker info -f {% raw %}'{{ index .RegistryConfig.Mirrors 0 }}'{% endraw %}"
+      register: docker_info
+      tags:
+        - skip_ansible_lint  # info gathering
+
+    - debug:
+        var: docker_info
+
+    - name: Assert that installed Docker version matches version string
+      assert:
+        that:
+          - "'http://registry.aetherproject.net' in docker_info.stdout"
+
+    - name: Check installed Docker compose version string
+      command:
+        cmd: "docker-compose --version"
+      register: docker_compose_version
+      tags:
+        - skip_ansible_lint  # info gathering
+
+    - debug:
+        var: docker_compose_version
+
+    - name: Assert that installed Docker compose version matches version string
+      assert:
+        that:
+          - "'docker-compose version 1.29.2' in docker_compose_version.stdout"
diff --git a/tasks/Debian-repo.yml b/tasks/Debian-repo.yml
new file mode 100644
index 0000000..adf5c74
--- /dev/null
+++ b/tasks/Debian-repo.yml
@@ -0,0 +1,29 @@
+---
+# docker tasks/Debian-repo.yml
+#
+# SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org>
+# SPDX-License-Identifier: Apache-2.0
+
+- name: Install apt related prereqs
+  apt:
+    name:
+      - "apt-transport-https"
+      - "ca-certificates"
+      - "gpg"
+      - "software-properties-common"
+    state: "present"
+    update_cache: true
+
+- name: Add Docker apt repo key
+  apt_key:
+    data: "{{ lookup('file','docker.gpg') }}"
+    state: "present"
+
+# https://docs.docker.com/engine/install/debian/#install-using-the-repository
+- name: Add Official Docker apt repo
+  apt_repository:
+    repo: >-
+      deb https://download.docker.com/linux/{{ ansible_lsb['id'] | lower }}
+      {{ ansible_lsb['codename'] }} stable
+    mode: 0644
+    update_cache: true
diff --git a/tasks/Debian.yml b/tasks/Debian.yml
index 8633b01..f64c0f4 100644
--- a/tasks/Debian.yml
+++ b/tasks/Debian.yml
@@ -4,30 +4,6 @@
 # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
 # SPDX-License-Identifier: Apache-2.0
 
-- name: Install apt related prereqs
-  apt:
-    name:
-      - "apt-transport-https"
-      - "ca-certificates"
-      - "gpg"
-      - "software-properties-common"
-    state: "present"
-    update_cache: true
-
-- name: Add Docker apt repo key
-  apt_key:
-    data: "{{ lookup('file','docker.gpg') }}"
-    state: "present"
-
-# https://docs.docker.com/engine/install/debian/#install-using-the-repository
-- name: Add Official Docker apt repo
-  apt_repository:
-    repo: >-
-      deb https://download.docker.com/linux/{{ ansible_lsb['id'] | lower }}
-      {{ ansible_lsb['codename'] }} stable
-    mode: 0644
-    update_cache: true
-
 - name: Install Docker daemon apt package
   apt:
     name: "{{ docker_package }}={{ docker_apt_epoch }}:{{ docker_version }}*"
diff --git a/tasks/main.yml b/tasks/main.yml
index 5364bd1..476af22 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -7,5 +7,32 @@
 - name: include OS-specific vars
   include_vars: "{{ ansible_os_family }}.yml"
 
+- name: include OS-specific package repo updates
+  include_tasks: "{{ ansible_os_family }}-repo.yml"
+  when: docker_add_package_repo | bool
+
 - name: include OS-specific tasks
   include_tasks: "{{ ansible_os_family }}.yml"
+
+- name: ensure /etc/docker/ directory exists
+  file:
+    path: /etc/docker
+    state: directory
+    mode: 0755
+  when: docker_daemon_options.keys() | length > 0
+
+- name: configure Docker daemon options
+  copy:
+    content: "{{ docker_daemon_options | to_nice_json }}"
+    dest: /etc/docker/daemon.json
+    mode: 0644
+  when: docker_daemon_options.keys() | length > 0
+  notify: restart docker
+
+- name: install docker compose (if configured).
+  get_url:
+    url: "{{ docker_compose_url }}"
+    checksum: "{{ docker_compose_checksum }}"
+    dest: "{{ docker_compose_path }}"
+    mode: 0755
+  when: docker_install_compose | bool