From 9f07f2a951eb3bdcf258fee1cc8208d0e32c3cc6 Mon Sep 17 00:00:00 2001
From: Smaine Kahlouch <smaine.kahlouch@arkena.com>
Date: Wed, 30 Dec 2015 17:11:33 +0100
Subject: [PATCH] install docker on a largest number of linux distribution
 (based on https://github.com/marklee77/ansible-role-docker)

---
 roles/common/defaults/main.yml            | 20 +++++++++
 roles/common/tasks/calico.yml             | 27 ++++++++++++
 roles/common/tasks/flannel.yml            | 39 ++++++++++++++++
 roles/common/tasks/kubernetes.yml         | 17 +++++++
 roles/common/tasks/main.yml               | 12 +++++
 roles/docker/.gitignore                   |  2 +
 roles/docker/files/systemd-docker.service | 17 -------
 roles/docker/handlers/main.yml            | 12 -----
 roles/docker/tasks/configure.yml          | 16 -------
 roles/docker/tasks/install.yml            | 24 ----------
 roles/docker/tasks/main.yml               | 54 ++++++++++++++++++++++-
 roles/docker/templates/default-docker.j2  | 13 ------
 roles/docker/templates/docker.list.j2     |  1 -
 roles/docker/vars/centos-6.yml            | 24 ++++++++++
 roles/docker/vars/debian.yml              | 36 +++++++++++++++
 roles/docker/vars/fedora-20.yml           | 22 +++++++++
 roles/docker/vars/main.yml                |  4 --
 roles/docker/vars/redhat.yml              | 22 +++++++++
 18 files changed, 273 insertions(+), 89 deletions(-)
 create mode 100644 roles/common/defaults/main.yml
 create mode 100644 roles/common/tasks/calico.yml
 create mode 100644 roles/common/tasks/flannel.yml
 create mode 100644 roles/common/tasks/kubernetes.yml
 create mode 100644 roles/common/tasks/main.yml
 create mode 100644 roles/docker/.gitignore
 delete mode 100644 roles/docker/files/systemd-docker.service
 delete mode 100644 roles/docker/handlers/main.yml
 delete mode 100644 roles/docker/tasks/configure.yml
 delete mode 100644 roles/docker/tasks/install.yml
 delete mode 100644 roles/docker/templates/default-docker.j2
 delete mode 100644 roles/docker/templates/docker.list.j2
 create mode 100644 roles/docker/vars/centos-6.yml
 create mode 100644 roles/docker/vars/debian.yml
 create mode 100644 roles/docker/vars/fedora-20.yml
 delete mode 100644 roles/docker/vars/main.yml
 create mode 100644 roles/docker/vars/redhat.yml

diff --git a/roles/common/defaults/main.yml b/roles/common/defaults/main.yml
new file mode 100644
index 000000000..339c72ac0
--- /dev/null
+++ b/roles/common/defaults/main.yml
@@ -0,0 +1,20 @@
+---
+etcd_version: v2.2.2
+flannel_version: 0.5.5
+
+kube_version: v1.1.3
+kubectl_checksum: "01b9bea18061a27b1cf30e34fd8ab45cfc096c9a9d57d0ed21072abb40dd3d1d"
+kubelet_checksum: "62191c66f2d670dd52ddf1d88ef81048977abf1ffaa95ee6333299447eb6a482"
+
+calico_version: v0.13.0
+calico_plugin_version: v0.7.0
+
+etcd_download_url: "https://github.com/coreos/etcd/releases/download"
+flannel_download_url: "https://github.com/coreos/flannel/releases/download"
+kube_download_url: "https://storage.googleapis.com/kubernetes-release/release/{{ kube_version }}/bin/linux/amd64"
+calico_download_url: "https://github.com/Metaswitch/calico-docker/releases/download"
+calico_plugin_download_url: "https://github.com/projectcalico/calico-kubernetes/releases/download/{{calico_plugin_version}}/calico_kubernetes"
+
+ansible_python_interpreter: PATH="{{ ansible_user_dir }}"/bin:$PATH python
+pip_python_modules:
+  - httplib2
diff --git a/roles/common/tasks/calico.yml b/roles/common/tasks/calico.yml
new file mode 100644
index 000000000..9fa82cac1
--- /dev/null
+++ b/roles/common/tasks/calico.yml
@@ -0,0 +1,27 @@
+---
+- name: Create calico release directory
+  local_action: file
+     path={{ local_release_dir }}/calico/bin
+     recurse=yes
+     state=directory
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+- name: Check if calicoctl has been downloaded
+  local_action: stat
+     path={{ local_release_dir }}/calico/bin/calicoctl
+  register: c_tar
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+# issues with get_url module and redirects, to be tested again in the near future
+- name: Download calico
+  local_action: shell
+    curl -o {{ local_release_dir }}/calico/bin/calicoctl -Ls {{ calico_download_url }}/{{ calico_version }}/calicoctl
+  when: not c_tar.stat.exists
+  register: dl_calico
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+
+- name: Download calico-kubernetes-plugin
+  local_action: get_url
+    url="{{calico_plugin_download_url}}"
+    dest="{{ local_release_dir }}/calico/bin/calico"
diff --git a/roles/common/tasks/flannel.yml b/roles/common/tasks/flannel.yml
new file mode 100644
index 000000000..2de0ae547
--- /dev/null
+++ b/roles/common/tasks/flannel.yml
@@ -0,0 +1,39 @@
+---
+- name: Create flannel release directory
+  local_action: file
+     path={{ local_release_dir }}/flannel
+     recurse=yes
+     state=directory
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+- name: Check if flannel release archive has been downloaded
+  local_action: stat
+     path={{ local_release_dir }}/flannel/flannel-{{ flannel_version }}-linux-amd64.tar.gz
+  register: f_tar
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+# issues with get_url module and redirects, to be tested again in the near future
+- name: Download flannel
+  local_action: shell
+    curl -o {{ local_release_dir }}/flannel/flannel-{{ flannel_version }}-linux-amd64.tar.gz -Ls {{ flannel_download_url }}/v{{ flannel_version }}/flannel-{{ flannel_version }}-linux-amd64.tar.gz
+  when: not f_tar.stat.exists
+  register: dl_flannel
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+- name: Extract flannel archive
+  local_action: unarchive
+     src={{ local_release_dir }}/flannel/flannel-{{ flannel_version }}-linux-amd64.tar.gz
+     dest={{ local_release_dir }}/flannel copy=no
+  when: dl_flannel|changed
+  delegate_to: "{{ groups['kube-master'][0] }}"
+
+- name: Pick up only flannel binaries
+  local_action: copy
+     src={{ local_release_dir }}/flannel/flannel-{{ flannel_version }}/flanneld
+     dest={{ local_release_dir }}/flannel/bin
+  when: dl_flannel|changed
+
+- name: Delete unused flannel files
+  local_action: file
+     path={{ local_release_dir }}/flannel/flannel-{{ flannel_version }} state=absent
+  when: dl_flannel|changed
diff --git a/roles/common/tasks/kubernetes.yml b/roles/common/tasks/kubernetes.yml
new file mode 100644
index 000000000..0985a17d3
--- /dev/null
+++ b/roles/common/tasks/kubernetes.yml
@@ -0,0 +1,17 @@
+---
+- name: Create kubernetes binary directory
+  local_action: file
+     path="{{ local_release_dir }}/kubernetes/bin"
+     state=directory
+     recurse=yes
+
+- name: Download kubelet and kubectl
+  local_action: get_url
+    url="{{ kube_download_url }}/{{ item.name }}"
+    dest="{{ local_release_dir }}/kubernetes/bin"
+    sha256sum="{{ item.checksum }}"
+  with_items:
+    - name: kubelet
+      checksum: "{{ kubelet_checksum }}"
+    - name: kubectl
+      checksum: "{{ kubectl_checksum }}"
diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml
new file mode 100644
index 000000000..d9ca46086
--- /dev/null
+++ b/roles/common/tasks/main.yml
@@ -0,0 +1,12 @@
+---
+- include: download.yml
+  delegate_to: localhost
+  when: inventory_hostname == "{{ groups['kube-master'][0] }}"
+
+- include: pip-bootstrap.yml
+
+- name: Install httplib2 python module
+  pip:
+    executable: "{{ ansible_user_dir }}/bin/pip"
+    name: "{{ item }}"
+  with_items: pip_python_modules
diff --git a/roles/docker/.gitignore b/roles/docker/.gitignore
new file mode 100644
index 000000000..e11a4750e
--- /dev/null
+++ b/roles/docker/.gitignore
@@ -0,0 +1,2 @@
+.*.swp
+.vagrant
diff --git a/roles/docker/files/systemd-docker.service b/roles/docker/files/systemd-docker.service
deleted file mode 100644
index 25eb328d5..000000000
--- a/roles/docker/files/systemd-docker.service
+++ /dev/null
@@ -1,17 +0,0 @@
-[Unit]
-Description=Docker Application Container Engine
-Documentation=https://docs.docker.com
-After=network.target docker.socket
-Requires=docker.socket
-
-[Service]
-EnvironmentFile=-/etc/default/docker
-Type=notify
-ExecStart=/usr/bin/docker daemon -H fd:// $DOCKER_OPTS
-MountFlags=slave
-LimitNOFILE=1048576
-LimitNPROC=1048576
-LimitCORE=infinity
-
-[Install]
-WantedBy=multi-user.target
diff --git a/roles/docker/handlers/main.yml b/roles/docker/handlers/main.yml
deleted file mode 100644
index 2644b6dfd..000000000
--- a/roles/docker/handlers/main.yml
+++ /dev/null
@@ -1,12 +0,0 @@
----
-- name: restart docker
-  command: /bin/true
-  notify:
-    - reload systemd
-    - restart docker service
-
-- name: reload systemd
-  shell: systemctl daemon-reload
-
-- name: restart docker service
-  service: name=docker state=restarted
diff --git a/roles/docker/tasks/configure.yml b/roles/docker/tasks/configure.yml
deleted file mode 100644
index 1e337ce5d..000000000
--- a/roles/docker/tasks/configure.yml
+++ /dev/null
@@ -1,16 +0,0 @@
----
-- name: enable docker
-  service:
-    name: docker
-    enabled: yes
-    state: started
-  tags:
-    - docker
-
-#- name: login to arkena's docker registry
-#  shell : >
-#    docker login --username={{ dockerhub_user }}
-#    --password={{ dockerhub_pass }}
-#    --email={{ dockerhub_email }}
-
-- meta: flush_handlers
diff --git a/roles/docker/tasks/install.yml b/roles/docker/tasks/install.yml
deleted file mode 100644
index 473e132fb..000000000
--- a/roles/docker/tasks/install.yml
+++ /dev/null
@@ -1,24 +0,0 @@
----
-- name: Install prerequisites for https transport
-  apt: pkg={{ item }} state=present update_cache=yes
-  with_items:
-    - apt-transport-https
-    - ca-certificates
-
-- name: Configure docker apt repository
-  template: src=docker.list.j2 dest=/etc/apt/sources.list.d/docker.list backup=yes
-
-- name: Install docker-engine
-  apt: pkg={{ item }} state=present force=yes update_cache=yes
-  with_items:
-    - aufs-tools
-    - cgroupfs-mount
-    - docker-engine=1.9.1-0~{{ ansible_distribution_release }}
-
-- name: Copy default docker configuration
-  template: src=default-docker.j2 dest=/etc/default/docker backup=yes
-  notify: restart docker
-
-- name: Copy Docker systemd unit file
-  copy: src=systemd-docker.service dest=/lib/systemd/system/docker.service backup=yes
-  notify: restart docker
diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml
index fdb17cf51..4f95be0db 100644
--- a/roles/docker/tasks/main.yml
+++ b/roles/docker/tasks/main.yml
@@ -1,3 +1,53 @@
 ---
-- include: install.yml
-- include: configure.yml
+- name: gather os specific variables
+  include_vars: "{{ item }}"
+  with_first_found:
+    - files:
+      - "{{ ansible_distribution|lower }}-{{ ansible_distribution_version|lower|replace('/', '_') }}.yml"
+      - "{{ ansible_distribution|lower }}-{{ ansible_distribution_release }}.yml"
+      - "{{ ansible_distribution|lower }}-{{ ansible_distribution_major_version|lower|replace('/', '_') }}.yml"
+      - "{{ ansible_distribution|lower }}.yml"
+      - "{{ ansible_os_family|lower }}.yml"
+      - defaults.yml
+      paths:
+      - ../vars
+
+- name: check for minimum kernel version
+  fail:
+    msg: >
+          docker requires a minimum kernel version of
+          {{ docker_kernel_min_version }} on
+          {{ ansible_distribution }}-{{ ansible_distribution_version }}
+  when: ansible_kernel|version_compare(docker_kernel_min_version, "<")
+
+- name: ensure docker requirements packages are installed
+  action: "{{ docker_package_info.pkg_mgr }}"
+  args: docker_package_info.args
+  with_items: docker_package_info.pre_pkgs
+  when: docker_package_info.pre_pkgs|length > 0
+
+- name: ensure docker repository public key is installed
+  action: "{{ docker_repo_key_info.pkg_key }}"
+  args: docker_repo_key_info.args
+  with_items: docker_repo_key_info.repo_keys
+  when: docker_repo_key_info.repo_keys|length > 0
+
+- name: ensure docker repository is enabled
+  action: "{{ docker_repo_info.pkg_repo }}"
+  args: docker_repo_info.args
+  with_items: docker_repo_info.repos
+  when: docker_repo_info.repos|length > 0
+
+- name: ensure docker packages are installed
+  action: "{{ docker_package_info.pkg_mgr }}"
+  args: docker_package_info.args
+  with_items: docker_package_info.pkgs
+  when: docker_package_info.pkgs|length > 0
+
+- name: ensure docker service is started and enabled
+  service:
+    name: "{{ item }}"
+    enabled: yes
+    state: started
+  with_items:
+    - docker
diff --git a/roles/docker/templates/default-docker.j2 b/roles/docker/templates/default-docker.j2
deleted file mode 100644
index 3da7d3606..000000000
--- a/roles/docker/templates/default-docker.j2
+++ /dev/null
@@ -1,13 +0,0 @@
-# Docker Upstart and SysVinit configuration file
-
-# Customize location of Docker binary (especially for development testing).
-#DOCKER="/usr/local/bin/docker"
-
-# Use DOCKER_OPTS to modify the daemon startup options.
-#DOCKER_OPTS=""
-
-# If you need Docker to use an HTTP proxy, it can also be specified here.
-#export http_proxy="http://127.0.0.1:3128/"
-
-# This is also a handy place to tweak where Docker's temporary files go.
-#export TMPDIR="/mnt/bigdrive/docker-tmp"
diff --git a/roles/docker/templates/docker.list.j2 b/roles/docker/templates/docker.list.j2
deleted file mode 100644
index 2b2f4da63..000000000
--- a/roles/docker/templates/docker.list.j2
+++ /dev/null
@@ -1 +0,0 @@
-deb https://apt.dockerproject.org/repo {{ansible_distribution|lower}}-{{ ansible_distribution_release}} main
diff --git a/roles/docker/vars/centos-6.yml b/roles/docker/vars/centos-6.yml
new file mode 100644
index 000000000..e6937d595
--- /dev/null
+++ b/roles/docker/vars/centos-6.yml
@@ -0,0 +1,24 @@
+docker_kernel_min_version: '2.6.32-431'
+
+docker_package_info:
+  pkg_mgr: yum
+  args:
+      name: "{{ item }}"
+      state: latest
+      update_cache: yes
+  pre_pkgs:
+    - epel-release
+    - curl
+    - device-mapper-libs
+  pkgs:
+    - docker-io
+
+docker_repo_key_info:
+  pkg_key: ''
+  args: {}
+  repo_keys: []
+
+docker_repo_info:
+  pkg_repo: ''
+  args: {}
+  repos: []
diff --git a/roles/docker/vars/debian.yml b/roles/docker/vars/debian.yml
new file mode 100644
index 000000000..37cf3e021
--- /dev/null
+++ b/roles/docker/vars/debian.yml
@@ -0,0 +1,36 @@
+docker_kernel_min_version: '3.2'
+
+docker_package_info:
+  pkg_mgr: apt
+  args:
+    pkg: "{{ item }}"
+    update_cache: yes
+    cache_valid_time: 600
+    state: latest
+  pre_pkgs: 
+    - apt-transport-https
+    - curl
+    - software-properties-common
+  pkgs:
+    - docker-engine
+
+docker_repo_key_info:
+  pkg_key: apt_key
+  args:
+    id: "{{ item }}"
+    keyserver: hkp://p80.pool.sks-keyservers.net:80
+    state: present
+  repo_keys:
+    - 58118E89F3A912897C070ADBF76221572C52609D 
+
+docker_repo_info:
+  pkg_repo: apt_repository
+  args:
+    repo: "{{ item }}"
+    update_cache: yes
+    state: present
+  repos:
+    - >
+       deb https://apt.dockerproject.org/repo 
+       {{ ansible_distribution|lower }}-{{ ansible_distribution_release|lower }}
+       main
diff --git a/roles/docker/vars/fedora-20.yml b/roles/docker/vars/fedora-20.yml
new file mode 100644
index 000000000..94e51ce27
--- /dev/null
+++ b/roles/docker/vars/fedora-20.yml
@@ -0,0 +1,22 @@
+docker_kernel_min_version: '0'
+
+docker_package_info:
+  pkg_mgr: yum
+  args:
+      name: "{{ item }}"
+      state: latest
+      update_cache: yes
+  pre_pkgs: 
+    - curl
+  pkgs:
+    - docker-io
+
+docker_repo_key_info:
+  pkg_key: ''
+  args: {}
+  repo_keys: []
+
+docker_repo_info:
+  pkg_repo: ''
+  args: {}
+  repos: []
diff --git a/roles/docker/vars/main.yml b/roles/docker/vars/main.yml
deleted file mode 100644
index cc5ecdb5d..000000000
--- a/roles/docker/vars/main.yml
+++ /dev/null
@@ -1,4 +0,0 @@
----
-#dockerhub_user:
-#dockerhub_pass:
-#dockerhub_email:
diff --git a/roles/docker/vars/redhat.yml b/roles/docker/vars/redhat.yml
new file mode 100644
index 000000000..6c251ccec
--- /dev/null
+++ b/roles/docker/vars/redhat.yml
@@ -0,0 +1,22 @@
+docker_kernel_min_version: '0'
+
+docker_package_info:
+  pkg_mgr: yum
+  args:
+      name: "{{ item }}"
+      state: latest
+      update_cache: yes
+  pre_pkgs: 
+    - curl
+  pkgs:
+    - docker
+
+docker_repo_key_info:
+  pkg_key: ''
+  args: {}
+  repo_keys: []
+
+docker_repo_info:
+  pkg_repo: ''
+  args: {}
+  repos: []
-- 
GitLab