def _runAction(self, action, attrs, direct): if action == "execute": fault.check("cmd" in attrs, "Command not given") try: return self.execute(attrs["cmd"]) except exceptions.CommandError, exc: raise fault.new(str(exc), fault.USER_ERROR)
def interfacesRename(self, name, properties): #@UnusedVariable, pylint: disable-msg=W0613 fault.check(self.state != State.STARTED, "Changes of running KVMs are not supported") iface = self.interfaceSetGet(name) newName = properties["name"] fault.check(re.match("eth(\d+)", newName), "Invalid interface name: %s" % name) try: if self.interfaceSetGet(newName): raise fault.new("Duplicate interface name: %s" % newName, fault.USER_ERROR) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass if self.state == State.PREPARED: connector = None connectionAttributes = None if iface.isConnected(): connector = iface.connection.connector connectionAttributes = iface.connection.upcast().toDict(None)["attrs"] connector.upcast().connectionsDelete(unicode(iface)) qm.deleteInterface(self.host, self.getVmid(), iface.name) iface.name = newName iface.save() if self.state == State.PREPARED: qm.addInterface(self.host, self.getVmid(), iface.name) if connector: connector.upcast().connectionsAdd(unicode(iface), connectionAttributes) self.save()
def stop(self, direct, noProcess=False): fault.check(self.state != State.CREATED, "Connector must be started or prepared to be stopped but is %s: %s", (self.state, self.name)) proc = tasks.Process("stop") proc.add(tasks.Task("renew", self.topology.renew)) proc.add(self.upcast().getStopTasks()) if noProcess: return proc.start(direct) return self.topology.startProcess(proc, direct)
def configure(self, properties): if self.state != State.CREATED: fault.check(not "network_type" in properties, "Cannot change type of external network with prepared connections: %s", self.name) fault.check(not "network_group" in properties, "Cannot change group of external network with prepared connections: %s", self.name) Connector.configure(self, properties) if self.getNetworkGroup() == "auto": self.setNetworkGroup(None) self.save()
def checkImage(host, path): assert host assert fileutil.existsFile(host, path) try: res = host.execute("tar -tzvf %s ./sbin/init" % util.escape(path)) fault.check("0/0" in res, "Image contents not owned by root") except exceptions.CommandError, err: return err.errorMessage
def destroy(self, direct, noProcess=False): fault.check(self.state != State.STARTED, "Connector must not be started to be destroyed but is %s: %s", (self.state, self.name)) proc = tasks.Process("destroy") proc.add(tasks.Task("renew", self.topology.renew)) proc.add(self.upcast().getDestroyTasks()) if noProcess: return proc.start(direct) return self.topology.startProcess(proc, direct)
def interfacesDelete(self, name): #@UnusedVariable, pylint: disable-msg=W0613 fault.check(self.state != State.STARTED, "Changes of running KVMs are not supported") iface = self.interfaceSetGet(name) if iface.isConnected(): iface.connection.connector.upcast().connectionsDelete(unicode(iface)) if self.state == State.PREPARED: qm.deleteInterface(self.host, self.getVmid(), iface.name) iface.delete()
def connectionsAdd(self, iface_name, properties): #@UnusedVariable, pylint: disable-msg=W0613 iface = self.topology.interfacesGet(iface_name) fault.check(iface.device.state != State.STARTED, "Cannot add connections to running device: %s -> %s", (iface_name, self.name) ) con = Connection () con.connector = self con.interface = iface con.init() con.save()
def configure(self, properties): if "template" in properties: fault.check(self.state == State.CREATED, "Cannot change template of prepared device: %s" % self.name) Device.configure(self, properties) if "template" in properties: self.setTemplate(properties["template"]) self._assignTemplate() self.save()
def interfacesRename(self, name, properties): iface = self.interfaceSetGet(name).upcast() try: fault.check(not self.interfaceSetGet(properties["name"]), "Duplicate interface name: %s" % properties["name"]) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface.name = properties["name"] iface.save()
def _runAction(self, action, attrs, direct): if action == "download_capture": interface = self.topology.interfacesGet(attrs["iface"]) fault.check(interface, "No such interface: %s", attrs["iface"]) con = interface.connection.upcast() assert con.connector.id == self.id return con.downloadCaptureUri() else: return Connector._runAction(self, action, attrs, direct)
def configure(self, properties): if "template" in properties: fault.check(self.state != State.STARTED, "Cannot change template of running device: %s" % self.name) Device.configure(self, properties) if "template" in properties: self.setTemplate(properties["template"]) if self.state == State.PREPARED: qm.useTemplate(self.host, self.getVmid(), self.getTemplate()) self.save()
def _runAction(self, action, attrs, direct): if action == "download_capture": interface = self.topology.interfacesGet(attrs["iface"]) fault.check(interface, "No such interface: %s", attrs["iface"]) con = interface.connection.upcast() assert con.connector.id == self.id return tasks.runTask(tasks.Task("%s-download-capture-uri" % self, con.downloadCaptureUri, kwargs={"onlyLatest": "onlyLatest" in attrs and attrs["onlyLatest"]})) else: return Connector._runAction(self, action, attrs, direct)
def _assignVtunData(self): host = None for con in self.connectionSetAll(): if con.interface.device.host: host = con.interface.device.host break fault.check(host, "Failed to assign a host for external access") if not self.getExternalAccessPort(): self.external_access_port = resources.take(host, "port", self, self.EXTERNAL_ACCESS_PORT_SLOT) self.save()
def prepare(self, direct, noProcess=False): fault.check(self.state == State.CREATED, "Connector must be created to be prepared but is %s: %s", (self.state, self.name)) for con in self.connectionSetAll(): dev = con.interface.device fault.check(dev.state != State.CREATED, "Device %s must be prepared before connector %s", (dev.name, self.name)) proc = tasks.Process("prepare") proc.add(tasks.Task("renew", self.topology.renew)) proc.add(self.upcast().getPrepareTasks()) if noProcess: return proc.start(direct) return self.topology.startProcess(proc, direct)
def interfacesRename(self, name, properties): iface = self.interfaceSetGet(name).upcast() import re newName = properties["name"] fault.check(re.match("eth(\d+)", newName), "Invalid interface name: %s" % name) try: fault.check(not self.interfaceSetGet(properties["name"]), "Duplicate interface name: %s" % properties["name"]) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface.name = properties["name"] iface.save()
def configure(self, properties): if "template" in properties: fault.check(self.state == State.CREATED, "Cannot change template of prepared device: %s" % self.name) if "args" in properties: fault.check(self.state != State.STARTED, "Cannot change arguments of running device: %s" % self.name) Device.configure(self, properties) if "template" in properties: self.setTemplate(properties["template"]) self._assignTemplate() if "args" in properties: self.setArgs(properties["args"]) self._assignTemplate() self.save()
def _assignVtunData(self): host = None if self.getExternalAccessCon(): host = self.getExternalAccessHost() if not host: for con in self.connectionSetAll(): if con.interface.device.host: self.setExternalAccessCon(con) host = con.interface.device.host self.setExternalAccessPort(None) break fault.check(host, "Failed to assign a host for external access") if not self.getExternalAccessPort(): host.takeId("port", self.setExternalAccessPort)
def interfacesRename(self, name, properties): iface = self.interfaceSetGet(name).upcast() if self.state == State.PREPARED or self.state == State.STARTED: vzctl.deleteInterface(self.host, self.getVmid(), iface.name) try: fault.check(not self.interfaceSetGet(properties["name"]), "Duplicate interface name: %s" % properties["name"]) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface.name = properties["name"] if self.state == State.PREPARED or self.state == State.STARTED: iface.prepare_run() if self.state == State.STARTED: iface.start_run() iface.save()
def interfacesRename(self, name, properties): iface = self.interfaceSetGet(name).upcast() if self.state == State.PREPARED or self.state == State.STARTED: vzctl.deleteInterface(self.host, self.getVmid(), iface.name) try: fault.check(not self.interfaceSetGet(properties["name"]), "Duplicate interface name: %s" % properties["name"]) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface.name = properties["name"] if self.state == State.PREPARED or self.state == State.STARTED: vzctl.addInterface(self.host, self.getVmid(), iface.name) if self.state == State.STARTED: self.connectToBridge(iface, self.getBridge(iface)) iface._configureNetwork() iface.save()
def interfacesAdd(self, name, properties): #@UnusedVariable, pylint: disable-msg=W0613 fault.check(self.state != State.STARTED, "Changes of running KVMs are not supported") fault.check(re.match("eth(\d+)", name), "Invalid interface name: %s" % name) iface = Interface() try: if self.interfaceSetGet(name): raise fault.new("Duplicate interface name: %s" % name) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface.name = name iface.device = self iface.init() if self.state == State.PREPARED: qm.addInterface(self.host, self.getVmid(), iface.name) iface.save() Device.interfaceSetAdd(self, iface)
def configure(self, properties): if "gateway4" in properties or "gateway6" in properties: fault.check(self.connector.state == State.CREATED, "Cannot change gateways on prepared or started router: %s" % self) dummynet.EmulatedConnection.configure(self, properties) if self.connector.type == "router": for key in ["gateway4", "gateway6"]: if key in properties: self.setAttribute(key, properties[key]) if not self.hasAttribute("gateway4"): self.setAttribute("gateway4", "10.0.0.254/24") if not self.hasAttribute("gateway6"): self.setAttribute("gateway6", "fd01:ab1a:b1ab:0:0:FFFF:FFFF:FFFF/80") if not len(self.getAttribute("gateway4").split("/")) == 2: self.setAttribute("gateway4", self.getAttribute("gateway4") + "/24") if not len(self.getAttribute("gateway6").split("/")) == 2: self.setAttribute("gateway6", self.getAttribute("gateway6") + "/80")
def _prepareDev(self): #assign host if not self.host: self.host = self.hostOptions().best() fault.check(self.host, "No matching host found") self.save() host = self.host self._assignTemplate() self._assignBridges() if not self.getVmid(): self.host.takeId("vmid", self.setVmid) fault.check(self.getVmid(), "No free vmid") vmid = self.getVmid() state = qm.getState(host, vmid) if state == State.STARTED: qm.stop(host, vmid) state = qm.getState(host, vmid) if state == State.PREPARED: qm.destroy(host, vmid) state = qm.getState(host, vmid) assert state == State.CREATED #nothing happened until here qm.create(host, vmid) try: qm.useTemplate(host, vmid, self.getTemplate()) self._configureVm() for iface in self.interfaceSetAll(): qm.addInterface(host, vmid, iface.name) self.state = State.PREPARED self.save() except: try: qm.destroy(host, vmid) except: pass raise
def configure(self, properties): if "template" in properties: fault.check(self.state == State.CREATED, "Cannot change template of prepared device: %s" % self.name) Device.configure(self, properties) if "root_password" in properties: self.setRootPassword(properties["root_password"]) if self.state == State.PREPARED or self.state == State.STARTED: vzctl.setUserPassword(self.host, self.getVmid(), self.getRootPassword(), username="******") if "gateway4" in properties: self.setAttribute("gateway4", properties["gateway4"]) if self.state == State.STARTED: self._configureRoutes() if "gateway6" in properties: self.setAttribute("gateway6", properties["gateway6"]) if self.state == State.STARTED: self._configureRoutes() if "template" in properties: self.setTemplate(properties["template"]) self._assignTemplate() fault.check(self.getTemplate(), "Template not found: %s" % properties["template"]) self.save()
def _prepareDev(self): #assign host self._assignHost() host = self.host self._assignBridges() self._assignVmid() fault.check(self.getVmid(), "No free vmid") vmid = self.getVmid() state = vzctl.getState(host, vmid) if state == State.STARTED: vzctl.stop(host, vmid) state = vzctl.getState(host, vmid) if state == State.PREPARED: self.state = vzctl.getState(host, vmid) self.save() return assert state == State.CREATED #nothing happened until here vzctl.create(host, vmid, self.getTemplate()) try: self._configureVm() for iface in self.interfaceSetAll(): vzctl.addInterface(host, vmid, iface.name) self.state = State.PREPARED self.save() except: try: vzctl.destroy(host, vmid) except: pass raise
def configure(self, properties): #FIXME: default route does not work if "template" in properties: fault.check(self.state == State.CREATED, "Cannot change template of prepared device: %s" % self.name) Device.configure(self, properties) if "root_password" in properties: self.setRootPassword(properties["root_password"]) if self.state == State.PREPARED or self.state == State.STARTED: vzctl.setUserPassword(self.host, self.getVmid(), self.getRootPassword(), username="******") if "gateway4" in properties: self.setAttribute("gateway4", properties["gateway4"]) if self.getAttribute("gateway4") and self.state == State.STARTED: #Note: usage of self as host is intentional ifaceutil.setDefaultRoute(self, self.getAttribute("gateway4")) if "gateway6" in properties: self.setAttribute("gateway6", properties["gateway6"]) if self.getAttribute("gateway6") and self.state == State.STARTED: #Note: usage of self as host is intentional ifaceutil.setDefaultRoute(self, self.getAttribute("gateway6")) if "template" in properties: self.setTemplate(properties["template"]) self._assignTemplate() fault.check(self.getTemplate(), "Template not found: %s" % properties["template"]) self.save()
def connectionsAdd(self, iface_name, properties): iface = self.topology.interfacesGet(iface_name) fault.check(self.state == State.CREATED, "Cannot add connections to started or prepared connector: %s -> %s", (iface_name, self.name) ) fault.check(iface.device.state != State.STARTED, "Cannot add connections to running device: %s -> %s", (iface_name, self.name) ) fault.check(not iface.isConnected(), "Cannot add connections to connected interface: %s -> %s", (iface_name, self.name) ) con = TincConnection () con.connector = self con.interface = iface con.init() con.configure(properties) con.save()
def configure(self, properties): oldCaptureToFile = self.getCaptureToFile() oldCaptureViaNet = self.getCaptureViaNet() oldCaptureFilter = self.getCaptureFilter() if "capture_filter" in properties: filter = properties["capture_filter"] host = self.getHost() if host: fault.check(tcpdump._checkSyntax(host, filter=filter), "Syntax error in filter") self.setCaptureFilter(properties["capture_filter"]) if "capture_to_file" in properties: self.setCaptureToFile(properties["capture_to_file"]) if "capture_via_net" in properties: self.setCaptureViaNet(properties["capture_via_net"]) for p in netemProperties: if p in properties: val = properties[p] val = netemValueConvert[p](val) fault.check(netemValuesChecks[p](val), "Invalid value for %s: %s", (p, val)) self.setAttribute(p, val) if p + "_to" in properties: val = properties[p + "_to"] val = netemValueConvert[p](val) fault.check(netemValuesChecks[p](val), "Invalid value for %s: %s", (p + "_to", val)) self.setAttribute(p + "_to", val) if p + "_from" in properties: val = properties[p + "_from"] val = netemValueConvert[p](val) fault.check(netemValuesChecks[p](val), "Invalid value for %s: %s", (p + "_from", val)) self.setAttribute(p + "_from", val) Connection.configure(self, properties) if self.connector.state == State.STARTED: self._configLink() if oldCaptureToFile and (not self.getCaptureToFile() or self.getCaptureFilter() != oldCaptureFilter): self._stopCaptureToFile() if oldCaptureViaNet and (not self.getCaptureViaNet() or self.getCaptureFilter() != oldCaptureFilter): self._stopCaptureViaNet() if self.getCaptureToFile() and (not oldCaptureToFile or self.getCaptureFilter() != oldCaptureFilter): self._startCaptureToFile() if self.getCaptureViaNet() and (not oldCaptureViaNet or self.getCaptureFilter() != oldCaptureFilter): self._startCaptureViaNet()
def interfacesAdd(self, name, properties): fault.check(self.state != State.STARTED, "Repy does not support adding interfaces to running VMs: %s" % self.name) import re fault.check(re.match("eth(\d+)", name), "Invalid interface name: %s" % name) try: fault.check(not self.interfaceSetGet(name), "Duplicate interface name: %s" % name) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface = Interface() iface.name = name iface.device = self iface.init() iface.save() Device.interfaceSetAdd(self, iface)
def interfacesAdd(self, name, properties): fault.check(self.state != State.STARTED, "OpenVZ does not support adding interfaces to running VMs: %s" % self.name) import re fault.check(re.match("eth(\d+)", name), "Invalid interface name: %s" % name) try: fault.check(not self.interfaceSetGet(name), "Duplicate interface name: %s" % name) except Interface.DoesNotExist: #pylint: disable-msg=W0702 pass iface = ConfiguredInterface() iface.name = name iface.device = self iface.init() if self.state == State.PREPARED or self.state == State.STARTED: vzctl.addInterface(self.host, self.getVmid(), iface.name) iface.configure(properties) iface.save() Device.interfaceSetAdd(self, iface)