91 lines
2.8 KiB
Python
Executable File
91 lines
2.8 KiB
Python
Executable File
#!/usr/bin/env python
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
BASE_PATH = Path(__file__).parent
|
|
CONFIG_FILE = BASE_PATH / "config.yaml"
|
|
OUTPUT_FOLDER = BASE_PATH / "output"
|
|
DNSMASQ_CONF = OUTPUT_FOLDER / "etc/dnsmasq.conf"
|
|
ETHERS = OUTPUT_FOLDER / "etc/ethers"
|
|
SUMMARY = OUTPUT_FOLDER / "summary.yaml"
|
|
WHITELIST = OUTPUT_FOLDER / "white.list"
|
|
|
|
shutil.rmtree(OUTPUT_FOLDER)
|
|
DNSMASQ_CONF.parent.mkdir(exist_ok=True, parents=True)
|
|
ETHERS.parent.mkdir(exist_ok=True, parents=True)
|
|
|
|
root_domain = "internal"
|
|
ip_prefix_map = {
|
|
"lab": "10.0.10.",
|
|
"k3s": "10.0.11.",
|
|
"hub": "10.0.12.",
|
|
"cam": "10.0.13.",
|
|
"iot": "10.0.14.",
|
|
"apl": "10.0.15.",
|
|
"router": "10.0.16.",
|
|
"devices": "10.0.0.",
|
|
}
|
|
defaults = {
|
|
'devices': {
|
|
'justMac': True,
|
|
}
|
|
}
|
|
|
|
def cap1_and_join(*args: str) -> str:
|
|
out = ""
|
|
for a in args:
|
|
out += a[0].upper()
|
|
out += a[1:].lower()
|
|
return out
|
|
|
|
with CONFIG_FILE.open('r') as fp:
|
|
settings = yaml.safe_load(fp)
|
|
|
|
mac_addrs = set()
|
|
|
|
with DNSMASQ_CONF.open('w') as dfp, SUMMARY.open('w') as sfp, ETHERS.open('w') as efp:
|
|
for sub_domain, configs in settings['networks'].items():
|
|
default_values = defaults.get(sub_domain, {})
|
|
ip_prefix = ip_prefix_map[sub_domain]
|
|
sfp.write(f'\n{cap1_and_join(sub_domain)}:')
|
|
efp.write(f'#{cap1_and_join(sub_domain)}\n')
|
|
ip_suffix = 0
|
|
seen_nets = set()
|
|
for dvc in configs:
|
|
if dvc.get('id'):
|
|
ip_suffix = dvc.get('id')
|
|
else:
|
|
ip_suffix += 1
|
|
for _ in range(254):
|
|
if ip_suffix in seen_nets:
|
|
ip_suffix += 1
|
|
else:
|
|
break
|
|
seen_nets.add(ip_suffix)
|
|
dvc = {**default_values, **dvc}
|
|
mac = dvc['mac']
|
|
name = dvc['name']
|
|
mac_addrs.add(mac)
|
|
sfp.write(f'\n {cap1_and_join(name)}:')
|
|
ip = f"{ip_prefix}{ip_suffix}"
|
|
host = f"{name}.{sub_domain}.{root_domain}"
|
|
if not dvc.get('justMac', False):
|
|
sfp.write(f'\n ip: "{ip}"')
|
|
sfp.write(f'\n host: "{host}"')
|
|
if not dvc.get('justMac', False):
|
|
efp.write(f'{mac} {ip}\n')
|
|
if domains := dvc.get('domain'):
|
|
for domain in domains.split(','):
|
|
dfp.write(f"address=/{domain}/{ip}\n")
|
|
dfp.write(f"address=/.{domain}/{ip}\n")
|
|
sfp.write(f'\n domain: "{domains}"')
|
|
efp.write(f'{mac} {host}\n')
|
|
for config in settings['general_mappings']:
|
|
ip = config['ip']
|
|
for domain in config['domains']:
|
|
dfp.write(f"address=/{domain}/{ip}\n")
|
|
dfp.write(f"address=/.{domain}/{ip}\n")
|
|
|
|
WHITELIST.write_text('\n'.join(mac_addrs)) |