Example #1
0
    def add_dir(self, pool_dir):
        logging.info("Processing pool dir '%s'" % pool_dir)
        dentries = os.listdir(pool_dir)

        res = []
        for dirent in dentries:
            res.append(self.add_file("%s/%s" % (pool_dir, dirent)))

        if len(res) == 0:
            logging.warn("No machines found in this directory")

        max_len = 0
        for m_id, _ in res:
            if len(m_id) > max_len:
                max_len = len(m_id)
        for m_id, available in res:
            if available:
                machine_spec = self._pool[m_id]
                if "libvirt_domain" in machine_spec["params"]:
                    libvirt_msg = "   libvirt_domain: %s" % machine_spec["params"]["libvirt_domain"]
                else:
                    libvirt_msg = ""
                msg = "%s%s [%s] %s" % (
                    m_id,
                    (max_len - len(m_id)) * " ",
                    decorate_with_preset("UP", "pass"),
                    libvirt_msg,
                )
            else:
                msg = "%s%s [%s]" % (m_id, (max_len - len(m_id)) * " ", decorate_with_preset("DOWN", "fail"))

            logging.info(msg)
Example #2
0
 def _format_success(self, success):
     if success:
         return (decorate_with_preset("PASS", "pass")
                 if self._colourize
                 else "PASS")
     else:
         return (decorate_with_preset("FAIL", "fail")
                 if self._colourize
                 else "FAIL")
Example #3
0
    def _run_command(self, command):
        logging.info("Executing command: [%s]", str_command(command))

        if "desc" in command:
            logging.info("Cmd description: %s", command["desc"])

        if command["type"] == "ctl_wait":
            sleep(command["seconds"])
            cmd_res = {
                "passed": True,
                "res_header": "%-9s%ss" % ("ctl_wait", command["seconds"]),
                "msg": "",
                "res_data": None
            }
            if self._res_serializer:
                self._res_serializer.add_cmd_result(command, cmd_res)
            return cmd_res

        machine_id = command["host"]
        machine = self._machines[machine_id]

        try:
            cmd_res = machine.run_command(command)
        except (KeyboardInterrupt, Exception) as exc:
            cmd_res = {
                "passed": False,
                "res_data": {
                    "Exception": str(exc)
                },
                "msg": "Exception raised.",
                "res_header": "EXCEPTION",
                "report": str(exc)
            }
            raise
        finally:
            if self._res_serializer:
                self._res_serializer.add_cmd_result(command, cmd_res)

        if cmd_res["passed"]:
            res_str = decorate_with_preset("PASS", "pass")
        else:
            res_str = decorate_with_preset("FAIL", "fail")
        logging.info("Result: %s" % res_str)
        if "report" in cmd_res and cmd_res["report"] != "":
            logging.info("Result data:")
            for line in cmd_res["report"].splitlines():
                logging.info(4 * " " + line)
        if "msg" in cmd_res and cmd_res["msg"] != "":
            logging.info("Status message from slave: \"%s\"" % cmd_res["msg"])

        return cmd_res
Example #4
0
    def add_result(self, result):
        if not isinstance(result, BaseResult):
            raise RecipeError("result must be a BaseActionResult instance.")

        self._results.append(result)

        result_str = (decorate_with_preset("PASS", "pass") if result.success
                      else decorate_with_preset("FAIL", "fail"))
        if len(result.description.split("\n")) == 1:
            logging.info("Result: {}, What: {}".format(result_str,
                                                       result.description))
        else:
            logging.info("Result: {}, What:".format(result_str))
            logging.info("{}".format(result.description))
Example #5
0
    def _format_command(self, command, cmd_res, output_pairs, m_id_max):
        if cmd_res["passed"]:
            res = "PASS"
        else:
            res = "FAIL"

        if "host" in command:
            m_id = "host %s: " % command["host"]
            m_id += " " * (m_id_max - len(command["host"]))
        else:
            #len("ctl") == 3; len("host ") == 5; 5-3 = 2
            m_id = "ctl: " + " " * (m_id_max + 2)

        output_pairs.append((8*" " + m_id + cmd_res["res_header"], res))

        if "desc" in command and command["desc"] != None:
            output_pairs.append((12*" " + "description: " + command["desc"], ""))

        if "msg" in cmd_res and cmd_res["msg"] != "":
            output_pairs.append((12*" " + "message: " + cmd_res["msg"], ""))

        if "report" in cmd_res and cmd_res["report"] != "":
            for line in cmd_res["report"].splitlines():
                out = decorate_with_preset(line, "faded")
                output_pairs.append((12*" " + out, ""))
