def edit(request, domainId): domain = get_object_or_404(Domain, id=domainId) if not isAllowedTo(request, domain, "change_domain"): return HttpResponseRedirect("%s" % (reverse("403"))) #endif secrets = request.session.get("domains.domain.edit", {}) secret = sha.sha(str(domain.id) + ":" + str(time.time())).hexdigest() secrets[secret] = (domainId, time.time()) # Session cleanup hashes = secrets.keys() for per in hashes: if (time.time() - secrets[per][1]) > 1800: del secrets[per] #endif #endfor request.session['domains.domain.edit'] = secrets return render_to_response( "domains/domain/edit.html", { "managedDomain": domain, "secret": secret }, context_instance=RequestContext(request) )
def migrate(request, domainId): domain = get_object_or_404(Domain, id=domainId) if not isAllowedTo(request, domain, "migrate_domain"): return HttpResponseRedirect("%s" % (reverse("403"))) #endif permis = request.session.get("domains.domain.migrate", {}) permisHash = sha.sha(request.user.username.upper() + ":" + str(time.time())).hexdigest() permis[permisHash] = (time.time(), { "domain": domain }) # Session cleanup hashes = permis.keys() for per in hashes: if (time.time() - permis[per][0]) > 3600: del permis[per] #endif #endfor request.session['domains.domain.migrate'] = permis return render_to_response( "domains/domain/migrate.html", { "domain": domain, "secret": permisHash }, context_instance=RequestContext(request) )
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 detail(request, domainId): domain = get_object_or_404(Domain, id=domainId) if not isAllowedTo(request, domain, "view_domain"): return HttpResponseRedirect("%s" % (reverse("403"))) #endif secrets = request.session.get("domains.domain.command", {}) secret = sha.sha(str(domain.id) + ":" + str(time.time())).hexdigest() secrets[secret] = (domainId, time.time()) # Session cleanup hashes = secrets.keys() for per in hashes: if (time.time() - secrets[per][1]) > 1800: del secrets[per] #endif #endfor request.session['domains.domain.command'] = secrets return render_to_response( "domains/domain/detail.html", { "managedDomain": domain, "secret": secret, "run": isAllowedTo(request, domain, "resume_domain"), "shutdown": isAllowedTo(request, domain, "shutdown_domain"), "suspend": isAllowedTo(request, domain, "suspend_domain"), "reboot": isAllowedTo(request, domain, "reboot_domain"), "save": isAllowedTo(request, domain, "save_domain") }, context_instance=RequestContext(request) )
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 remove(request, domainId): domain = get_object_or_404(Domain, id=domainId) if not isAllowedTo(request, domain, "delete_domain"): return HttpResponseRedirect("%s" % (reverse("403"))) #endif secrets = request.session.get("domains.domain.command", {}) secret = sha.sha(str(domain.id) + ":" + str(time.time())).hexdigest() secrets[secret] = (domainId, time.time()) # Session cleanup hashes = secrets.keys() for per in hashes: if (time.time() - secrets[per][1]) > 1800: del secrets[per] #endif #endfor request.session['domains.domain.command'] = secrets if request.method == "POST": if "yes" in request.POST and domain.id == int(request.POST['domainId']): domain.delete() #endif return HttpResponseRedirect( reverse("domains:domain_remove__select_domain") ) #endif return render_to_response( "domains/domain/remove.html", { "managedDomain": domain, "secret": secret }, context_instance=RequestContext(request) )
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))