From 41f9315a49faf851e3175c6b661c2778442cb5eb Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Wed, 26 Sep 2018 22:13:32 -0500 Subject: [PATCH 1/8] Switch tests to use Molecule. --- .gitignore | 3 +- .travis.yml | 40 ++++++++++--------- defaults/main.yml | 6 +-- meta/main.yml | 31 +++++++------- molecule/default/molecule.yml | 27 +++++++++++++ .../test.yml => molecule/default/playbook.yml | 9 ++--- molecule/default/tests/test_default.py | 14 +++++++ molecule/default/yaml-lint.yml | 6 +++ tasks/docker-users.yml | 2 +- tasks/setup-Debian.yml | 4 +- tests/README.md | 11 ----- 11 files changed, 97 insertions(+), 56 deletions(-) create mode 100644 molecule/default/molecule.yml rename tests/test.yml => molecule/default/playbook.yml (56%) create mode 100644 molecule/default/tests/test_default.py create mode 100644 molecule/default/yaml-lint.yml delete mode 100644 tests/README.md diff --git a/.gitignore b/.gitignore index c9b2377..f56f5b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.retry -tests/test.sh +*/__pycache__ +*.pyc diff --git a/.travis.yml b/.travis.yml index 07a44c9..adca0ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,28 +1,32 @@ --- +language: python services: docker env: - - distro: centos7 - - distro: ubuntu1804 - - distro: ubuntu1604 - - distro: ubuntu1404 - - distro: debian9 - - distro: fedora27 + global: + - ROLE_NAME: docker + matrix: + - MOLECULE_DISTRO: centos7 + MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + - MOLECULE_DISTRO: ubuntu1804 + - MOLECULE_DISTRO: ubuntu1604 + - MOLECULE_DISTRO: ubuntu1404 + - MOLECULE_DISTRO: debian9 + - MOLECULE_DISTRO: fedora27 + +install: + # Install test dependencies. + - pip install molecule docker + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME + - cd geerlingguy.$ROLE_NAME script: - # Configure test script so we can run extra tests after playbook is run. - - export container_id=$(date +%s) - - export cleanup=false - - # Download test shim. - - wget -O ${PWD}/tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/ - - chmod +x ${PWD}/tests/test.sh - # Run tests. - - ${PWD}/tests/test.sh - - # Test whether Docker is running correctly (Dockerception!). - - docker exec --tty ${container_id} docker run hello-world + - molecule test notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/defaults/main.yml b/defaults/main.yml index c12e9e9..2bcb620 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -6,11 +6,11 @@ docker_package_state: present # Service options. docker_service_state: started -docker_service_enabled: yes +docker_service_enabled: true docker_restart_handler_state: restarted # Docker Compose options. -docker_install_compose: True +docker_install_compose: true docker_compose_version: "1.22.0" docker_compose_path: /usr/local/bin/docker-compose @@ -18,7 +18,7 @@ docker_compose_path: /usr/local/bin/docker-compose docker_apt_release_channel: stable docker_apt_arch: amd64 docker_apt_repository: "deb [arch={{ docker_apt_arch }}] https://download.docker.com/linux/{{ ansible_distribution|lower }} {{ ansible_distribution_release }} {{ docker_apt_release_channel }}" -docker_apt_ignore_key_error: True +docker_apt_ignore_key_error: true # Used only for RedHat/CentOS/Fedora. docker_yum_repo_url: https://download.docker.com/linux/{{ (ansible_distribution == "Fedora") | ternary("fedora","centos") }}/docker-{{ docker_edition }}.repo diff --git a/meta/main.yml b/meta/main.yml index 939d881..0dfa4cd 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -8,21 +8,22 @@ galaxy_info: license: "license (BSD, MIT)" min_ansible_version: 2.4 platforms: - - name: EL - versions: - - 6 - - 7 - - name: Fedora - versions: - - all - - name: Debian - versions: - - jessie - - name: Ubuntu - versions: - - trusty - - xenial - - bionic + - name: EL + versions: + - 6 + - 7 + - name: Fedora + versions: + - all + - name: Debian + versions: + - jessie + - stretch + - name: Ubuntu + versions: + - trusty + - xenial + - bionic galaxy_tags: - web - system diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..0339702 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,27 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint + options: + config-file: molecule/default/yaml-lint.yml +platforms: + - name: instance + image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible + command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"} + privileged: true + pre_build_image: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + converge: ${MOLECULE_PLAYBOOK:-playbook.yml} +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/tests/test.yml b/molecule/default/playbook.yml similarity index 56% rename from tests/test.yml rename to molecule/default/playbook.yml index cdbbb3f..dad331d 100644 --- a/tests/test.yml +++ b/molecule/default/playbook.yml @@ -1,13 +1,12 @@ --- -- hosts: all +- name: Converge + hosts: all + become: true pre_tasks: - name: Update apt cache. apt: update_cache=yes cache_valid_time=600 when: ansible_os_family == 'Debian' - - name: Install test dependencies. - package: name=curl state=present - roles: - - role_under_test + - role: geerlingguy.docker diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..eedd64a --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,14 @@ +import os + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_hosts_file(host): + f = host.file('/etc/hosts') + + assert f.exists + assert f.user == 'root' + assert f.group == 'root' diff --git a/molecule/default/yaml-lint.yml b/molecule/default/yaml-lint.yml new file mode 100644 index 0000000..7aeec5a --- /dev/null +++ b/molecule/default/yaml-lint.yml @@ -0,0 +1,6 @@ +--- +extends: default +rules: + line-length: + max: 200 + level: warning diff --git a/tasks/docker-users.yml b/tasks/docker-users.yml index afebd9a..b3b6e0f 100644 --- a/tasks/docker-users.yml +++ b/tasks/docker-users.yml @@ -3,5 +3,5 @@ user: name: "{{ item }}" groups: docker - append: yes + append: true with_items: "{{ docker_users }}" diff --git a/tasks/setup-Debian.yml b/tasks/setup-Debian.yml index 312de84..914797e 100644 --- a/tasks/setup-Debian.yml +++ b/tasks/setup-Debian.yml @@ -29,11 +29,11 @@ - name: Add Docker apt key (alternative for older systems without SNI). shell: "curl -sSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -" args: - warn: no + warn: false when: add_repository_key is failed - name: Add Docker repository. apt_repository: repo: "{{ docker_apt_repository }}" state: present - update_cache: yes + update_cache: true diff --git a/tests/README.md b/tests/README.md deleted file mode 100644 index 6fb2117..0000000 --- a/tests/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Ansible Role tests - -To run the test playbook(s) in this directory: - - 1. Install and start Docker. - 1. Download the test shim (see .travis.yml file for the URL) into `tests/test.sh`: - - `wget -O tests/test.sh https://gist.githubusercontent.com/geerlingguy/73ef1e5ee45d8694570f334be385e181/raw/` - 1. Make the test shim executable: `chmod +x tests/test.sh`. - 1. Run (from the role root directory) `distro=[distro] playbook=[playbook] ./tests/test.sh` - -If you don't want the container to be automatically deleted after the test playbook is run, add the following environment variables: `cleanup=false container_id=$(date +%s)` From 33df4f2078319e18be258e7460630e75162436c3 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Wed, 26 Sep 2018 22:26:19 -0500 Subject: [PATCH 2/8] Fix Ansible lint issues. --- .travis.yml | 2 ++ README.md | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index adca0ce..aa8ff9a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,8 +11,10 @@ env: - MOLECULE_DISTRO: ubuntu1804 - MOLECULE_DISTRO: ubuntu1604 - MOLECULE_DISTRO: ubuntu1404 + MOLECULE_DOCKER_COMMAND: /sbin/init - MOLECULE_DISTRO: debian9 - MOLECULE_DISTRO: fedora27 + MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd install: # Install test dependencies. diff --git a/README.md b/README.md index 8b0755f..9a49840 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,12 @@ The `docker_edition` should be either `ce` (Community Edition) or `ee` (Enterpri You can control whether the package is installed, uninstalled, or at the latest version by setting `docker_package_state` to `present`, `absent`, or `latest`, respectively. Note that the Docker daemon will be automatically restarted if the Docker package is updated. This is a side effect of flushing all handlers (running any of the handlers that have been notified by this and any other role up to this point in the play). docker_service_state: started - docker_service_enabled: yes + docker_service_enabled: true docker_restart_handler_state: restarted Variables to control the state of the `docker` service, and whether it should start on boot. If you're installing Docker inside a Docker container without systemd or sysvinit, you should set these to `stopped` and set the enabled variable to `no`. - docker_install_compose: True + docker_install_compose: true docker_compose_version: "1.22.0" docker_compose_path: /usr/local/bin/docker-compose From a42939be873fba2daa69fa96069c543300e251e1 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Wed, 26 Sep 2018 22:38:50 -0500 Subject: [PATCH 3/8] Fix issue with Fedora 27 test build. --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index aa8ff9a..f023be6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,8 +13,8 @@ env: - MOLECULE_DISTRO: ubuntu1404 MOLECULE_DOCKER_COMMAND: /sbin/init - MOLECULE_DISTRO: debian9 - - MOLECULE_DISTRO: fedora27 - MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + # - MOLECULE_DISTRO: fedora27 + # MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd install: # Install test dependencies. From d3afc01efcb60a548e342d0b41b799ce9bbfdd81 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Mon, 22 Oct 2018 23:32:38 -0500 Subject: [PATCH 4/8] Fix broken Fedora 27 test, clean up uninstall task. --- .travis.yml | 5 +---- molecule/default/molecule.yml | 6 ++++-- molecule/default/tests/test_default.py | 14 -------------- tasks/setup-Debian.yml | 7 +++---- tasks/setup-RedHat.yml | 9 ++++----- 5 files changed, 12 insertions(+), 29 deletions(-) delete mode 100644 molecule/default/tests/test_default.py diff --git a/.travis.yml b/.travis.yml index f023be6..a8851b7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,14 +7,11 @@ env: - ROLE_NAME: docker matrix: - MOLECULE_DISTRO: centos7 - MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd - MOLECULE_DISTRO: ubuntu1804 - MOLECULE_DISTRO: ubuntu1604 - MOLECULE_DISTRO: ubuntu1404 - MOLECULE_DOCKER_COMMAND: /sbin/init - MOLECULE_DISTRO: debian9 - # - MOLECULE_DISTRO: fedora27 - # MOLECULE_DOCKER_COMMAND: /usr/lib/systemd/systemd + - MOLECULE_DISTRO: fedora27 install: # Install test dependencies. diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 0339702..5322c4c 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -9,8 +9,10 @@ lint: config-file: molecule/default/yaml-lint.yml platforms: - name: instance - image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible - command: ${MOLECULE_DOCKER_COMMAND:-"sleep infinity"} + image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest + command: ${MOLECULE_DOCKER_COMMAND:-""} + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro privileged: true pre_build_image: true provisioner: diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py deleted file mode 100644 index eedd64a..0000000 --- a/molecule/default/tests/test_default.py +++ /dev/null @@ -1,14 +0,0 @@ -import os - -import testinfra.utils.ansible_runner - -testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( - os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') - - -def test_hosts_file(host): - f = host.file('/etc/hosts') - - assert f.exists - assert f.user == 'root' - assert f.group == 'root' diff --git a/tasks/setup-Debian.yml b/tasks/setup-Debian.yml index 914797e..23a49ae 100644 --- a/tasks/setup-Debian.yml +++ b/tasks/setup-Debian.yml @@ -1,11 +1,10 @@ --- - name: Ensure old versions of Docker are not installed. package: - name: '{{ item }}' + name: + - docker + - docker-engine state: absent - with_items: - - docker - - docker-engine - name: Ensure dependencies are installed. apt: diff --git a/tasks/setup-RedHat.yml b/tasks/setup-RedHat.yml index 1d44e7b..0cd1a50 100644 --- a/tasks/setup-RedHat.yml +++ b/tasks/setup-RedHat.yml @@ -1,12 +1,11 @@ --- - name: Ensure old versions of Docker are not installed. package: - name: '{{ item }}' + name: + - docker + - docker-common + - docker-engine state: absent - with_items: - - docker - - docker-common - - docker-engine - name: Add Docker GPG key. rpm_key: From 58e2dc8ab3e7e2854781978d9d771db676899ac9 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Tue, 23 Oct 2018 10:15:28 -0500 Subject: [PATCH 5/8] Update tests for optimum efficiency. --- molecule/default/molecule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 5322c4c..a4da6e9 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -9,7 +9,7 @@ lint: config-file: molecule/default/yaml-lint.yml platforms: - name: instance - image: geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest":latest command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro From 4a71ec75cb8c6ce21256f14e96e5d3d1e033d761 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Tue, 23 Oct 2018 10:49:04 -0500 Subject: [PATCH 6/8] Remove redundant latest tag. --- molecule/default/molecule.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index a4da6e9..2ca6fea 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -9,7 +9,7 @@ lint: config-file: molecule/default/yaml-lint.yml platforms: - name: instance - image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest":latest + image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro From d8a4438b3c99ea4c8401ad2c06bfb2f6ea7305cf Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Mon, 12 Nov 2018 21:14:20 -0600 Subject: [PATCH 7/8] Fixes #97, Triggers #98, Makes me sad: Fix Docker 18.09.0 startup issue on systemd systems. --- tasks/docker-1809-shim.yml | 10 ++++++++++ tasks/main.yml | 4 ++++ templates/override.conf.j2 | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 tasks/docker-1809-shim.yml create mode 100644 templates/override.conf.j2 diff --git a/tasks/docker-1809-shim.yml b/tasks/docker-1809-shim.yml new file mode 100644 index 0000000..f59f85a --- /dev/null +++ b/tasks/docker-1809-shim.yml @@ -0,0 +1,10 @@ +--- +- name: Ensure containerd service dir exists. + file: + path: /etc/systemd/system/containerd.service.d + state: directory + +- name: Add shim to ensure Docker can start in all environments. + template: + src: override.conf.j2 + dest: /etc/systemd/system/containerd.service.d/override.conf diff --git a/tasks/main.yml b/tasks/main.yml index 8444ac7..f248279 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -11,6 +11,10 @@ state: "{{ docker_package_state }}" notify: restart docker +# TODO: Remove this shim once 18.09.1 or later is released. +- import_tasks: docker-1809-shim.yml + when: ansible_service_mgr == 'systemd' + - name: Ensure Docker is started and enabled at boot. service: name: docker diff --git a/templates/override.conf.j2 b/templates/override.conf.j2 new file mode 100644 index 0000000..76864e2 --- /dev/null +++ b/templates/override.conf.j2 @@ -0,0 +1,2 @@ +[Service] +ExecStartPre= From fa1a56824eb6caf18872a01095094aa2702a5fc0 Mon Sep 17 00:00:00 2001 From: Jeff Geerling Date: Mon, 12 Nov 2018 21:28:36 -0600 Subject: [PATCH 8/8] Fixes #97, Mentions #98, Still makes me sade: Fix Docker 18.09.0 startup issue. --- tasks/docker-1809-shim.yml | 6 ++++++ templates/override.conf.j2 | 1 + 2 files changed, 7 insertions(+) diff --git a/tasks/docker-1809-shim.yml b/tasks/docker-1809-shim.yml index f59f85a..286254b 100644 --- a/tasks/docker-1809-shim.yml +++ b/tasks/docker-1809-shim.yml @@ -8,3 +8,9 @@ template: src: override.conf.j2 dest: /etc/systemd/system/containerd.service.d/override.conf + register: override_template + +- name: Reload systemd daemon if template is changed. + systemd: + daemon_reload: true + when: override_template is changed diff --git a/templates/override.conf.j2 b/templates/override.conf.j2 index 76864e2..adab53c 100644 --- a/templates/override.conf.j2 +++ b/templates/override.conf.j2 @@ -1,2 +1,3 @@ +# {{ ansible_managed }} [Service] ExecStartPre=