Example #6
0
    def _format_command(self, command, cmd_res, output_pairs, m_id_max):
        if cmd_res["passed"]:
            res = "PASS"
        else:
            res = "FAIL"

        if "host" in command:
            m_id = "host %s: " % command["host"]
            m_id += " " * (m_id_max - len(command["host"]))
        else:
            #len("ctl") == 3; len("host ") == 5; 5-3 = 2
            m_id = "ctl: " + " " * (m_id_max + 2)

        output_pairs.append((8 * " " + m_id + cmd_res["res_header"], res))

        if "desc" in command and command["desc"] != None:
            output_pairs.append(
                (12 * " " + "description: " + command["desc"], ""))

        if "msg" in cmd_res and cmd_res["msg"] != "":
            output_pairs.append((12 * " " + "message: " + cmd_res["msg"], ""))

        if "report" in cmd_res and cmd_res["report"] != "":
            for line in cmd_res["report"].splitlines():
                out = decorate_with_preset(line, "faded")
                output_pairs.append((12 * " " + out, ""))
Example #7
0
    def _run_command(self, command):
        logging.info("Executing command: [%s]", str_command(command))

        if "desc" in command:
            logging.info("Cmd description: %s", command["desc"])

        if command["type"] == "ctl_wait":
            sleep(command["seconds"])
            cmd_res = {"passed": True,
                       "res_header": "%-9s%ss" % ("ctl_wait",
                                                   command["seconds"]),
                       "msg": "",
                       "res_data": None}
            if self._res_serializer:
                self._res_serializer.add_cmd_result(command, cmd_res)
            return cmd_res

        machine_id = command["host"]
        machine = self._machines[machine_id]

        try:
            cmd_res = machine.run_command(command)
        except Exception as exc:
            cmd_res = {"passed": False,
                       "res_data": {"Exception": str(exc)},
                       "msg": "Exception raised.",
                       "res_header": "EXCEPTION",
                       "report": str(exc)}
            raise
        finally:
            if self._res_serializer:
                self._res_serializer.add_cmd_result(command, cmd_res)

        if cmd_res["passed"]:
            res_str = decorate_with_preset("PASS", "pass")
        else:
            res_str = decorate_with_preset("FAIL", "fail")
        logging.info("Result: %s" % res_str)
        if "report" in cmd_res and cmd_res["report"] != "":
            logging.info("Result data:")
            for line in cmd_res["report"].splitlines():
                logging.info(4*" " + line)
        if "msg" in cmd_res and cmd_res["msg"] != "":
            logging.info("Status message from slave: \"%s\"" % cmd_res["msg"])

        return cmd_res
    def _print_pairs(self, output_pairs):
        max_left = 0
        max_right = 0
        for left, right in output_pairs:
            if len(left) > max_left:
                max_left = len(left)
            if len(right) > max_right:
                max_right = len(right)

        # +1 for the alignment of " PASS" or " FAIL"
        # +2 for spacing aroun the whole block
        full_length = max_left + max_right + 1 + 2

        if full_length % 2:
            full_length = full_length + 2
        else:
            full_length = full_length + 1

        header = " SUMMARY ".center(full_length, "=")
        coloured_summary = decorate_with_preset("SUMMARY", "highlight")
        logging.info(header.replace("SUMMARY", coloured_summary))

        for left, right in output_pairs:
            if right != "":
                space_fill = full_length - len(left) - len(right) - 1 - 2
                if right == "PASS":
                    right = decorate_with_preset(right, "pass")
                elif right == "FAIL":
                    right = decorate_with_preset(right, "fail")
                right = " %s" % right

                output = left + (space_fill)*" " + right
            else:
                output = left + " "
            logging.info(" %s " % output)
        logging.info("="*(full_length))
