Commit b31f9b65 by Őry Máté

vm: add Node.update_vm_states()

parent 8f4f5a93
...@@ -75,6 +75,7 @@ class Trait(Model): ...@@ -75,6 +75,7 @@ class Trait(Model):
class VirtualMachineDescModel(BaseResourceConfigModel): class VirtualMachineDescModel(BaseResourceConfigModel):
"""Abstract base for virtual machine describing models. """Abstract base for virtual machine describing models.
""" """
access_method = CharField(max_length=10, choices=ACCESS_METHODS, access_method = CharField(max_length=10, choices=ACCESS_METHODS,
...@@ -157,6 +158,36 @@ class Node(TimeStampedModel): ...@@ -157,6 +158,36 @@ class Node(TimeStampedModel):
else: else:
return default return default
def update_vm_states(self):
domains = {}
for i in self.remote_query(vm_tasks.list_domains_info, timeout=5):
# [{'name': 'cloud-1234', 'state': 'RUNNING', ...}, ...]
try:
id = int(i['name'].split('-')[1])
except:
pass # name format doesn't match
else:
domains[id] = i['state']
instances = self.instance_set.order_by('id').values('id', 'state')
for i in instances:
try:
d = domains[i['id']]
except KeyError:
logger.info('Node %s update: instance %s missing from '
'libvirt', self, i['id'])
else:
if d != i['state']:
logger.info('Node %s update: instance %s state changed '
'(libvirt: %s, db: %s)',
self, i['id'], d, i['state'])
Instance.objects.get(id=i['id']).state_changed(d)
del domains[i['id']]
for i in domains.keys():
logger.info('Node %s update: domain %s in libvirt but not in db.',
self, i)
def __unicode__(self): def __unicode__(self):
return self.name return self.name
...@@ -808,6 +839,11 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -808,6 +839,11 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
return tmpl return tmpl
def state_changed(self, new_state):
logger.debug('Instance %s state changed '
'(db: %s, new: %s)',
self, self.state, new_state)
class InstanceActivity(ActivityModel): class InstanceActivity(ActivityModel):
instance = ForeignKey(Instance, related_name='activity_log', instance = ForeignKey(Instance, related_name='activity_log',
......
...@@ -61,6 +61,11 @@ def list_domains(params): ...@@ -61,6 +61,11 @@ def list_domains(params):
pass pass
@celery.task(name='vmdriver.list_domains_info')
def list_domains_info(params):
pass
@celery.task(name='vmdriver.ping') @celery.task(name='vmdriver.ping')
def ping(params): def ping(params):
pass pass
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment