コード例 #1
0
ファイル: license.py プロジェクト: walong365/icsw
 def get_info_line(self):
     _consumers = self.get_num_consumers()
     return [
         logging_tools.form_entry(self.action, header="action"),
         logging_tools.form_entry(str(self.config_service_enum),
                                  header="ConfigService"),
         logging_tools.form_entry_right(self.multiplier, header="Weight"),
         logging_tools.form_entry_center("yes" if self.ghost else "no",
                                         header="Ghost"),
         logging_tools.form_entry_center(str(self.content_type),
                                         header="ContentType"),
         logging_tools.form_entry_center(self.license_id_name or "global",
                                         header="License"),
         logging_tools.form_entry_right(
             logging_tools.get_diff_time_str(self.timeframe_secs)
             if self.timeframe_secs else "---",
             header="timeframe",
         ),
         logging_tools.form_entry_right(_consumers, header="entries"),
         logging_tools.form_entry_right(self.consumed, header="consumed"),
         logging_tools.form_entry_right(
             "{:.2f}".format(float(self.consumed) /
                             float(_consumers)) if _consumers else "-",
             header="mean"),
     ]
コード例 #2
0
ファイル: main.py プロジェクト: walong365/icsw
def raw_license_info(opts):
    if opts.delete:
        print("Deleting LicenseFile Entry from database with idx {:d}".format(
            opts.delete))
        try:
            License.objects.get(Q(idx=opts.delete)).delete()
        except License.DoesNotExist:
            # ignore
            pass
    out_list = logging_tools.new_form_list()
    _to_save = []
    _query = License.objects.all()
    if opts.only_valid:
        _query = _query.filter(Q(valid=True))
    for lic in _query:
        try:
            _info = License.objects.get_license_info(lic)
        except:
            _info = process_tools.get_except_info()
            _raw_info = None
            _error = True
        else:
            _raw_info = License.objects.get_raw_license_info(lic)
            _error = False
        if _error:
            if opts.mark_error:
                _valid = False
            elif opts.unmark_all:
                _valid = True
            else:
                _valid = lic.valid
        else:
            _valid = True
        if lic.valid != _valid:
            lic.valid = _valid
            _to_save.append(lic)
        # todo, extract fingerprint info from raw_license_info
        # import pprint
        # pprint.pprint(_raw_info)
        out_list.append([
            logging_tools.form_entry(lic.file_name, header="Filename"),
            logging_tools.form_entry(lic.date.isoformat(), header="created"),
            logging_tools.form_entry_right(lic.idx, header="idx"),
            logging_tools.form_entry_center(
                "valid" if lic.valid else "invalid", header="validity"),
            logging_tools.form_entry_center("error" if _error else "ok",
                                            header="error"),
            logging_tools.form_entry(_info, header="Info"),
        ])
    print(str(out_list))
    if len(_to_save):
        print("")
        print("Updating LicenseFile states ({:d})".format(len(_to_save)))
        for lic_to_save in _to_save:
            lic_to_save.save(update_fields=["valid"])
        print("...done")
コード例 #3
0
    def show(self, **opions):

        present_perms = self._get_perms()
        out_list = logging_tools.new_form_list()
        for perm in present_perms:
            out_list.append([
                logging_tools.form_entry(perm.content_type.app_label,
                                         header="App Label"),
                logging_tools.form_entry(perm.content_type.model,
                                         header="Model"),
                logging_tools.form_entry(perm.codename, header="Code"),
                logging_tools.form_entry(perm.name, header="Info"),
                logging_tools.form_entry_center(
                    "G/O" if perm.valid_for_object_level else "G",
                    header="Scope"),
                logging_tools.form_entry(
                    perm.created.strftime("%Y-%m-%d %H:%M:%S"),
                    header="Created"),
                logging_tools.form_entry_right(
                    perm.rolepermission_set.all().count(), header="RolePerms"),
                logging_tools.form_entry_right(
                    perm.csw_object_permission_set.all().count(),
                    header="RoleObjPerms"),
            ])
        print("{} defined:".format(
            logging_tools.get_plural("Permission", len(out_list)), ))
        print unicode(out_list)
