From 948731535ec28bf11631e4409296f7e82c0eb2e4 Mon Sep 17 00:00:00 2001 From: Ernestas Poskus Date: Mon, 12 Sep 2016 17:32:47 +0300 Subject: [PATCH] Features/introduce multi platform travis (#45) * Remove dockefiles directory * Remove ci directory * Check for ansible_service_mgr when installing services * Remove recurse directive when creating directories * Add new .travis and tests * Install systemd unit file under /lib/systemd/system * Reload systemctl daemon when systemd --- .travis.yml | 83 +++++++++++++++++++++++++++-------- ci/hosts_hash.yml | 23 ---------- ci/hosts_list.yml | 22 ---------- ci/inventory | 2 - ci/playbook.yml | 13 ------ ci/tests.yml | 13 ------ dockerfiles/centos/Dockerfile | 37 ---------------- dockerfiles/ubuntu/Dockerfile | 37 ---------------- handlers/main.yml | 4 ++ tasks/systemd.yml | 9 ++-- tasks/tarball.yml | 13 +++--- tasks/upstart.yml | 4 +- tests/dependencies.yml | 6 +++ tests/playbook.yml | 6 +++ tests/test.yml | 57 ++++++++++++++++++++++++ 15 files changed, 150 insertions(+), 179 deletions(-) delete mode 100644 ci/hosts_hash.yml delete mode 100644 ci/hosts_list.yml delete mode 100644 ci/inventory delete mode 100644 ci/playbook.yml delete mode 100644 ci/tests.yml delete mode 100644 dockerfiles/centos/Dockerfile delete mode 100644 dockerfiles/ubuntu/Dockerfile create mode 100644 tests/dependencies.yml create mode 100644 tests/playbook.yml create mode 100644 tests/test.yml diff --git a/.travis.yml b/.travis.yml index 4bbaeb5..70dd4fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,29 +1,76 @@ ---- -language: python -python: "2.7" +sudo: required -before_install: - - sudo apt-get update -qq - - sudo apt-get install curl -y +env: + global: + # https://github.com/travis-ci/travis-ci/issues/6461#issuecomment-239577306 + DOCKER_VERSION: "1.9.1-0~trusty" + matrix: + - distro: ernestasposkus/centos7 + init: /usr/lib/systemd/systemd + run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" + - distro: ernestasposkus/centos6 + init: /sbin/init + run_opts: "" + - distro: ernestasposkus/ubuntu1604 + init: /lib/systemd/systemd + run_opts: "--privileged --volume=/sys/fs/cgroup:/sys/fs/cgroup:ro" + - distro: ernestasposkus/ubuntu1404 + init: /sbin/init + run_opts: "" + +services: + - docker -install: - - pip install -U ansible +before_install: + # Downgrade to specific version of Docker engine. + - sudo apt-get update + - sudo apt-get remove docker-engine -yq + - sudo apt-get install docker-engine=$DOCKER_VERSION -yq --no-install-suggests --no-install-recommends --force-yes -o Dpkg::Options::="--force-confnew" - # Add ansible.cfg to pick up roles path. - - "{ echo '[defaults]'; echo 'roles_path = ../'; } >> ansible.cfg" + # Pull container. + - 'sudo docker pull ${distro}:latest' script: - # Check the role/playbook's syntax. - - "ansible-playbook -i ci/inventory ci/playbook.yml --syntax-check" + - container_id=$(mktemp) + # Run container in detached state. + - 'sudo docker run --detach --volume="${PWD}":/etc/ansible/roles/role_under_test:ro ${run_opts} ${distro}:latest "${init}" > "${container_id}"' + + # Inspect docker container + - 'sudo docker inspect $(cat ${container_id})' + + # Print ansible version + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible --version' + + # Check Ansible host setup + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible all -i "localhost," -c local -m setup' + + # Install dependencies + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-galaxy install geerlingguy.java' + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/dependencies.yml' + + # Ansible syntax check. + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/playbook.yml --syntax-check' + + # Test role. + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/playbook.yml' + + # Test role idempotence. + - idempotence=$(mktemp) + - sudo docker exec "$(cat ${container_id})" ansible-playbook /etc/ansible/roles/role_under_test/tests/playbook.yml | tee -a ${idempotence} + - > + tail ${idempotence} + | grep -q 'changed=0.*failed=0' + && (echo 'Idempotence test: pass' && exit 0) + || (echo 'Idempotence test: fail' && exit 1) - # Run the ansible ci playbook - - "ansible-playbook -i ci/inventory ci/playbook.yml --connection=local --sudo -vvvv" + # Test role. + - 'sudo docker exec --tty "$(cat ${container_id})" env TERM=xterm ansible-playbook /etc/ansible/roles/role_under_test/tests/test.yml' - # Run the ansible hosts list - - "ansible-playbook -i ci/inventory ci/hosts_list.yml --connection=local --sudo -vvvv" + # View container logs + - 'sudo docker logs "$(cat ${container_id})"' - # Run the ansible hosts hash - - "ansible-playbook -i ci/inventory ci/hosts_hash.yml --connection=local --sudo -vvvv" + # Clean up. + - 'sudo docker stop "$(cat ${container_id})"' notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/ci/hosts_hash.yml b/ci/hosts_hash.yml deleted file mode 100644 index a16d25b..0000000 --- a/ci/hosts_hash.yml +++ /dev/null @@ -1,23 +0,0 @@ ---- -# -# Host hash playbook -# - -- hosts: localhost - connection: local - sudo: yes - roles: - - role: ../../ - zookeeper_hosts: - - host: "{{inventory_hostname}}" # the machine running - id: 2 - -- hosts: localhost - connection: local - sudo: yes - gather_facts: false - tasks: - # Expecting myid to be 2 as defined in zookeeper_hosts variable - - shell: "grep 2 /var/lib/zookeeper/myid" - register: status - failed_when: status.rc != 0 diff --git a/ci/hosts_list.yml b/ci/hosts_list.yml deleted file mode 100644 index 7c2b84e..0000000 --- a/ci/hosts_list.yml +++ /dev/null @@ -1,22 +0,0 @@ ---- -# -# Host list playbook -# - -- hosts: localhost - connection: local - sudo: yes - roles: - - role: ../../ - zookeeper_hosts: - - "{{inventory_hostname}}" - -- hosts: localhost - connection: local - sudo: yes - gather_facts: false - tasks: - # Expecting myid to be 1 defined by loop.index - - shell: "grep 1 /var/lib/zookeeper/myid" - register: status - failed_when: status.rc != 0 diff --git a/ci/inventory b/ci/inventory deleted file mode 100644 index 5f86e9c..0000000 --- a/ci/inventory +++ /dev/null @@ -1,2 +0,0 @@ -[local] -localhost \ No newline at end of file diff --git a/ci/playbook.yml b/ci/playbook.yml deleted file mode 100644 index f227517..0000000 --- a/ci/playbook.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- -# -# Test Playbook -# - -- hosts: localhost - connection: local - sudo: yes - - roles: - - {role: ../../} - -- include: tests.yml diff --git a/ci/tests.yml b/ci/tests.yml deleted file mode 100644 index 1554d04..0000000 --- a/ci/tests.yml +++ /dev/null @@ -1,13 +0,0 @@ ---- - -- hosts: localhost - connection: local - sudo: yes - gather_facts: false - - tasks: - - wait_for: port=2181 state=started delay=1 - - - shell: echo stat | nc 127.0.0.1 2181 - register: status - failed_when: status.rc != 0 diff --git a/dockerfiles/centos/Dockerfile b/dockerfiles/centos/Dockerfile deleted file mode 100644 index 12638df..0000000 --- a/dockerfiles/centos/Dockerfile +++ /dev/null @@ -1,37 +0,0 @@ -# ------------------------------------------------------ -# Dockerfile -# ------------------------------------------------------ -# image: ansible-zookeeper-centos -# tag: latest -# name: ansibleshipyard/ansible-zookeeper-centos -# version: 1.0.0 -# repo: https://github.com/ansibleshipyard/ansible-zookeeper -# how-to: docker build --force-rm -t ansibleshipyard/ansible-zookeeper-centos . -# requires: centos -# authors: jason.giedymin@gmail.com -# desc: Zookeeper Docker Deployed by Ansible -# Created via Ansible CI/Docker Playbook Generator -# ------------------------------------------------------ - -FROM ansibleshipyard/ansible-base:centos -MAINTAINER ansibleshipyard - -# -----> Env -ENV WORKDIR /tmp/build/roles/ansible-zookeeper -WORKDIR /tmp/build/roles/ansible-zookeeper - -# -----> Add assets -ADD ./defaults $WORKDIR/defaults -ADD ./meta $WORKDIR/meta -ADD ./tasks $WORKDIR/tasks -ADD ./templates $WORKDIR/templates -ADD ./ci $WORKDIR/ci - -# -----> Install Galaxy Dependencies - -# -----> Execute -RUN ansible-playbook -i $WORKDIR/ci/inventory $WORKDIR/ci/playbook.yml -c local -vvvv - -# -----> Cleanup -WORKDIR / -RUN rm -R /tmp/build diff --git a/dockerfiles/ubuntu/Dockerfile b/dockerfiles/ubuntu/Dockerfile deleted file mode 100644 index 9aa1bc2..0000000 --- a/dockerfiles/ubuntu/Dockerfile +++ /dev/null @@ -1,37 +0,0 @@ -# ------------------------------------------------------ -# Dockerfile -# ------------------------------------------------------ -# image: ansible-zookeeper -# tag: latest -# name: ansibleshipyard/ansible-zookeeper -# version: 1.0.0 -# repo: https://github.com/ansibleshipyard/ansible-zookeeper -# how-to: docker build --force-rm -t ansibleshipyard/ansible-zookeeper . -# requires: ansibleshipyard/ansible-base:ubuntu -# authors: jason.giedymin@gmail.com -# desc: Zookeeper Docker Deployed by Ansible -# Created via Ansible CI/Docker Playbook Generator -# ------------------------------------------------------ - -FROM ansibleshipyard/ansible-base:ubuntu -MAINTAINER ansibleshipyard - -# -----> Env -ENV WORKDIR /tmp/build/roles/ansible-zookeeper -WORKDIR /tmp/build/roles/ansible-zookeeper - -# -----> Add assets -ADD ./defaults $WORKDIR/defaults -ADD ./meta $WORKDIR/meta -ADD ./tasks $WORKDIR/tasks -ADD ./templates $WORKDIR/templates -ADD ./ci $WORKDIR/ci - -# -----> Install Galaxy Dependencies - -# -----> Execute -RUN ansible-playbook -i $WORKDIR/ci/inventory $WORKDIR/ci/playbook.yml -c local -vvvv - -# -----> Cleanup -WORKDIR / -RUN rm -R /tmp/build diff --git a/handlers/main.yml b/handlers/main.yml index 3818cc1..44d2582 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,3 +1,7 @@ --- - name: Restart zookeeper service: name=zookeeper state=restarted + +- name: Reload systemctl daemon + command: systemctl daemon-reload + when: ansible_service_mgr == 'systemd' diff --git a/tasks/systemd.yml b/tasks/systemd.yml index 4adef10..815d108 100644 --- a/tasks/systemd.yml +++ b/tasks/systemd.yml @@ -1,10 +1,7 @@ --- -- name: Check if systemd exists - stat: path=/usr/lib/systemd/system/ - register: systemd_check - - name: Systemd script. - template: src=zookeeper.service.j2 dest=/usr/lib/systemd/system/zookeeper.service - when: systemd_check.stat.exists == true + template: src=zookeeper.service.j2 dest=/lib/systemd/system/zookeeper.service + when: ansible_service_mgr == 'systemd' notify: + - Reload systemctl daemon - Restart zookeeper diff --git a/tasks/tarball.yml b/tasks/tarball.yml index f3dd27d..1648724 100644 --- a/tasks/tarball.yml +++ b/tasks/tarball.yml @@ -17,16 +17,15 @@ - user: name=zookeeper group=zookeeper system=yes - name: Change ownership on zookeeper directory. - file: path={{zookeeper_dir}} state=directory owner=zookeeper group=zookeeper recurse=yes + file: path={{zookeeper_dir}} state=directory owner=zookeeper group=zookeeper tags: bootstrap -- name: Create zookeeper data folder. - file: path={{data_dir}} state=directory recurse=yes owner=zookeeper group=zookeeper - tags: bootstrap - -- name: Create zookeeper logs folder. - file: path={{log_dir}} state=directory recurse=yes owner=zookeeper group=zookeeper +- name: "Create zookeeper {{item}} directory." + file: path={{item}} state=directory owner=zookeeper group=zookeeper tags: bootstrap + with_items: + - "{{data_dir}}" + - "{{log_dir}}" - name: Write myid file. template: src=myid.j2 dest={{data_dir}}/myid owner=zookeeper group=zookeeper diff --git a/tasks/upstart.yml b/tasks/upstart.yml index f20f982..5043083 100644 --- a/tasks/upstart.yml +++ b/tasks/upstart.yml @@ -5,6 +5,8 @@ - name: Upstart script. template: src=zookeeper.conf.j2 dest=/etc/init/zookeeper.conf - when: etc_init.stat.exists == true + when: + - etc_init.stat.exists == true + - ansible_service_mgr != 'systemd' notify: - Restart zookeeper diff --git a/tests/dependencies.yml b/tests/dependencies.yml new file mode 100644 index 0000000..6d8c867 --- /dev/null +++ b/tests/dependencies.yml @@ -0,0 +1,6 @@ +--- +- hosts: localhost + connection: local + remote_user: root + roles: + - role: geerlingguy.java diff --git a/tests/playbook.yml b/tests/playbook.yml new file mode 100644 index 0000000..dfbbea9 --- /dev/null +++ b/tests/playbook.yml @@ -0,0 +1,6 @@ +--- +- hosts: localhost + connection: local + remote_user: root + roles: + - role: role_under_test diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..154db26 --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,57 @@ +--- +- hosts: localhost + connection: local + remote_user: root + tasks: + - shell: "ps aux | grep -i zookeeper" + register: status + failed_when: status.rc != 0 + when: ansible_service_mgr != 'systemd' + + - shell: "systemctl status zookeeper | grep running" + register: status + failed_when: status.rc != 0 + when: ansible_service_mgr == 'systemd' + +# +# Host hash playbook +# + +- hosts: localhost + connection: local + remote_user: root + roles: + - role: role_under_test + zookeeper_hosts: + - host: "{{inventory_hostname}}" # the machine running + id: 2 + +- hosts: localhost + connection: local + remote_user: root + tasks: + # Expecting myid to be 2 as defined in zookeeper_hosts variable + - shell: "grep 2 /var/lib/zookeeper/myid" + register: status + failed_when: status.rc != 0 + +# +# Host list playbook +# + +- hosts: localhost + connection: local + remote_user: root + roles: + - role: role_under_test + zookeeper_hosts: + - "{{inventory_hostname}}" + +- hosts: localhost + connection: local + remote_user: root + tasks: + # Expecting myid to be 1 defined by loop.index + - shell: "grep 1 /var/lib/zookeeper/myid" + register: status + failed_when: status.rc != 0