def migrate(request): if request.method != "POST" or "secret" not in request.POST or "action" not in request.POST: return HttpResponseRedirect("%s" % (reverse("403"))) #endif secret = request.POST['secret'] action = request.POST['action'] permis = request.session.get("domains.domain.migrate", {}) if secret not in permis: return HttpResponseRedirect("%s" % (reverse("403"))) #endif domainData = permis[secret][1] domain = domainData['domain'] if not isAllowedTo(request, domain, "migrate_domain"): return HttpResponseRedirect("%s" % (reverse("403"))) #endif data = { "status": 200, "statusMessage": _("OK") } if action == "nodeList": # Ziskaj zoznam uzlov, na ktore mas pravo "add_domain" nodes = getNodes(request, "add_domain") # Filtruj iba uzle, ktore maju rovnaky hypervizor nodes = nodes.filter(driver=domain.node.driver).exclude(name=domain.node.name) # Vsetky otestuje na status availNodes = [] for node in nodes: try: virNode = virtualization.virNode(node) result = virNode.getInfo() except: continue finally: availNodes.append(node) #endtry #endfor data['nodes'] = [ { "label": node.name, "value": node.id } for node in availNodes ] elif action == "migrate": # Premigruje sa domena a v DB sa zmeni odkaz na node nodeId = int(request.POST['node']) node = get_object_or_404(Node, id=nodeId) try: virDomain = virtualization.virDomain(domain) virDomain.migrate(node) except virtualization.ErrorException, e: data['error'] = unicode(e) else: domain.node = node data['migrated'] = True data['id'] = domain.id
def __init__(self, domain): threading.Thread.__init__(self) self.domain = domain self.time = None self.cpuTime = None self.cores = None self.virDomain = virtualization.virDomain(domain)
def checkStatus(request, domainId): domain = get_object_or_404(Domain, id=domainId) if not isAllowedTo(request, domain, "view_domain"): return HttpResponseRedirect("%s" % (reverse("403"))) #endif data = { "status": 200, "statusMessage": _("OK") } try: virDomain = virtualization.virDomain(domain) status = virDomain.getState() del virDomain except virtualization.ErrorException, e: data['status'] = 503 data['statusMessage'] = _(unicode(e)) return HttpResponse(simplejson.dumps(data))
def edit_ajax(request): if request.method != "POST" or "secret" not in request.POST: raise Http404 #endif secret = request.POST['secret'] secrets = request.session.get("domains.domain.edit", {}) if not secret in secrets or (time.time() - secrets[secret][1]) > 1800: raise Http404 #endif domainId = secrets[secret][0] domain = get_object_or_404(Domain, id=domainId) data = { "status": 200, "statusMessage": _("OK") } try: virDomain = virtualization.virDomain(domain) if "vcpu" in request.POST: vcpus = int(request.POST['vcpu']) virDomain.setVCPUs(vcpus) elif "max_memory" in request.POST: memory = int(request.POST['max_memory']) virDomain.setMaxMemory(memory) elif "cur_memory" in request.POST: memory = int(request.POST['cur_memory']) virDomain.setMemory(memory) else: data['status'] = 404 data['statusMessage'] = _("Parameter not found!") #endif except virtualization.ErrorException, e: data['status'] = 503 data['statusMessage'] = _(unicode(e))
def command(request): if not request.method == "POST" or not "command" in request.POST \ or not "secret" in request.POST: raise Http404 #endif secret = request.POST['secret'] command = request.POST['command'] if command not in [ "run", "shutdown", "suspend", "reboot", "save", "destroy" ]: raise Http404 #endif secrets = request.session.get("domains.domain.command", {}) if not secret in secrets or (time.time() - secrets[secret][1]) > 1800: raise Http404 #endif domainId = secrets[secret][0] domain = get_object_or_404(Domain, id=domainId) data = { "status": 200, "statusMessage": _("OK") } # Test Permissions if (command == "run" and not isAllowedTo(request, domain, "resume_domain")) \ or (command == "shutdown" and not isAllowedTo(request, domain, "shutdown_domain")) \ or (command == "suspend" and not isAllowedTo(request, domain, "suspend_domain")) \ or (command == "reboot" and not isAllowedTo(request, domain, "reboot_domain")) \ or (command == "save" and not isAllowedTo(request, domain, "save_domain")) \ or (command == "destroy" and not isAllowedTo(request, domain, "delete_domain")): data['status'] = 403 data['statusMessage'] = _("You don't have permission to do this.") #endif try: virDomain = virtualization.virDomain(domain) if command == "run": status = virDomain.getState() # TODO: If domain is shutoff --> start it (or recreate) if status == virtualization.DOMAIN_STATE_PAUSED: virDomain.resume() elif status == virtualization.DOMAIN_STATE_SHUTOFF: virDomain.run() #endif elif command == "shutdown": virDomain.shutdown() elif command == "suspend": virDomain.suspend() elif command == "reboot": virDomain.reboot() elif command == "save": # TODO: Generate some name for the file # where the domain will be saved # Not Implemented data['status'] = 501 data['status'] = _("Currently unsupported by Libvirt.") elif command == "destroy": virDomain.destroy() #endif except virtualization.ErrorException, e: data['status'] = 503 data['statusMessage'] = _(unicode(e))