blob: 8131335724ff1d9b53a924cdcbb330202993b51b [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()
65 output_yaml.update(device.generate_extra_config())
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080066 output_yaml = nbhelper.utils.apply_as_router(output_yaml)
Zack Williams5d66d182021-07-27 23:08:28 -070067
Wei-Yu Chenc517f552021-07-29 21:16:45 +080068 output_yaml["netprep_netplan"] = device.generate_netplan()
69
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080070 with open("inventory/host_vars/%s.yaml" % device.fullname, "w") as f:
71 # yaml.compact(seq_seq=False, seq_map=False)
72 yaml.dump(output_yaml, f)