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