Skip to content

Commit

Permalink
[GITHUB-17] Update to Latest Standards
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisconrad committed Apr 12, 2018
1 parent be91cc1 commit 0174723
Show file tree
Hide file tree
Showing 31 changed files with 624 additions and 474 deletions.
25 changes: 21 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
# http://editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 4

[*.{yml,md,json.j2}]
indent_style = space
insert_final_newline = false
trim_trailing_whitespace = true

[*.cf]
indent_size = 2
max_line_length = 119

[*.md]
indent_size = 2
trim_trailing_whitespace = false

[*.py]
max_line_length = 119

[*.{js,json,yaml,yml}]
indent_size = 2

[Makefile]
indent_style = tab
14 changes: 6 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## IDEs, editors, ...
.\#*
.DS_Store
.idea
.project
Expand All @@ -7,15 +8,12 @@
*.sublime*
*.swp
/.settings



\#*\#

# Build and Tests Artefacts
*.pyc
*.retry
/.make
/.venv_*
.cache/
.molecule/
.venv_*
/meta/.galaxy_install_info
/tests/.vagrant
/tests/*.log
/tests/roles
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---

- repo: https://github.com/willthames/ansible-lint.git
sha: v3.4.17
hooks:
- id: ansible-lint
types: [yaml]
exclude: >
(?x)^(
\.travis\.yml|
molecule/.*/\.molecule/.*|
molecule/.*/create\.yml
)$
- id: ansible-lint
files: \.travis\.yml
args: [-x, ANSIBLE0008]
- id: ansible-lint
files: molecule/.*/create\.yml
args: [-x, ANSIBLE0016]

- repo: git://github.com/pre-commit/pre-commit-hooks
sha: v0.9.1
hooks:
- id: check-json
- id: flake8
- id: trailing-whitespace
21 changes: 12 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@

group: stable
dist: trusty
sudo: required

language: python
python: "2.7"
python:
- 2.7

branches:
only:
- develop
- master

env:
- ANSIBLE_INSTALL_VERSION=2.0.2.0
- ANSIBLE_INSTALL_VERSION=2.1.5.0
- ANSIBLE_INSTALL_VERSION=2.2.3.0
- ANSIBLE_INSTALL_VERSION=2.3.1.0
- ANSIBLE_INSTALL_VERSION=2.3.3.0
- ANSIBLE_INSTALL_VERSION=2.4.3.0
- ANSIBLE_INSTALL_VERSION=2.5.0

services:
- docker

before_install:
# Make sure everything's up to date.
- sudo apt-get update -qq
- sudo apt-get install -o Dpkg::Options::="--force-confold" --force-yes -y docker-ce

install:
# dowload dependencies
- make test_deps
- make deps

script:
- make lint
- make test_ansible
- make test

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2
v2.0
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016-2017
Copyright (c) 2018

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
164 changes: 68 additions & 96 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,111 +1,83 @@
include .make

ANSIBLE_INSTALL_VERSION ?= 2.2.3.0
ANSIBLE_CONFIG ?= tests/ansible.cfg
ROLE_NAME ?= $(shell basename $$(pwd))
TEST_PLAYBOOK ?= test.yml
VAGRANT_BOX ?= ubuntu/trusty64
PATH := $(PWD)/.venv_$(ANSIBLE_INSTALL_VERSION)/bin:$(shell printenv PATH)
PATH := $(PWD)/.venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin:$(shell printenv PATH)
SHELL := env PATH=$(PATH) /bin/bash

.DEFAULT_GOAL := help
.PHONY: help
.PHONY: all clean destroy help test


_check_venv:
@if [ ! -e .venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/activate ]; then \
echo -e "\033[0;31mERROR: No virtualenv found - run 'make deps' first\033[0m"; \
false; \
fi


## Make deps, test
all: deps test


## Activate the virtualenv
activate: _check_venv
@echo -e "\033[0;32mINFO: Activating venv_ansible$(ANSIBLE_INSTALL_VERSION) (ctrl-d to exit)\033[0m"
@exec $(SHELL) --init-file .venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/activate


## Destroy docker instances, remove virtualenv, molecule temp, .pyc files
clean: destroy
rm -rf .venv_ansible*
rm -rf molecule/*/.cache/
rm -rf molecule/*/.molecule/
find . -name "*.pyc" -exec rm {} \;

export ANSIBLE_CONFIG
export PATH
export VAGRANT_BOX
export TEST_PLAYBOOK

all: test clean
## Run 'molecule destroy'
destroy:
@if [ -x .venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/molecule ]; then \
.venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/molecule destroy; \
elif (which -s molecule); then \
echo -e "\033[0;33mWARNING: molecule not found in virtualenv - trying to use molecule in PATH\033[0m"; \
molecule destroy; \
else \
echo -e "\033[0;33mWARNING: molecule not found - either remove potential containers manually or run 'make deps' first\033[0m"; \
fi

