[Git][root/services/setup-hosts][main] feat: add custom port support, always create users.yml

Fernando K pushed to branch main at Root / Serviços / Setup Hosts Commits: ad015921 by Fernando Monteiro Kiotheka at 2025-08-08T14:44:42-03:00 feat: add custom port support, always create users.yml - - - - - 3 changed files: - all_inventory.py - configure_hosts.py - env_inventory.py Changes: ===================================== all_inventory.py ===================================== @@ -1,6 +1,7 @@ from pathlib import Path import os import requests +import yaml NETBOX_URL = os.environ['NETBOX_URL'] NETBOX_TOKEN = os.environ['NETBOX_TOKEN'] @@ -23,10 +24,24 @@ for vm in response['data']['device_list']: if primary_ip: names.add(primary_ip['dns_name']) SCRIPT_DIR = Path(__file__).resolve().parent +HOSTS_CONF = SCRIPT_DIR / 'hosts-conf' +HOSTS_DIR = HOSTS_CONF / 'hosts' -configured_hosts = set(d.name for d in (SCRIPT_DIR / 'hosts-conf' / 'hosts').iterdir()) +configured_hosts = set(d.name for d in HOSTS_DIR.iterdir()) + +with open(HOSTS_CONF / 'defaults' / 'config.yml') as f: + default_config = yaml.safe_load(f) + +def get_port_for_host(name): + config_file = HOSTS_DIR / name / 'config.yml' + if config_file.exists(): + with open(config_file) as f: + host_config = yaml.safe_load(f) + if 'port' in host_config: + return host_config['port'] + return default_config['port'] hosts = [ - (host, { 'ssh_config_file': 'ssh_config' }) + (host, { 'ssh_config_file': 'ssh_config', 'ssh_port': get_port_for_host(host) }) for host in (names & configured_hosts) ] ===================================== configure_hosts.py ===================================== @@ -33,7 +33,7 @@ if host_config_path.exists(): config = dict() for section, defaults in default_config.items(): config[section] = defaults - if section in host_config: + if section in host_config and isinstance(host_config[section], dict): for key, value in host_config[section].items(): if key not in config[section]: raise Exception(f'[{host.name}] invalid key in config: {section}.{key}') @@ -176,7 +176,7 @@ if ('users/etc' in TAGS or 'all' in TAGS): with open(users_path, 'w') as f: yaml.dump({ 'users': users_new_config }, f) - if changed: + if changed or not users_path.exists(): python.call( name="Replace users.yml in the repository", function=replace_users, ===================================== env_inventory.py ===================================== @@ -1,8 +1,9 @@ import os +from all_inventory import get_port_for_host HOSTS = os.environ['HOSTS'].replace(',', ' ').split() hosts = [ - (host, { 'ssh_config_file': 'ssh_config' }) + (host, { 'ssh_config_file': 'ssh_config', 'ssh_port': get_port_for_host(host) }) for host in HOSTS ] View it on GitLab: https://gitlab.c3sl.ufpr.br/root/services/setup-hosts/-/commit/ad01592105a86... -- View it on GitLab: https://gitlab.c3sl.ufpr.br/root/services/setup-hosts/-/commit/ad01592105a86... You're receiving this email because of your account on gitlab.c3sl.ufpr.br.
participantes (1)
-
Fernando K (@fmkiotheka)