diff --git a/.gitignore b/.gitignore index 640c8cc..8c40243 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ tests/.vagrant test.retry +*.pyc +__pycache__ diff --git a/.travis.yml b/.travis.yml index 186c696..d37f2c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,35 @@ --- language: python python: "2.7" -before_install: - - sudo apt-get update -qq - - sudo apt-get install -qq python-apt python-pycurl +services: docker + +env: + global: + - ROLE_NAME: logrotate + - GALAXY_AUTHOR: nickhammond + - GIT_REPO_NAME: ansible-logrotate + matrix: + - MOLECULE_DISTRO_IMAGE: ubuntu:18.04 + - MOLECULE_DISTRO_IMAGE: ubuntu:16.04 + - MOLECULE_DISTRO_IMAGE: ubuntu:14.04 + - MOLECULE_DISTRO_IMAGE: centos:7 + - MOLECULE_DISTRO_IMAGE: centos:6 + - MOLECULE_DISTRO_IMAGE: fedora:29 + - MOLECULE_DISTRO_IMAGE: debian:9 + - MOLECULE_DISTRO_IMAGE: debian:8 + install: - - pip install ansible + # Install test dependencies. + - pip install molecule docker + +before_script: + # Use actual Ansible Galaxy role name for the project directory. + - cd ../ + - mv $GIT_REPO_NAME $GALAXY_AUTHOR.$ROLE_NAME + - cd $GALAXY_AUTHOR.$ROLE_NAME + script: - - "printf '[defaults]\nroles_path = ../' > ansible.cfg" - - ansible-playbook -i tests/inventory --syntax-check tests/test.yml - - ansible-playbook -i tests/inventory --connection=local --become -vvvv tests/test.yml + # Run tests. + - molecule test notifications: email: false diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..3a2255e --- /dev/null +++ b/.yamllint @@ -0,0 +1,13 @@ +extends: default + +rules: + braces: + max-spaces-inside: 1 + level: error + brackets: + max-spaces-inside: 1 + level: error + line-length: disable + # NOTE(retr0h): Templates no longer fail this lint rule. + # Uncomment if running old Molecule templates. + # truthy: disable diff --git a/LICENSE b/LICENSE index 041aee4..eaf6cb4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2016-14, Nick Hammond +Copyright (c) 2016-18, Nick Hammond All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index 6a6c681..0275d72 100644 --- a/README.md +++ b/README.md @@ -86,13 +86,12 @@ Setting up logrotate for additional Nginx logs, with postrotate script. ## Testing locally -This role is already configured to run on travis CI within a test playbook but it's useful to be able to run and debug a role locally which can be done via Vagrant and the `ansible_local` provisioner. +This role is already configured to run on travis CI within a test playbook but it's useful to be able to run and debug a role locally which can be done via molecule and docker provisioner. -To run the test playbook locally within a Vagrant virtual machine: +To run the test locally, [install molecule](https://molecule.readthedocs.io/en/latest/installation.html), then run ``` -cd tests -vagrant up --provision +molecule test ``` ## License diff --git a/defaults/main.yml b/defaults/main.yml index bfe0981..0171e7e 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,3 @@ +--- logrotate_conf_dir: "/etc/logrotate.d/" logrotate_scripts: [] diff --git a/meta/main.yml b/meta/main.yml index 7e422a8..f2c516c 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -5,14 +5,15 @@ galaxy_info: license: BSD min_ansible_version: 1.9 platforms: - - name: Ubuntu - versions: - - lucid - - precise - - trusty - - name: EL - versions: - - 7 - categories: - - system + - name: Ubuntu + versions: + - bionic + - xenial + - trusty + - name: CentOS + versions: + - 7 + - 6 + galaxy_tags: + - system dependencies: [] diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 new file mode 100644 index 0000000..348bea4 --- /dev/null +++ b/molecule/default/Dockerfile.j2 @@ -0,0 +1,17 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi +RUN if [ $(command -v apt-get) ]; then apt-get install -y python-yaml ; \ + elif [ $(command -v yum) ]; then yum install -y PyYAML ; \ + fi diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst new file mode 100644 index 0000000..b42edf5 --- /dev/null +++ b/molecule/default/INSTALL.rst @@ -0,0 +1,16 @@ +******* +Docker driver installation guide +******* + +Requirements +============ + +* General molecule dependencies (see https://molecule.readthedocs.io/en/latest/installation.html) +* Docker Engine +* docker-py +* docker + +Install +======= + + $ sudo pip install docker-py diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..c9c2058 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,25 @@ +--- +dependency: + name: galaxy +driver: + name: docker +lint: + name: yamllint +platforms: + - name: instance + image: "${MOLECULE_DISTRO_IMAGE:-ubuntu:14.04}" + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:ro + privlidged: true +provisioner: + name: ansible + lint: + name: ansible-lint + playbook: + converge: playbook.yml +scenario: + name: default +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..50768f4 --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,43 @@ +--- +- name: Converge + hosts: all + + tasks: + - name: molecule playbook | create nginx dir + file: + path: '/var/log/nginx' + state: 'directory' + become: true + + - name: molecule playbook | create nginx log files + file: + path: '/var/log/nginx/{{ item }}' + state: 'touch' + changed_when: false + loop: + - 'options.log' + - 'scripts.log' + + roles: + - role: nickhammond.logrotate + vars: + logrotate_scripts: + - name: nginx-options + path: /var/log/nginx/options.log + options: + - daily + - weekly + - size 25M + - rotate 7 + - missingok + - compress + - delaycompress + - copytruncate + - name: nginx-scripts + path: /var/log/nginx/scripts.log + options: + - daily + - weekly + - size 25M + scripts: + postrotate: "echo test" diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..1c79618 --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,33 @@ +import os +import yaml + +import testinfra.utils.ansible_runner + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def get_logrotate_d_log_files(): + """ + return a list of log files managed by logrotate + as defined in playbook.yml + """ + playbook_path = 'playbook.yml' + + with open(playbook_path) as y: + playbook_yml = yaml.safe_load(y) + + log_files = [] + for play in playbook_yml: + for role in play['roles']: + for script in role['vars']['logrotate_scripts']: + log_files.append(script['name']) + + return log_files + + +def test_logrotate_conf(host): + for log_file in get_logrotate_d_log_files(): + logrotate_d_path = '/etc/logrotate.d/' + log_file + cmd = host.run('logrotate -d "%s"', logrotate_d_path) + assert cmd.stderr.find('error') == -1 diff --git a/tests/Vagrantfile b/tests/Vagrantfile deleted file mode 100644 index 08d47f6..0000000 --- a/tests/Vagrantfile +++ /dev/null @@ -1,18 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : -@ansible_home = "/home/vagrant/.ansible" - -Vagrant.configure("2") do |config| - config.vm.box = "ubuntu/trusty64" - - # Copy the Ansible playbook over to the guest machine, run rsync-auto to automatically - # pull in the latest changes while a VM is running. - config.vm.synced_folder "../", "#{@ansible_home}/roles/ansible-logrotate", type: 'rsync' - - # The working ansible directory created by ansible_local is owned by root - config.vm.provision "shell", inline: "chown vagrant:vagrant #{@ansible_home}" - - config.vm.provision "ansible_local" do |ansible| - ansible.playbook = "test.yml" - end -end diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index 2fbb50c..0000000 --- a/tests/inventory +++ /dev/null @@ -1 +0,0 @@ -localhost diff --git a/tests/test.yml b/tests/test.yml deleted file mode 100644 index 180a460..0000000 --- a/tests/test.yml +++ /dev/null @@ -1,29 +0,0 @@ ---- -- hosts: all - become: True - vars: - logrotate_scripts: - - name: nginx-options - path: /var/log/nginx/options.log - options: - - daily - - - name: nginx-scripts - path: /var/log/nginx/scripts.log - scripts: - postrotate: "echo test" - - - name: multiple-paths - paths: - - /var/log/nginx/options.log - - /var/log/nginx/scripts.log - - roles: - - ansible-logrotate - - tasks: - - name: Verify logrotate config check passes - shell: logrotate -d "{{ logrotate_conf_dir }}{{ item.name }}" - with_items: "{{ logrotate_scripts }}" - register: logrotate_tests - failed_when: "'error' in logrotate_tests.stderr"