コード例 #4
0
ファイル: license.py プロジェクト: walong365/icsw
 def get_info_line(self):
     return [
         logging_tools.form_entry_center("yes" if self.dummy else "no",
                                         header="Dummy"),
         logging_tools.form_entry(str(self.valid_from),
                                  header="valid from"),
         logging_tools.form_entry(str(self.valid_to), header="valid to"),
         logging_tools.form_entry(self.license_id_name or "global",
                                  header="License"),
         logging_tools.form_entry_center("yes" if self.is_valid else "no",
                                         header="valid"),
         logging_tools.form_entry_right(self.installed, header="installed"),
         logging_tools.form_entry_right(self.available, header="available"),
         logging_tools.form_entry_right(self.available_ghost,
                                        header="ghost"),
     ]
コード例 #5
0
ファイル: weathermap_helper.py プロジェクト: walong365/icsw
 def get_form_entry(self, max_num_keys):
     act_line = [
         logging_tools.form_entry(self.wm_type.value.name, header="Type"),
         logging_tools.form_entry_center(
             "yes" if self.wm_type.value.has_sub_values else "no",
             header="SubValue"),
         logging_tools.form_entry(self.db_idx, header="db_idx"),
         logging_tools.form_entry(self.spec, header="Spec"),
     ]
     sub_keys = (self.key.split(".") + [""] * max_num_keys)[0:max_num_keys]
     for key_idx, sub_key in zip(range(max_num_keys), sub_keys):
         act_line.append(
             logging_tools.form_entry("{}{}".format(
                 "" if (key_idx == 0 or sub_key == "") else ".", sub_key),
                                      header="key{:d}".format(key_idx)))
     # check for unknow
     if self.value is None:
         # unknown value
         act_pf, val_str = ("", "<unknown>")
     else:
         act_pf, val_str = self._get_val_str(self.value)
     act_line.extend([
         logging_tools.form_entry_right(val_str, header="value"),
         logging_tools.form_entry_right(act_pf, header=" "),
     ])
     return act_line
コード例 #6
0
ファイル: license.py プロジェクト: bopopescu/icsw
 def get_info_line(self):
     return [
         logging_tools.form_entry(self.action, header="action"),
         logging_tools.form_entry(unicode(self.config_service_enum),
                                  header="ConfigService"),
         logging_tools.form_entry_right(self.multiplier, header="Weight"),
         logging_tools.form_entry_center(unicode(self.content_type),
                                         header="ContentType"),
         logging_tools.form_entry_right(
             logging_tools.get_diff_time_str(self.timeframe_secs)
             if self.timeframe_secs else "---",
             header="timeframe",
         ),
         logging_tools.form_entry_right(self.get_num_consumers(),
                                        header="entries"),
         logging_tools.form_entry_right(self.get_all_consumed(),
                                        header="consumed"),
     ]
コード例 #7
0
ファイル: main.py プロジェクト: walong365/icsw
def _domain_enum_show_command(options):
    from initat.cluster.backbone.domain_enum import icswDomainEnum
    from initat.cluster.backbone.models import DomainTypeEnum
    print("")
    print("DomainEnums defined: {:d}".format(len(icswDomainEnum)))
    _list = logging_tools.NewFormList()
    _c_dict = {
        entry.enum_name: entry
        for entry in DomainTypeEnum.objects.all()
    }
    for entry in icswDomainEnum:
        if entry.name not in _c_dict:
            if options.sync:
                new_entry = DomainTypeEnum.create_db_entry(entry)
                _c_dict[new_entry.enum_name] = new_entry
            else:
                _db_str = "no"
        if entry.name in _c_dict:
            # if options.sync:
            #     _c_dict[entry.name].update_values(entry)
            _db_str = "yes ({:d})".format(_c_dict[entry.name].pk)
        if entry.value.default_enum:
            _default_info = entry.value.default_enum.name
        else:
            _default_info = "---"
        if entry.value.domain_enum:
            _domain_info = entry.value.domain_enum.name
        else:
            _domain_info = "---"
        _list.append([
            logging_tools.form_entry(entry.name, header="EnumName"),
            logging_tools.form_entry(entry.value.name, header="Name"),
            logging_tools.form_entry(entry.value.info, header="Info"),
            logging_tools.form_entry_center(_db_str, header="DB info"),
            logging_tools.form_entry(_default_info, header="Default Enum"),
            logging_tools.form_entry(_domain_info, header="Domain Enum"),
        ])
    print(str(_list))
