-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMakefile
More file actions
293 lines (266 loc) · 11.3 KB
/
Makefile
File metadata and controls
293 lines (266 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Let's use bash!
SHELL := /bin/bash
.SHELLFLAGS := -euo pipefail -c
# Directory structure variables
COLLECTION_ROOT := $(CURDIR)
MODULES_DIR := $(COLLECTION_ROOT)/plugins/modules
VENV_DIR := $(COLLECTION_ROOT)/.venv/
# Configuration variables
CONTAINER_ENGINE := podman
# Use CentoS Stream 9 as base image because UPX is not available in CentOS 10
# @TODO: move to 10 when the payload size will be increase in Galaxy
CONTAINER_IMAGE := quay.io/centos/centos:stream9
BUILD_SCRIPT := /code/scripts/build.sh
PYTHON_VERSION ?= 3.12
ANSIBLE_TEST_PYTHON_VERSION ?= 3.12
MOUNT_PATH := $(COLLECTION_ROOT):/code/
# Check if SELinux is enabled by testing if getenforce exists and returns "Enforcing"
GETENFORCE_CMD := $(shell command -v getenforce 2>/dev/null)
SELINUX_ENFORCING := $(shell $(GETENFORCE_CMD) 2>/dev/null | grep -q "Enforcing" && echo "yes" || echo "no")
# Set security options only if SELinux is enforcing
ifeq ($(SELINUX_ENFORCING),yes)
SECURITY_OPT := --security-opt label=disable
else
SECURITY_OPT :=
endif
# Define a function to verify we're in the ansible collection root
define verify_collection_root
@if [ ! -f "$(COLLECTION_ROOT)/galaxy.yml" ]; then \
echo "Error: Must be run from the ansible collection root directory."; \
echo "Missing galaxy.yml file."; \
exit 1; \
fi
endef
# Extract collection metadata from galaxy.yml (if it exists)
GALAXY_YML := $(COLLECTION_ROOT)/galaxy.yml
ifneq ($(wildcard $(GALAXY_YML)),)
COLLECTION_NAMESPACE := $(shell grep -E "^namespace:" $(GALAXY_YML) | sed 's/namespace: *//g')
COLLECTION_NAME := $(shell grep -E "^name:" $(GALAXY_YML) | sed 's/name: *//g')
COLLECTION_VERSION := $(shell grep -E "^version:" $(GALAXY_YML) | sed 's/version: *//g')
COLLECTION_TARBALL := $(COLLECTION_NAMESPACE)-$(COLLECTION_NAME)-$(COLLECTION_VERSION).tar.gz
else
COLLECTION_TARBALL := *.tar.gz
endif
# Define phony targets (targets that don't create files)
.PHONY: all binaries clean-binaries help check-root tests test-pytest test-ansible-sanity build clean-build \
create-venv clean-venv check-python-version
# Makes `make` less verbose :)
ifndef VERBOSE
MAKEFLAGS += --no-print-directory
endif
# Validate collection root directory
check-root:
$(call verify_collection_root)
# Default target
all: binaries
# Help target
help:
@echo "Available targets:"
@echo " help - Display this help message"
@echo " binaries - Build binaries using container"
@echo " clean-binaries - Remove all built binaries from plugins/modules/"
@echo " build - Build the collection using ansible-galaxy"
@echo " clean-build - Remove the built collection tarball"
@echo " test-ansible-lint - Launch ansible-lint test"
@echo " test-ansible-sanity - Launch ansible-sanity tests"
@echo " test-pytest - Launch pytest test"
@echo " tests - Launch all tests"
@echo " create-venv - Create the virtualenv environment"
@echo " clean-venv - Remove the virtualenv environment"
@echo " all - Same as binaries (default target)"
@echo ""
@echo "Customizable variables:"
@echo " CONTAINER_ENGINE - Container runtime (default: $(CONTAINER_ENGINE))"
@echo " CONTAINER_IMAGE - Container image (default: $(CONTAINER_IMAGE))"
@echo " BUILD_SCRIPT - Path to build script in container (default: $(BUILD_SCRIPT))"
@echo " SECURITY_OPT - Security options (based on SELinux status)"
@echo " Current value: $(SECURITY_OPT)"
@echo ""
@echo "Collection root: $(COLLECTION_ROOT)"
@echo "Modules directory: $(MODULES_DIR)"
@echo "SELinux status: $(if $(SELINUX_ENFORCING:yes=),Disabled or Permissive,Enforcing)"
@echo ""
@echo "Collection information:"
@if [ "$(COLLECTION_TARBALL)" != "*.tar.gz" ]; then \
echo " Namespace: $(COLLECTION_NAMESPACE)"; \
echo " Name: $(COLLECTION_NAME)"; \
echo " Version: $(COLLECTION_VERSION)"; \
echo " Tarball: $(COLLECTION_TARBALL)"; \
else \
echo " (galaxy.yml not found or incomplete)"; \
fi
# Target to build binaries using container
binaries: check-root clean-binaries
$(CONTAINER_ENGINE) run --rm -v $(MOUNT_PATH) $(SECURITY_OPT) $(CONTAINER_IMAGE) $(BUILD_SCRIPT)
# Target to clean built binaries
clean-binaries: check-root
@# Check if modules directory exists
@if [ ! -d "$(MODULES_DIR)" ]; then \
echo "*** Error: $(MODULES_DIR) directory not found. ***"; \
exit 1; \
fi
@# Count files that would be deleted
@files_to_delete=$$(find $(MODULES_DIR) -type f ! -name "*.py" -a ! -name "*.go" | wc -l); \
if [ $$files_to_delete -eq 0 ]; then \
echo "*** No binary files found to delete in $(MODULES_DIR) ***"; \
else \
echo "*** Found $$files_to_delete files to delete ***"; \
echo "*** Removing binary files from $(MODULES_DIR) ... ***"; \
find $(MODULES_DIR) -type f ! -name "*.go" -a ! -name "*.py" -delete; \
echo "*** Cleanup complete. ***"; \
fi
# Target to build the collection
build: check-root clean-build clean-binaries binaries
@echo "*** Building Ansible collection...***"
@ansible-galaxy collection build
@echo "*** Built collection: $(COLLECTION_TARBALL) ***"
# Target to build the collection for production (without teardown tasks)
build-prod: check-root clean-build clean-binaries clean-build-prod binaries
@echo "*** Building Ansible collection for production...***"
@echo "*** Remove AEE and scripts from the build...***"
rm -rf $(COLLECTION_ROOT)/aee
rm -rf $(COLLECTION_ROOT)/scripts
@ANSIBLE_GALAXY_DISABLE_GIT_CHECKSUM=1 ansible-galaxy collection build
@echo "*** Built collection: $(COLLECTION_TARBALL) ***"
# Target to clean teardown tasks from the collection for production build
clean-build-prod:
@echo "*** Cleaning the teardown tasks as they are not needed in production ***"
sed -i '22,$$d' $(COLLECTION_ROOT)/roles/import_workloads/tasks/main.yml
truncate -s -1 $(COLLECTION_ROOT)/roles/import_workloads/tasks/main.yml
rm -f $(COLLECTION_ROOT)/roles/import_workloads/tasks/teardown.yml
sed -i '/plugins\/modules\/delete_/d' tests/sanity/ignore-2.*.txt
sed -i '/plugins\/modules\/src/d' tests/sanity/ignore-2.*.txt
@echo "*** Remove teardown modules before building...***"
rm -f $(MODULES_DIR)/delete_*
rm -rf $(MODULES_DIR)/src/delete_*
# Target to clean the built collection
clean-build:
@echo "*** Cleaning built collection...***"
@if [ -n "$(COLLECTION_TARBALL)" ] && [ "$(COLLECTION_TARBALL)" != "*.tar.gz" ]; then \
if [ -f "$(COLLECTION_TARBALL)" ]; then \
echo "*** Removing $(COLLECTION_TARBALL) ***"; \
rm -f "$(COLLECTION_TARBALL)"; \
else \
echo "*** Collection tarball $(COLLECTION_TARBALL) not found ***"; \
fi; \
else \
echo "*** Removing all collection tarballs ***"; \
rm -f *.tar.gz; \
fi
check-python-version:
@echo "*** Check if Python $(PYTHON_VERSION) is available ***"
@if [[ ! -x $$(command -v python$(PYTHON_VERSION)) ]]; then \
echo "*** Installing Python $(PYTHON_VERSION) ***"; \
sudo dnf -y install python$(PYTHON_VERSION)-devel 2>/dev/null || \
echo "package python$(PYTHON_VERSION) is unavailable"; exit 1; \
else \
echo "*** Python $(PYTHON_VERSION) is already available ***"; \
fi
create-venv: clean-venv check-python-version
@echo "*** Creating venv for Python $(PYTHON_VERSION)... ***"
ifeq ($(PYTHON_VERSION),2.7)
@echo "*** Using virtualenv for Python 2.7 ***"
@python2.7 -m pip install --upgrade pip setuptools virtualenv
@python2.7 -m virtualenv $(VENV_DIR)
else
@echo "*** Using built-in venv for Python $(PYTHON_VERSION) ***"
@python$(PYTHON_VERSION) -m venv $(VENV_DIR)
endif
clean-venv:
@if [ -d "$(VENV_DIR)" ]; then \
echo "*** Removing virtual environment at $(VENV_DIR) ***" && \
rm -fr "$(VENV_DIR)"; \
fi
install:
@echo "*** Installing dependencies... ***"
@$(MAKE) create-venv && \
source $(VENV_DIR)/bin/activate && \
pip install -q --upgrade pip && \
pip install -q -r requirements.txt && \
$(MAKE) build
@echo "*** Dependencies installed successfully ***"
ansible-galaxy collection install $(COLLECTION_TARBALL) --force-with-deps
test-pytest:
@$(MAKE) create-venv && \
source $(VENV_DIR)/bin/activate && \
pip install -q --upgrade pip && \
pip install -q -r requirements-tests.txt && \
pytest -q && \
deactivate && \
$(MAKE) clean-venv
test-ansible-lint:
@$(MAKE) create-venv && \
source $(VENV_DIR)/bin/activate && \
pip install -q --upgrade pip && \
pip install -q -r requirements.txt && \
echo "*** Launching ansible-lint ***" && \
ansible-lint && \
deactivate && \
$(MAKE) clean-venv
test-ansible-sanity:
@$(MAKE) create-venv && \
source $(VENV_DIR)/bin/activate && \
pip install -q --upgrade pip && \
pip install -q -r requirements.txt && \
export TMPDIR="$$(mktemp -d)" && \
export ANSIBLE_COLLECTIONS_PATH="$$TMPDIR/ansible_collections/" && \
echo "*** Using temporary collections path: $$ANSIBLE_COLLECTIONS_PATH ***" && \
$(MAKE) build && \
echo "*** Installing collection dependencies... ***" && \
ansible-galaxy collection install $(COLLECTION_TARBALL) --force-with-deps --collections-path "$$ANSIBLE_COLLECTIONS_PATH" && \
cd "$$ANSIBLE_COLLECTIONS_PATH/$(COLLECTION_NAMESPACE)/$(COLLECTION_NAME)" && \
echo "*** Running Ansible sanity tests...***" && \
ansible-test sanity --test compile \
--exclude aee/ \
--exclude scripts/ \
--exclude plugins/modules/best_match_flavor \
--exclude plugins/modules/create_network_port \
--exclude plugins/modules/create_server \
--exclude plugins/modules/import_flavor \
--exclude plugins/modules/create_heat_stack \
--exclude plugins/modules/generate_heat_template \
--exclude plugins/modules/delete_flavor \
--exclude plugins/modules/delete_port \
--exclude plugins/modules/delete_server \
--exclude plugins/modules/delete_volume \
--exclude plugins/modules/flavor_info \
--exclude plugins/modules/migrate \
--exclude plugins/modules/volume_info \
--exclude plugins/modules/volume_metadata_info && \
cd $(COLLECTION_ROOT) && \
echo "*** Sanity tests completed successfully ***" && \
rm -fr $$TMPDIR && \
deactivate
@$(MAKE) clean-venv
integration-test:
@$(MAKE) create-venv && \
source $(VENV_DIR)/bin/activate && \
pip install -q --upgrade pip && \
pip install -q -r requirements.txt && \
export TMPDIR="$$(mktemp -d)" && \
export ANSIBLE_COLLECTIONS_PATH="$$TMPDIR/ansible_collections/" && \
echo "*** Using temporary collections path: $$ANSIBLE_COLLECTIONS_PATH ***" && \
$(MAKE) build && \
echo "*** Installing collection dependencies... ***" && \
ansible-galaxy collection install $(COLLECTION_TARBALL) --force-with-deps --collections-path "$$ANSIBLE_COLLECTIONS_PATH" && \
echo "*** Running prelude (clone + start gostack)... ***" && \
$(COLLECTION_ROOT)/tests/integration/scripts/prelude.sh && \
echo "*** Running integration tests... ***" && \
(trap '$(COLLECTION_ROOT)/tests/integration/scripts/cleanup.sh' EXIT; \
for test in $(COLLECTION_ROOT)/tests/integration/test_*.yml; do \
echo "*** Running integration test: $$test ***"; \
ansible-playbook -i $(COLLECTION_ROOT)/localhost_inventory.yml $$test || exit 1; \
done) && \
echo "*** Integration tests completed successfully ***" && \
rm -rf $$TMPDIR && \
deactivate
@$(MAKE) clean-venv
test-golangci-lint: check-root
@echo "*** Running golangci-lint in container ***"
$(CONTAINER_ENGINE) run --rm -t \
-v $(MOUNT_PATH) \
-w /code \
$(SECURITY_OPT) \
golangci/golangci-lint:v2.5.0 \
golangci-lint run --timeout 5m
tests: test-pytest test-ansible-sanity test-ansible-lint test-golangci-lint