blob: 5a809fbcacb39426e0eb7408ae369ef377ff0518 [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
Zack Williams2aeb3ef2021-06-11 17:10:36 -070014import os
Zack Williams2aeb3ef2021-06-11 17:10:36 -070015
Zack Williamsdac2be42021-08-19 16:14:31 -070016import nbhelper
17
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080018from ruamel.yaml import YAML
Zack Williams2aeb3ef2021-06-11 17:10:36 -070019
20# main function that calls other functions
21if __name__ == "__main__":
22
23 # this is passed to argparse, key is option name, rest is kwargs
24 extra_args = {
25 "base_config": {
26 "default": os.path.join(
27 os.path.dirname(os.path.realpath(__file__)), "base_edgeconfig.yaml"
28 ),
29 "nargs": "?",
30 "type": argparse.FileType("r"),
31 "help": "Config (optional, default: base_edgeconfig.yaml)",
32 },
33 }
34
Wei-Yu Chen55a86822021-07-08 14:34:59 +080035 args = nbhelper.initialize(extra_args)
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080036 tenant = nbhelper.Tenant()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070037
38 # use base_config for additional items
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080039 yaml = YAML(typ="rt")
40 base_yaml = yaml.load(args.base_config.read())
Zack Williams2aeb3ef2021-06-11 17:10:36 -070041
Zack Williams2aeb3ef2021-06-11 17:10:36 -070042 dhcpd_interfaces = []
43
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080044 # TODO
45 # dhcpd_if = dhcpd_subnet.dhcpd_interface
46 dns_forward_zones = nbhelper.service.dnsFowardZoneConfigGenerator()
47 dns_reverse_zones = nbhelper.service.dnsReverseZoneConfigGenerator()
48 dhcpd_subnets = nbhelper.service.dhcpSubnetConfigGenerator()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070049
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080050 for device in tenant.get_devices(device_types=["server", "router", "switch"]):
Wei-Yu Chenc517f552021-07-29 21:16:45 +080051 output_yaml = base_yaml.copy()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070052
Wei-Yu Chenc517f552021-07-29 21:16:45 +080053 if (
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080054 isinstance(device, nbhelper.Device)
Wei-Yu Chenc517f552021-07-29 21:16:45 +080055 and device.data.device_role.slug == "router"
56 ) or (
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080057 isinstance(device, nbhelper.VirtualMachine)
Wei-Yu Chenc517f552021-07-29 21:16:45 +080058 and device.data.role.slug == "router"
59 ):
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080060 output_yaml["dns_forward_zones"] = dns_forward_zones
Wei-Yu Chenc517f552021-07-29 21:16:45 +080061 output_yaml["dns_reverse_zones"] = dns_reverse_zones
62 output_yaml["dhcpd_subnets"] = dhcpd_subnets
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080063 output_yaml["dhcpd_interfaces"] = list(device.internal_interfaces.keys())
Wei-Yu Chenc517f552021-07-29 21:16:45 +080064 output_yaml["netprep_nftables"] = device.generate_nftables()
Wei-Yu Chen9b55d362021-09-22 11:04:31 +080065
66 # If the key exists in generated config, warning with the key name
67 extra_config = device.generate_extra_config()
68 for key in extra_config.keys():
69 if key in output_yaml:
70 nbhelper.utils.logger.warning(
71 "Output YAML Key %s was overwritten", key
72 )
73
74 output_yaml.update(extra_config)
75
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080076 output_yaml = nbhelper.utils.apply_as_router(output_yaml)
Zack Williams5d66d182021-07-27 23:08:28 -070077
Wei-Yu Chenc517f552021-07-29 21:16:45 +080078 output_yaml["netprep_netplan"] = device.generate_netplan()
79
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080080 with open("inventory/host_vars/%s.yaml" % device.fullname, "w") as f:
81 # yaml.compact(seq_seq=False, seq_map=False)
82 yaml.dump(output_yaml, f)