[ovirt-users] Python API question

Juan Hernández jhernand at redhat.com
Fri Jan 15 05:04:45 EST 2016


On 01/15/2016 05:23 AM, Colin Coe wrote:
> Hi all
> 
> I've written a Python script to take nightly snapshots of VMs which are
> kept for x days.  After x days the snapshot is deleted.
> 
> I can't work out how to wait for the snapshot deletion to complete.  I
> know how to do it for creating the snapshot but I've not been able to
> get it right for the deletion.
> 
>      
> api.vms.get(VM_NAME).snapshots.add(params.Snapshot(description=SNAPSHOT_NAME,
> vm=api.vms.get(VM_NAME), persist_memorystate=True))
>       while api.vms.get(VM_NAME).status.state == 'image_locked':
>           sleep(1)
> 
> Any ideas?
> 
> Thanks
> 
> CC
> 
> 

The more reliable way to wait till the snapshot is effectively deleted
is just to try to get it, and finish when the result is "None":

  snapshot.delete()

  while vm.snapshots.get(id=snapshot.get_id()) is not None:
    sleep(1)

Alternatively you can use the return of the delete operation. It returns
an "Action" object, that contains a reference to the job that was
started to delete the snapshot. The XML representation looks like this:

  <action>
    <job href="/api/jobs/f6e4279a-a8e1-44a3-86db-d53336b0eb5a"
id="f6e4279a-a8e1-44a3-86db-d53336b0eb5a"/>
    <status>
      <state>complete</state>
    </status>
  </action>

As you can see that contains a link to the job. With the Python SDK you
can use it as follows:

  # Get the id of the job:
  action = snapshot.delete()
  job_id = action.get_job().get_id()

  # Wait till the job is finished:
  while api.jobs.get(id=job_id).get_status().get_state() != 'FINISHED':
    time.sleep(1)

This is less reliable, because actions may or may not return a job, and
that depends on how the action is implemented in the engine, and there
is no backwards compatibility guarantee for that. A minor change in the
engine may result in the job not being returned.

So my suggestion is to use the simple loop to check if the snapshot exists.

-- 
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta
3ºD, 28016 Madrid, Spain
Inscrita en el Reg. Mercantil de Madrid – C.I.F. B82657941 - Red Hat S.L.


More information about the Users mailing list