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() |
Wei-Yu Chen | 9b55d36 | 2021-09-22 11:04:31 +0800 | [diff] [blame] | 65 | |
| 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 Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 76 | output_yaml = nbhelper.utils.apply_as_router(output_yaml) |
Zack Williams | 5d66d18 | 2021-07-27 23:08:28 -0700 | [diff] [blame] | 77 | |
Wei-Yu Chen | c517f55 | 2021-07-29 21:16:45 +0800 | [diff] [blame] | 78 | output_yaml["netprep_netplan"] = device.generate_netplan() |
| 79 | |
Wei-Yu Chen | bd495ba | 2021-08-31 19:46:35 +0800 | [diff] [blame] | 80 | 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) |