def post(self, veid): try: sql = models.VM.select(models.VM.q.veid == int(veid))[0] except IndexError: self.redirect(self.reverse_url("containers")+"?error=1") return if sql.user != self.current_user and sql.user: self.redirect(self.reverse_url("containers")+"?error=1") return hostnames = map(lambda x: x.hostname,openvz.listVM()) if self.get_argument("hostname") in hostnames and self.get_argument("hostname") != sql.vz.hostname: self.redirect(self.reverse_url("vminfo", veid)+"?error=3") return change = [] # Hostname change if self.get_argument("hostname") != sql.vz.hostname: sql.vz.hostname = self.get_argument("hostname") change.append("hostname") # Disk space change if int(float(self.get_argument("disk"))) != sql.vz.diskinfo[0]: sql.vz.diskinfo = float(self.get_argument("disk")) change.append("disk") # Memory change if int(float(self.get_argument("ram"))) != sql.vz.memlimit[0]: ram = float(self.get_argument("ram")) burst = math.floor(ram+(ram*int(_config.get("billing", "memoryBurst"))/100)) memlimit = [ram, burst, burst+10240] sql.vz.memlimit = memlimit change.append("memlimit") if change: if sql.vz.running: sql.vz.restart().wait() self.redirect(self.reverse_url("vminfo", veid)+"?message=1") else: self.redirect(self.reverse_url("vminfo", veid)+"?message=2")
def get(self): import commands, re # copied from openvz.VM.meminfo d = open("/proc/meminfo").read().strip() out= {} for i in d.split("\n"): i = re.split(":[ ]+", i) if i[1].endswith(" kB"): i[1] = int(i[1].split(" ")[0])*1024 out[i[0]] = i[1] mem=out.copy() # copied from openvz.VM.diskinfo d = commands.getoutput("df -P") d = re.split(" [ ]+", d.split("\n")[1]) disk = [int(d[1])*1000, int(d[2])*1000, int(d[3])*1000] # copied from openvz.VM.loadAvg d = open("/proc/loadavg").read() loadAvg= map(lambda x:float(x), d.split(" ")[:3]) # copied from openvz.VM.uptime uptime = float(open("/proc/uptime").read().split(" ")[0]) dist = commands.getoutput("lsb_release -a|grep Description").split("\n")[1].split("\t")[1] kernel = commands.getoutput("uname -a") nproc = commands.getoutput('ps ax | wc -l | tr -d " "') runningVM = len(filter(lambda x: x.running,openvz.listVM())) cpu = commands.getoutput('cat /proc/cpuinfo |grep "model name"').split("\n")[0].split("\t")[1][2:] hostname = open("/etc/hostname").read().strip() self.render("spec.html", mem=mem, disk=disk, loadAvg=loadAvg, os=dist, uptime=uptime, kernel=kernel, nproc=nproc, runningVM=runningVM, cpu=cpu, hostname=hostname, title=_("Host OS specification"))
def get(self): # sync the vm list for i in openvz.listVM(): if not models.VM.select(models.VM.q.veid == i.veid).count(): models.VM(veid=i.veid) errmsg = "" txtmsg = "" if self.get_argument("error", None): err = self.get_argument("error") if err == "1": # NOTE: When accessing /vm/<veid> but OvzCP detect that the VM is not belongs to the current user, it will return this error errmsg = _("VM not owned by current user") elif err == "2": errmsg = _("VM is not running") elif err == "3": # NOTE: Displayed when trying to claim a VM that belongs to yourself errmsg = _("VM is already owned by current user") elif err == "4": # NOTE: Displayed when trying to start a VM but user's credit is lower than 1,000 credits. The %s is the unit name such as credit errmsg = _("%s is under 1,000, please refill.") % _config.get("billing", "unit") elif err == "5": errmsg = _("VM is running") elif err == "6": # NOTE: Displayed when trying to create a VM but user's credit is lower than 5,000 credits. The %s is the unit name such as credit errmsg = _("You need 5,000 %s to create a VM." % _config.get("billing", "unit")) if self.get_argument("msg", None): msg = self.get_argument("msg") if msg == "1": txtmsg = _("VM now belongs to you") elif msg == "2": txtmsg = _("VM ownership removed. Other can now claim this VM") elif msg == "3": txtmsg = _("VM destroyed") self.render("container.html", container=myVM(self.current_user), title=_("Containers"), error=errmsg, message=txtmsg)
def post(self): if self.current_user.credit < 5000: self.redirect(self.reverse_url("containers")+"?error=6") return if not self.get_argument("tos", False): self.redirect(self.reverse_url("createvm")+"?error=1") return if self.get_argument("os", "_/|\_") not in openvz.listTemplates(): self.redirect(self.reverse_url("createvm")+"?error=2") return if self.get_argument("root", "omgwtflol") != self.get_argument("root2", "roflcopters"): self.redirect(self.reverse_url("createvm")+"?error=3") return hostnames = map(lambda x: x.hostname,openvz.listVM()) if not re.match("^([\.0-9A-Za-z_\-]+)$", self.get_argument("hostname", "^^!!!!")) or self.get_argument("hostname") in hostnames: self.redirect(self.reverse_url("createvm")+"?error=4") return try: models.Nonce.get(int(self.get_argument("nonce"))).destroySelf() except: self.redirect(self.reverse_url("createvm")+"?error=5") return try: veid = openvz.listVM()[-1].veid+1 except IndexError: veid = 101 if not models.VM.select(models.VM.q.veid == veid).count(): models.VM(veid=veid, user=self.current_user) else: v = models.VM.select(models.VM.q.veid == veid)[0] v.user = self.current_user self.redirect(self.reverse_url("containers")) vm=openvz.createVM(self.get_argument("os"), veid, _config.get("iface", "nameserver"), self.get_argument("root")) # hostname vm.hostname = self.get_argument("hostname") # ram ram = float(self.get_argument("ram")) burst = math.floor(ram+(ram*int(_config.get("billing", "memoryBurst"))/100)) memlimit = [ram, burst, burst+10240] vm.memlimit = memlimit # disk vm.diskinfo = float(self.get_argument("disk")) # add IP vm.ip = _config.get("iface", "vmIP")+str(vm.veid)
def get(self): # sync the vm list for i in openvz.listVM(): if not models.VM.select(models.VM.q.veid == i.veid).count(): models.VM(veid=i.veid) vmcost = [] for i in myVM(self.current_user, True): if i.vz.running: vmcost.append((i.veid, vmBilling(i.vz))) cloud = get_cloud_usage(self.current_user.email) totalcost = 0 for i in myVM(self.current_user, True): if i.vz.running: totalcost += vmBilling(i.vz) self.render("billing.html", vmcost=vmcost, title=_("Billing"), cloud=cloud, totalcost=totalcost)
def get(self, veid): try: sql = models.VM.select(models.VM.q.veid == int(veid))[0] except IndexError: self.redirect(self.reverse_url("containers")+"?error=1") return if sql.user != self.current_user and sql.user: self.redirect(self.reverse_url("containers")+"?error=1") return errmsg = "" txtmsg = "" if self.get_argument("error", None): pass hostnames = map(lambda x: x.hostname, filter(lambda x: x.veid != int(veid),openvz.listVM())) self.render("edit.html", veid=veid, vz=sql.vz, title=_("Edit %s")%veid, error=errmsg, message=txtmsg, hostnames=hostnames)
def get(self): if self.current_user.credit < 5000: self.redirect(self.reverse_url("containers")+"?error=6") return hostnames = map(lambda x: x.hostname,openvz.listVM()) err="" if self.get_argument("error", None): e = self.get_argument("error") if e == "1": err=_("Terms of Service not accepted") elif e == "2": err=_("Invalid template name") elif e == "3": err=_("Passwords do not match") elif e == "4": err=_("Invalid hostname") elif e == "5": err = _("Verification failed. Did you double-submitted the form?") import random nonce=models.Nonce().id self.render("create.html", templates=openvz.listTemplates(), hostnames = hostnames, title=_("Creating VM"), error=err, nonce=nonce)