Fernando K pushed to branch main at Root / Serviços / Setup Hosts

Commits:

3 changed files:

Changes:

  • all_inventory.py
    1 1
     from pathlib import Path
    
    2 2
     import os
    
    3 3
     import requests
    
    4
    +import yaml
    
    4 5
     
    
    5 6
     NETBOX_URL = os.environ['NETBOX_URL']
    
    6 7
     NETBOX_TOKEN = os.environ['NETBOX_TOKEN']
    
    ... ... @@ -23,10 +24,24 @@ for vm in response['data']['device_list']:
    23 24
         if primary_ip: names.add(primary_ip['dns_name'])
    
    24 25
     
    
    25 26
     SCRIPT_DIR = Path(__file__).resolve().parent
    
    27
    +HOSTS_CONF = SCRIPT_DIR / 'hosts-conf'
    
    28
    +HOSTS_DIR = HOSTS_CONF / 'hosts'
    
    26 29
     
    
    27
    -configured_hosts = set(d.name for d in (SCRIPT_DIR / 'hosts-conf' / 'hosts').iterdir())
    
    30
    +configured_hosts = set(d.name for d in HOSTS_DIR.iterdir())
    
    31
    +
    
    32
    +with open(HOSTS_CONF / 'defaults' / 'config.yml') as f:
    
    33
    +    default_config = yaml.safe_load(f)
    
    34
    +
    
    35
    +def get_port_for_host(name):
    
    36
    +    config_file = HOSTS_DIR / name / 'config.yml'
    
    37
    +    if config_file.exists():
    
    38
    +        with open(config_file) as f:
    
    39
    +            host_config = yaml.safe_load(f)
    
    40
    +        if 'port' in host_config:
    
    41
    +            return host_config['port']
    
    42
    +    return default_config['port']
    
    28 43
     
    
    29 44
     hosts = [
    
    30
    -    (host, { 'ssh_config_file': 'ssh_config' })
    
    45
    +    (host, { 'ssh_config_file': 'ssh_config', 'ssh_port': get_port_for_host(host) })
    
    31 46
         for host in (names & configured_hosts)
    
    32 47
     ]

  • configure_hosts.py
    ... ... @@ -33,7 +33,7 @@ if host_config_path.exists():
    33 33
     config = dict()
    
    34 34
     for section, defaults in default_config.items():
    
    35 35
         config[section] = defaults
    
    36
    -    if section in host_config:
    
    36
    +    if section in host_config and isinstance(host_config[section], dict):
    
    37 37
             for key, value in host_config[section].items():
    
    38 38
                 if key not in config[section]:
    
    39 39
                     raise Exception(f'[{host.name}] invalid key in config: {section}.{key}')
    
    ... ... @@ -176,7 +176,7 @@ if ('users/etc' in TAGS or 'all' in TAGS):
    176 176
             with open(users_path, 'w') as f:
    
    177 177
                 yaml.dump({ 'users': users_new_config }, f)
    
    178 178
     
    
    179
    -    if changed:
    
    179
    +    if changed or not users_path.exists():
    
    180 180
             python.call(
    
    181 181
                 name="Replace users.yml in the repository",
    
    182 182
                 function=replace_users,
    

  • env_inventory.py
    1 1
     import os
    
    2
    +from all_inventory import get_port_for_host
    
    2 3
     
    
    3 4
     HOSTS = os.environ['HOSTS'].replace(',', ' ').split()
    
    4 5
     
    
    5 6
     hosts = [
    
    6
    -    (host, { 'ssh_config_file': 'ssh_config' })
    
    7
    +    (host, { 'ssh_config_file': 'ssh_config', 'ssh_port': get_port_for_host(host) })
    
    7 8
         for host in HOSTS
    
    8 9
     ]