blob: fc49606f060a0e48547508e199498af9368472c6 [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
Wei-Yu Chenc517f552021-07-29 21:16:45 +080038 base_yaml = yaml.safe_load(args.base_config.read())
Zack Williams2aeb3ef2021-06-11 17:10:36 -070039
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 Chenc517f552021-07-29 21:16:45 +080057 for device in tenant.get_devices():
58 output_yaml = base_yaml.copy()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070059
Wei-Yu Chenc517f552021-07-29 21:16:45 +080060 if (
61 isinstance(device, nbhelper.NBDevice)
62 and device.data.device_role.slug == "router"
63 ) or (
64 isinstance(device, nbhelper.NBVirtualMachine)
65 and device.data.role.slug == "router"
66 ):
67 output_yaml["dns_forward_zones"] = nbhelper.NBDNSForwardZone.all_fwd_zones()
68 output_yaml["dns_reverse_zones"] = dns_reverse_zones
69 output_yaml["dhcpd_subnets"] = dhcpd_subnets
70 output_yaml["dhcpd_interfaces"] = dhcpd_interfaces
71 output_yaml["netprep_nftables"] = device.generate_nftables()
72 output_yaml.update(device.generate_extra_config())
Zack Williams5d66d182021-07-27 23:08:28 -070073
Wei-Yu Chenc517f552021-07-29 21:16:45 +080074 output_yaml["netprep_netplan"] = device.generate_netplan()
75
76 with open("inventory/host_vars/%s.yaml" % device.name, "w") as f:
77 f.write(yaml.safe_dump(output_yaml, indent=2))