Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 1 | #!/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 | |
| 11 | from __future__ import absolute_import |
| 12 | |
| 13 | import argparse |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 14 | import os |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 15 | |
Zack Williams | dac2be4 | 2021-08-19 16:14:31 -0700 | [diff] [blame^] | 16 | import nbhelper |
| 17 | |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 18 | from ruamel.yaml import YAML |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 19 | |
| 20 | # main function that calls other functions |
| 21 | if __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 Chen | 55a8682 | 2021-07-08 14:34:59 +0800 | [diff] [blame] | 35 | args = nbhelper.initialize(extra_args) |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 36 | tenant = nbhelper.Tenant() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 37 | |
| 38 | # use base_config for additional items |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 39 | yaml = YAML(typ="rt") |
| 40 | base_yaml = yaml.load(args.base_config.read()) |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 41 | |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 42 | dhcpd_interfaces = [] |
| 43 | |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 44 | # 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 Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 49 | |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 50 | for device in tenant.get_devices(device_types=["server", "router", "switch"]): |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 51 | output_yaml = base_yaml.copy() |
Zack Williams | 2aeb3ef | 2021-06-11 17:10:36 -0700 | [diff] [blame] | 52 | |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 53 | if ( |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 54 | isinstance(device, nbhelper.Device) |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 55 | and device.data.device_role.slug == "router" |
| 56 | ) or ( |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 57 | isinstance(device, nbhelper.VirtualMachine) |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 58 | and device.data.role.slug == "router" |
| 59 | ): |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 60 | output_yaml["dns_forward_zones"] = dns_forward_zones |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 61 | output_yaml["dns_reverse_zones"] = dns_reverse_zones |
| 62 | output_yaml["dhcpd_subnets"] = dhcpd_subnets |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 63 | output_yaml["dhcpd_interfaces"] = list(device.internal_interfaces.keys()) |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 64 | output_yaml["netprep_nftables"] = device.generate_nftables() |
| 65 | output_yaml.update(device.generate_extra_config()) |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 66 | output_yaml = nbhelper.utils.apply_as_router(output_yaml) |
Zack Williams | 5d66d18 | 2021-07-27 23:08:28 -0700 | [diff] [blame] | 67 | |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 68 | output_yaml["netprep_netplan"] = device.generate_netplan() |
| 69 | |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 70 | 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) |