add commit check
This commit is contained in:
parent
f09f86232d
commit
57328922af
2 changed files with 80 additions and 0 deletions
10
.pre-commit-config.yaml
Normal file
10
.pre-commit-config.yaml
Normal file
|
@ -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
|
70
scripts/check-port-conflicts.py
Executable file
70
scripts/check-port-conflicts.py
Executable file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue