Commit 1067a9ec by Dudás Ádám

vm: derive instance state from activity log

parent b478ebb0
...@@ -166,14 +166,6 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -166,14 +166,6 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
* base resource configuration values * base resource configuration values
* owner and privilege information * owner and privilege information
""" """
STATES = [('NOSTATE', _('nostate')),
('RUNNING', _('running')),
('BLOCKED', _('blocked')),
('PAUSED', _('paused')),
('SHUTDOWN', _('shutdown')),
('SHUTOFF', _('shutoff')),
('CRASHED', _('crashed')),
('PMSUSPENDED', _('pmsuspended'))] # libvirt domain states
ACL_LEVELS = ( ACL_LEVELS = (
('user', _('user')), # see all details ('user', _('user')), # see all details
('operator', _('operator')), # console, networking, change state ('operator', _('operator')), # console, networking, change state
...@@ -204,7 +196,6 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -204,7 +196,6 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
related_name='instance_set', related_name='instance_set',
help_text=_("Current hypervisor of this instance."), help_text=_("Current hypervisor of this instance."),
verbose_name=_('host node')) verbose_name=_('host node'))
state = CharField(choices=STATES, default='NOSTATE', max_length=20)
disks = ManyToManyField(Disk, related_name='instance_set', disks = ManyToManyField(Disk, related_name='instance_set',
help_text=_("Set of mounted disks."), help_text=_("Set of mounted disks."),
verbose_name=_('disks')) verbose_name=_('disks'))
...@@ -258,6 +249,19 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -258,6 +249,19 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
parts = [self.name, "(" + str(self.id) + ")"] parts = [self.name, "(" + str(self.id) + ")"]
return " ".join([s for s in parts if s != ""]) return " ".join([s for s in parts if s != ""])
@property
def state(self):
"""State of the virtual machine instance.
"""
if self.activity_log.filter(activity_code__endswith='migrate',
finished__isnull=True).exists():
return 'MIGRATING'
act = next(self.activity_log.filter(finished__isnull=False,
resultant_state__isnull=False)
.order_by('-finished')[:1], None)
return 'NOSTATE' if act is None else act.resultant_state
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
if self.time_of_delete is None: if self.time_of_delete is None:
self.renew(which='delete') self.renew(which='delete')
...@@ -914,6 +918,4 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -914,6 +918,4 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
logger.info('Instance %s state change ignored: %s', logger.info('Instance %s state change ignored: %s',
unicode(self), unicode(e)) unicode(self), unicode(e))
else: else:
self.state = new_state
self.save()
post_state_changed.send(sender=self, new_state=new_state) post_state_changed.send(sender=self, new_state=new_state)
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