mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 19:26:48 +00:00
feat(instances): a new VM can get a cloud-init seed with its hostname and SSH keys
Cloud images read their first-boot settings with cloud-init. The panel served them only over HTTP, which a VM reaches only with the network and a reverse DNS name for its address. A new VM can now carry them itself: Cloud-init seed (Advanced) on the create form, or cloud_init in the API. The job writes a NoCloud seed, an ISO labelled cidata, into the disk's directory pool as cidata-<uuid>.iso and puts it in the VM's CD-ROM drive. Its meta-data holds the VM's UUID as instance-id and its name as hostname, written as JSON so a VM named 123 or yes keeps a string hostname; its user-data is what the HTTP datasource serves, with the creating user's SSH keys. The ISO is built with pycdlib and written through a libvirt stream, as the NVRAM copy is, now one helper for both. A seed needs a CD-ROM bus and a directory pool, else the create is refused. A failed create deletes the seed with the new disk, recovery names one left behind, and deleting the VM with its disks deletes its seed, found in the pools by name even when ejected. A clone's drive is left empty: the seed is the source VM's identity.
This commit is contained in:
parent
890d82f44a
commit
f004552148
13 changed files with 352 additions and 20 deletions
|
|
@ -1,10 +1,32 @@
|
|||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import string
|
||||
|
||||
import pycdlib
|
||||
from vrtManager import util
|
||||
from vrtManager.connection import wvmConnect
|
||||
|
||||
|
||||
def nocloud_seed(instance_id, hostname, user_data):
|
||||
"""A cloud-init NoCloud seed: an ISO labelled cidata with the VM's
|
||||
meta-data and user-data, read at first boot without the network.
|
||||
cloud-init runs again only for a new instance-id."""
|
||||
iso = pycdlib.PyCdlib()
|
||||
iso.new(interchange_level=3, joliet=3, rock_ridge="1.09", vol_ident="cidata")
|
||||
# JSON is YAML whose values stay strings: a VM named 123 or yes keeps its name
|
||||
meta_data = json.dumps({"instance-id": instance_id, "local-hostname": hostname})
|
||||
files = {"meta-data": meta_data, "user-data": user_data}
|
||||
for name, text in files.items():
|
||||
data = text.encode()
|
||||
iso_name = name.replace("-", "").upper()
|
||||
iso.add_fp(io.BytesIO(data), len(data), f"/{iso_name}.;1", rr_name=name, joliet_path=f"/{name}")
|
||||
out = io.BytesIO()
|
||||
iso.write_fp(out)
|
||||
iso.close()
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
def get_rbd_storage_data(stg):
|
||||
xml = stg.XMLDesc(0)
|
||||
ceph_user = util.get_xml_path(xml, "/pool/source/auth/@username")
|
||||
|
|
@ -144,6 +166,11 @@ class wvmCreate(wvmConnect):
|
|||
vol = self.get_volume_by_path(path)
|
||||
vol.delete()
|
||||
|
||||
def create_seed(self, pool, uuid, hostname, user_data):
|
||||
"""The VM's NoCloud seed as a volume cidata-<uuid>.iso in the pool; its path."""
|
||||
seed = nocloud_seed(uuid, hostname, user_data)
|
||||
return self.upload_volume(self.get_storage(pool), f"cidata-{uuid}.iso", seed).path()
|
||||
|
||||
def create_instance(
|
||||
self,
|
||||
name,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue