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 | |
| 36 | args = nbhelper.parse_cli_args(extra_args) |
| 37 | nbh = nbhelper.NBHelper(args) |
| 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() |
| 47 | |
| 48 | for prefix in nbh.all_prefixes(): |
| 49 | |
| 50 | nbhelper.NBDNSForwardZone.get_fwd_zone(prefix) |
| 51 | |
| 52 | dns_reverse_zones.add_prefix(prefix) |
| 53 | |
| 54 | dhcpd_subnet = nbhelper.NBDHCPSubnet(prefix) |
| 55 | |
| 56 | dhcpd_if = dhcpd_subnet.dhcpd_interface |
| 57 | |
| 58 | if dhcpd_if and dhcpd_if not in dhcpd_interfaces: |
| 59 | dhcpd_interfaces.append(dhcpd_if) |
| 60 | |
| 61 | dhcpd_subnets.append(dhcpd_subnet) |
| 62 | |
| 63 | # yaml_out["devices"] = nbhelper.NBDevice.all_devs() |
| 64 | yaml_out["dns_forward_zones"] = nbhelper.NBDNSForwardZone.all_fwd_zones() |
| 65 | yaml_out["dns_reverse_zones"] = dns_reverse_zones |
| 66 | yaml_out["dhcpd_subnets"] = dhcpd_subnets |
| 67 | yaml_out["dhcpd_interfaces"] = dhcpd_interfaces |
| 68 | |
| 69 | print(yaml.safe_dump(yaml_out, indent=2)) |