Example #9
0
    def _print_pairs(self, output_pairs):
        max_left = 0
        max_right = 0
        for left, right in output_pairs:
            if len(left) > max_left and right != "":
                max_left = len(left)
            if len(right) > max_right:
                max_right = len(right)

        # +1 for the alignment of " PASS" or " FAIL"
        # +2 for spacing aroun the whole block
        full_length = max_left + max_right + 1 + 2

        if full_length % 2:
            full_length = full_length + 2
        else:
            full_length = full_length + 1

        header = " SUMMARY ".center(full_length, "=")
        coloured_summary = decorate_with_preset("SUMMARY", "highlight")
        logging.info(header.replace("SUMMARY", coloured_summary))

        for left, right in output_pairs:
            if right != "":
                space_fill = full_length - len(left) - len(right) - 1 - 2
                if right == "PASS":
                    right = decorate_with_preset(right, "pass")
                elif right == "FAIL":
                    right = decorate_with_preset(right, "fail")
                right = " %s" % right

                output = left + (space_fill) * " " + right
            else:
                output = left + " "
            logging.info(" %s " % output)
        logging.info("=" * (full_length))
Example #10
0
    def add_dir(self, pool_name, dir_path):
        logging.info("Processing pool '%s', directory '%s'" %
                     (pool_name, dir_path))
        pool = self._pools[pool_name]

        try:
            dentries = os.listdir(dir_path)
        except OSError:
            logging.warn("Directory '%s' does not exist for pool '%s'" %
                         (dir_path, pool_name))
            return

        for dirent in dentries:
            m_id, m = self.add_file(pool_name, dir_path, dirent)
            if m_id != None and m != None:
                pool[m_id] = m

        if len(pool) == 0:
            logging.warn("No machines found in pool '%s', directory '%s'" %
                         (pool_name, dir_path))

        max_len = 0
        for m_id in list(pool.keys()):
            if len(m_id) > max_len:
                max_len = len(m_id)

        if self._pool_checks:
            check_sockets = {}
            for m_id, m in sorted(pool.items()):
                hostname = m["params"]["hostname"]
                if "rpc_port" in m["params"]:
                    port = int(m["params"]["rpc_port"])
                else:
                    port = self._ctl_config.get_option('environment',
                                                       'rpcport')

                logging.debug("Querying machine '%s': %s:%s" %\
                                                (m_id, hostname, port))

                s = socket.socket()
                s.settimeout(0)
                try:
                    s.connect((hostname, port))
                except socket.error as msg:
                    # if the error is other than EINPROGRESS, e.g. the stack
                    # could not resolve name, the machine should become unavailable
                    try:
                        en = msg.errno
                    except AttributeError:
                        en = 0

                    if en != errno.EINPROGRESS:
                        pool[m_id]["available"] = False
                        s.close()
                        logging.debug("Bypassing machine '%s' (%s)" %
                                      (m_id, msg))
                        continue

                check_sockets[s] = m_id

            while len(check_sockets) > 0:
                rl, wl, el = select.select([], list(check_sockets.keys()), [])
                for s in wl:
                    err = s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
                    m_id = check_sockets[s]
                    if err == 0:
                        pool[m_id]["available"] = True
                        s.shutdown(socket.SHUT_RDWR)
                        s.close()
                        del check_sockets[s]
                    else:
                        pool[m_id]["available"] = False
                        s.close()
                        del check_sockets[s]
        else:
            for m_id in list(pool.keys()):
                pool[m_id]["available"] = True

        for m_id in sorted(list(pool.keys())):
            m = pool[m_id]
            if m["available"]:
                if 'libvirt_domain' in m['params']:
                    libvirt_msg = "   libvirt_domain: %s" %\
                                        m['params']['libvirt_domain']
                else:
                    libvirt_msg = ""
                msg = "%s%s [%s] %s" % (m_id, (max_len - len(m_id)) * " ",
                                        decorate_with_preset(
                                            "UP", "pass"), libvirt_msg)
            else:
                msg = "%s%s [%s]" % (m_id, (max_len - len(m_id)) * " ",
                                     decorate_with_preset("DOWN", "fail"))
                del pool[m_id]

            logging.info(msg)
