Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # SPDX-FileCopyrightText: © 2021 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # edgeconfig.py |
| 7 | # Given a yaml config file (same as ansible inventory for a site), create a |
| 8 | # YAML file consumable by ansible as variables that configures the managmeent |
| 9 | # node for that site |
| 10 | |
| 11 | from __future__ import absolute_import |
| 12 | |
| 13 | import argparse |
| 14 | import nbhelper |
| 15 | import json |
| 16 | import os |
| 17 | import pprint |
| 18 | |
| 19 | from ruamel import yaml |
| 20 | |
| 21 | # main function that calls other functions |
| 22 | if __name__ == "__main__": |
| 23 | |
| 24 | # this is passed to argparse, key is option name, rest is kwargs |
| 25 | extra_args = { |
| 26 | "base_config": { |
| 27 | "default": os.path.join( |
| 28 | os.path.dirname(os.path.realpath(__file__)), "base_edgeconfig.yaml" |
| 29 | ), |
| 30 | "nargs": "?", |
| 31 | "type": argparse.FileType("r"), |
| 32 | "help": "Config (optional, default: base_edgeconfig.yaml)", |
| 33 | }, |
| 34 | } |
| 35 | |
Wei-Yu Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 36 | args = nbhelper.initialize(extra_args) |
| 37 | tenant = nbhelper.NBTenant() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 38 | |
| 39 | # use base_config for additional items |
| 40 | yaml_out = yaml.safe_load(args.base_config.read()) |
| 41 | |
| 42 | dhcpd_subnets = [] |
| 43 | dhcpd_interfaces = [] |
| 44 | |
| 45 | # reverse zones aggregate across RFC1918 IP prefix |
| 46 | dns_reverse_zones = nbhelper.NBDNSReverseZones() |
Wei-Yu Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 47 | for prefix in tenant.get_prefixes().values(): |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 48 | |
| 49 | nbhelper.NBDNSForwardZone.get_fwd_zone(prefix) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 50 | dns_reverse_zones.add_prefix(prefix) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 51 | dhcpd_subnet = nbhelper.NBDHCPSubnet(prefix) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 52 | dhcpd_if = dhcpd_subnet.dhcpd_interface |
| 53 | |
| 54 | if dhcpd_if and dhcpd_if not in dhcpd_interfaces: |
| 55 | dhcpd_interfaces.append(dhcpd_if) |
| 56 | |
| 57 | dhcpd_subnets.append(dhcpd_subnet) |
| 58 | |
Wei-Yu Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 59 | # yaml_out["devices"] = nbhelper.NBDevice.all_devs() |
| 60 | yaml_out["netprep_netplan"] = tenant.generate_netplan() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 61 | yaml_out["dns_forward_zones"] = nbhelper.NBDNSForwardZone.all_fwd_zones() |
| 62 | yaml_out["dns_reverse_zones"] = dns_reverse_zones |
| 63 | yaml_out["dhcpd_subnets"] = dhcpd_subnets |
| 64 | yaml_out["dhcpd_interfaces"] = dhcpd_interfaces |
| 65 | |
| 66 | print(yaml.safe_dump(yaml_out, indent=2)) |