From 57328922af18fda6d1140a219dbe26a9f04bf72e Mon Sep 17 00:00:00 2001 From: rebel onion <87634197+rebelonion@users.noreply.github.com> Date: Sat, 21 Jun 2025 08:32:12 -0500 Subject: [PATCH] add commit check --- .pre-commit-config.yaml | 10 +++++ scripts/check-port-conflicts.py | 70 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 .pre-commit-config.yaml create mode 100755 scripts/check-port-conflicts.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..8ca0240 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + - repo: local + hooks: + - id: check-port-conflicts + name: Check for port conflicts within docker groups + entry: scripts/check-port-conflicts.py + language: python3 + files: '.*compose\.yaml$' + pass_filenames: false + always_run: true \ No newline at end of file diff --git a/scripts/check-port-conflicts.py b/scripts/check-port-conflicts.py new file mode 100755 index 0000000..d9fd4ac --- /dev/null +++ b/scripts/check-port-conflicts.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import os +import re +import sys +from pathlib import Path + +def extract_ports_from_compose(file_path): + """Extract host ports from a docker-compose file using regex.""" + ports = [] + try: + with open(file_path, 'r') as f: + content = f.read() + + # Find ports section and extract port mappings + # Match patterns like: - "8080:8080", - 8080:8080, - "127.0.0.1:8080:8080" + port_pattern = r'^\s*-\s*["\']?(?:\d+\.\d+\.\d+\.\d+:)?(\d+):\d+["\']?' + + for line in content.split('\n'): + match = re.match(port_pattern, line) + if match: + host_port = int(match.group(1)) + ports.append(host_port) + + except Exception as e: + print(f"Error reading {file_path}: {e}") + + return ports + +def check_port_conflicts(): + """Check for port conflicts within each docker group.""" + base_dir = Path('.') + docker_groups = ['main-docker', 'auth-docker', 'admin-docker', 'komodo-periphery'] + + conflicts_found = False + + for group in docker_groups: + group_path = base_dir / group + if not group_path.exists(): + continue + + group_ports = {} # port -> [service_files] + + # Find all compose files in this group + for compose_file in group_path.rglob('compose.yaml'): + service_name = compose_file.parent.name + ports = extract_ports_from_compose(compose_file) + + for port in ports: + if port not in group_ports: + group_ports[port] = [] + group_ports[port].append(f"{group}/{service_name}") + + # Check for conflicts within this group + for port, services in group_ports.items(): + if len(services) > 1: + print(f"❌ Port conflict in {group}: Port {port} used by {', '.join(services)}") + conflicts_found = True + + if group_ports and not any(len(services) > 1 for services in group_ports.values()): + print(f"✅ No port conflicts in {group}") + + return conflicts_found + +if __name__ == "__main__": + if check_port_conflicts(): + sys.exit(1) + else: + print("🎉 No port conflicts detected!") + sys.exit(0) \ No newline at end of file