Example #11
0
 def _decorate_value(self, string, preset):
     value = strip_colours(string)
     if sys.stdout.isatty() and self._coloured:
         return decorate_with_preset(value, preset)
     else:
         return value
Example #12
0
    def add_dir(self, dir_path):
        logging.info("Processing pool dir '%s'" % dir_path)

        pool_dir = self._pool_dirs[dir_path]

        dentries = os.listdir(dir_path)
        for dirent in dentries:
            m_id, m = self.add_file(dir_path, dirent)
            if m_id != None and m != None:
                pool_dir[m_id] = m

        if len(pool_dir) == 0:
            logging.warn("No machines found in this directory")

        max_len = 0
        for m_id in pool_dir.keys():
            if len(m_id) > max_len:
                max_len = len(m_id)

        if self._pool_checks:
            check_sockets = {}
            for m_id, m in pool_dir.iteritems():
                hostname = m["params"]["hostname"]
                if "rpc_port" in m["params"]:
                    port = m["params"]["rpc_port"]
                else:
                    port = lnst_config.get_option('environment', 'rpcport')

                logging.debug("Querying machine '%s': %s:%s" %\
                                                (m_id, hostname, port))

                s = socket.socket()
                s.settimeout(0)
                try:
                    s.connect((hostname, port))
                except:
                    pass
                check_sockets[s] = m_id

            while len(check_sockets) > 0:
                rl, wl, el = select.select([], check_sockets.keys(), [])
                for s in wl:
                    err = s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
                    m_id = check_sockets[s]
                    if err == 0:
                        pool_dir[m_id]["available"] = True
                        del check_sockets[s]
                    else:
                        pool_dir[m_id]["available"] = False
                        del check_sockets[s]
        else:
            for m_id in pool_dir.keys():
                pool_dir[m_id]["available"] = True

        for m_id in list(pool_dir.keys()):
            m = pool_dir[m_id]
            if m["available"]:
                if 'libvirt_domain' in m['params']:
                    libvirt_msg = "   libvirt_domain: %s" %\
                                        m['params']['libvirt_domain']
                else:
                    libvirt_msg = ""
                msg = "%s%s [%s] %s" % (m_id, (max_len - len(m_id)) * " ",
                                        decorate_with_preset(
                                            "UP", "pass"), libvirt_msg)
            else:
                msg = "%s%s [%s]" % (m_id, (max_len - len(m_id)) * " ",
                                     decorate_with_preset("DOWN", "fail"))
                del pool_dir[m_id]

            logging.info(msg)
Example #13
0
    def add_dir(self, pool_name, dir_path):
        logging.info("Processing pool '%s', directory '%s'" % (pool_name,
                                                               dir_path))
        pool = self._pools[pool_name]

        try:
            dentries = os.listdir(dir_path)
        except OSError:
            logging.warn("Directory '%s' does not exist for pool '%s'" %
                                                                  (dir_path,
                                                                   pool_name))
            return

        for dirent in dentries:
            m_id, m = self.add_file(pool_name, dir_path, dirent)
            if m_id != None and m != None:
                pool[m_id] = m

        if len(pool) == 0:
            logging.warn("No machines found in pool '%s', directory '%s'" %
                                                                   (pool_name,
                                                                    dir_path))

        max_len = 0
        for m_id in pool.keys():
            if len(m_id) > max_len:
                max_len = len(m_id)

        if self._pool_checks:
            check_sockets = {}
            for m_id, m in sorted(pool.iteritems()):
                hostname = m["params"]["hostname"]
                if "rpc_port" in m["params"]:
                    port = m["params"]["rpc_port"]
                else:
                    port = lnst_config.get_option('environment', 'rpcport')

                logging.debug("Querying machine '%s': %s:%s" %\
                                                (m_id, hostname, port))

                s = socket.socket()
                s.settimeout(0)
                try:
                    s.connect((hostname, port))
                except socket.error as msg:
                    # if the error is other than EINPROGRESS, e.g. the stack
                    # could not resolve name, the machine should become unavailable
                    try:
                        en = msg.errno
                    except AttributeError:
                        en = 0

                    if en != errno.EINPROGRESS:
                        pool[m_id]["available"] = False
                        s.close()
                        logging.debug("Bypassing machine '%s' (%s)" %
                            (m_id, msg))
                        continue

                check_sockets[s] = m_id

            while len(check_sockets) > 0:
                rl, wl, el = select.select([], check_sockets.keys(), [])
                for s in wl:
                    err = s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
                    m_id = check_sockets[s]
                    if err == 0:
                        pool[m_id]["available"] = True
                        s.shutdown(socket.SHUT_RDWR)
                        s.close()
                        del check_sockets[s]
                    else:
                        pool[m_id]["available"] = False
                        s.close()
                        del check_sockets[s]
        else:
            for m_id in pool.keys():
                pool[m_id]["available"] = True

        for m_id in sorted(list(pool.keys())):
            m = pool[m_id]
            if m["available"]:
                if 'libvirt_domain' in m['params']:
                    libvirt_msg = "   libvirt_domain: %s" %\
                                        m['params']['libvirt_domain']
                else:
                    libvirt_msg = ""
                msg = "%s%s [%s] %s" % (m_id, (max_len - len(m_id)) * " ",
                                        decorate_with_preset("UP", "pass"),
                                        libvirt_msg)
            else:
                msg = "%s%s [%s]" % (m_id, (max_len - len(m_id)) * " ",
                                     decorate_with_preset("DOWN", "fail"))
                del pool[m_id]

            logging.info(msg)
