|
| 1 | +from functools import cached_property |
| 2 | + |
| 3 | +from .esxhost import ESXHost |
| 4 | +from .network_data import NetworkData |
| 5 | +from .nic import NIC |
| 6 | +from .nic_list import NICList |
| 7 | + |
| 8 | + |
| 9 | +class ESXConfig: |
| 10 | + def __init__(self, network_data: NetworkData, dry_run=False) -> None: |
| 11 | + self.network_data = network_data |
| 12 | + self.dry_run = dry_run |
| 13 | + self.host = ESXHost(dry_run) |
| 14 | + |
| 15 | + def add_default_mgmt_interface( |
| 16 | + self, portgroup_name, switch_name, interface_name="vmk0" |
| 17 | + ): |
| 18 | + self.host.portgroup_add(portgroup_name=portgroup_name, switch_name=switch_name) |
| 19 | + self.host.add_ip_interface(name=interface_name, portgroup_name=portgroup_name) |
| 20 | + |
| 21 | + def clean_default_network_setup(self, portgroup_name, switch_name): |
| 22 | + """Removes default networking setup left by the installer.""" |
| 23 | + self.host.delete_vmknic(portgroup_name=portgroup_name) |
| 24 | + self.host.portgroup_remove( |
| 25 | + switch_name=switch_name, portgroup_name=portgroup_name |
| 26 | + ) |
| 27 | + self.host.destroy_vswitch(name=switch_name) |
| 28 | + |
| 29 | + def configure_default_route(self): |
| 30 | + """Configures default route. |
| 31 | +
|
| 32 | + If multiple default routes are present, only first one is used. |
| 33 | + """ |
| 34 | + route = self.network_data.default_route() |
| 35 | + self.host.configure_default_route(route.gateway) |
| 36 | + |
| 37 | + def configure_portgroups(self, switch_name="vSwitch0"): |
| 38 | + portgroups = [] |
| 39 | + for link in self.network_data.links: |
| 40 | + if link.type == "vlan": |
| 41 | + vid = link.vlan_id |
| 42 | + pg_name = f"internal_net_vid_{vid}" |
| 43 | + self.host.portgroup_add(portgroup_name=pg_name, switch_name=switch_name) |
| 44 | + self.host.portgroup_set_vlan(portgroup_name=pg_name, vlan_id=vid) |
| 45 | + portgroups.append(pg_name) |
| 46 | + return portgroups |
| 47 | + |
| 48 | + def configure_management_interface(self): |
| 49 | + mgmt_network = next( |
| 50 | + net for net in self.network_data.networks if net.default_routes() |
| 51 | + ) |
| 52 | + return self.host.change_ip( |
| 53 | + "vmk0", mgmt_network.ip_address, mgmt_network.netmask |
| 54 | + ) |
| 55 | + |
| 56 | + def configure_vswitch(self, uplink: NIC, switch_name: str, mtu: int): |
| 57 | + """Sets up vSwitch.""" |
| 58 | + self.host.create_vswitch(switch_name) |
| 59 | + self.host.uplink_add(nic=uplink.name, switch_name=switch_name) |
| 60 | + self.host.vswitch_failover_uplinks( |
| 61 | + active_uplinks=[uplink.name], name=switch_name |
| 62 | + ) |
| 63 | + self.host.vswitch_security(name=switch_name) |
| 64 | + self.host.vswitch_settings(mtu=mtu, name=switch_name) |
| 65 | + |
| 66 | + def configure_requested_dns(self): |
| 67 | + """Configures DNS servers that were provided in network_data.json.""" |
| 68 | + dns_servers = [ |
| 69 | + srv.address for srv in self.network_data.services if srv.type == "dns" |
| 70 | + ] |
| 71 | + if not dns_servers: |
| 72 | + return |
| 73 | + |
| 74 | + return self.host.configure_dns(servers=dns_servers) |
| 75 | + |
| 76 | + def identify_uplink(self) -> NIC: |
| 77 | + eligible_networks = [ |
| 78 | + net for net in self.network_data.networks if net.default_routes() |
| 79 | + ] |
| 80 | + if len(eligible_networks) != 1: |
| 81 | + raise ValueError( |
| 82 | + "the network_data.json should only contain a single default route." |
| 83 | + "Unable to identify uplink interface" |
| 84 | + ) |
| 85 | + link = eligible_networks[0].link |
| 86 | + return self.nics.find_by_mac(link.ethernet_mac_address) |
| 87 | + |
| 88 | + @cached_property |
| 89 | + def nics(self): |
| 90 | + return NICList() |
0 commit comments