Milestone 5.1: More Automation Repo for the code used in this milestone Link Linked Clone def linked_clone_vm(si, vm_name, clone_name, silent): """Linked clone a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to clone param clone_name (str): The name of the clone param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOn': print(f"VM {vm_name} is powered on. Please power off the VM before cloning") power_off_vm(si, vm_name, silent) input("Press Enter to continue... Once the VM is powered off") linked_clone_vm(si, vm_name, clone_name, silent) else: relocate_spec = vim.vm.RelocateSpec() relocate_spec.diskMoveType = 'createNewChildDiskBacking' relocate_spec.pool = vm.resourcePool clone_spec = vim.vm.CloneSpec() clone_spec.powerOn = True clone_spec.location = relocate_spec try: clone_spec.snapshot = vm.snapshot.rootSnapshotList[0].snapshot except: print(f"VM {vm_name} does not have any snapshots. Taking a snapshot now") take_snapshot_vm(si, vm_name, 'Base', silent) vm.Clone(name=clone_name, folder=vm.parent, spec=clone_spec) print(f"Linked clone VM {vm_name} to {clone_name}") Power On and Off def power_on_vm(si, vm_name, silent): """Power on a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to power on param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOff': vm.PowerOn() print(f"VM {vm_name} has been powered on") else: print(f"VM {vm_name} is already powered on") def power_off_vm(si, vm_name, silent): """Power off a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to power off param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOn': vm.PowerOff() print(f"VM {vm_name} has been powered off") else: print(f"VM {vm_name} is already powered off") Take Snapshot def take_snapshot_vm(si, vm_name, snapshot_name, silent): """Take a snapshot of a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to take a snapshot param snapshot_name (str): The name of the snapshot param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOff': vm.CreateSnapshot_Task(name=snapshot_name, memory=True, quiesce=False) print(f"Snapshot {snapshot_name} has been created for VM {vm_name}") else: print(f"VM {vm_name} is powered on. Please power on the VM before taking a snapshot") power_off_vm(si, vm_name, silent) input("Press Enter to continue... Once the VM is powered off") take_snapshot_vm(si, vm_name, snapshot_name, silent) Restore Last Snapshot def restore_last_snapshot_vm(si, vm_name, silent): """Restore the last snapshot of a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to restore the snapshot param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOn': print(f"VM {vm_name} is powered on. Please power off the VM before restoring a snapshot") power_off_vm(si, vm_name, silent) input("Press Enter to continue... Once the VM is powered off") restore_last_snapshot_vm(si, vm_name, silent) else: try: vm.snapshot.rootSnapshotList[-1].snapshot.RevertToSnapshot_Task() print(f"Snapshot {vm.snapshot.rootSnapshotList[-1].name} has been restored for VM {vm_name}") except: print(f"VM {vm_name} does not have any snapshots. Taking a snapshot now") take_snapshot_vm(si, vm_name, 'Base', silent) input("Press Enter to continue... Once the snapshot is taken") restore_last_snapshot_vm(si, vm_name, silent) Delete Snapshot def delete_snapshot_vm(si, vm_name, snapshot_name, silent): """Delete a snapshot from a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to delete the snapshot param snapshot_name (str): The name of the snapshot param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOn': print(f"VM {vm_name} is powered on. Please power off the VM before deleting a snapshot") power_off_vm(si, vm_name, silent) input("Press Enter to continue... Once the VM is powered off") delete_snapshot_vm(si, vm_name, snapshot_name, silent) else: try: for snapshot in vm.snapshot.rootSnapshotList: if snapshot.name == snapshot_name: snapshot.snapshot.RemoveSnapshot_Task(removeChildren=False) print(f"Snapshot {snapshot_name} has been deleted for VM {vm_name}") except: print(f"VM {vm_name} does not have any snapshots. Taking a snapshot now") take_snapshot_vm(si, vm_name, 'Base', silent) input("Press Enter to continue... Once the snapshot is taken") delete_snapshot_vm(si, vm_name, snapshot_name, silent) Delete VM def delete_vm(si, vm_name, silent): """Delete a VM param si (obj): The connection to vCenter param vm_name (str): The name of the VM to delete param silent (bool): Whether or not to print the VM info return None""" vm = search_vms(si, vm_name, silent) if vm.runtime.powerState == 'poweredOn': print(f"VM {vm_name} is powered on. Please power off the VM before deleting") power_off_vm(si, vm_name, silent) input("Press Enter to continue... Once the VM is powered off") delete_vm(si, vm_name, silent) else: vm.Destroy_Task() print(f"VM {vm_name} has been deleted") Exit Handler def exit_handler(si): """Disconnect from vCenter param si (obj): The connection to vCenter return None""" si.content.sessionManager.Logout() print('Logged out!')