From 600dfe561a3e36d449f4f23639bbd8927eea8da0 Mon Sep 17 00:00:00 2001 From: Sean Johnson Date: Sat, 20 May 2017 23:17:46 +1000 Subject: [PATCH] Docker Based Tests Issue #6 This commit changes the Travic CI pipeline to run ansible in a docker container and additionally tests the deployment tasks. Updated README.md to contain new test script --- .gitlab-ci.yml | 7 ----- .travis.yml | 15 ++++++----- README.md | 31 +++++++++++++--------- environments/host_vars/testserver.yml | 7 +++++ environments/inventory | 3 +++ roles/pythondev-site/handlers/main.yml | 2 +- roles/pythondev-site/tasks/certificate.yml | 2 +- roles/setup-server/tasks/common.yml | 1 - roles/sirbot/handlers/main.yml | 3 +++ roles/sirbot/tasks/certificate.yml | 4 +-- roles/sirbot/tasks/main.yml | 1 + run_tests.sh | 11 ++++++++ test.yml | 4 +-- 13 files changed, 57 insertions(+), 34 deletions(-) delete mode 100644 .gitlab-ci.yml create mode 100755 run_tests.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index 133c8e6..0000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,7 +0,0 @@ -image: geerlingguy/docker-ubuntu1604-ansible - -test: - script: - - apt update - - echo "${ANSIBLE_VAULT_PASSWORD}" > .pass - - ansible-playbook -i environments/inventory test.yml -vvv --vault-password-file=.pass diff --git a/.travis.yml b/.travis.yml index f7e6ee1..310b9c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,17 @@ +sudo: required + language: python python: - "3.5" +services: + - docker + before_install: - - sudo apt-get update -qq + - echo "${ANSIBLE_VAULT_PASSWORD}" > .pass + - docker pull geerlingguy/docker-ubuntu1604-ansible -install: - - pip install ansible script: - - ansible-playbook -i environments/test_inventory prepare_ubuntu16.04.yml - - ansible-playbook -i environments/test_inventory test.yml --syntax-check - - "ansible-playbook -i environments/test_inventory test.yml -vvv --sudo" - - "ansible-playbook -i environments/test_inventory test.yml --sudo | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1)" + - docker run -h testserver -t -i -v $PWD:/data geerlingguy/docker-ubuntu1604-ansible /data/run_tests.sh diff --git a/README.md b/README.md index 78a168e..d29556f 100644 --- a/README.md +++ b/README.md @@ -14,11 +14,11 @@ Interested in becoming a member? Get your invite here: http://pythondevelopers.h Install Python 2 on the server using the raw command $ ansible -i -m raw -a "apt-get install -y python-simplejson" - + **Example:** - + $ ansible sirbot -i -m raw -a "apt-get install -y python-simplejson" - + -OR- Run the `prepare_ubuntu16.04` playbook for the server @@ -28,7 +28,7 @@ Run the `prepare_ubuntu16.04` playbook for the server ### Provisioning > The following is for securing your server. If you are deploying the app on a new server. - + $ ansible-playbook -i provisioning.yml ### Run the deployment for the specific app you want to deploy @@ -42,6 +42,15 @@ Run the deployment. $ ansible-playbook -i deploy.yml --tags +## Testing + +Tests are run via Travis CI's `.travis.yml`. Unit tests can be manually via docker with: + +`docker run -t -i -v ${PWD}:/data geerlingguy/docker-ubuntu1604-ansible /data/run_tests.sh`` + +Ensure that you have placed the Ansible vault password into `.pass` beforehand. + + ## Configuration ### Provisioning @@ -49,7 +58,7 @@ Run the deployment. #### Required variables * `ssh_keys`: Path to your ssh authorized_keys file - + ### Sirbot To run this playbook the host must be in the `sirbot` group. @@ -77,22 +86,18 @@ To run this playbook the host must be in the `sirbot` group. 2. Install the `make-deb` python module $ pip install make-deb - + 3. Configure the package $ make-deb - + 4. Build the package > Make sure the package is being build with minimum a python3.5 executable - $ export VIRTUALENV_PYTHON=/usr/bin/python3.5 + $ export VIRTUALENV_PYTHON=/usr/bin/python3.5 $ dpkg-buildpackage -us -uc - + 5. (Optional) Install the package $ dpkg -i sirbot__amd64.deb - - - - diff --git a/environments/host_vars/testserver.yml b/environments/host_vars/testserver.yml index 14daa75..8f949aa 100644 --- a/environments/host_vars/testserver.yml +++ b/environments/host_vars/testserver.yml @@ -3,3 +3,10 @@ letsencrypt_run: false systemd_enabled: false ansible_connection: local travis_ci: true +email: pythondev.slack@gmail.com +sirbot_domain: sirbot.pyslackers.com +pythondev_domain: pyslackers.com +SIRBOT_SLACK_TOKEN: "" +SIRBOT_SLACK_BOT_TOKEN: "" +SIRBOT_SLACK_VERIFICATION_TOKEN: "" +SIRBOT_GITHUB_SECRET: "" diff --git a/environments/inventory b/environments/inventory index 9f95d2d..9ae9f99 100644 --- a/environments/inventory +++ b/environments/inventory @@ -1,2 +1,5 @@ [sirbot] pythondev-01 + +[test] +testserver ansible_connection=local diff --git a/roles/pythondev-site/handlers/main.yml b/roles/pythondev-site/handlers/main.yml index dda0fa1..ce1fe9b 100644 --- a/roles/pythondev-site/handlers/main.yml +++ b/roles/pythondev-site/handlers/main.yml @@ -8,7 +8,7 @@ - Restart pythondev when: systemd_enabled == true -- name: Restart pythondev +- name: Restart pythondev-site service: name: pythondev.service state: restarted diff --git a/roles/pythondev-site/tasks/certificate.yml b/roles/pythondev-site/tasks/certificate.yml index f9de961..59a7db7 100644 --- a/roles/pythondev-site/tasks/certificate.yml +++ b/roles/pythondev-site/tasks/certificate.yml @@ -13,7 +13,7 @@ - name: Generate certificate command: "/usr/bin/certbot certonly --standalone -d {{ pythondev_domain }} -m {{ email }} -n --agree-tos {{letsencrypt_args}}" - when: pythondev_certificate.stat.isdir is not defined and letsencrypt_enabled == true + when: pythondev_certificate.stat.isdir is not defined and letsencrypt_run == true - name: Start nginx service: diff --git a/roles/setup-server/tasks/common.yml b/roles/setup-server/tasks/common.yml index 8a8874a..3f35a04 100644 --- a/roles/setup-server/tasks/common.yml +++ b/roles/setup-server/tasks/common.yml @@ -7,7 +7,6 @@ - name: Add certbot PPA apt_repository: repo: 'ppa:certbot/certbot' - when: letsencrypt_run # May change due to https://github.com/ansible/ansible-modules-core/issues/3523 - name: Install aptitude diff --git a/roles/sirbot/handlers/main.yml b/roles/sirbot/handlers/main.yml index c2e176a..0b376b1 100644 --- a/roles/sirbot/handlers/main.yml +++ b/roles/sirbot/handlers/main.yml @@ -6,13 +6,16 @@ daemon_reload: yes notify: - Restart sirbot + when: systemd_enabled == true - name: Restart sirbot service: name: sirbot.service state: restarted + when: systemd_enabled == true - name: Restart nginx service: name: nginx.service state: restarted + when: systemd_enabled == true diff --git a/roles/sirbot/tasks/certificate.yml b/roles/sirbot/tasks/certificate.yml index 86a8baf..2ca900c 100644 --- a/roles/sirbot/tasks/certificate.yml +++ b/roles/sirbot/tasks/certificate.yml @@ -9,7 +9,7 @@ service: name: nginx.service state: stopped - when: sirbot_certificate.stat.isdir is not defined + when: sirbot_certificate.stat.isdir is not defined and systemd_enabled == true - name: Generate certificate command: "/usr/bin/certbot certonly --standalone -d {{ sirbot_domain }} -m {{ email }} -n --agree-tos" @@ -19,4 +19,4 @@ service: name: nginx.service state: started - when: sirbot_certificate.stat.isdir is not defined + when: sirbot_certificate.stat.isdir is not defined and systemd_enabled == true diff --git a/roles/sirbot/tasks/main.yml b/roles/sirbot/tasks/main.yml index dd0c210..ecd244b 100644 --- a/roles/sirbot/tasks/main.yml +++ b/roles/sirbot/tasks/main.yml @@ -79,6 +79,7 @@ systemd: name: sirbot.service enabled: yes + when: systemd_enabled == true - name: Database exist stat: diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..217886b --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -euo pipefail + +apt-get update +pushd /data +ansible-playbook -i environments/inventory test.yml --vault-password-file=.pass --syntax-check +ansible-playbook -i environments/inventory test.yml --vault-password-file=.pass +ansible-playbook -i environments/inventory test.yml --vault-password-file=.pass -v | grep -q 'changed=0.*failed=0' && (echo 'Idempotence test: pass' && exit 0) || (echo 'Idempotence test: fail' && exit 1) + + + diff --git a/test.yml b/test.yml index ac9585d..7fa949c 100644 --- a/test.yml +++ b/test.yml @@ -3,7 +3,7 @@ hosts: testserver roles: - role: setup-server - tags: setup-server - role: python - tags: python + - role: sirbot + - role: pythondev-site