## Run tests on any file change
watch: test_deps
while sleep 1; do \
find defaults/ handlers/ meta/ tasks/ templates/ tests/test.yml \
| entr -d make lint vagrant; \
## Login to docker container named '%'
login_%: _check_venv
@echo -e "\033[0;32mINFO: Logging into $(subst login_,,$@) (ctrl-d to exit)\033[0m"
@.venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/molecule login --host $(subst login_,,$@)

## Run 'molecule test --destroy=never' (run 'make destroy' to destroy containers)
test: _check_venv
@.venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/molecule test --destroy=never


## Create virtualenv, install dependencies
deps:
@if (python -V 2>&1 | grep -qv "Python 2.7"); then \
echo -e "\033[0;31mERROR: Only Python 2.7 is supported at this stage\033[0m"; \
false; \
fi
virtualenv .venv_ansible$(ANSIBLE_INSTALL_VERSION)
.venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/pip install -r requirements.txt --ignore-installed
.venv_ansible$(ANSIBLE_INSTALL_VERSION)/bin/pip install ansible==$(ANSIBLE_INSTALL_VERSION)
virtualenv --relocatable .venv_ansible$(ANSIBLE_INSTALL_VERSION)
@echo -e "\033[0;32mINFO: Run 'make activate' to activate the virtualenv for this shell\033[0m"


## Run 'make test' on any file change
watch: _check_venv
@while sleep 1; do \
find defaults/ files/ handlers/ meta/ molecule/*/*.yml molecule/*/test/*.py tasks/ templates/ vars/ 2> /dev/null \
| entr -d make test; \
done

## Run tests
test: lint test_deps vagrant

## Install test dependencies
test_deps: .venv_$(ANSIBLE_INSTALL_VERSION) tests/roles

tests/roles:
mkdir -p tests/roles
ln -s ../.. tests/roles/sansible.$(ROLE_NAME)
ansible-galaxy install -p tests/roles -r tests/local_requirements.yml --ignore-errors

## ! Executes Ansible tests using local connection
# run it ONLY from within a test VM.
# If you want to test this role, run `make test` instead.
# Example: make test_ansible
# make test_ansible TEST_PLAYBOOK=test-something-else.yml
test_ansible: test_ansible_build test_ansible_configure
cd tests && ansible-playbook \
--inventory inventory \
--connection local \
--tags assert \
$(TEST_PLAYBOOK)

## ! Executes Ansible tests using local connection
# run it ONLY from witinh a test VM.
# Example: make test_ansible_build
# make test_ansible_build TEST_PLAYBOOK=test-something-else.yml
test_ansible_%:
cd tests && ansible-playbook \
--inventory inventory \
--connection local \
--tags=$(subst test_ansible_,,$@) \
$(TEST_PLAYBOOK)
cd tests && ansible-playbook \
--inventory inventory \
--connection local \
--tags=$(subst test_ansible_,,$@) \
$(TEST_PLAYBOOK) \
| grep -q 'changed=0.*failed=0' \
&& (echo 'Idempotence test: pass' && exit 0) \
|| (echo 'Idempotence test: fail' && exit 1)

## Start and (re)provisiom Vagrant test box
vagrant:
cd tests && vagrant up --no-provision
cd tests && vagrant provision
@echo "- - - - - - - - - - - - - - - - - - - - - - -"
@echo " Provisioning Successful"
@echo "- - - - - - - - - - - - - - - - - - - - - - -"

## Execute simple Vagrant command
# Example: make vagrant_ssh
# make vagrant_halt
vagrant_%:
cd tests && vagrant $(subst vagrant_,,$@)

## Installs a virtual environment and all python dependencies
.venv_%:
virtualenv .venv_$(ANSIBLE_INSTALL_VERSION)
.venv_$(ANSIBLE_INSTALL_VERSION)/bin/pip install -r requirements.txt --ignore-installed
.venv_$(ANSIBLE_INSTALL_VERSION)/bin/pip install ansible==$(ANSIBLE_INSTALL_VERSION)
virtualenv --relocatable .venv_$(ANSIBLE_INSTALL_VERSION)

## lint Ansible files
lint: .venv_$(ANSIBLE_INSTALL_VERSION)
find defaults/ handlers/ meta/ tasks/ templates/ -name "*.yml" | xargs -I{} ansible-lint {}

## Prints this help

help:
@awk -v skip=1 \
'/^##/ { sub(/^[#[:blank:]]*/, "", $$0); doc_h=$$0; doc=""; skip=0; next } \
skip { next } \
/^#/ { doc=doc "\n" substr($$0, 2); next } \
/:/ { sub(/:.*/, "", $$0); printf "\033[34m%-30s\033[0m\033[1m%s\033[0m %s\n\n", $$0, doc_h, doc; skip=1 }' \
/:/ { sub(/:.*/, "", $$0); \
printf "\033[34m%-30s\033[0m\033[1m%s\033[0m %s\n\n", $$0, doc_h, doc; skip=1 }' \
$(MAKEFILE_LIST)

## Removes all downloaded dependencies
clean:
rm -rf .venv_*
rm -rf tests/roles
cd tests && (vagrant destroy || echo "skipping vagrant destroy")

.make:
touch .make
Loading

0 comments on commit 0174723

Please sign in to comment.