Revamp ordering and spacing of libvirt XML

This commit is contained in:
Joshua Boniface 2019-12-10 22:08:25 -05:00
parent 1377c15948
commit 911965c1f3
2 changed files with 69 additions and 80 deletions

View File

@ -28,8 +28,7 @@
# * vm_memory
# * vm_vcpus
# * vm_architecture
libvirt_header = """
<domain type='kvm'>
libvirt_header = """<domain type='kvm'>
<name>{vm_name}</name>
<uuid>{vm_uuid}</uuid>
<description>{vm_description}</description>
@ -57,14 +56,11 @@ libvirt_header = """
"""
# File footer, closing devices and domain elements
libvirt_footer = """
</devices>
</domain>
"""
libvirt_footer = """ </devices>
</domain>"""
# Default devices for all VMs
devices_default = """
<emulator>/usr/bin/kvm</emulator>
devices_default = """ <emulator>/usr/bin/kvm</emulator>
<controller type='usb' index='0'/>
<controller type='pci' index='0' model='pci-root'/>
<rng model='virtio'>
@ -76,8 +72,7 @@ devices_default = """
# Serial device
# Variables:
# * vm_name
devices_serial = """
<serial type='pty'>
devices_serial = """ <serial type='pty'>
<log file='/var/log/libvirt/{vm_name}.log' append='on'/>
</serial>
<console type='pty'/>
@ -88,13 +83,11 @@ devices_serial = """
# * vm_vncport
# * vm_vnc_autoport
# * vm_vnc_bind
devices_vnc = """
<graphics type='vnc' port='{vm_vncport}' autoport='{vm_vnc_autoport}' listen='{vm_vnc_bind}'/>
devices_vnc = """ <graphics type='vnc' port='{vm_vncport}' autoport='{vm_vnc_autoport}' listen='{vm_vnc_bind}'/>
"""
# VirtIO SCSI device
devices_scsi_controller = """
<controller type='scsi' index='0' model='virtio-scsi'/>
devices_scsi_controller = """ <controller type='scsi' index='0' model='virtio-scsi'/>
"""
# Disk device header
@ -103,8 +96,7 @@ devices_scsi_controller = """
# * disk_pool
# * vm_name
# * disk_id
devices_disk_header = """
<disk type='network' device='disk'>
devices_disk_header = """ <disk type='network' device='disk'>
<driver name='qemu' discard='unmap'/>
<target dev='{disk_id}' bus='scsi'/>
<auth username='libvirt'>
@ -117,19 +109,16 @@ devices_disk_header = """
# Variables:
# * coordinator_name
# * coordinator_ceph_mon_port
devices_disk_coordinator = """
<host name='{coordinator_name}' port='{coordinator_ceph_mon_port}'/>
devices_disk_coordinator = """ <host name='{coordinator_name}' port='{coordinator_ceph_mon_port}'/>
"""
# Disk device footer
devices_disk_footer = """
</source>
devices_disk_footer = """ </source>
</disk>
"""
# vhostmd virtualization passthrough device
devices_vhostmd = """
<disk type='file' device='disk'>
devices_vhostmd = """ <disk type='file' device='disk'>
<drive name='qemu' type='raw'/>
<source file='/dev/shm/vhostmd0'/>
<target dev='sdz' bus='usb'/>
@ -141,8 +130,7 @@ devices_vhostmd = """
# Variables:
# * eth_macaddr
# * eth_bridge
devices_net_interface = """
<interface type='bridge'>
devices_net_interface = """ <interface type='bridge'>
<mac address='{eth_macaddr}'/>
<source bridge='{eth_bridge}'/>
<model type='virtio'/>

View File

@ -781,7 +781,7 @@ def create_vm(self, vm_name, vm_profile):
if not target_node:
raise ClusterError("No ready cluster node contains at least {}+512 MB of free RAM".format(vm_data['system_details']['vram_mb']))
print("Selecting target node {} with {} MB free ram".format(target_node, last_free))
print("Selecting target node {} with {} MB free RAM".format(target_node, last_free))
# Verify that all configured networks are present on the cluster
cluster_networks, _discard = pvc_network.getClusterNetworkList(zk_conn)
@ -1005,59 +1005,7 @@ def create_vm(self, vm_name, vm_profile):
vm_memory=vm_data['system_details']['vram_mb'],
vm_vcpus=vm_data['system_details']['vcpu_count'],
vm_architecture=system_architecture
).strip()
# Add default devices
vm_schema += libvirt_schema.devices_default.strip()
# Add serial device
if vm_data['system_details']['serial']:
vm_schema += libvirt_schema.devices_serial.format(
vm_name=vm_name
).strip()
# Add VNC device
if vm_data['system_details']['vnc']:
if vm_data['system_details']['vnc_bind']:
vm_vnc_bind = vm_data['system_details']['vnc_bind']
else:
vm_vnc_bind = "127.0.0.1"
vm_vncport = 5900
vm_vnc_autoport = "yes"
vm_schema += libvirt_schema.devices_vnc.format(
vm_vncport=vm_vncport,
vm_vnc_autoport=vm_vnc_autoport,
vm_vnc_bind=vm_vnc_bind
).strip()
# Add SCSI controller
vm_schema += libvirt_schema.devices_scsi_controller.strip()
# Add disk devices
monitor_list = list()
coordinator_names = config['storage_hosts']
for coordinator in coordinator_names:
monitor_list.append("{}.{}".format(coordinator, config['storage_domain']))
ceph_storage_secret = config['ceph_storage_secret_uuid']
for volume in vm_data['volumes']:
vm_schema += libvirt_schema.devices_disk_header.format(
ceph_storage_secret=ceph_storage_secret,
disk_pool=volume['pool'],
vm_name=vm_name,
disk_id=volume['disk_id']
).strip()
for monitor in monitor_list:
vm_schema += libvirt_schema.devices_disk_coordinator.format(
coordinator_name=monitor,
coordinator_ceph_mon_port=config['ceph_monitor_port']
).strip()
vm_schema += libvirt_schema.devices_disk_footer.strip()
vm_schema += libvirt_schema.devices_vhostmd.strip()
)
# Add network devices
network_id = 0
@ -1093,11 +1041,64 @@ def create_vm(self, vm_name, vm_profile):
vm_schema += libvirt_schema.devices_net_interface.format(
eth_macaddr=eth_macaddr,
eth_bridge=eth_bridge
).strip()
)
network_id += 1
vm_schema += libvirt_schema.libvirt_footer.strip()
# Add disk devices
monitor_list = list()
coordinator_names = config['storage_hosts']
for coordinator in coordinator_names:
monitor_list.append("{}.{}".format(coordinator, config['storage_domain']))
ceph_storage_secret = config['ceph_storage_secret_uuid']
for volume in vm_data['volumes']:
vm_schema += libvirt_schema.devices_disk_header.format(
ceph_storage_secret=ceph_storage_secret,
disk_pool=volume['pool'],
vm_name=vm_name,
disk_id=volume['disk_id']
)
for monitor in monitor_list:
vm_schema += libvirt_schema.devices_disk_coordinator.format(
coordinator_name=monitor,
coordinator_ceph_mon_port=config['ceph_monitor_port']
)
vm_schema += libvirt_schema.devices_disk_footer
vm_schema += libvirt_schema.devices_vhostmd
# Add default devices
vm_schema += libvirt_schema.devices_default
# Add serial device
if vm_data['system_details']['serial']:
vm_schema += libvirt_schema.devices_serial.format(
vm_name=vm_name
)
# Add VNC device
if vm_data['system_details']['vnc']:
if vm_data['system_details']['vnc_bind']:
vm_vnc_bind = vm_data['system_details']['vnc_bind']
else:
vm_vnc_bind = "127.0.0.1"
vm_vncport = 5900
vm_vnc_autoport = "yes"
vm_schema += libvirt_schema.devices_vnc.format(
vm_vncport=vm_vncport,
vm_vnc_autoport=vm_vnc_autoport,
vm_vnc_bind=vm_vnc_bind
)
# Add SCSI controller
vm_schema += libvirt_schema.devices_scsi_controller
# Add footer
vm_schema += libvirt_schema.libvirt_footer
print("Final VM schema:\n{}\n".format(vm_schema))