Skip to content

Commit

Permalink
Merge pull request #613 from Ana06/export-snapshots-fix
Browse files Browse the repository at this point in the history
[vbox-export-snapshots] Fix VM name & small improvements
  • Loading branch information
Ana06 authored Sep 5, 2024
2 parents e69caa7 + c2dcf89 commit 0cc241d
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions virtualbox/vbox-export-snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import virtualbox
from virtualbox.library import VirtualSystemDescriptionType as DescType
from virtualbox.library import NetworkAttachmentType as NetType
from virtualbox.library import ExportOptions as ExportOps
from datetime import datetime

# Base name of the exported VMs
Expand Down Expand Up @@ -41,27 +42,25 @@ def change_network_adapters(vm, max_adapters):
for i in range(max_adapters):
adapter = vm.get_network_adapter(i)
adapter.attachment_type = NetType.host_only
vm.save_settings()


if __name__ == "__main__":
date = datetime.today().strftime("%Y%m%d")

vbox = virtualbox.VirtualBox()
vm = vbox.find_machine(VM_NAME)
session = vm.create_session()
vm = session.machine

max_adapters = vbox.system_properties.get_max_network_adapters(vm.chipset_type)

for snapshot_name, extension, description in SNAPSHOTS:
try:
# Restore snapshot
snapshot = vm.find_snapshot(snapshot_name)
progress = vm.restore_snapshot(snapshot)
session = vm.create_session()
snapshot = session.machine.find_snapshot(snapshot_name)
progress = session.machine.restore_snapshot(snapshot)
progress.wait_for_completion(-1)

change_network_adapters(vm, max_adapters)

change_network_adapters(session.machine, max_adapters)
session.unlock_machine()
print(f"Restored '{snapshot_name}' and changed its adapter(s) to host-only")

# Export .ova
Expand All @@ -71,8 +70,9 @@ def change_network_adapters(vm, max_adapters):
filename = os.path.join(export_directory, f"{exported_vm_name}.ova")
appliance = vbox.create_appliance()
sys_description = vm.export_to(appliance, exported_vm_name)
sys_description.set_final_value(DescType.name, exported_vm_name)
sys_description.set_final_value(DescType.description, description)
progress = appliance.write("ovf-1.0", [], filename)
progress = appliance.write("ovf-1.0", [ExportOps.create_manifest], filename)
print(f"Exporting {filename} (this will take some time, go for an 🍦!)")
progress.wait_for_completion(-1)

Expand All @@ -84,6 +84,4 @@ def change_network_adapters(vm, max_adapters):

except Exception as e:
print(f"ERROR exporting {snapshot_name}: {e}")
next

session.unlock_machine()

0 comments on commit 0cc241d

Please sign in to comment.