blob: 2faf9defabe31c842463b6bc0d9b7969a2b6e807 [file] [log] [blame]
Zack Williams2aeb3ef2021-06-11 17:10:36 -07001#!/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
11from __future__ import absolute_import
12
13import argparse
14import nbhelper
15import json
16import os
17import pprint
18
19from ruamel import yaml
20
21# main function that calls other functions
22if __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 Chen55a86822021-07-08 14:34:59 +080036 args = nbhelper.initialize(extra_args)
37 tenant = nbhelper.NBTenant()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070038
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 Chen55a86822021-07-08 14:34:59 +080047 for prefix in tenant.get_prefixes().values():
Zack Williams2aeb3ef2021-06-11 17:10:36 -070048
49 nbhelper.NBDNSForwardZone.get_fwd_zone(prefix)
Zack Williams2aeb3ef2021-06-11 17:10:36 -070050 dns_reverse_zones.add_prefix(prefix)
Zack Williams2aeb3ef2021-06-11 17:10:36 -070051 dhcpd_subnet = nbhelper.NBDHCPSubnet(prefix)
Zack Williams2aeb3ef2021-06-11 17:10:36 -070052 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 Chen55a86822021-07-08 14:34:59 +080059 # yaml_out["devices"] = nbhelper.NBDevice.all_devs()
60 yaml_out["netprep_netplan"] = tenant.generate_netplan()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070061 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))