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 |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 15 | import os |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 16 | |
| 17 | from ruamel import yaml |
| 18 | |
| 19 | # main function that calls other functions |
| 20 | if __name__ == "__main__": |
| 21 | |
| 22 | # this is passed to argparse, key is option name, rest is kwargs |
| 23 | extra_args = { |
| 24 | "base_config": { |
| 25 | "default": os.path.join( |
| 26 | os.path.dirname(os.path.realpath(__file__)), "base_edgeconfig.yaml" |
| 27 | ), |
| 28 | "nargs": "?", |
| 29 | "type": argparse.FileType("r"), |
| 30 | "help": "Config (optional, default: base_edgeconfig.yaml)", |
| 31 | }, |
| 32 | } |
| 33 | |
Wei-Yu Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 34 | args = nbhelper.initialize(extra_args) |
| 35 | tenant = nbhelper.NBTenant() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 36 | |
| 37 | # use base_config for additional items |
| 38 | yaml_out = yaml.safe_load(args.base_config.read()) |
| 39 | |
| 40 | dhcpd_subnets = [] |
| 41 | dhcpd_interfaces = [] |
| 42 | |
| 43 | # reverse zones aggregate across RFC1918 IP prefix |
| 44 | dns_reverse_zones = nbhelper.NBDNSReverseZones() |
Wei-Yu Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 45 | for prefix in tenant.get_prefixes().values(): |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 46 | |
| 47 | nbhelper.NBDNSForwardZone.get_fwd_zone(prefix) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 48 | dns_reverse_zones.add_prefix(prefix) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 49 | dhcpd_subnet = nbhelper.NBDHCPSubnet(prefix) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 50 | dhcpd_if = dhcpd_subnet.dhcpd_interface |
| 51 | |
| 52 | if dhcpd_if and dhcpd_if not in dhcpd_interfaces: |
| 53 | dhcpd_interfaces.append(dhcpd_if) |
| 54 | |
| 55 | dhcpd_subnets.append(dhcpd_subnet) |
| 56 | |
Wei-Yu Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 57 | # yaml_out["devices"] = nbhelper.NBDevice.all_devs() |
| 58 | yaml_out["netprep_netplan"] = tenant.generate_netplan() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 59 | yaml_out["dns_forward_zones"] = nbhelper.NBDNSForwardZone.all_fwd_zones() |
| 60 | yaml_out["dns_reverse_zones"] = dns_reverse_zones |
| 61 | yaml_out["dhcpd_subnets"] = dhcpd_subnets |
| 62 | yaml_out["dhcpd_interfaces"] = dhcpd_interfaces |
| 63 | |
| 64 | print(yaml.safe_dump(yaml_out, indent=2)) |