blob: 2baa9a324fa7d2b82867cadf8fd8bfe630acfb1d [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
Zack Williams2aeb3ef2021-06-11 17:10:36 -070015import os
Zack Williams2aeb3ef2021-06-11 17:10:36 -070016
17from ruamel import yaml
18
19# main function that calls other functions
20if __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 Chen55a86822021-07-08 14:34:59 +080034 args = nbhelper.initialize(extra_args)
35 tenant = nbhelper.NBTenant()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070036
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 Chen55a86822021-07-08 14:34:59 +080045 for prefix in tenant.get_prefixes().values():
Zack Williams2aeb3ef2021-06-11 17:10:36 -070046
47 nbhelper.NBDNSForwardZone.get_fwd_zone(prefix)
Zack Williams2aeb3ef2021-06-11 17:10:36 -070048 dns_reverse_zones.add_prefix(prefix)
Zack Williams2aeb3ef2021-06-11 17:10:36 -070049 dhcpd_subnet = nbhelper.NBDHCPSubnet(prefix)
Zack Williams2aeb3ef2021-06-11 17:10:36 -070050 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 Chen55a86822021-07-08 14:34:59 +080057 # yaml_out["devices"] = nbhelper.NBDevice.all_devs()
58 yaml_out["netprep_netplan"] = tenant.generate_netplan()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070059 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))