Optimized deployment script

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-18 11:42:05 +02:00
parent d1982af63d
commit 0f1f40f2e0
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
3 changed files with 27 additions and 15 deletions

View File

@ -41,7 +41,7 @@ dockerignore:
cat .gitignore > .dockerignore cat .gitignore > .dockerignore
echo ".git" >> .dockerignore echo ".git" >> .dockerignore
build: clean dockerignore messy-build: dockerignore
@echo "🔧 Generating users defaults → $(USERS_OUT)" @echo "🔧 Generating users defaults → $(USERS_OUT)"
python3 $(USERS_SCRIPT) \ python3 $(USERS_SCRIPT) \
--roles-dir $(ROLES_DIR) \ --roles-dir $(ROLES_DIR) \
@ -65,14 +65,17 @@ build: clean dockerignore
echo "$$out"; \ echo "$$out"; \
) )
install: build
@echo "⚙️ Install complete."
messy-test: messy-test:
@echo "🧪 Running Python tests…" @echo "🧪 Running Python tests…"
PYTHONPATH=. python -m unittest discover -s tests PYTHONPATH=. python -m unittest discover -s tests
@echo "📑 Checking Ansible syntax…" @echo "📑 Checking Ansible syntax…"
ansible-playbook playbook.yml --syntax-check ansible-playbook playbook.yml --syntax-check
test: build partial-test install: build
@echo "Full test with build terminated." @echo "⚙️ Install complete."
build: clean messy-build
@echo "Full build with cleanup before was executed."
test: build messy-test
@echo "Full test with build before was executed."

View File

@ -1,13 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse import argparse
import os
import shutil import shutil
import sys
import ipaddress import ipaddress
import difflib import difflib
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from ruamel.yaml import YAML from ruamel.yaml import YAML
import sys, os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
from module_utils.entity_name_utils import get_entity_name
# Paths to the group-vars files # Paths to the group-vars files
PORTS_FILE = './group_vars/all/09_ports.yml' PORTS_FILE = './group_vars/all/09_ports.yml'
NETWORKS_FILE = './group_vars/all/10_networks.yml' NETWORKS_FILE = './group_vars/all/10_networks.yml'
@ -65,6 +67,7 @@ def prompt_conflict(dst_file):
def render_templates(src_dir, dst_dir, context): def render_templates(src_dir, dst_dir, context):
env = Environment(loader=FileSystemLoader(src_dir), keep_trailing_newline=True, autoescape=False) env = Environment(loader=FileSystemLoader(src_dir), keep_trailing_newline=True, autoescape=False)
env.filters['bool'] = lambda x: bool(x) env.filters['bool'] = lambda x: bool(x)
env.filters['get_entity_name'] = get_entity_name
for root, _, files in os.walk(src_dir): for root, _, files in os.walk(src_dir):
rel = os.path.relpath(root, src_dir) rel = os.path.relpath(root, src_dir)

View File

@ -15,14 +15,20 @@ def run_ansible_playbook(
verbose=0, verbose=0,
skip_tests=False, skip_tests=False,
skip_validation=False, skip_validation=False,
skip_build=False, # <-- new parameter skip_build=False,
): ):
start_time = datetime.datetime.now() start_time = datetime.datetime.now()
print(f"\n▶️ Script started at: {start_time.isoformat()}\n") print(f"\n▶️ Script started at: {start_time.isoformat()}\n")
if not skip_build: if not skip_build:
print("\n🛠️ Building project (make build)...\n") print("\n🧹 Cleaning up project (make clean)...\n")
subprocess.run(["make", "build"], check=True) subprocess.run(["make", "clean"], check=True)
else:
print("\n⚠️ Skipping build as requested.\n")
if not skip_build:
print("\n🛠️ Building project (make messy-build)...\n")
subprocess.run(["make", "messy-build"], check=True)
else: else:
print("\n⚠️ Skipping build as requested.\n") print("\n⚠️ Skipping build as requested.\n")
@ -50,8 +56,8 @@ def run_ansible_playbook(
print("\n⚠️ Skipping inventory validation as requested.\n") print("\n⚠️ Skipping inventory validation as requested.\n")
if not skip_tests: if not skip_tests:
print("\n🧪 Running tests (make test)...\n") print("\n🧪 Running tests (make messy-test)...\n")
subprocess.run(["make", "test"], check=True) subprocess.run(["make", "messy-test"], check=True)
# Build ansible-playbook command # Build ansible-playbook command
cmd = ["ansible-playbook", "-i", inventory, playbook] cmd = ["ansible-playbook", "-i", inventory, playbook]
@ -140,7 +146,7 @@ def main():
) )
parser.add_argument( parser.add_argument(
"-c", "--cleanup", action="store_true", "-c", "--cleanup", action="store_true",
help="Clean up unused files and outdated configurations after all tasks are complete." help="Clean up unused files and outdated configurations after all tasks are complete. Also cleans up the repository before the deployment procedure."
) )
parser.add_argument( parser.add_argument(
"-d", "--debug", action="store_true", "-d", "--debug", action="store_true",