feat: VM provisioning, and reboot_host on ESXi
create_vm_from_cloud_init takes the same qcow2/raw cloud images netOrk offers for Proxmox. ESXi can neither boot nor download them, so the driver does both: download with checksum check and one retry, convert with qemu-img to a streamOptimized VMDK (cached by URL), import through a minimal OVF descriptor over NFC, pin requested MACs, grow the disk, and attach a NoCloud seed ISO placed next to the VM's files. NoCloud rather than guestinfo because it needs nothing in the guest; the user-data installs open-vm-tools, which the driver declares as its guest agent. A failure after the import removes the VM again. Placement is a pure decision over inventory rows: a connected host outside maintenance mode that sees the datastore and every port group, with the resource pool and VM folder found by walking up to the datacenter -- one path for a standalone host and for a vCenter. Two faults vcsim surfaced and the tests now pin: a chunked upload body next to a Content-Length is refused with 500, so the disk goes up as a sized file object that also reports lease progress; and a device edit replaces the device as sent, so disk and NIC edits start from the live objects, backing included (vcsim panicked on a disk without one). destroy_vm, get_vm_status, get_network_targets (port groups with their fixed VLAN) and get_image_storages complete the contract. reboot_host on ESXi uses RebootHost_Task and refuses outside maintenance mode: force=True would cut power to running VMs. Tested against vcsim in ESXi and vCenter mode, end to end.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
"""cloud-init's NoCloud seed: user-data, meta-data, network-config on an ISO.
|
||||
|
||||
NoCloud rather than VMware's guestinfo datasource because it needs nothing in
|
||||
the guest: guestinfo is read through open-vm-tools, which a generic cloud
|
||||
image does not ship -- it is one of the things the user-data installs.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
from typing import Any
|
||||
|
||||
import pycdlib
|
||||
import yaml
|
||||
|
||||
#: The volume label cloud-init's NoCloud datasource looks for.
|
||||
_LABEL = "cidata"
|
||||
|
||||
|
||||
def user_data(cloud_init_config: dict[str, Any], ssh_public_keys: list[str] | None) -> str:
|
||||
"""The ``#cloud-config`` document, with ``ssh_public_keys`` merged in."""
|
||||
config = dict(cloud_init_config)
|
||||
if ssh_public_keys:
|
||||
keys = list(config.get("ssh_authorized_keys") or [])
|
||||
keys += [k for k in ssh_public_keys if k not in keys]
|
||||
config["ssh_authorized_keys"] = keys
|
||||
return "#cloud-config\n" + yaml.safe_dump(config, sort_keys=False)
|
||||
|
||||
|
||||
def meta_data(hostname: str, instance_id: str) -> str:
|
||||
return yaml.safe_dump({"instance-id": instance_id, "local-hostname": hostname}, sort_keys=False)
|
||||
|
||||
|
||||
def network_config(nics: list[tuple[str, bool]]) -> dict[str, Any] | None:
|
||||
"""Netplan v2 config: DHCP on every ``(mac, dhcp)`` NIC that asks for it.
|
||||
|
||||
``None`` when no NIC does -- cloud-init then leaves networking alone
|
||||
rather than being told to configure nothing.
|
||||
"""
|
||||
ethernets = {
|
||||
f"nic{i}": {"match": {"macaddress": mac}, "dhcp4": True}
|
||||
for i, (mac, dhcp) in enumerate(nics)
|
||||
if dhcp
|
||||
}
|
||||
return {"version": 2, "ethernets": ethernets} if ethernets else None
|
||||
|
||||
|
||||
def nocloud_iso(user: str, meta: str, network: dict[str, Any] | None) -> bytes:
|
||||
"""An ISO 9660 image (Rock Ridge + Joliet) holding the seed files."""
|
||||
files = {"user-data": user, "meta-data": meta}
|
||||
if network is not None:
|
||||
files["network-config"] = yaml.safe_dump(network, sort_keys=False)
|
||||
iso = pycdlib.PyCdlib()
|
||||
iso.new(interchange_level=3, joliet=3, rock_ridge="1.09", vol_ident=_LABEL)
|
||||
for index, (name, content) in enumerate(files.items()):
|
||||
data = content.encode()
|
||||
iso.add_fp(
|
||||
io.BytesIO(data),
|
||||
len(data),
|
||||
f"/SEED{index}.;1",
|
||||
rr_name=name,
|
||||
joliet_path=f"/{name}",
|
||||
)
|
||||
out = io.BytesIO()
|
||||
iso.write_fp(out)
|
||||
iso.close()
|
||||
return out.getvalue()
|
||||
Reference in New Issue
Block a user