<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none"><!--P{margin-top:0;margin-bottom:0;} p
        {margin-top:0;
        margin-bottom:0}--></style>
</head>
<body dir="ltr" style="font-size:12pt;color:#000000;background-color:#FFFFFF;font-family:Calibri,Arial,Helvetica,sans-serif;">
<p>Noticed that the code I wrote turned out a little wonky with a really misplaced &quot;while&quot; after the &quot;snap&quot; variable but I hope you understand what I was trying to get at.</p>
<p><br>
</p>
<p>#copy-paste fail :)</p>
<p><br>
</p>
<p>/K</p>
<p><br>
</p>
<div style="font-size:12pt; color:#000000; background-color:#FFFFFF; font-family:Calibri,Arial,Helvetica,sans-serif" dir="ltr">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>Från:</b> users-bounces@ovirt.org &lt;users-bounces@ovirt.org&gt; för Karli Sjöberg &lt;karli.sjoberg@slu.se&gt;<br>
<b>Skickat:</b> den 16 januari 2016 10:13<br>
<b>Till:</b> Colin Coe; Juan Hernández<br>
<b>Kopia:</b> Users@ovirt.org<br>
<b>Ämne:</b> Re: [ovirt-users] Python API question</font>
<div>&nbsp;</div>
</div>
<div>
<p><br>
</p>
<p>Hi Colin,</p>
<p><br>
</p>
<p>I´ve also noticed that behaviour, you need to redefine the variable in the loop as well, seems that &quot;snap == 'locked'&quot; forever otherwise:</p>
<p><br>
</p>
snap = api.vms.get(name=VM_NAME).snapshots.list()[-1]
<div>while (snap.get_snapshot_status() == 'locked'):<br>
</div>
&nbsp; &nbsp;print &quot;Waiting for snapshot creation to finish (status is %s)&quot; % snap.get_snapshot_status()<br>
&nbsp; &nbsp;snap = api.vms.get(name=VM_NAME).snapshots.list()[-1] while (snap.get_snapshot_status() == 'locked'):<br>
&nbsp; &nbsp;time.sleep(1)<br>
<br>
/K<br>
<br>
<div style="color:rgb(33,33,33)">
<hr tabindex="-1" style="display:inline-block; width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font style="font-size:11pt" face="Calibri, sans-serif" color="#000000"><b>Från:</b> users-bounces@ovirt.org &lt;users-bounces@ovirt.org&gt; för Colin Coe &lt;colin.coe@gmail.com&gt;<br>
<b>Skickat:</b> den 16 januari 2016 06:07<br>
<b>Till:</b> Juan Hernández<br>
<b>Kopia:</b> Users@ovirt.org<br>
<b>Ämne:</b> Re: [ovirt-users] Python API question</font>
<div>&nbsp;</div>
</div>
<div>
<div dir="ltr">Hi again all
<div><br>
</div>
<div>I've just noticed that the snapshot status appears not to update.</div>
<div><br>
</div>
<div>Consider the code below:</div>
<div>---</div>
<div>
<div>api.vms.get(VM_NAME).snapshots.add(params.Snapshot(description=SNAPSHOT_NAME, vm=api.vms.get(VM_NAME), persist_memorystate=True))</div>
<div><br>
</div>
<div># Wait for snapshot to finish</div>
<div>snap = api.vms.get(name=VM_NAME).snapshots.list()[-1]</div>
<div>while (snap.get_snapshot_status() == 'locked'):<br>
</div>
<div>&nbsp; &nbsp;print &quot;Waiting for snapshot creation to finish (status is %s)&quot; % snap.get_snapshot_status()</div>
<div>---<br>
</div>
</div>
<div><br>
</div>
<div>What I find is that even though the WebUI reports the snapshot is created, snap.get_snapshot_status() still reports 'locked'.</div>
<div><br>
</div>
<div>I'm using RHEV 3.5.7 with RHEL-H 7.1 hosts with vdsm-4.16.30-1.el7ev.x86_64.</div>
<div><br>
</div>
<div>Am I misunderstanding how this works or have I found a bug?</div>
<div><br>
</div>
<div>Thanks</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Jan 15, 2016 at 6:04 PM, Juan Hernández <span dir="ltr">
&lt;<a href="mailto:jhernand@redhat.com" target="_blank">jhernand@redhat.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
<div class="HOEnZb">
<div class="h5">On 01/15/2016 05:23 AM, Colin Coe wrote:<br>
&gt; Hi all<br>
&gt;<br>
&gt; I've written a Python script to take nightly snapshots of VMs which are<br>
&gt; kept for x days.&nbsp; After x days the snapshot is deleted.<br>
&gt;<br>
&gt; I can't work out how to wait for the snapshot deletion to complete.&nbsp; I<br>
&gt; know how to do it for creating the snapshot but I've not been able to<br>
&gt; get it right for the deletion.<br>
&gt;<br>
&gt;<br>
&gt; api.vms.get(VM_NAME).snapshots.add(params.Snapshot(description=SNAPSHOT_NAME,<br>
&gt; vm=api.vms.get(VM_NAME), persist_memorystate=True))<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp;while api.vms.get(VM_NAME).status.state == 'image_locked':<br>
&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sleep(1)<br>
&gt;<br>
&gt; Any ideas?<br>
&gt;<br>
&gt; Thanks<br>
&gt;<br>
&gt; CC<br>
&gt;<br>
&gt;<br>
<br>
</div>
</div>
The more reliable way to wait till the snapshot is effectively deleted<br>
is just to try to get it, and finish when the result is &quot;None&quot;:<br>
<br>
&nbsp; snapshot.delete()<br>
<br>
&nbsp; while vm.snapshots.get(id=snapshot.get_id()) is not None:<br>
&nbsp; &nbsp; sleep(1)<br>
<br>
Alternatively you can use the return of the delete operation. It returns<br>
an &quot;Action&quot; object, that contains a reference to the job that was<br>
started to delete the snapshot. The XML representation looks like this:<br>
<br>
&nbsp; &lt;action&gt;<br>
&nbsp; &nbsp; &lt;job href=&quot;/api/jobs/f6e4279a-a8e1-44a3-86db-d53336b0eb5a&quot;<br>
id=&quot;f6e4279a-a8e1-44a3-86db-d53336b0eb5a&quot;/&gt;<br>
&nbsp; &nbsp; &lt;status&gt;<br>
&nbsp; &nbsp; &nbsp; &lt;state&gt;complete&lt;/state&gt;<br>
&nbsp; &nbsp; &lt;/status&gt;<br>
&nbsp; &lt;/action&gt;<br>
<br>
As you can see that contains a link to the job. With the Python SDK you<br>
can use it as follows:<br>
<br>
&nbsp; # Get the id of the job:<br>
&nbsp; action = snapshot.delete()<br>
&nbsp; job_id = action.get_job().get_id()<br>
<br>
&nbsp; # Wait till the job is finished:<br>
&nbsp; while api.jobs.get(id=job_id).get_status().get_state() != 'FINISHED':<br>
&nbsp; &nbsp; time.sleep(1)<br>
<br>
This is less reliable, because actions may or may not return a job, and<br>
that depends on how the action is implemented in the engine, and there<br>
is no backwards compatibility guarantee for that. A minor change in the<br>
engine may result in the job not being returned.<br>
<br>
So my suggestion is to use the simple loop to check if the snapshot exists.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Dirección Comercial: C/Jose Bardasano Baos, 9, Edif. Gorbea 3, planta<br>
3ºD, 28016 Madrid, Spain<br>
Inscrita en el Reg. Mercantil de Madrid &#8211; C.I.F. B82657941 - Red Hat S.L.<br>
</font></span></blockquote>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>