Commit 15b660c1 by Bach Dániel

one: UpdateInstanceStateTask added

parent 93f3d61a
...@@ -203,6 +203,7 @@ CELERY_ROUTES = { ...@@ -203,6 +203,7 @@ CELERY_ROUTES = {
'firewall.tasks.reload_blacklist_task': {'queue': 'firewall'}, 'firewall.tasks.reload_blacklist_task': {'queue': 'firewall'},
'firewall.tasks.Periodic': {'queue': 'local'}, 'firewall.tasks.Periodic': {'queue': 'local'},
'one.tasks.SendMailTask': {'queue': 'local'}, 'one.tasks.SendMailTask': {'queue': 'local'},
'one.tasks.UpdateInstanceStateTask': {'queue': 'local'}
} }
CACHES = { CACHES = {
......
#!/usr/bin/env python
from celery import Celery, task
import subprocess
import time, re
import socket
import sys
BROKER_URL = 'amqp://nyuszi:teszt@localhost:5672/django'
try:
from local_settings import *
except:
pass
CELERY_CREATE_MISSING_QUEUES=True
celery = Celery('tasks', broker=BROKER_URL)
def main(argv):
celery.send_task('one.tasks.UpdateInstanceStateTask', [ int(sys.argv[1]),
], queue='local')
if __name__ == "__main__":
main(sys.argv)
...@@ -487,7 +487,8 @@ class Instance(models.Model): ...@@ -487,7 +487,8 @@ class Instance(models.Model):
"""Submit a new instance to OpenNebula.""" """Submit a new instance to OpenNebula."""
from django.template.defaultfilters import escape from django.template.defaultfilters import escape
out = "" out = ""
inst = Instance(pw=pwgen(), template=template, owner=owner, share=share) inst = Instance(pw=pwgen(), template=template, owner=owner,
share=share, state='PENDING')
inst.save() inst.save()
hostname = u"cloud-%d" % (inst.id, ) hostname = u"cloud-%d" % (inst.id, )
with tempfile.NamedTemporaryFile(delete=False) as f: with tempfile.NamedTemporaryFile(delete=False) as f:
...@@ -551,7 +552,6 @@ class Instance(models.Model): ...@@ -551,7 +552,6 @@ class Instance(models.Model):
{'neptun': owner.username, 'template': template.name, {'neptun': owner.username, 'template': template.name,
'id': inst.one_id}) 'id': inst.one_id})
inst.save() inst.save()
inst.update_state()
host = Host(vlan=Vlan.objects.get(name=template.network.name), host = Host(vlan=Vlan.objects.get(name=template.network.name),
owner=owner) owner=owner)
host.hostname = hostname host.hostname = hostname
...@@ -618,17 +618,21 @@ class Instance(models.Model): ...@@ -618,17 +618,21 @@ class Instance(models.Model):
def _change_state(self, new_state): def _change_state(self, new_state):
"""Change host state in OpenNebula.""" """Change host state in OpenNebula."""
self._update_vm("<STATE>" + new_state + "</STATE>") self._update_vm("<STATE>" + new_state + "</STATE>")
self.waiting = True
self.save()
def stop(self): def stop(self):
self._change_state("STOPPED") self._change_state("STOPPED")
self.waiting = True
self.save()
def resume(self): def resume(self):
self._change_state("RESUME") self._change_state("RESUME")
def poweroff(self): def poweroff(self):
self._change_state("POWEROFF") self._change_state("POWEROFF")
def restart(self): def restart(self):
self._change_state("RESET") self._change_state("RESET")
def renew(self, which='both'): def renew(self, which='both'):
if which in ['suspend', 'both']: if which in ['suspend', 'both']:
self.time_of_suspend = self.share.get_type()['suspendx'] self.time_of_suspend = self.share.get_type()['suspendx']
...@@ -637,12 +641,12 @@ class Instance(models.Model): ...@@ -637,12 +641,12 @@ class Instance(models.Model):
if not (which in ['suspend', 'delete', 'both']): if not (which in ['suspend', 'delete', 'both']):
raise ValueError('No such expiration type.') raise ValueError('No such expiration type.')
self.save() self.save()
def save_as(self): def save_as(self):
"""Save image and shut down.""" """Save image and shut down."""
imgname = "template-%d-%d" % (self.template.id, self.id) imgname = "template-%d-%d" % (self.template.id, self.id)
self._update_vm('<DISK id="0"><SAVE_AS name="%s"/></DISK>' % imgname) self._update_vm('<DISK id="0"><SAVE_AS name="%s"/></DISK>' % imgname)
self._change_state("SHUTDOWN") self._change_state("SHUTDOWN")
self.waiting = True
self.save() self.save()
t = self.template t = self.template
t.state = 'SAVING' t.state = 'SAVING'
......
...@@ -5,6 +5,7 @@ import os ...@@ -5,6 +5,7 @@ import os
import sys import sys
import time import time
from django.core.mail import send_mail from django.core.mail import send_mail
from one.models import Instance
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -12,3 +13,15 @@ class SendMailTask(Task): ...@@ -12,3 +13,15 @@ class SendMailTask(Task):
def run(self, to, subject, msg, sender=u'noreply@cloud.ik.bme.hu'): def run(self, to, subject, msg, sender=u'noreply@cloud.ik.bme.hu'):
send_mail(subject, msg, sender, [ to ], fail_silently=False) send_mail(subject, msg, sender, [ to ], fail_silently=False)
logger.info("[django][one][tasks.py] %s->%s [%s]" % (sender, to, subject) ) logger.info("[django][one][tasks.py] %s->%s [%s]" % (sender, to, subject) )
class UpdateInstanceStateTask(Task):
def run(self, one_id):
print one_id
try:
inst = Instance.objects.get(one_id=one_id)
except:
print 'nincs ilyen'
return
inst.update_state()
print inst.state
...@@ -32,8 +32,6 @@ logger = logging.getLogger(__name__) ...@@ -32,8 +32,6 @@ logger = logging.getLogger(__name__)
def _list_instances(request): def _list_instances(request):
instances = Instance.objects.exclude(state='DONE').filter(owner=request.user) instances = Instance.objects.exclude(state='DONE').filter(owner=request.user)
for i in instances:
i.update_state()
instances = instances.exclude(state='DONE') instances = instances.exclude(state='DONE')
return instances return instances
...@@ -356,9 +354,6 @@ vm_list = login_required(VmListView.as_view()) ...@@ -356,9 +354,6 @@ vm_list = login_required(VmListView.as_view())
@login_required @login_required
def vm_show(request, iid): def vm_show(request, iid):
inst = get_object_or_404(Instance, id=iid, owner=request.user) inst = get_object_or_404(Instance, id=iid, owner=request.user)
inst.update_state()
if inst.template.state == "SAVING":
inst.check_if_is_save_as_done()
try: try:
ports = inst.firewall_host.list_ports() ports = inst.firewall_host.list_ports()
except: except:
...@@ -388,7 +383,6 @@ def vm_show(request, iid): ...@@ -388,7 +383,6 @@ def vm_show(request, iid):
@login_required @login_required
def vm_ajax_instance_status(request, iid): def vm_ajax_instance_status(request, iid):
inst = get_object_or_404(Instance, id=iid, owner=request.user) inst = get_object_or_404(Instance, id=iid, owner=request.user)
inst.update_state()
return HttpResponse(json.dumps({ return HttpResponse(json.dumps({
'booting': not inst.active_since, 'booting': not inst.active_since,
'state': inst.state, 'state': inst.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