[ovirt-users] Python API question

Karli Sjöberg karli.sjoberg at slu.se
Sat Jan 16 09:18:51 UTC 2016


Noticed that the code I wrote turned out a little wonky with a really misplaced "while" after the "snap" variable but I hope you understand what I was trying to get at.


#copy-paste fail :)


/K


________________________________
Från: users-bounces at ovirt.org <users-bounces at ovirt.org> för Karli Sjöberg <karli.sjoberg at slu.se>
Skickat: den 16 januari 2016 10:13
Till: Colin Coe; Juan Hernández
Kopia: Users at ovirt.org
Ämne: Re: [ovirt-users] Python API question



Hi Colin,


I´ve also noticed that behaviour, you need to redefine the variable in the loop as well, seems that "snap == 'locked'" forever otherwise:


snap = api.vms.get(name=VM_NAME).snapshots.list()[-1]
while (snap.get_snapshot_status() == 'locked'):
   print "Waiting for snapshot creation to finish (status is %s)" % snap.get_snapshot_status()
   snap = api.vms.get(name=VM_NAME).snapshots.list()[-1] while (snap.get_snapshot_status() == 'locked'):
   time.sleep(1)

/K

________________________________
Från: users-bounces at ovirt.org <users-bounces at ovirt.org> för Colin Coe <colin.coe at gmail.com>
Skickat: den 16 januari 2016 06:07
Till: Juan Hernández
Kopia: Users at ovirt.org
Ämne: Re: [ovirt-users] Python API question

Hi again all

I've just noticed that the snapshot status appears not to update.

Consider the code below:
---
api.vms.get(VM_NAME).snapshots.add(params.Snapshot(description=SNAPSHOT_NAME, vm=api.vms.get(VM_NAME), persist_memorystate=True))

# Wait for snapshot to finish
snap = api.vms.get(name=VM_NAME).snapshots.list()[-1]
while (snap.get_snapshot_status() == 'locked'):
   print "Waiting for snapshot creation to finish (status is %s)" % snap.get_snapshot_status()
---

What I find is that even though the WebUI reports the snapshot is created, snap.get_snapshot_status() still reports 'locked'.

I'm using RHEV 3.5.7 with RHEL-H 7.1 hosts with vdsm-4.16.30-1.el7ev.x86_64.

Am I misunderstanding how this works or have I found a bug?

Thanks

On Fri, Jan 15, 2016 at 6:04 PM, Juan Hernández <jhernand at redhat.com<mailto:jhernand at redhat.com>> wrote:
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/users/attachments/20160116/1cf71d7a/attachment-0001.html>


More information about the Users mailing list