Example #14
0
File: Logs.py Project: pazdera/lnst
 def _decorate_value(self, string, preset):
     value = strip_colours(string)
     if sys.stdout.isatty() and self._coloured:
         return decorate_with_preset(value, preset)
     else:
         return value
Example #15
0
    def add_dir(self, pool_name, dir_path):
        logging.info("Processing pool '%s', directory '%s'" % (pool_name,
                                                               dir_path))
        pool = self._pools[pool_name]

        dentries = os.listdir(dir_path)
        for dirent in dentries:
            m_id, m = self.add_file(pool_name, dir_path, dirent)
            if m_id != None and m != None:
                pool[m_id] = m

        if len(pool) == 0:
            logging.warn("No machines found in this pool")

        max_len = 0
        for m_id in pool.keys():
            if len(m_id) > max_len:
                max_len = len(m_id)

        if self._pool_checks:
            check_sockets = {}
            for m_id, m in sorted(pool.iteritems()):
                hostname = m["params"]["hostname"]
                if "rpc_port" in m["params"]:
                    port = m["params"]["rpc_port"]
                else:
                    port = lnst_config.get_option('environment', 'rpcport')

                logging.debug("Querying machine '%s': %s:%s" %\
                                                (m_id, hostname, port))

                s = socket.socket()
                s.settimeout(0)
                try:
                    s.connect((hostname, port))
                except:
                    pass
                check_sockets[s] = m_id

            while len(check_sockets) > 0:
                rl, wl, el = select.select([], check_sockets.keys(), [])
                for s in wl:
                    err = s.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
                    m_id = check_sockets[s]
                    if err == 0:
                        pool[m_id]["available"] = True
                        s.shutdown(socket.SHUT_RDWR)
                        s.close()
                        del check_sockets[s]
                    else:
                        pool[m_id]["available"] = False
                        s.close()
                        del check_sockets[s]
        else:
            for m_id in pool.keys():
                pool[m_id]["available"] = True

        for m_id in sorted(list(pool.keys())):
            m = pool[m_id]
            if m["available"]:
                if 'libvirt_domain' in m['params']:
                    libvirt_msg = "   libvirt_domain: %s" %\
                                        m['params']['libvirt_domain']
                else:
                    libvirt_msg = ""
                msg = "%s%s [%s] %s" % (m_id, (max_len - len(m_id)) * " ",
                                        decorate_with_preset("UP", "pass"),
                                        libvirt_msg)
            else:
                msg = "%s%s [%s]" % (m_id, (max_len - len(m_id)) * " ",
                                     decorate_with_preset("DOWN", "fail"))
                del pool[m_id]

            logging.info(msg)