def get_text(self, prepend_code=True, translate=True): msg = self._get_text(translate) try: msg = decode_value(msg) % self.args except KeyError, e: # When new args are added to existing log messages, old entries in # log for the same message would fail due to lack of that new arg. # This avoids whole log functionality to break due to that, while # registers the problem. msg = decode_value(msg) cherrypy.log.error_log.error("KeyError: %s - %s" % (str(e), msg))
def save_config_for_ubuntu(iface, gateway=None): if gateway: try: gingerNetworkLock.acquire() pattern = "etc/network/interfaces/iface[*]" listout = parser.match(decode_value(pattern)) for list_iface in listout: list_iface = decode_value(list_iface) iface_from_match = parser.get(list_iface) if iface_from_match == decode_value(iface): parser.set(list_iface + "/gateway", gateway) parser.save() except Exception as e: raise OperationFailed("GINNET0074E", {'error': e}) finally: gingerNetworkLock.release()
def get_text(self, prepend_code=True, translate=True): msg = self._get_text(translate) msg = decode_value(msg) % self.args if prepend_code: return "%s: %s" % (self.code, msg) return msg
def __init__(self, code='', args=None, plugin=None): if args is None: args = {} # make all args unicode for key, value in args.iteritems(): if isinstance(value, unicode): continue try: # In case the value formats itself to an ascii string. args[key] = decode_value(value) except UnicodeEncodeError: # In case the value is a WokException or it formats # itself to a unicode string. args[key] = unicode(value) self.code = code self.args = args self.plugin = plugin
def run_command(cmd, timeout=None, silent=False, out_cb=None, env_vars=None): """ cmd is a sequence of command arguments. timeout is a float number in seconds. timeout default value is None, means command run without timeout. silent is bool, it will log errors using debug handler not error. silent default value is False. out_cb is a callback that receives the whole command output every time a new line is thrown by command. Default value is None, meaning that whole output will be returned at the end of execution. Returns a tuple (out, error, returncode) where: out is the output thrown by command error is an error message if applicable returncode is an integer equal to the result of command execution """ # subprocess.kill() can leave descendants running # and halting the execution. Using psutil to # get all descendants from the subprocess and # kill them recursively. def kill_proc(proc, timeout_flag): try: parent = psutil.Process(proc.pid) for child in parent.get_children(recursive=True): child.kill() # kill the process after no children is left proc.kill() except OSError: pass else: timeout_flag[0] = True proc = None timer = None timeout_flag = [False] if env_vars is None: env_vars = os.environ.copy() env_vars['LC_ALL'] = 'en_US.UTF-8' elif env_vars.get('LC_ALL') is None: env_vars['LC_ALL'] = 'en_US.UTF-8' try: proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env_vars) if timeout is not None: timer = Timer(timeout, kill_proc, [proc, timeout_flag]) timer.setDaemon(True) timer.start() wok_log.debug("Run command: '%s'", " ".join(cmd)) if out_cb is not None: output = [] while True: line = "" try: line = proc.stdout.readline() line = line.decode('utf_8') except Exception: type, e, tb = sys.exc_info() wok_log.error(e) wok_log.error("The output of the command could not be " "decoded as %s\ncmd: %s\n line ignored: %s" % ('utf_8', cmd, repr(line))) pass output.append(line) if not line: break out_cb(''.join(output)) out = ''.join(output) error = proc.stderr.read() returncode = proc.poll() else: out, error = proc.communicate() if out: wok_log.debug("out:\n%s", out) returncode = proc.returncode if returncode != 0: msg = "rc: %s error: %s returned from cmd: %s" %\ (returncode, decode_value(error), decode_value(' '.join(cmd))) if silent: wok_log.debug(msg) else: wok_log.error(msg) if out_cb is not None: out_cb(msg) elif error: wok_log.debug("error: %s returned from cmd: %s", decode_value(error), decode_value(' '.join(cmd))) if timeout_flag[0]: msg = ("subprocess is killed by signal.SIGKILL for " "timeout %s seconds" % timeout) wok_log.error(msg) msg_args = {'cmd': " ".join(cmd), 'seconds': str(timeout)} raise TimeoutExpired("WOKUTILS0002E", msg_args) return out, error, returncode except TimeoutExpired: raise except OSError as e: msg = "Impossible to execute '%s'" % ' '.join(cmd) wok_log.debug("%s", msg) return None, "%s %s" % (msg, e), -1 except Exception as e: msg = "Failed to run command: %s." % " ".join(cmd) msg = msg if proc is None else msg + "\n error code: %s." wok_log.error("%s %s", msg, e) if proc: return out, error, proc.returncode else: return None, msg, -1 finally: if timer and not timeout_flag[0]: timer.cancel()