def _run(self): os.setpgrp() signal.signal(signal.SIGHUP, self._sig_ign) signal.signal(signal.SIGINT, self._sig_ign) signal.signal(signal.SIGTERM, self._sig_ign) self._connection_pipe = self._write_pipe self._log_ctl.disable_logging() self._log_ctl.set_connection(self._connection_pipe) result = {} try: self._cmd_cls.run() except (KeyboardInterrupt, SystemExit): pass except: log_exc_traceback() type, value, tb = sys.exc_info() data = {"Exception": "%s" % value} self._cmd_cls.set_fail(data) finally: res_data = self._cmd_cls.get_result() result["type"] = "result" result["cmd_id"] = self._id result["result"] = res_data send_data(self._write_pipe, result) self._write_pipe.close()
def _run(self): os.setpgrp() signal.signal(signal.SIGHUP, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL) self._log_ctl.disable_logging() self._log_ctl.set_connection(self._child_pipe) result = {} try: self._job_cls.run() except: log_exc_traceback() type, value, tb = sys.exc_info() data = {"Exception": "%s" % value} # self._job_cls.set_fail(data) finally: res_data = self._job_cls.get_result() result["type"] = "job_finished" result["job_id"] = self._id result["result"] = res_data send_data(self._child_pipe, result) self._child_pipe.close()
def _run(self): self._parent_pipe.close() os.setpgrp() signal.signal(signal.SIGHUP, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL) signal.signal(signal.SIGTERM, signal.SIG_DFL) self._log_ctl.disable_logging() self._log_ctl.set_connection(self._child_pipe) result = {} try: self._job_cls.run() job_result = self._job_cls.get_result() except Exception as e: log_exc_traceback() job_result = {} job_result["passed"] = False job_result["type"] = "exception" job_result["res_data"] = self._job_cls.get_result() job_result["res_data"]["exception"] = e finally: result["type"] = "job_finished" result["job_id"] = self._id result["result"] = job_result send_data(self._child_pipe, result) self._child_pipe.close()
def _run(self): os.setpgrp() self._cmd_cls.set_handle_intr() self._connection_pipe = self._write_pipe self._log_ctl.disable_logging() self._log_ctl.set_connection(self._connection_pipe) result = {} try: self._cmd_cls.run() res_data = self._cmd_cls.get_result() result["type"] = "result" result["cmd_id"] = self._id result["result"] = res_data except KeyboardInterrupt: res_data = self._cmd_cls.get_result() result["type"] = "result" result["cmd_id"] = self._id result["result"] = res_data except: type, value, tb = sys.exc_info() result = {"type": "exception", "cmd_id": self._id, "Exception": ''.join(traceback.format_exception(type, value, tb))} send_data(self._write_pipe, result) self._write_pipe.close()
def _run(self): os.setpgrp() self._cmd_cls.set_handle_intr() self._connection_pipe = self._write_pipe self._log_ctl.disable_logging() self._log_ctl.set_connection(self._connection_pipe) result = {} try: self._cmd_cls.run() except KeyboardInterrupt: pass except: type, value, tb = sys.exc_info() data = {"Exception": "%s" % value} self._cmd_cls.set_fail(data) finally: res_data = self._cmd_cls.get_result() result["type"] = "result" result["cmd_id"] = self._id result["result"] = res_data send_data(self._write_pipe, result) self._write_pipe.close()
def emit(self, record): r = dict(record.__dict__) r['msg'] = record.getMessage() r['args'] = None r['exc_info'] = None data = {"type": "log", "record": r} send_data(self.target, data)
def remove_saved_machine_config(cls): #removes previously saved configuration cfg = None try: with open("/tmp/.lnst_machine_conf", "rb") as f: cfg = cPickle.load(f) except: logging.info("No previous configuration found.") return if cfg: logging.info("Cleaning up leftover configuration from previous "\ "config_only run.") for hostname, machine in cfg["machines"].iteritems(): port = lnst_config.get_option("environment", "rpcport") if test_tcp_connection(hostname, port): rpc_con = socket.create_connection((hostname, port)) rpc_msg = { "type": "command", "method_name": "machine_cleanup", "args": [] } logging.debug("Calling cleanup on slave '%s'" % hostname) send_data(rpc_con, rpc_msg) while True: msg = recv_data(rpc_con) if msg['type'] == 'result': break rpc_con.close() if "libvirt_dom" in machine: libvirt_dom = machine["libvirt_dom"] domain_ctl = VirtDomainCtl(libvirt_dom) logging.info("Detaching dynamically created interfaces.") for i in machine["interfaces"]: try: domain_ctl.detach_interface(i) except: pass logging.info("Removing dynamically created bridges.") for br in cfg["bridges"]: try: net_ctl = VirtNetCtl(br) net_ctl.cleanup() except: pass os.remove("/tmp/.lnst_machine_conf")
def remove_saved_machine_config(cls): #removes previously saved configuration cfg = None try: with open("/tmp/.lnst_machine_conf", "rb") as f: cfg = cPickle.load(f) except: logging.info("No previous configuration found.") return if cfg: logging.info("Cleaning up leftover configuration from previous "\ "config_only run.") for hostname, machine in cfg["machines"].iteritems(): port = lnst_config.get_option("environment", "rpcport") if test_tcp_connection(hostname, port): rpc_con = socket.create_connection((hostname, port)) rpc_msg= {"type": "command", "method_name": "machine_cleanup", "args": []} logging.debug("Calling cleanup on slave '%s'" % hostname) send_data(rpc_con, rpc_msg) while True: msg = recv_data(rpc_con) if msg['type'] == 'result': break rpc_con.close() if "libvirt_dom" in machine: libvirt_dom = machine["libvirt_dom"] domain_ctl = VirtDomainCtl(libvirt_dom) logging.info("Detaching dynamically created interfaces.") for i in machine["interfaces"]: try: domain_ctl.detach_interface(i) except: pass logging.info("Removing dynamically created bridges.") for br in cfg["bridges"]: try: net_ctl = VirtNetCtl(br) net_ctl.cleanup() except: pass os.remove("/tmp/.lnst_machine_conf")
def send_message(self, machine, data): soc = self.get_connection(machine) data = remote_device_to_deviceref(data) if send_data(soc, data) == False: msg = "Connection error from slave %s" % machine.get_id() raise ConnectionError(msg) result = None while True: connected_slaves = list(self._connection_mapping.keys()) messages = self.check_connections() for msg in messages: if msg[1]["type"] == "result" and msg[0] == machine: if result is not None: msg = ("Multiple result messages from the same slave " "'{}'".format(machine.get_id())) raise ConnectionError(msg) result = msg[1] else: self._process_message(msg) remaining_slaves = list(self._connection_mapping.keys()) if connected_slaves != remaining_slaves: self._handle_disconnects(set(connected_slaves)- set(remaining_slaves)) if result is not None: netns = data.get("netns", None) return deviceref_to_remote_device(machine, result["result"], netns)
def send_data_to_ctl(self, data): if self._c_socket != None: if self._netns != None: data = {"type": "from_netns", "netns": self._netns, "data": data} return send_data(self._c_socket[0], data) else: return False
def send_data_to_ctl(self, data): if self._c_socket != None: if self._netns != None: data = {"type": "from_netns", "netns": self._netns, "data": data} data = device_to_deviceref(data) return send_data(self._c_socket[0], data) else: return False
def send_message(self, machine, data): soc = self.get_connection(machine) if data["type"] == "command": data["args"] = remote_device_to_deviceref(data["args"]) data["kwargs"] = remote_device_to_deviceref(data["kwargs"]) if send_data(soc, data) == False: msg = "Connection error from slave %s" % machine.get_id() raise ConnectionError(msg)
def kill(self, sig=signal.SIGKILL): if self._finished: logging.debug("Job finished before sending the signal") return True try: logging.debug("Sending signal %s to pid %d" % (sig, self._pid)) os.killpg(self._pid, sig) if sig == signal.SIGKILL: self.set_finished(dict(type = "job_finished", job_id = self._id, result = dict(passed = False, res_data = "Job killed", type = "result"))) send_data(self._child_pipe, self.get_result()) return True except OSError as exc: logging.error(str(exc)) return False
def _get_machine_interfaces(self, sock): """ Gets machine interfaces via RPC call @param sock Socket used for connecting to machine @return Dictionary with machine interfaces or None if RPC call fails """ msg = {"type": "command", "method_name": "get_devices", "args": {}} if not send_data(sock, msg): sys.stderr.write("Could not send request to slave machine\n") return None while True: data = recv_data(sock) if data["type"] == "result": return data["result"]
def send_data_to_netns(self, netns, data): if netns not in self._netns_con_mapping: raise Exception("No network namespace '%s'!" % netns) else: netns_con = self._netns_con_mapping[netns] return send_data(netns_con, data)
def send_data_to_ctl(self, data): if self._c_socket != None: return send_data(self._c_socket[0], data) else: return False
def send_message(self, machine_id, data): soc = self.get_connection(machine_id) if send_data(soc, data) == False: msg = "Connection error from slave %s" % machine_id raise NetTestError(msg)
def send_data_to_netns(self, netns, data): if netns not in self._netns_con_mapping: raise Exception("No such namespace!") else: netns_con = self._netns_con_mapping[netns] return send_data(netns_con, data)