blob: 8ba4395fe6d005e24b4bd48e225cfdca00a54d25 [file] [log] [blame]
Zack Williamsad45bf02020-03-04 21:37:20 -07001# 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
8SHELL = bash -e -o pipefail
9
10# tooling
11VIRTUALENV ?= python -m venv
12
13# ansible files is all top-level playbooks
14ANSIBLE_PLAYBOOKS ?= $(wildcard *.yml)
15ANSIBLE_ROLES ?= $(wildcard roles/*)
16
17# YAML files, excluding venv and cookiecutter directories
18YAML_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
21PYTHON_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
27VENV_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
36galaxy: $(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
40license: $(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
46test: yamllint ansiblelint flake8 pylint black ## run all standard tests
47
48yamllint: $(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
55ansiblelint: $(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
60flake8: $(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
65pylint: $(VENV_NAME) ## pylint check for python 3 compliance
66 source ./$</bin/activate ; set -u ;\
67 pylint --version ;\
68 pylint --py3k $(PYTHON_FILES)
69
70black: $(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
75blacken: $(VENV_NAME) ## run black on python files to reformat
76 source ./$</bin/activate ; set -u ;\
77 black --version ;\
78 black $(PYTHON_FILES)
79
80# Password generation for use with ansible
81# from: https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-generate-encrypted-passwords-for-the-user-module
82# Ref on supported crypt types per OS: https://passlib.readthedocs.io/en/stable/modular_crypt_format.html#os-defined-hashes
83
84# sha512 linux passwords
85sha512_passwd: $(VENV_NAME)
86 source ./$</bin/activate ; set -u ;\
87 python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
88
89# bcrypt openbsd passwords, per https://man.openbsd.org/crypt.3
90bcrypt_passwd: $(VENV_NAME)
91 source ./$</bin/activate ; set -u ;\
92 python -c "from passlib.hash import bcrypt; import getpass; print(bcrypt.using(rounds=8).hash(getpass.getpass()))"
93
94passwd: $(VENV_NAME) ## encrypt a password using required formats
95 source ./$</bin/activate ; set -u ;\
96 python -c "from passlib.hash import bcrypt,sha512_crypt; import getpass; pw=getpass.getpass(); print('bcrypt: %s\nsha512crypt: %s' % (bcrypt.using(rounds=8).hash(pw), sha512_crypt.using(rounds=5000).hash(pw)))"
97
98clean:
99 rm -rf $(VENV_NAME) ansible_collections
100
101help: ## Print help for each target
102 @echo infra-playbooks make targets
103 @echo
104 @grep '^[[:alnum:]_-]*:.* ##' $(MAKEFILE_LIST) \
105 | sort | awk 'BEGIN {FS=":.* ## "}; {printf "%-25s %s\n", $$1, $$2};'