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) |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 15 | |
| 16 | # YAML files, excluding venv and cookiecutter directories |
Wei-Yu Chen | dda7817 | 2021-07-15 23:18:57 +0800 | [diff] [blame] | 17 | YAML_FILES ?= $(shell find . -type d \( -path "./venv_onfansible" -o -path "./cookiecutters" -o -path "./ansible_collections" -o -path "./roles" -o -path "./inventory/host_vars" \) -prune -o -type f \( -name '*.yaml' -o -name '*.yml' \) -print ) |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 18 | |
| 19 | # all files with extensions |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 20 | PYTHON_FILES ?= $(wildcard scripts/*.py scripts/*/*.py filter_plugins/*.py lint_rules/*.py cookiecutters/*/hooks/*.py) |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 21 | |
| 22 | .DEFAULT_GOAL := help |
| 23 | .PHONY: test lint yamllint ansiblelint license help |
| 24 | |
| 25 | # Create the virtualenv with all the tools installed |
| 26 | VENV_NAME = venv_onfansible |
| 27 | |
| 28 | $(VENV_NAME): requirements.txt |
| 29 | $(VIRTUALENV) $@ ;\ |
| 30 | source ./$@/bin/activate ; set -u ;\ |
| 31 | python -m pip install --upgrade pip;\ |
| 32 | python -m pip install -r requirements.txt |
| 33 | echo "To enter virtualenv, run 'source $@/bin/activate'" |
| 34 | |
| 35 | galaxy: $(VENV_NAME) galaxy.yml ## Download ansible galaxy provided collection and roles |
| 36 | source ./$</bin/activate ; set -u ;\ |
| 37 | ansible-galaxy collection install -r galaxy.yml |
| 38 | |
| 39 | license: $(VENV_NAME) ## Check license with the reuse tool |
| 40 | source ./$</bin/activate ; set -u ;\ |
| 41 | reuse --version ;\ |
| 42 | reuse --root . lint |
| 43 | |
| 44 | # Cookiecutter tests |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 45 | test: ansiblelint flake8 pylint black ## run all standard tests |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 46 | |
| 47 | yamllint: $(VENV_NAME) ## lint YAML format using yamllint |
| 48 | source ./$</bin/activate ; set -u ;\ |
| 49 | yamllint --version ;\ |
| 50 | yamllint \ |
| 51 | -d "{extends: default, rules: {line-length: {max: 99}}}" \ |
| 52 | -s $(YAML_FILES) |
| 53 | |
| 54 | ansiblelint: $(VENV_NAME) ## lint ansible-specific format using ansible-lint |
| 55 | source ./$</bin/activate ; set -u ;\ |
| 56 | ansible-lint --version ;\ |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 57 | ansible-lint -R -v $(ANSIBLE_PLAYBOOKS) |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 58 | |
| 59 | flake8: $(VENV_NAME) ## check python formatting with flake8 |
| 60 | source ./$</bin/activate ; set -u ;\ |
| 61 | flake8 --version ;\ |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 62 | flake8 --max-line-length 99 --per-file-ignores="__init__.py:F401" $(PYTHON_FILES) |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 63 | |
| 64 | pylint: $(VENV_NAME) ## pylint check for python 3 compliance |
| 65 | source ./$</bin/activate ; set -u ;\ |
| 66 | pylint --version ;\ |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame] | 67 | pylint --rcfile=pylint.ini $(PYTHON_FILES) |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 68 | |
| 69 | black: $(VENV_NAME) ## run black on python files in check mode |
| 70 | source ./$</bin/activate ; set -u ;\ |
| 71 | black --version ;\ |
| 72 | black --check $(PYTHON_FILES) |
| 73 | |
| 74 | blacken: $(VENV_NAME) ## run black on python files to reformat |
| 75 | source ./$</bin/activate ; set -u ;\ |
| 76 | black --version ;\ |
| 77 | black $(PYTHON_FILES) |
| 78 | |
Zack Williams | ad45bf0 | 2020-03-04 21:37:20 -0700 | [diff] [blame] | 79 | clean: |
| 80 | rm -rf $(VENV_NAME) ansible_collections |
| 81 | |
| 82 | help: ## Print help for each target |
| 83 | @echo infra-playbooks make targets |
| 84 | @echo |
| 85 | @grep '^[[:alnum:]_-]*:.* ##' $(MAKEFILE_LIST) \ |
| 86 | | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s %s\n", $$1, $$2};' |