Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/esxi-netinit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ESX Networking setup scripts
34 changes: 34 additions & 0 deletions python/esxi-netinit/doc/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"links": [
{
"ethernet_mac_address": "14:23:f3:f5:21:50",
"id": "c23067a8-b2ca-4114-9fa9-32b0b9151e18",
"mtu": 1500,
"type": "vif",
"vif_id": "145cfec6-bedb-444c-9ef1-de1bfef67b2a"
}
],
"networks": [
{
"id": "b0fa63d0-fb0c-446f-bfd3-26c0a50730c0",
"ip_address": "10.4.50.118",
"link": "c23067a8-b2ca-4114-9fa9-32b0b9151e18",
"netmask": "255.255.255.192",
"network_id": "c57e4a02-73bb-4c6e-ab03-537ea11168e3",
"routes": [
{
"gateway": "10.4.50.65",
"netmask": "0.0.0.0",
"network": "0.0.0.0"
}
],
"type": "ipv4"
}
],
"services": [
{
"address": "10.4.204.3",
"type": "dns"
}
]
}
90 changes: 90 additions & 0 deletions python/esxi-netinit/doc/multiif.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"links": [
{
"id": "tap47bb4c37-f6",
"vif_id": "47bb4c37-f60d-474f-8ce5-c7c1d9982585",
"type": "phy",
"mtu": 1450,
"ethernet_mac_address": "14:23:f3:f5:3a:d0"
},
{
"id": "tap1b9c25a9-39",
"vif_id": "1b9c25a9-396f-43e7-9f1c-a2dcdfd3989c",
"type": "vlan",
"mtu": 1450,
"ethernet_mac_address": "fa:16:3e:07:86:96",
"vlan_link": "tap47bb4c37-f6",
"vlan_id": 111,
"vlan_mac_address": "fa:16:3e:07:86:96"
},
{
"id": "tape3fbe9a7-93",
"vif_id": "e3fbe9a7-933d-4e27-8ac5-858054be7772",
"type": "vlan",
"mtu": 1450,
"ethernet_mac_address": "fa:16:3e:31:50:d6",
"vlan_link": "tap47bb4c37-f6",
"vlan_id": 222,
"vlan_mac_address": "fa:16:3e:31:50:d6"
},
{
"id": "tapd097f698-89",
"vif_id": "d097f698-8926-44e1-afe7-09fb03947f23",
"type": "vlan",
"mtu": 1450,
"ethernet_mac_address": "fa:16:3e:48:91:ef",
"vlan_link": "tap47bb4c37-f6",
"vlan_id": 444,
"vlan_mac_address": "fa:16:3e:48:91:ef"
}
],
"networks": [
{
"id": "network0",
"type": "ipv4",
"link": "tap47bb4c37-f6",
"ip_address": "192.168.100.170",
"netmask": "255.255.255.0",
"routes": [
{
"network": "0.0.0.0",
"netmask": "0.0.0.0",
"gateway": "192.168.100.1"
}
],
"network_id": "783b4239-7220-4a74-8253-415539469860",
"services": []
},
{
"id": "network1",
"type": "ipv4",
"link": "tap1b9c25a9-39",
"ip_address": "192.168.200.174",
"netmask": "255.255.255.0",
"routes": [],
"network_id": "9608ea7d-18d9-4298-8951-ac9dbe20db06",
"services": []
},
{
"id": "network2",
"type": "ipv4",
"link": "tape3fbe9a7-93",
"ip_address": "192.168.0.24",
"netmask": "255.255.255.0",
"routes": [],
"network_id": "ecff22e4-b364-4575-9d2b-dffc83c8d5b7",
"services": []
},
{
"id": "network3",
"type": "ipv4",
"link": "tapd097f698-89",
"ip_address": "192.168.10.133",
"netmask": "255.255.255.0",
"routes": [],
"network_id": "c47d5a38-c646-42e8-b6ca-3eecc977d645",
"services": []
}
],
"services": []
}
Empty file.
90 changes: 90 additions & 0 deletions python/esxi-netinit/netinit/esxconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from functools import cached_property

from .esxhost import ESXHost
from .network_data import NetworkData
from .nic import NIC
from .nic_list import NICList


class ESXConfig:
def __init__(self, network_data: NetworkData, dry_run=False) -> None:
self.network_data = network_data
self.dry_run = dry_run
self.host = ESXHost(dry_run)

def add_default_mgmt_interface(
self, portgroup_name, switch_name, interface_name="vmk0"
):
self.host.portgroup_add(portgroup_name=portgroup_name, switch_name=switch_name)
self.host.add_ip_interface(name=interface_name, portgroup_name=portgroup_name)

def clean_default_network_setup(self, portgroup_name, switch_name):
"""Removes default networking setup left by the installer."""
self.host.delete_vmknic(portgroup_name=portgroup_name)
self.host.portgroup_remove(
switch_name=switch_name, portgroup_name=portgroup_name
)
self.host.destroy_vswitch(name=switch_name)

def configure_default_route(self):
"""Configures default route.

If multiple default routes are present, only first one is used.
"""
route = self.network_data.default_route()
self.host.configure_default_route(route.gateway)

def configure_portgroups(self, switch_name="vSwitch0"):
portgroups = []
for link in self.network_data.links:
if link.type == "vlan":
vid = link.vlan_id
pg_name = f"internal_net_vid_{vid}"
self.host.portgroup_add(portgroup_name=pg_name, switch_name=switch_name)
self.host.portgroup_set_vlan(portgroup_name=pg_name, vlan_id=vid)
portgroups.append(pg_name)
return portgroups

def configure_management_interface(self):
mgmt_network = next(
net for net in self.network_data.networks if net.default_routes()
)
return self.host.change_ip(
"vmk0", mgmt_network.ip_address, mgmt_network.netmask
)

def configure_vswitch(self, uplink: NIC, switch_name: str, mtu: int):
"""Sets up vSwitch."""
self.host.create_vswitch(switch_name)
self.host.uplink_add(nic=uplink.name, switch_name=switch_name)
self.host.vswitch_failover_uplinks(
active_uplinks=[uplink.name], name=switch_name
)
self.host.vswitch_security(name=switch_name)
self.host.vswitch_settings(mtu=mtu, name=switch_name)

def configure_requested_dns(self):
"""Configures DNS servers that were provided in network_data.json."""
dns_servers = [
srv.address for srv in self.network_data.services if srv.type == "dns"
]
if not dns_servers:
return

return self.host.configure_dns(servers=dns_servers)

def identify_uplink(self) -> NIC:
eligible_networks = [
net for net in self.network_data.networks if net.default_routes()
]
if len(eligible_networks) != 1:
raise ValueError(
"the network_data.json should only contain a single default route."
"Unable to identify uplink interface"
)
link = eligible_networks[0].link
return self.nics.find_by_mac(link.ethernet_mac_address)

@cached_property
def nics(self):
return NICList()
Loading