Commit 15b660c1 by Bach Dániel

one: UpdateInstanceStateTask added

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