コード例 #8
0
def main(args):
    ignore_re = re.compile(args.ignore)
    coffefiles = []
    htmlfiles = []
    for root, dirs, files in os.walk(args.path, topdown=False):
        coffefiles.extend([
            os.path.join(root, f) for f in files
            if f.endswith("coffee") and not ignore_re.search(f)
        ])
        htmlfiles.extend([
            os.path.join(root, f) for f in files
            if f.endswith("html") and not ignore_re.search(f)
        ])

    print("{:d} Coffee and {:d} HTML files".format(len(coffefiles),
                                                   len(htmlfiles)))

    def_matcher = re.compile(
        ".*\.(?P<type>(directive|service|controller|factory))\((\'|\")(?P<name>(.*?))(\'|\").*"
    )
    html_matcher = re.compile(
        ".*script type=.text/ng-template. id=(\'|\")(?P<name>.*)(\'|\").")

    my_sink = DataSink(args.path)

    print("Getting defs...")

    # get definitions

    for name in coffefiles:
        for line_num, line in enumerate(open(name, "rb"), 1):
            match = def_matcher.match(line)
            if match:
                _gd = match.groupdict()
                my_sink.feed(name, line_num, line, _gd["type"], _gd["name"])
    print("done (found {:d})".format(len(my_sink._defs)))

    # find refs in HTML to services

    dir_defs = my_sink.get_type_defs("directive") + my_sink.get_type_defs(
        "controller")
    dir_dict = {}
    for _def in dir_defs:
        dir_dict[_def.camel_name] = _def
        dir_dict[_def.hyphen_name] = _def
    dir_matcher = set(dir_dict.keys())

    _refs = 0
    s_time = time.time()
    for name in htmlfiles:
        for line_num, line in enumerate(open(name, "rb"), 1):
            match = html_matcher.match(line)
            if match:
                _gd = match.groupdict()
                my_sink.feed(name, line_num, line, "html", _gd["name"])
            else:
                # print line
                _add_dict = {}
                for word in re.split("([^a-zA-Z\-])+", line):
                    if word in dir_matcher:
                        # skip lines with only closing tags
                        if "</{}".format(word) in line and "<{}".format(
                                word) not in line:
                            continue
                        # only one match per line
                        _add_dict[word] = True
                for word in _add_dict.keys():
                    dir_dict[word].add_reference(name, line_num, line)
                    _refs += 1
    e_time = time.time()
    print("Reference from HTML to directive took {} (found: {:d})".format(
        logging_tools.get_diff_time_str(e_time - s_time),
        _refs,
    ))

    # find refs to Services and Factories in coffee
    sf_refs = my_sink.get_type_defs("factory") + my_sink.get_type_defs(
        "service")
    sf_dict = {_sf.camel_name: _sf for _sf in sf_refs}
    sf_matcher = set(sf_dict.keys())
    # also find refs to html templates in coffee
    html_ref_re = re.compile(
        ".*(template|templateUrl)\s*:\s*.*(\'|\")(?P<temp_name>.*?)(\'|\").*")
    html_dict = {
        _html.hyphen_name: _html
        for _html in my_sink.get_type_defs("html")
    }
    html_matcher = set(html_dict.keys())
    # print html_matcher
    _refs = 0
    s_time = time.time()
    for name in coffefiles:
        for line_num, line in enumerate(open(name, "rb"), 1):
            # print line
            for word in re.split("([^a-zA-Z])+", line):
                if word in sf_matcher:
                    # check if reference is by literal
                    if "'{}'".format(word) in line or "\"{}\"".format(
                            word) in line:
                        sf_dict[word].add_reference(name, line_num, line)
                        _refs += 1
            _html_match = html_ref_re.match(line)
            if _html_match:
                _temp_ref = _html_match.groupdict()["temp_name"]
                if _temp_ref in html_matcher:
                    html_dict[_temp_ref].add_reference(name, line_num, line)
    e_time = time.time()
    print("Reference from coffee to service / factory took {} (found: {:d})".
          format(
              logging_tools.get_diff_time_str(e_time - s_time),
              _refs,
          ))

    # generate output
    # raw list
    _list = sum(
        [my_sink.get_type_defs(_type) for _type in my_sink.get_types()], [])

    # filter
    if args.ignore_valid:
        _list = [entry for entry in _list if not entry.is_valid]

    name_re = re.compile(args.filter, re.IGNORECASE)
    _list = [entry for entry in _list if name_re.search(entry.name)]

    if _list:
        print("{} in result list:".format(
            logging_tools.get_plural("entry", len(_list)), ))

        if args.order_by == "name":
            _list = sorted(_list, key=attrgetter("name"))
        if args.order_by == "toplevel":
            _list = sorted(_list, key=attrgetter("top_level_dir"))
        out_list = logging_tools.NewFormList()

        files_referenced = {}
        for _def in _list:
            files_referenced.setdefault(_def.file_name, []).append(_def)
            out_list.append([
                logging_tools.form_entry(_def.type, header="Type"),
                logging_tools.form_entry(_def.name, header="Name"),
                logging_tools.form_entry(_def.file_name, header="File"),
                logging_tools.form_entry_right(_def.line_num, header="line"),
                logging_tools.form_entry_right(len(_def.refs), header="#refs"),
                logging_tools.form_entry_center(
                    "yes" if _def.namespace_ok else "no", header="NS ok"),
                logging_tools.form_entry_center(
                    "yes" if _def.name_valid else "no", header="valid"),
            ])
            if args.show_refs:
                out_list.extend(_def.get_ref_list())
                _def.add_file_refs(files_referenced)
        print(str(out_list))

        if args.show_refs and files_referenced:
            print()
            print("Referenced files:")
            print()
            pprint.pprint(files_referenced)
