One of the REST api calls that where added for Horizon 8 2012 was the ability to push images to Desktop Pools (sadly not for farms yet). This week I added that functionality to the VMware Horizon Python Module. Looking at the swagger UI these are the needed arguments:
So the source can be either the streams from Horizon Cloud or a regular vm/snapshot combo. For the time you will need to use some moment in epoch. The optional items for adding the virtual tpm, stop on error I have set the default for what they are listed. As logoff policy I have chosen to set a default in WAIT_FOR_LOGOFF.
For this blog posts I have to go with the vm/snapshot combo as I don’t have streams setup at the moment. First I need to connect:
import requests, getpass, urllib, json, operator, numpy, time import vmware_horizon requests.packages.urllib3.disable_warnings() url="https://pod2cbr1.loft.lab" username = input("Username\n") domain = input("Domain\n") pw = getpass.getpass() hvconnectionobj = vmware_horizon.Connection(username = username,domain = domain,password = pw,url = url) hvconnectionobj.hv_connect() print("connected")
Than I open the ports for the classes I will be using
monitor = obj=vmware_horizon.Monitor(url=hvconnectionobj.url, access_token=hvconnectionobj.access_token) external=vmware_horizon.External(url=hvconnectionobj.url, access_token=hvconnectionobj.access_token) inventory=vmware_horizon.Inventory(url=hvconnectionobj.url, access_token=hvconnectionobj.access_token)
Now let’s look at what the desktop_pool_push_image method needs
First I will grab the correct desktop pool, I will use Pod02-Pool02 this time. There are several ways to get the correct pool but I have chosen to use this one.
desktop_pools=inventory.get_desktop_pools() desktop_pool = next(item for item in desktop_pools if item["name"] == "Pod02-Pool02") poolid=desktop_pool["id"]
To get the VM and Snapshots I first need to get the vCenter and datacenter id’s
vcenters = monitor.virtual_centers() vcid = vcenters[0]["id"] dcs = external.get_datacenters(vcenter_id=vcid) dcid = dcs[0]["id"]
I created a new golden image last Friday and it has this name: W10-L-2021-03-19-17-27 so I need to get the compatible base vm’s and get the id for this one
base_vms = external.get_base_vms(vcenter_id=vcid,datacenter_id=dcid,filter_incompatible_vms=True) base_vm = next(item for item in base_vms if item["name"] == "W10-L-2021-03-19-17-27") basevmid=base_vm["id"]
I had Packer create a snapshot and I can get that in a similar way
base_snapshots = external.get_base_snapshots(vcenter_id=vcid, base_vm_id=base_vm["id"]) base_snapshot = next(item for item in base_snapshots if item["name"] == "Created by Packer") snapid=base_snapshot["id"]
I get the current time in epoch using the time module (google is your best friend to define a moment in the future in epoch)
current_time = time.time()
For this example I add all the arguments but if you don’t change fromt he defaults that’s not needed
inventory.desktop_pool_push_image(desktop_pool_id=poolid,parent_vm_id=basevmid,snapshot_id=snapid, start_time=current_time, add_virtual_tpm=False, stop_on_first_error=False, logoff_policy="FORCE_LOGOFF")
And closing the connection
end=hvconnectionobj.hv_disconnect() print(end)
and when I now look at my desktop pool it’s pushing the new image
I have created a new folder on Github for examples and the script to deploy new images is the first example. I did move a couple of the names to variables so make ie better usable. You can find it here. Or see the code below this.
import requests, getpass, urllib, time import vmware_horizon requests.packages.urllib3.disable_warnings() url = "https://pod2cbr1.loft.lab" desktop_pool_name = "Pod02-Pool01" base_vm_name = "W10-L-2021-03-19-17-27" snapshot_name = "Snap_2" username = input("Username\n") domain = input("Domain\n") pw = getpass.getpass() hvconnectionobj = vmware_horizon.Connection(username = username,domain = domain,password = pw,url = url) hvconnectionobj.hv_connect() print("connected") monitor = obj=vmware_horizon.Monitor(url=hvconnectionobj.url, access_token=hvconnectionobj.access_token) external=vmware_horizon.External(url=hvconnectionobj.url, access_token=hvconnectionobj.access_token) inventory=vmware_horizon.Inventory(url=hvconnectionobj.url, access_token=hvconnectionobj.access_token) desktop_pools=inventory.get_desktop_pools() desktop_pool = next(item for item in desktop_pools if item["name"] == desktop_pool_name) poolid=desktop_pool["id"] vcenters = monitor.virtual_centers() vcid = vcenters[0]["id"] dcs = external.get_datacenters(vcenter_id=vcid) dcid = dcs[0]["id"] base_vms = external.get_base_vms(vcenter_id=vcid,datacenter_id=dcid,filter_incompatible_vms=True) base_vm = next(item for item in base_vms if item["name"] == base_vm_name) basevmid=base_vm["id"] base_snapshots = external.get_base_snapshots(vcenter_id=vcid, base_vm_id=base_vm["id"]) base_snapshot = next(item for item in base_snapshots if item["name"] == snapshot_name) snapid=base_snapshot["id"] current_time = time.time() inventory.desktop_pool_push_image(desktop_pool_id=poolid,parent_vm_id=basevmid,snapshot_id=snapid, start_time=current_time) end=hvconnectionobj.hv_disconnect() print(end)
Pingback: Pushing a new image using the VMware Horizon Python Module