-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMakefile
More file actions
208 lines (185 loc) · 9.79 KB
/
Makefile
File metadata and controls
208 lines (185 loc) · 9.79 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
# Set PROJECT_DIR to the CI-provided project directory if available; otherwise fallback to the current directory.
ifndef CI_PROJECT_DIR
ifndef GITHUB_WORKSPACE
PROJECT_DIR := $(shell pwd)
else
PROJECT_DIR := $(GITHUB_WORKSPACE)
endif
else
PROJECT_DIR := $(CI_PROJECT_DIR)
endif
.DEFAULT_GOAL := help
.PHONY: format-code format-all run-megalinter ansible-build test-local test-quick validate-docker validate-renovate validate-renovate-deps tests help structure-test security-test performance-test integration-test unit-test upgrade-test comprehensive-test clean clean-all release-check debug-container
format-code: ## Format code files using Prettier via Docker.
@docker run --rm --name prettier -v $(PROJECT_DIR):$(PROJECT_DIR) -w /$(PROJECT_DIR) node:24-alpine3.20 npx prettier . --write
format-all: format-code ## Run both format-code and format-eclint.
@echo "Formatting completed."
run-megalinter: ## Run Megalinter locally.
@docker run --rm --name megalint -v $(PROJECT_DIR):/tmp/lint busybox:1.37.0 rm -rf /tmp/lint/megalinter-reports
@docker run --rm --name megalint -v $(PROJECT_DIR):/tmp/lint oxsecurity/megalinter:v9.3.0
ansible-build: ## Build the Ansible Docker image with optimizations
@echo "Building Ansible container..."
@docker build \
--build-arg BUILD_DATE=$$(date -I) \
--build-arg ANSIBLE_VERSION=$$(grep '^ansible-core==' requirements.txt | cut -d'=' -f3) \
--build-arg VCS_REF=$$(git rev-parse --short HEAD 2>/dev/null || echo "unknown") \
--build-arg TARGETPLATFORM=$$(docker version --format '{{.Server.Arch}}') \
-t ansible:latest \
-f Dockerfile .
@echo "Build completed successfully"
test-quick: ansible-build ## Run quick basic tests
@echo "Running quick validation tests..."
@docker run --rm ansible:latest ansible --version | head -1
@docker run --rm ansible:latest ansible-playbook --version | head -1
@docker run --rm ansible:latest /pipx/venvs/ansible/bin/python3 -c "import ansible_mitogen; print('Mitogen available')"
@echo "Quick tests passed"
test-local: ansible-build ## Run comprehensive local container tests
@echo "=== Running Local Container Tests ==="
@echo "1. Basic version checks..."
@docker run --rm ansible:latest ansible --version | head -1
@docker run --rm ansible:latest python --version
@docker run --rm ansible:latest kubectl version --client
@echo ""
@echo "2. YAML inventory configuration..."
@docker run --rm ansible:latest test -f /etc/ansible/hosts.yml && echo "YAML inventory file exists"
@docker run --rm ansible:latest ansible-inventory --list --yaml | head -10
@echo ""
@echo "3. Mitogen integration..."
@docker run --rm ansible:latest test -d /pipx/venvs/ansible/lib/python3.12/site-packages/ansible_mitogen && echo "Mitogen directory exists"
@docker run --rm ansible:latest /pipx/venvs/ansible/bin/python3 -c "import ansible_mitogen; print('Mitogen successfully imported')"
@docker run --rm ansible:latest grep "strategy = mitogen_linear" /etc/ansible/ansible.cfg && echo "Mitogen strategy configured"
@echo ""
@echo "4. Container health check..."
@docker run --rm ansible:latest ansible --version > /dev/null && echo "Healthcheck command successful"
@echo ""
@echo "=== All local tests passed successfully! ==="
validate-docker: ## Validate Dockerfile with hadolint
@docker run --rm -i hadolint/hadolint:v2.14.0 < Dockerfile
validate-renovate: ## Validate renovate configuration
@docker run --rm -v $(PROJECT_DIR)/.github:/usr/src/app node:22.15.0-alpine3.19 npx renovate-config-validator /usr/src/app/renovate.json
validate-renovate-deps: ## Show detected dependencies in Renovate
@docker run --rm -t \
-e LOG_LEVEL="debug" \
-e RENOVATE_PLATFORM=local \
-v "$(PROJECT_DIR):/usr/src/app" \
renovate/renovate:40.1 \
--dry-run=full
structure-test: ansible-build ## Run advanced container structure tests
@echo "Running advanced container structure tests..."
@docker run --rm \
-v "$(PROJECT_DIR)/tests/structure/container-test.yml":/structure-test.yml \
-v /var/run/docker.sock:/var/run/docker.sock \
ghcr.io/googlecontainertools/container-structure-test:1.19.3 \
test --image=ansible:latest --config=/structure-test.yml
security-test: ansible-build ## Run comprehensive security checks
@echo "=== Running Security Tests ==="
@mkdir -p $(PROJECT_DIR)/test-results
@echo "1. Container hardening checks..."
@docker run --rm -v "$(PROJECT_DIR)/tests/security:/tests" ansible:latest bash -c "bash /tests/container-hardening.sh" || (echo "Security check warnings found"; exit 0)
@echo ""
@echo "2. Trivy vulnerability scan..."
@docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$(PROJECT_DIR)/test-results:/results" aquasec/trivy:0.68.2 image --exit-code 0 --severity HIGH,CRITICAL --format table -o /results/trivy-results.txt ansible:latest || (echo "Trivy scan found issues - check test-results/trivy-results.txt"; exit 0)
@echo ""
@echo "3. User permissions check..."
@docker run --rm ansible:latest id | grep "uid=1000(ansible) gid=1000(ansible)" && echo "Non-root user correctly configured"
@echo "=== Security tests completed - review results in test-results/ ==="
performance-test: ansible-build ## Run performance benchmarks for the container
@echo "Starting performance tests..."
@mkdir -p $(PROJECT_DIR)/test-results
@docker run --rm --name ansible-perf-test \
-v $(PROJECT_DIR)/tests/performance:/tests \
-v $(PROJECT_DIR)/test-results:/results \
ansible:latest sh /tests/run-performance-tests.sh
@echo "Performance tests completed. Results in $(PROJECT_DIR)/test-results/"
integration-test: ansible-build ## Run integration tests with other Docker services
@echo "Starting integration tests..."
@docker network create ansible-test-network || true
@export POSTGRES_PASSWORD=test_secure_password && \
docker compose -f tests/integration/docker-compose.yml up -d
@docker run --rm --network ansible-test-network \
-v $(PROJECT_DIR)/tests/integration:/tests \
-v $(PROJECT_DIR)/test-results:/results \
ansible:latest bash /tests/run-integration-tests.sh
# @docker compose -f tests/integration/docker-compose.yml down
# @docker network rm ansible-test-network || true
@echo "Integration tests completed."
unit-test: ansible-build ## Run unit tests with Python
@echo "Starting Python unit tests..."
@python3 -m pip install pytest pytest-cov
@ANSIBLE_IMAGE=ansible:latest python3 -m pytest tests/unit/test_ansible_container.py -v
upgrade-test: ansible-build ## Test the container's ability to upgrade
@echo "Testing upgrade capability..."
@bash $(PROJECT_DIR)/tests/upgrade/test-upgrade.sh || (echo "Some upgrade tests failed, but continuing"; exit 0)
comprehensive-test: test-quick structure-test security-test performance-test integration-test unit-test upgrade-test ## Run all tests in order
@echo ""
@echo "=== ALL COMPREHENSIVE TESTS COMPLETED SUCCESSFULLY ==="
@echo "- Quick validation tests"
@echo "- Container structure tests"
@echo "- Security scans and hardening"
@echo "- Performance benchmarks"
@echo "- Integration tests"
@echo "- Python unit tests"
@echo "- Upgrade capability tests"
@echo ""
@echo "Container is ready for production deployment"
release-check: comprehensive-test ## Comprehensive release readiness check
@echo ""
@echo "=== RELEASE READINESS CHECK ==="
@ansible_version=$$(grep '^ansible-core==' requirements.txt | cut -d'=' -f3); \
echo "Current Ansible version: $$ansible_version"; \
echo "Checking CHANGELOG.md for version entry..."; \
if grep -q "$$ansible_version" CHANGELOG.md; then \
echo "CHANGELOG.md contains entry for $$ansible_version"; \
else \
echo "CHANGELOG.md missing entry for $$ansible_version"; \
echo "Please add changelog entry before release!"; \
exit 1; \
fi; \
echo "Checking git status..."; \
if [ -n "$$(git status --porcelain 2>/dev/null)" ]; then \
echo "Warning: Uncommitted changes detected"; \
git status --short; \
else \
echo "Git working directory clean"; \
fi; \
echo ""; \
echo "RELEASE CHECK SUMMARY:"; \
echo "- All tests passed"; \
echo "- Changelog updated"; \
echo "- Ready for release"; \
echo ""; \
echo "To create release: git tag v$$ansible_version && git push origin v$$ansible_version"
debug-container: ansible-build ## Debug container with interactive shell
@echo "Starting debug session for ansible:latest..."
@echo "Available commands: ansible, ansible-playbook, kubectl, helm, kustomize, jq"
@echo "Config files: /etc/ansible/ansible.cfg, /etc/ansible/hosts.yml"
@echo "Virtual env: /pipx/venvs/ansible/bin/python3"
@docker run --rm -it -v $(PROJECT_DIR):/workspace -w /workspace ansible:latest bash
clean: ## Clean up local Docker images and test artifacts
@echo "Cleaning up local artifacts..."
@docker rmi ansible:latest 2>/dev/null || echo "No ansible:latest image to remove"
@rm -rf $(PROJECT_DIR)/test-results
@docker system prune -f
@echo "Cleanup completed"
clean-all: clean ## Clean up everything including build cache
@echo "Deep cleaning Docker build cache..."
@docker builder prune -f
@docker volume prune -f
@echo "Deep cleanup completed"
help: ## Show an overview of available targets with categories
@echo "Ansible Container Makefile"
@echo ""
@echo "BUILD TARGETS:"
@grep -E '^[a-zA-Z_-]+:.*?## .*[Bb]uild' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
@echo ""
@echo "TEST TARGETS:"
@grep -E '^[a-zA-Z_-]+:.*?## .*([Tt]est|[Rr]un|[Cc]heck)' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
@echo ""
@echo "UTILITY TARGETS:"
@grep -E '^[a-zA-Z_-]+:.*?## .*([Cc]lean|[Ff]ormat|[Vv]alidate|[Dd]ebug)' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
@echo ""
@echo "RELEASE TARGETS:"
@grep -E '^[a-zA-Z_-]+:.*?## .*([Rr]elease)' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " %-20s %s\n", $$1, $$2}'
@echo ""
@echo "Quick start: make test-quick"
@echo "Full test: make comprehensive-test"