コード例 #9
0
def do_info(cur_opts, log_com):
    if not cur_opts.username:
        print("No user name given")
        return 1
    _user = _get_user(cur_opts.username)
    _ret_state = 0
    if _user is None:
        _ret_state = 1
    else:
        from initat.cluster.backbone.models import user_quota_setting, user_variable
        from django.db.models import Q
        print("")
        print(
            "User with loginname '{}' (user {}), uid={:d}, group={} (gid={:d})".format(
                _user.login,
                str(_user),
                _user.uid,
                str(_user.group),
                _user.group.gid,
            )
        )
        num_qs = _user.user_quota_setting_set.all().count()
        if num_qs and cur_opts.system_wide_quota:
            print("")
            print(
                "{} found:".format(
                    logging_tools.get_plural("system-wide quota setting", num_qs)
                )
            )
            for _qs in _user.user_quota_setting_set.all():
                _bd = _qs.quota_capable_blockdevice
                print(
                    "    device {} ({} on {}): {}".format(
                        str(_bd.device.full_name),
                        _bd.block_device_path,
                        _bd.mount_path,
                        get_quota_str(_qs),
                    )
                )
        try:
            _cmd = "quota --show-mntpoint -wp -u {}".format(
                _user.login,
            )
            _res = subprocess.check_output(
                _cmd.split(),
                stderr=subprocess.STDOUT,
            ).decode("utf-8")
        except subprocess.CalledProcessError as sb_exc:
            _res = sb_exc.output.decode("utf-8")
            # print("error calling '{}': {}".format(_cmd, process_tools.get_except_info()))
            _ret_state = 1
        except OSError as sb_exc:
            # quota command not founda
            _res = "denied: {}".format(sb_exc)
            # print("error calling '{}': {}".format(_cmd, process_tools.get_except_info()))
            _ret_state = 1
        else:
            _ret_state = 0
        if _res.lower().count("denied"):
            print("    error getting local quotas for {}: {}".format(_user.login, _res))
        else:
            # print _res
            _lines = [_line.strip().split() for _line in _res.split("\n") if _line.strip()]
            _lines = [_line for _line in _lines if len(_line) == 10]
            if _lines:
                print("", "local quota:", sep="\n")
                _line = _lines[-1]
                _bytes_violate = _line[2].count("*") > 0
                _local = user_quota_setting(
                    bytes_used=int(_line[2].replace("*", "")) * 1024,
                    bytes_soft=int(_line[3]) * 1024,
                    bytes_hard=int(_line[4]) * 1024,
                    bytes_gracetime=int(_line[5]),
                )
                print(
                    "    local mountpoint: {}".format(
                        get_quota_str(_local),
                    )
                )
        if cur_opts.delete_var:
            print("")
            try:
                _cv = user_variable.objects.get(Q(user=_user) & Q(idx=cur_opts.delete_var))
            except user_variable.DoesNotExist:
                print("Variable to delete does not exist")
            else:
                print("Deleting '{}'".format(str(_cv)))
                _cv.delete()
        if cur_opts.show_vars:
            out_list = logging_tools.NewFormList()
            for _var in _user.user_variable_set.all().order_by("name"):
                out_list.append(
                    [
                        logging_tools.form_entry(_var.idx, header="idx"),
                        logging_tools.form_entry(_var.name, header="name"),
                        logging_tools.form_entry(_var.var_type, header="type"),
                        logging_tools.form_entry_right(_var.value if _var.var_type != "j" else "{:d} Bytes".format(len(_var.json_value)), header="value"),
                        logging_tools.form_entry_center("yes" if _var.editable else "no", header="editable"),
                        logging_tools.form_entry_center("yes" if _var.hidden else "no", header="hidden"),
                        logging_tools.form_entry(_var.date.strftime("%H:%m:%S %a, %d. %b %Y"), header="created"),
                        logging_tools.form_entry(_var.description, header="description"),
                    ]
                )
            print(str(out_list))

    return _ret_state
