blob: 7034f6c4f06d5d60c2259b9ae27f0248a2322a36 [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
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080017from ruamel.yaml import YAML
Zack Williams2aeb3ef2021-06-11 17:10:36 -070018
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)
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080035 tenant = nbhelper.Tenant()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070036
37 # use base_config for additional items
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080038 yaml = YAML(typ="rt")
39 base_yaml = yaml.load(args.base_config.read())
Zack Williams2aeb3ef2021-06-11 17:10:36 -070040
Zack Williams2aeb3ef2021-06-11 17:10:36 -070041 dhcpd_interfaces = []
42
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080043 # TODO
44 # dhcpd_if = dhcpd_subnet.dhcpd_interface
45 dns_forward_zones = nbhelper.service.dnsFowardZoneConfigGenerator()
46 dns_reverse_zones = nbhelper.service.dnsReverseZoneConfigGenerator()
47 dhcpd_subnets = nbhelper.service.dhcpSubnetConfigGenerator()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070048
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080049 for device in tenant.get_devices(device_types=["server", "router", "switch"]):
Wei-Yu Chenc517f552021-07-29 21:16:45 +080050 output_yaml = base_yaml.copy()
Zack Williams2aeb3ef2021-06-11 17:10:36 -070051
Wei-Yu Chenc517f552021-07-29 21:16:45 +080052 if (
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080053 isinstance(device, nbhelper.Device)
Wei-Yu Chenc517f552021-07-29 21:16:45 +080054 and device.data.device_role.slug == "router"
55 ) or (
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080056 isinstance(device, nbhelper.VirtualMachine)
Wei-Yu Chenc517f552021-07-29 21:16:45 +080057 and device.data.role.slug == "router"
58 ):
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080059 output_yaml["dns_forward_zones"] = dns_forward_zones
Wei-Yu Chenc517f552021-07-29 21:16:45 +080060 output_yaml["dns_reverse_zones"] = dns_reverse_zones
61 output_yaml["dhcpd_subnets"] = dhcpd_subnets
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080062 output_yaml["dhcpd_interfaces"] = list(device.internal_interfaces.keys())
Wei-Yu Chenc517f552021-07-29 21:16:45 +080063 output_yaml["netprep_nftables"] = device.generate_nftables()
64 output_yaml.update(device.generate_extra_config())
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080065 output_yaml = nbhelper.utils.apply_as_router(output_yaml)
Zack Williams5d66d182021-07-27 23:08:28 -070066
Wei-Yu Chenc517f552021-07-29 21:16:45 +080067 output_yaml["netprep_netplan"] = device.generate_netplan()
68
Wei-Yu Chenbd495ba2021-08-31 19:46:35 +080069 with open("inventory/host_vars/%s.yaml" % device.fullname, "w") as f:
70 # yaml.compact(seq_seq=False, seq_map=False)
71 yaml.dump(output_yaml, f)