Commit b88a5c9f by Dudás Ádám

vm: rearrage methods

parent 839207de
...@@ -276,38 +276,33 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -276,38 +276,33 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
else: else:
return act.resultant_state return act.resultant_state
def manual_state_change(self, new_state, reason=None, user=None): @classmethod
# TODO cancel concurrent activity (if exists) def create(cls, params, disks, networks, req_traits, tags):
act = InstanceActivity.create(code_suffix='manual_state_change', # create instance and do additional setup
instance=self, user=user) inst = cls(**params)
act.finished = act.started
act.result = reason
act.resultant_state = new_state
act.succeeded = True
act.save()
def vm_state_changed(self, new_state): # save instance
# log state change inst.full_clean()
try: inst.save()
act = InstanceActivity.create(code_suffix='vm_state_changed', inst.set_level(inst.owner, 'owner')
instance=self)
except ActivityInProgressError:
pass # discard state change if another activity is in progress.
else:
act.finished = act.started
act.resultant_state = new_state
act.succeeded = True
act.save()
if new_state == 'STOPPED': def __on_commit(activity):
self.vnc_port = None activity.resultant_state = 'PENDING'
self.node = None
self.save()
def clean(self, *args, **kwargs): with instance_activity(code_suffix='create', instance=inst,
if self.time_of_delete is None: on_commit=__on_commit, user=inst.owner) as act:
self._do_renew(which='delete') # create related entities
super(Instance, self).clean(*args, **kwargs) inst.disks.add(*[disk.get_exclusive() for disk in disks])
for net in networks:
Interface.create(instance=inst, vlan=net.vlan,
owner=inst.owner, managed=net.managed,
base_activity=act)
inst.req_traits.add(*req_traits)
inst.tags.add(*tags)
return inst
@classmethod @classmethod
def create_from_template(cls, template, owner, disks=None, networks=None, def create_from_template(cls, template, owner, disks=None, networks=None,
...@@ -369,33 +364,38 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel): ...@@ -369,33 +364,38 @@ class Instance(AclBase, VirtualMachineDescModel, TimeStampedModel):
return [cls.create(cps, disks, networks, req_traits, tags) return [cls.create(cps, disks, networks, req_traits, tags)
for cps in customized_params] for cps in customized_params]
@classmethod def clean(self, *args, **kwargs):
def create(cls, params, disks, networks, req_traits, tags): if self.time_of_delete is None:
# create instance and do additional setup self._do_renew(which='delete')
inst = cls(**params) super(Instance, self).clean(*args, **kwargs)
# save instance
inst.clean()
inst.save()
inst.set_level(inst.owner, 'owner')
def __on_commit(activity):
activity.resultant_state = 'PENDING'
with instance_activity(code_suffix='create', instance=inst,
on_commit=__on_commit, user=inst.owner) as act:
# create related entities
inst.disks.add(*[disk.get_exclusive() for disk in disks])
for net in networks: def manual_state_change(self, new_state, reason=None, user=None):
Interface.create(instance=inst, vlan=net.vlan, # TODO cancel concurrent activity (if exists)
owner=inst.owner, managed=net.managed, act = InstanceActivity.create(code_suffix='manual_state_change',
base_activity=act) instance=self, user=user)
act.finished = act.started
act.result = reason
act.resultant_state = new_state
act.succeeded = True
act.save()
inst.req_traits.add(*req_traits) def vm_state_changed(self, new_state):
inst.tags.add(*tags) # log state change
try:
act = InstanceActivity.create(code_suffix='vm_state_changed',
instance=self)
except ActivityInProgressError:
pass # discard state change if another activity is in progress.
else:
act.finished = act.started
act.resultant_state = new_state
act.succeeded = True
act.save()
return inst if new_state == 'STOPPED':
self.vnc_port = None
self.node = None
self.save()
@permalink @permalink
def get_absolute_url(self): def get_absolute_url(self):
......
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