Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 1 | # ONF Ansbile Makefile |
| 2 | # |
| 3 | # SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # Use bash for pushd/popd, and to fail quickly. |
| 7 | # No -u as virtualenv activate script has undefined vars |
| 8 | SHELL = bash -e -o pipefail |
| 9 | |
| 10 | # tooling |
Zack Williams | dea8b77 | 2020-12-17 08:22:49 -0700 | [diff] [blame] | 11 | VIRTUALENV ?= python3 -m venv |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 12 | |
| 13 | # ansible files is all top-level playbooks |
| 14 | ANSIBLE_PLAYBOOKS ?= $(wildcard *.yml) |
| 15 | ANSIBLE_ROLES ?= $(wildcard roles/*) |
| 16 | |
| 17 | # YAML files, excluding venv and cookiecutter directories |
| 18 | YAML_FILES ?= $(shell find . -type d \( -path "./venv_onfansible" -o -path "./cookiecutters" -o -path "./ansible_collections" -o -path "./roles" \) -prune -o -type f \( -name '*.yaml' -o -name '*.yml' \) -print ) |
| 19 | |
| 20 | # all files with extensions |
| 21 | PYTHON_FILES ?= $(wildcard filter_plugins/*.py lint_rules/*.py cookiecutters/*/hooks/*.py roles/*/filter_plugins/*.py) |
| 22 | |
| 23 | .DEFAULT_GOAL := help |
| 24 | .PHONY: test lint yamllint ansiblelint license help |
| 25 | |
| 26 | # Create the virtualenv with all the tools installed |
| 27 | VENV_NAME = venv_onfansible |
| 28 | |
| 29 | $(VENV_NAME): requirements.txt |
| 30 | $(VIRTUALENV) $@ ;\ |
| 31 | source ./$@/bin/activate ; set -u ;\ |
| 32 | python -m pip install --upgrade pip;\ |
| 33 | python -m pip install -r requirements.txt |
| 34 | echo "To enter virtualenv, run 'source $@/bin/activate'" |
| 35 | |
| 36 | galaxy: $(VENV_NAME) galaxy.yml ## Download ansible galaxy provided collection and roles |
| 37 | source ./$</bin/activate ; set -u ;\ |
| 38 | ansible-galaxy collection install -r galaxy.yml |
| 39 | |
| 40 | license: $(VENV_NAME) ## Check license with the reuse tool |
| 41 | source ./$</bin/activate ; set -u ;\ |
| 42 | reuse --version ;\ |
| 43 | reuse --root . lint |
| 44 | |
| 45 | # Cookiecutter tests |
| 46 | test: yamllint ansiblelint flake8 pylint black ## run all standard tests |
| 47 | |
| 48 | yamllint: $(VENV_NAME) ## lint YAML format using yamllint |
| 49 | source ./$</bin/activate ; set -u ;\ |
| 50 | yamllint --version ;\ |
| 51 | yamllint \ |
| 52 | -d "{extends: default, rules: {line-length: {max: 99}}}" \ |
| 53 | -s $(YAML_FILES) |
| 54 | |
| 55 | ansiblelint: $(VENV_NAME) ## lint ansible-specific format using ansible-lint |
| 56 | source ./$</bin/activate ; set -u ;\ |
| 57 | ansible-lint --version ;\ |
| 58 | ansible-lint -R -v $(ANSIBLE_PLAYBOOKS) $(ANSIBLE_ROLES) |
| 59 | |
| 60 | flake8: $(VENV_NAME) ## check python formatting with flake8 |
| 61 | source ./$</bin/activate ; set -u ;\ |
| 62 | flake8 --version ;\ |
| 63 | flake8 --max-line-length 99 $(PYTHON_FILES) |
| 64 | |
| 65 | pylint: $(VENV_NAME) ## pylint check for python 3 compliance |
| 66 | source ./$</bin/activate ; set -u ;\ |
| 67 | pylint --version ;\ |
| 68 | pylint --py3k $(PYTHON_FILES) |
| 69 | |
| 70 | black: $(VENV_NAME) ## run black on python files in check mode |
| 71 | source ./$</bin/activate ; set -u ;\ |
| 72 | black --version ;\ |
| 73 | black --check $(PYTHON_FILES) |
| 74 | |
| 75 | blacken: $(VENV_NAME) ## run black on python files to reformat |
| 76 | source ./$</bin/activate ; set -u ;\ |
| 77 | black --version ;\ |
| 78 | black $(PYTHON_FILES) |
| 79 | |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 80 | clean: |
| 81 | rm -rf $(VENV_NAME) ansible_collections |
| 82 | |
| 83 | help: ## Print help for each target |
| 84 | @echo infra-playbooks make targets |
| 85 | @echo |
| 86 | @grep '^[[:alnum:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 87 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s %s\n", $$1, $$2};' |