[Users] Unable to get latest resource status
Alex Jia
ajia at redhat.com
Fri Apr 27 15:27:34 UTC 2012
Hi Juan,
Although I haven't try it, I think you're right, I should refresh VM
in loop body.
In addition, as I said, it will be convenient if we have wait_for_status
function like before.
Thanks,
Alex
----- Original Message -----
From: "Juan Hernandez" <juan.hernandez at redhat.com>
To: "Alex Jia" <ajia at redhat.com>
Cc: users at ovirt.org, "Rita Wu" <rwu at redhat.com>
Sent: Friday, April 27, 2012 8:17:05 PM
Subject: Re: [Users] Unable to get latest resource status
On 04/27/2012 12:06 PM, Alex Jia wrote:
> On 04/27/2012 04:06 PM, Juan Hernandez wrote:
>> On 04/26/2012 05:49 PM, Alex Jia wrote:
>>> I built ovirt-engine-sdk rpm based on git repo 'http://gerrit.ovirt.org/p/ovirt-engine-sdk.git',
>>> then the ovirt-engine-sdk works well for me, I can get resource information from Ovirt/RHEVM(3.0)
>>> on the rhel6.2.
>>>
>>> The only question is I can't get latest resource status when I changed VM from 'suspended' to 'up'
>>> status, the api.vms.get(vm_name).status.state is always 'suspended' status, I can get a correct
>>> VM status unless I reconnect Ovirt/RHEVM, it's not convenient for users, I remember there are reload
>>> and wait_for_status method in old python binding API of RHEV(python-rhev), the wait_for_status method
>>> will reload resource then get current resource status, however, I haven't found similar method in
>>> ovirt-engine-sdk.
>> Are you sure you are calling api.vms.get(vm_name) each time? Or are you
>> doing something like this:
>>
>> vm = api.vms.get(vm_name)
>> while vm.status.state == "suspended":
>> sleep(10)
> Yeah, I put 'api.vms.get(vm_name)' in a loop body like above codes.
>> If you are doing that the vm information is retrieved only once, not
>> each time. If this is the case try something like this:
>>
>> while api.vms.get(VM_NAME).status.state == "suspended":
>> sleep(1)
>>
>> Can you share that snippet of code so that I can try to reproduce it?
> Okay, I will list my snippet of code in here, you may replace 'logging'
> with 'print' then remove
> useless '()' if need. thanks.
>
>
> <snip>
> import time, logging
> from ovirtsdk.api import API
> from ovirtsdk.xml import params
>
> class RHEV(object):
> """
> RHEV class
> """
>
> def __init__(self, url, username, password):
> try:
> self.api = API(url, username, password)
> except Exception as e:
> logging.error('could not connect: %s\n' % str(e))
> else:
> logging.info('success: RHEV manager could be reached OK\n')
>
> def vm_start(self, vm_name):
> try:
> vm = self.api.vms.get(vm_name)
> if vm.status.state != 'up':
> logging.info('Starting VM')
> vm.start()
> logging.info('Waiting for VM to reach Up status')
> while vm.status.state != 'up':
I think that the problem is with the line above. The VM object that you
get with "self.api.vms.get(vm_name)" is not automatically refreshed, you
have to refresh it before each iteration calling the "api.vms.get(...)"
method again:
while api.vms.get(vm_name).status.state != 'up':
Can you try that?
> time.sleep(1)
> else:
> logging.debug('VM already up')
> except Exception as e:
> logging.error('Failed to start VM:\n%s' % str(e))
>
> def vm_suspend(self, vm_name):
> vm = self.api.vms.get(vm_name)
> while vm.status.state != 'suspended':
> try:
> logging.info('Suspend VM')
> vm.suspend()
> logging.info('Waiting for VM to reach suspended status')
> while vm.status.state != 'suspended':
Same here ^.
> time.sleep(1)
>
> except Exception as e:
> if e.reason == 'Bad Request' \
> and 'asynchronous running tasks' in e.detail:
> logging.warning('VM has asynchronous running tasks,
> trying again')
> time.sleep(1)
> else:
> logging.error('Failed to suspend VM:\n%s' % str(e))
> break
>
> def vm_resume(self, vm_name):
> try:
> vm = self.api.vms.get(vm_name)
> if vm.status.state != 'up':
> logging.info('Resume VM')
> vm.start()
> logging.info('Waiting for VM to resume')
> while vm.status.state != 'up':
Same here ^.
> time.sleep(1)
> else:
> logging.debug('VM already up')
> except Exception as e:
> logging.error('Failed to resume VM:\n%s' % str(e))
>
> </snip>
>
More information about the Users
mailing list