| 1 | PYTHON_COMMAND ?= python |
| 2 | NAMESPACE := $(shell $(PYTHON_COMMAND) -c 'import yaml; print(yaml.safe_load(open("galaxy.yml"))["namespace"])') |
| 3 | NAME := $(shell $(PYTHON_COMMAND) -c 'import yaml; print(yaml.safe_load(open("galaxy.yml"))["name"])') |
| 4 | VERSION := $(shell $(PYTHON_COMMAND) -c 'import yaml; print(yaml.safe_load(open("galaxy.yml"))["version"])') |
| 5 | MANIFEST := build/collections/ansible_collections/$(NAMESPACE)/$(NAME)/MANIFEST.json |
| 6 | |
| 7 | ROLES := $(wildcard roles/*) |
| 8 | PLUGIN_TYPES := $(filter-out __%,$(notdir $(wildcard plugins/*))) |
| 9 | RUNTIME_YML := meta/runtime.yml |
| 10 | METADATA := galaxy.yml LICENSE README.md $(RUNTIME_YML) requirements.txt changelogs/changelog.yaml CHANGELOG.rst bindep.txt PSF-license.txt meta/execution-environment.yml |
| 11 | TESTDATA := .ansible-lint Makefile pytest.ini $(shell find tests/ ! -type d ! -path '*/__pycache__/*' ! -path '*/test_playbooks/fixtures/*' ! -path '*/fixtures/apidoc/*') |
| 12 | $(foreach PLUGIN_TYPE,$(PLUGIN_TYPES),$(eval _$(PLUGIN_TYPE) := $(filter-out %__init__.py,$(wildcard plugins/$(PLUGIN_TYPE)/*.py)) $(wildcard plugins/$(PLUGIN_TYPE)/*.yml))) |
| 13 | DEPENDENCIES := $(METADATA) $(foreach PLUGIN_TYPE,$(PLUGIN_TYPES),$(_$(PLUGIN_TYPE))) $(foreach ROLE,$(ROLES),$(wildcard $(ROLE)/*/*)) $(foreach ROLE,$(ROLES),$(ROLE)/README.md) $(TESTDATA) |
| 14 | |
| 15 | PYTHON_VERSION = $(shell $(PYTHON_COMMAND) -c 'import sys; print("{}.{}".format(sys.version_info.major, sys.version_info.minor))') |
| 16 | COLLECTION_COMMAND ?= ansible-galaxy |
| 17 | SANITY_OPTS = --venv |
| 18 | TEST = |
| 19 | FLAGS = |
| 20 | PYTEST_COMMAND ?= $(PYTHON_COMMAND) -m pytest |
| 21 | PYTEST = $(PYTEST_COMMAND) -n 4 --forked |
| 22 | # PYTEST_ADDOPTS is exported to sub-shells and picked up by *all* pytest invocations |
| 23 | PYTEST_ADDOPTS ?= -vv |
| 24 | export PYTEST_ADDOPTS |
| 25 | |
| 26 | APIPIE_VERSION ?= v0.8.0 |
| 27 | |
| 28 | default: help |
| 29 | help: |
| 30 | @echo "Please use \`make <target>' where <target> is one of:" |
| 31 | @echo " help to show this message" |
| 32 | @echo " info to show infos about the collection" |
| 33 | @echo " lint to run code linting" |
| 34 | @echo " test to run unit tests" |
| 35 | @echo " livetest to run test playbooks live (without vcr)" |
| 36 | @echo " sanity to run santy tests" |
| 37 | @echo " setup to set up test, lint" |
| 38 | @echo " test-setup to install test dependencies" |
| 39 | @echo " lint-setup to install lint dependencies" |
| 40 | @echo " test_<test> to run a specific unittest" |
| 41 | @echo " livetest_<test> to run a specific unittest live (without vcr)" |
| 42 | @echo " record_<test> to (re-)record the server answers for a specific test" |
| 43 | @echo " clean_<test> to run a specific test playbook with the teardown and cleanup tags" |
| 44 | @echo " dist to build the collection artifact" |
| 45 | |
| 46 | info: |
| 47 | @echo "Building collection $(NAMESPACE)-$(NAME)-$(VERSION)" |
| 48 | @echo -e " roles:\n $(foreach ROLE,$(notdir $(ROLES)), - $(ROLE)\n)" |
| 49 | @echo -e " $(foreach PLUGIN_TYPE,$(PLUGIN_TYPES), $(PLUGIN_TYPE):\n $(foreach PLUGIN,$(basename $(notdir $(_$(PLUGIN_TYPE)))), - $(PLUGIN)\n)\n)" |
| 50 | |
| 51 | lint: $(MANIFEST) $(RUNTIME_YML) | tests/test_playbooks/vars/server.yml |
| 52 | yamllint -f parsable tests/test_playbooks roles |
| 53 | ansible-lint -v --offline |
| 54 | ansible-playbook --syntax-check tests/test_playbooks/*.yml | grep -v '^$$' |
| 55 | flake8 --ignore=E402,W503 --max-line-length=160 plugins/ tests/ |
| 56 | @echo "Check that there are no changes to $(RUNTIME_YML)" |
| 57 | git diff --exit-code $(RUNTIME_YML) |
| 58 | |
| 59 | galaxy-importer: $(MANIFEST) |
| 60 | GALAXY_IMPORTER_CONFIG=tests/galaxy-importer.cfg $(PYTHON_COMMAND) -m galaxy_importer.main $(NAMESPACE)-$(NAME)-$(VERSION).tar.gz |
| 61 | |
| 62 | sanity: $(MANIFEST) |
| 63 | # Fake a fresh git repo for ansible-test |
| 64 | cd $(<D) ; git init ; echo tests > .gitignore ; ansible-test sanity $(SANITY_OPTS) --python $(PYTHON_VERSION) |
| 65 | |
| 66 | test: $(MANIFEST) | tests/test_playbooks/vars/server.yml |
| 67 | $(PYTEST) $(TEST) |
| 68 | |
| 69 | test-crud: $(MANIFEST) | tests/test_playbooks/vars/server.yml |
| 70 | $(PYTEST) 'tests/test_crud.py::test_crud' 'tests/test_crud.py::test_inventory' |
| 71 | |
| 72 | test-check-mode: $(MANIFEST) | tests/test_playbooks/vars/server.yml |
| 73 | $(PYTEST) 'tests/test_crud.py::test_check_mode' |
| 74 | |
| 75 | test-other: |
| 76 | $(PYTEST) -k 'not test_crud.py' |
| 77 | |
| 78 | livetest: $(MANIFEST) | tests/test_playbooks/vars/server.yml |
| 79 | $(PYTEST_COMMAND) 'tests/test_crud.py::test_crud' --vcrmode live $(FLAGS) |
| 80 | |
| 81 | test_%: FORCE $(MANIFEST) | tests/test_playbooks/vars/server.yml |
| 82 | $(PYTEST_COMMAND) 'tests/test_crud.py::test_crud[$*]' 'tests/test_crud.py::test_check_mode[$*]' $(FLAGS) |
| 83 | |
| 84 | livetest_%: FORCE $(MANIFEST) | tests/test_playbooks/vars/server.yml |
| 85 | $(PYTEST_COMMAND) 'tests/test_crud.py::test_crud[$*]' --vcrmode live $(FLAGS) |
| 86 | |
| 87 | record_%: FORCE $(MANIFEST) |
| 88 | $(RM) tests/test_playbooks/fixtures/$*-*.yml |
| 89 | $(PYTEST_COMMAND) 'tests/test_crud.py::test_crud[$*]' --vcrmode record $(FLAGS) |
| 90 | |
| 91 | clean_%: FORCE $(MANIFEST) |
| 92 | ansible-playbook --tags teardown,cleanup -i tests/inventory/hosts 'tests/test_playbooks/$*.yml' |
| 93 | |
| 94 | setup: test-setup |
| 95 | |
| 96 | lint-setup: | tests/test_playbooks/vars/server.yml |
| 97 | $(PYTHON_COMMAND) -m pip install --upgrade pip |
| 98 | $(PYTHON_COMMAND) -m pip install --upgrade -r requirements-lint.txt |
| 99 | |
| 100 | test-setup: | tests/test_playbooks/vars/server.yml |
| 101 | $(PYTHON_COMMAND) -m pip install --upgrade pip |
| 102 | $(PYTHON_COMMAND) -m pip install --upgrade -r requirements-dev.txt |
| 103 | |
| 104 | tests/test_playbooks/vars/server.yml: |
| 105 | cp [email protected] $@ |
| 106 | @echo "For recording, please adjust $@ to match your reference server." |
| 107 | |
| 108 | dist-test: $(MANIFEST) |
| 109 | FOREMAN_SERVER_URL=https://foreman.example.test ansible -m $(NAMESPACE).$(NAME).organization -a "username=admin password=changeme name=collectiontest" localhost | grep -q "Failed to connect to Foreman server.*foreman.example.test" |
| 110 | FOREMAN_SERVER_URL=https://foreman.example.test ansible -m $(NAMESPACE).$(NAME).foreman_organization -a "username=admin password=changeme name=collectiontest" localhost | grep -q "Failed to connect to Foreman server.*foreman.example.test" |
| 111 | FOREMAN_SERVER_URL=http://foreman.example.test ansible -m $(NAMESPACE).$(NAME).organization -a "username=admin password=changeme name=collectiontest" localhost 2>&1| grep -q "You have configured a plain HTTP server URL." |
| 112 | ansible-doc $(NAMESPACE).$(NAME).organization | grep -q "Manage Organization" |
| 113 | |
| 114 | $(MANIFEST): $(NAMESPACE)-$(NAME)-$(VERSION).tar.gz |
| 115 | ansible-galaxy collection install -p build/collections $< --force |
| 116 | |
| 117 | build/src/%: % |
| 118 | install -m 644 -DT $< $@ |
| 119 | |
| 120 | build/src/tests/test_%.py: FORCE |
| 121 | install -m 644 -DT tests/test_$*.py $@ |
| 122 | |
| 123 | $(NAMESPACE)-$(NAME)-$(VERSION).tar.gz: $(addprefix build/src/,$(DEPENDENCIES)) |
| 124 | ansible-galaxy collection build build/src --force |
| 125 | |
| 126 | dist: $(NAMESPACE)-$(NAME)-$(VERSION).tar.gz |
| 127 | |
| 128 | publish: $(NAMESPACE)-$(NAME)-$(VERSION).tar.gz |
| 129 | ansible-galaxy collection publish --api-key $(GALAXY_API_KEY) $< |
| 130 | |
| 131 | clean: |
| 132 | rm -rf build docs/plugins |
| 133 | |
| 134 | doc-setup: |
| 135 | $(PYTHON_COMMAND) -m pip install --upgrade -r docs/requirements.txt |
| 136 | doc: $(MANIFEST) |
| 137 | mkdir -p ./docs/plugins ./docs/roles |
| 138 | cat ./docs/roles.rst.template > ./docs/roles/index.rst |
| 139 | for role_readme in roles/*/README.md; do \ |
| 140 | ln -f -s ../../$$role_readme ./docs/roles/$$(basename $$(dirname $$role_readme)).md; \ |
| 141 | echo " * :doc:\`$$(basename $$(dirname $$role_readme))\`" >> ./docs/roles/index.rst; \ |
| 142 | done |
| 143 | antsibull-docs collection --use-current --squash-hierarchy --dest-dir ./docs/plugins $(NAMESPACE).$(NAME) |
| 144 | make -C docs html |
| 145 | |
| 146 | vendor: |
| 147 | git clone --depth=1 --branch=$(APIPIE_VERSION) https://github.com/Apipie/apypie/ build/apypie-git |
| 148 | $(PYTHON_COMMAND) vendor.py build/apypie-git/apypie/*.py > plugins/module_utils/_apypie.py |
| 149 | |
| 150 | $(RUNTIME_YML): FORCE |
| 151 | $(PYTHON_COMMAND) generate_action_groups.py |
| 152 | |
| 153 | FORCE: |
| 154 | |
| 155 | .PHONY: help dist lint sanity test test-crud test-check-mode test-other livetest setup test-setup lint-setup doc-setup doc publish FORCE |