Port provisioner scripts to updated framework

Updates all the example provisioner scripts to use the new functions
exposed by the VMBuilder class as an illustration of how best to use
them.

Also adds a wrapper fail() handler to ensure the cleanup of the script,
as well as the global cleanup, are run on an exception.
This commit is contained in:
2023-11-16 17:03:51 -05:00
parent 618a1c1c10
commit 2a637c62e8
6 changed files with 162 additions and 119 deletions

View File

@ -32,6 +32,20 @@
# function is provided in context of the example; see the other examples for
# more potential uses.
# Within the VMBuilderScript class, several helper functions are exposed through
# the parent VMBuilder class:
# self.log_info(message):
# Use this function to log an "informational" message instead of "print()"
# self.log_warn(message):
# Use this function to log a "warning" message
# self.log_err(message):
# Use this function to log an "error" message outside of an exception (see below)
# self.fail(message, exception=<ExceptionClass>):
# Use this function to bail out of the script safely instead if raising a
# normal Python exception. You may pass an optional exception class keyword
# argument for posterity in the logs if you wish; otherwise, ProvisioningException
# is used. This function implicitly calls a "self.log_err" with the passed message
# Within the VMBuilderScript class, several common variables are exposed through
# the parent VMBuilder class:
# self.vm_name: The name of the VM from PVC's perspective
@ -133,9 +147,8 @@
# since they could still do destructive things to /dev and the like!
# This import is always required here, as VMBuilder is used by the VMBuilderScript class
# and ProvisioningError is the primary exception that should be raised within the class.
from pvcapid.vmbuilder import VMBuilder, ProvisioningError
# This import is always required here, as VMBuilder is used by the VMBuilderScript class.
from pvcapid.vmbuilder import VMBuilder
# The VMBuilderScript class must be named as such, and extend VMBuilder.
@ -283,9 +296,9 @@ class VMBuilderScript(VMBuilder):
import os
# First loop: Create the destination disks
print("Creating destination disk volumes")
self.log_info("Creating destination disk volumes")
for volume in self.vm_data["volumes"]:
print(f"Processing volume {volume['volume_name']}")
self.log_info(f"Processing volume {volume['volume_name']}")
with open_zk(config) as zkhandler:
success, message = pvc_ceph.add_volume(
zkhandler,
@ -293,16 +306,14 @@ class VMBuilderScript(VMBuilder):
f"{self.vm_name}_{volume['disk_id']}",
f"{volume['disk_size_gb']}G",
)
print(message)
self.log_info(message)
if not success:
raise ProvisioningError(
f"Failed to create volume '{volume['disk_id']}'."
)
self.fail(f"Failed to create volume '{volume['disk_id']}'.")
# Second loop: Map the destination disks
print("Mapping destination disk volumes")
self.log_info("Mapping destination disk volumes")
for volume in self.vm_data["volumes"]:
print(f"Processing volume {volume['volume_name']}")
self.log_info(f"Processing volume {volume['volume_name']}")
dst_volume_name = f"{self.vm_name}_{volume['disk_id']}"
dst_volume = f"{volume['pool']}/{dst_volume_name}"
@ -312,14 +323,14 @@ class VMBuilderScript(VMBuilder):
volume["pool"],
dst_volume_name,
)
print(message)
self.log_info(message)
if not success:
raise ProvisioningError(f"Failed to map volume '{dst_volume}'.")
self.fail(f"Failed to map volume '{dst_volume}'.")
# Third loop: Map the source disks
print("Mapping source disk volumes")
self.log_info("Mapping source disk volumes")
for volume in self.vm_data["volumes"]:
print(f"Processing volume {volume['volume_name']}")
self.log_info(f"Processing volume {volume['volume_name']}")
src_volume_name = volume["volume_name"]
src_volume = f"{volume['pool']}/{src_volume_name}"
@ -329,9 +340,9 @@ class VMBuilderScript(VMBuilder):
volume["pool"],
src_volume_name,
)
print(message)
self.log_info(message)
if not success:
raise ProvisioningError(f"Failed to map volume '{src_volume}'.")
self.fail(f"Failed to map volume '{src_volume}'.")
def install(self):
"""
@ -351,14 +362,14 @@ class VMBuilderScript(VMBuilder):
dst_volume = f"{volume['pool']}/{dst_volume_name}"
dst_devpath = f"/dev/rbd/{dst_volume}"
print(
self.log_info(
f"Converting {volume['volume_format']} {src_volume} at {src_devpath} to {dst_volume} at {dst_devpath}"
)
retcode, stdout, stderr = pvc_common.run_os_command(
f"qemu-img convert -C -f {volume['volume_format']} -O raw {src_devpath} {dst_devpath}"
)
if retcode:
raise ProvisioningError(
self.fail(
f"Failed to convert {volume['volume_format']} volume '{src_volume}' to raw volume '{dst_volume}' with qemu-img: {stderr}"
)
@ -368,7 +379,7 @@ class VMBuilderScript(VMBuilder):
This function is also called if there is ANY exception raised in the prepare()
or install() steps. While this doesn't mean you shouldn't or can't raise exceptions
here, be warned that doing so might cause loops. Do this only if you really need to.
here, be warned that doing so might cause loops. Do this only if you really need to!
"""
# Run any imports first
@ -388,7 +399,7 @@ class VMBuilderScript(VMBuilder):
src_volume_name,
)
if not success:
raise ProvisioningError(
self.log_err(
f"Failed to unmap source volume '{src_volume_name}': {message}"
)
@ -404,6 +415,6 @@ class VMBuilderScript(VMBuilder):
dst_volume_name,
)
if not success:
raise ProvisioningError(
self.log_err(
f"Failed to unmap destination volume '{dst_volume_name}': {message}"
)