コード例 #10
0
ファイル: main.py プロジェクト: walong365/icsw
def _service_enum_show_command(options):

    from initat.cluster.backbone.server_enums import icswServiceEnum
    from initat.cluster.backbone.models import ConfigServiceEnum, config
    from initat.cluster.backbone import factories
    from django.core.exceptions import ValidationError

    _c_dict = {
        entry.enum_name: entry
        for entry in ConfigServiceEnum.objects.all()
    }
    print("")
    print("ServiceEnums defined: {:d}".format(len(icswServiceEnum)))
    _list = logging_tools.NewFormList()
    for entry in icswServiceEnum:
        if entry.name not in _c_dict:
            if options.sync and (entry.value.server_service
                                 or entry.value.relayer_service
                                 ) and entry.value.sync_config:
                new_entry = ConfigServiceEnum.create_db_entry(entry)
                _c_dict[new_entry.enum_name] = new_entry
            else:
                _db_str = "no"
        if entry.name in _c_dict:
            if options.sync:
                _c_dict[entry.name].update_values(entry)
            _db_str = "yes ({:d})".format(_c_dict[entry.name].pk)
        if entry.value.server_service:
            _egg_action = ", ".join(
                [str(_action)
                 for _action in entry.value.egg_actions]) or "none"
        else:
            _egg_action = "---"
        _list.append([
            logging_tools.form_entry(entry.name, header="EnumName"),
            logging_tools.form_entry(entry.value.name, header="Name"),
            logging_tools.form_entry_center(
                "yes" if entry.value.root_service else "no",
                header="Root Service"),
            logging_tools.form_entry_center(
                "yes" if entry.value.server_service else "no",
                header="Server"),
            logging_tools.form_entry_center(
                "yes" if entry.value.relayer_service else "no",
                header="Relayer"),
            logging_tools.form_entry(entry.value.info, header="Info"),
            logging_tools.form_entry_center(_db_str, header="DB info"),
            logging_tools.form_entry(_egg_action, header="Egg actions"),
        ])
    print(str(_list))
    if options.sync:
        _change_list = []
        # compat dict
        comp_dict = {
            "rrd_grapher": icswServiceEnum.grapher_server.name,
            "rrd_server": icswServiceEnum.collectd_server.name,
            "rrd_collector": icswServiceEnum.collectd_server.name,
            "server": icswServiceEnum.cluster_server.name,
            "ldap_server": icswServiceEnum.ldap_server.name,
        }
        for c_con in config.objects.all():
            if not c_con.config_service_enum_id:
                _check_names = [c_con.name]
                if c_con.name in comp_dict:
                    _check_names.append(comp_dict[c_con.name])
                for _check_name in _check_names:
                    if _check_name in _c_dict:
                        c_con.config_service_enum = _c_dict[_check_name]
                        try:
                            c_con.save(update_fields=["config_service_enum"])
                        except ValidationError:
                            print("cannot save {}: {}".format(
                                str(c_con), process_tools.get_except_info()))
                        else:
                            _change_list.append(c_con)
                            break
        _create_list = []
        for db_enum in _c_dict.values():
            if not db_enum.config_set.all().count():
                _create_list.append(
                    factories.Config(
                        name=db_enum.name,
                        description=db_enum.info,
                        config_service_enum=db_enum,
                        server_config=True,
                    ))

        if len(_change_list):
            print("")
            print("{} moved to ConfigServiceEnum:".format(
                logging_tools.get_plural("Config", len(_change_list))))
            for entry in _change_list:
                print("    {} ({})".format(entry.name,
                                           str(entry.config_service_enum)))
        if len(_create_list):
            print("")
            print("{} created:".format(
                logging_tools.get_plural("Config", len(_create_list))))
            for entry in _create_list:
                print("    {} ({})".format(entry.name,
                                           str(entry.config_service_enum)))
