XenServer Software Development Kit Guide 4.1.0

Using the API
21
# use a rotation as a permutation
hosts = [hosts[-1]] + hosts[:(len(hosts)-1)]
Each VM is then moved via XenMotion to the new host under this rotation (i.e. a VM running on host at
position 2 in the list will be moved to the host at position 1 in the list etc.) In order to execute each of
the movements in parallel, the asynchronous version of the VM.pool_migrate is used and a list of task
references constructed. Note the live flag passed to the VM.pool_migrate; this causes the VMs to be
moved while they are still running.
tasks = []
for i in range(0, len(vms)):
vm = vms[i]
host = hosts[i]
task = session.xenapi.Async.VM.pool_migrate(vm, host, { "live": "true" })
tasks.append(task)
The list of tasks is then polled for completion:
finished = False
records = {}
while not(finished):
finished = True
for task in tasks:
record = session.xenapi.task.get_record(task)
records[task] = record
if record["status"] == "pending":
finished = False
time.sleep(1)
Once all tasks have left the pending state (i.e. they have successfully completed, failed or been cancelled)
the tasks are polled once more to see if they all succeeded:
allok = True
for task in tasks:
record = records[task]
if record["status"] <> "success":
allok = False
If any one of the tasks failed then details are printed, an exception is raised and the task objects left around
for further inspection. If all tasks succeeded then the task objects are destroyed and the function returns.