コード例 #11
0
ファイル: main.py プロジェクト: walong365/icsw
def show_overview(local_mc, valid_names):
    mod_list = logging_tools.NewFormList()
    cmd_list = logging_tools.NewFormList()
    # iterate over modules
    for _idx, mod in enumerate(local_mc.module_list, 1):
        c_names = [name for name in valid_names if local_mc.command_dict[name].module == mod]
        local_valid_names = []
        for com_name in c_names:
            local_valid_names.append(com_name)
        local_valid_names = sorted(local_valid_names)
        # show module overview
        mod_list.append(
            [
                logging_tools.form_entry_right(_idx, header="#"),
                logging_tools.form_entry(mod.name, header="Module name"),
                logging_tools.form_entry(mod.Meta.uuid, header="uuid"),
                logging_tools.form_entry(mod.checksum, header="Checksum"),
                logging_tools.form_entry_center(mod.Meta.required_access.name, header="Access"),
                logging_tools.form_entry_center(
                    ",".join(
                        [
                            _platform.name for _platform in mod.Meta.required_platform
                        ]
                    ),
                    header="Platform",
                ),
                logging_tools.form_entry_right(mod.Meta.priority, header="priority"),
                logging_tools.form_entry_right(
                    "yes" if hasattr(mod, "init_machine_vector") else "no",
                    header="MachineVector"
                ),
                logging_tools.form_entry_right(len(local_valid_names), header="#coms"),
                logging_tools.form_entry(", ".join(local_valid_names), header="commands"),
            ]
        )
    # iterate over command
    for _idx, cmd_name in enumerate(sorted(local_mc.command_dict.keys()), 1):
        cmd = local_mc[cmd_name]
        # print(cmd)
        # print(inspect.getsource(cmd.__class__))
        cmd_list.append(
            [
                logging_tools.form_entry_right(_idx, header="#"),
                logging_tools.form_entry(cmd_name, header="Name"),
                logging_tools.form_entry(cmd.module.name, header="Module name"),
                logging_tools.form_entry(cmd.Meta.uuid, header="uuid"),
                logging_tools.form_entry(cmd.checksum, header="Checksum"),
                logging_tools.form_entry(cmd.Meta.check_instance.name, header="Server"),
                logging_tools.form_entry_center(cmd.Meta.required_access.name, header="Access"),
                logging_tools.form_entry_center(
                    ",".join(
                        [
                            _platform.name for _platform in cmd.Meta.required_platform
                        ]
                    ),
                    header="Platform",
                ),
                logging_tools.form_entry_center(
                    "yes" if cmd.Meta.has_perfdata else "no",
                    header="perfdata",
                ),
                logging_tools.form_entry_center(
                    "yes" if cmd.Meta.create_mon_check_command else "no",
                    header="create MCC",
                ),
                logging_tools.form_entry(
                    ", ".join(
                        cmd.Meta.alternate_names
                    ) if cmd.Meta.alternate_names else "---",
                    header="Alternate names",
                ),
                logging_tools.form_entry(
                    cmd.Meta.ports.get_port_spec(),
                    header="PortSpec",
                ),
                logging_tools.form_entry(
                    cmd.Meta.description,
                    header="description",
                ),
            ]
        )
    print("\nModule overview:\n{}".format(str(mod_list)))
    print("\nCommand overview:\n{}".format(str(cmd_list)))