Ejemplo n.º 1
0
def _lic_fetch(opts, act_conf):
    # query license server and update licenses
    _cur_lic = sge_license_tools.LicenseCheck(
        log_com=_log,
        lmutil_path=act_conf["LMUTIL_PATH"],
        license_file=act_conf["LICENSE_FILE"],
        verbose=opts.verbose,
    )
    _xml = _cur_lic.check()
    if int(_xml.attrib["state"]) > logging_tools.LOG_LEVEL_OK:
        print("error calling license server: {}".format(_xml.attrib["info"]))
    else:
        _srv_info = _xml.find(".//license_servers")
        new_lics = {}
        for _lic in _xml.findall(".//license"):
            new_lic = sge_license_tools.SGELicense(_lic,
                                                   site=opts.site,
                                                   server_info=_srv_info)
            new_lics[new_lic.name] = new_lic
        current_lics_file = sge_license_tools.LicenseTextFile(
            sge_license_tools.get_site_license_file_name(opts.base, opts.site),
            ignore_missing=True,
            strip_empty=False,
            strip_hash=False,
        )
        current_lics = sge_license_tools.parse_license_lines(
            current_lics_file.lines, opts.site)
        print("discovered {:d} licenses, {:d} of them are currently in use".
              format(
                  len(current_lics),
                  len([True for _lic in current_lics.values()
                       if _lic.is_used])))
        for _al_key in sorted(set(new_lics) - set(current_lics)):
            _lic_to_add = new_lics[_al_key]
            _lic_to_add.added = time.ctime()
            print("add new license {} ({}, {:d})".format(
                _lic_to_add.name,
                _lic_to_add.attribute,
                _lic_to_add.total,
            ))
            current_lics[_al_key] = _lic_to_add
        for _cmp_key in sorted(set(new_lics) & set(current_lics)):
            current_lics[_cmp_key].update(new_lics[_cmp_key])
        current_lics_file.write(
            etree.tostring(sge_license_tools.build_license_xml(
                opts.site, current_lics),
                           pretty_print=True))
Ejemplo n.º 2
0
def _lic_config(opts, act_conf):
    sge_dict = sge_license_tools.get_sge_environment()
    # complexes and complex names
    _sge_cxs, _sge_cns = sge_license_tools.get_sge_complexes(sge_dict)
    current_lics_file = sge_license_tools.LicenseTextFile(
        sge_license_tools.get_site_license_file_name(opts.base, opts.site),
        ignore_missing=True,
        strip_empty=False,
        strip_hash=False,
    )
    current_lics = sge_license_tools.parse_license_lines(
        current_lics_file.lines, opts.site)
    sge_license_tools.handle_complex_licenses(current_lics)
    _lics_to_use = [
        _lic.name for _lic in current_lics.values() if _lic.is_used
    ]
    # modify complexes
    with tempfile.NamedTemporaryFile("w") as _tmpfile:
        form_str = "{} {} INT <= YES YES 0 0\n"
        changed = False
        for new_lic_name in _lics_to_use:
            if new_lic_name not in _sge_cns:
                changed = True
                _tmpfile.write(form_str.format(new_lic_name, new_lic_name))
        if changed:
            _tmpfile.write("\n".join(_sge_cxs) + "\n")
            # rewind
            _tmpfile.seek(0)
            sge_license_tools.call_command(
                "{} -Mc {}".format(sge_dict["QCONF_BIN"], _tmpfile.name), 1,
                True)
    # modify global execution host
    # attribute string
    ac_str = ",".join([
        "{}={:d}".format(
            _lic_to_use,
            current_lics[_lic_to_use].total - current_lics[_lic_to_use].limit)
        for _lic_to_use in _lics_to_use
    ])
    if ac_str:
        _mod_stat, _mod_out = sge_license_tools.call_command(
            "{} -mattr exechost complex_values {} global".format(
                sge_dict["QCONF_BIN"], ac_str), 1, True)
Ejemplo n.º 3
0
 def _update(self):
     if not self._track:
         return
     _act_site_file = sge_license_tools.LicenseTextFile(
         os.path.join(sge_license_tools.BASE_DIR,
                      sge_license_tools.ACT_SITE_NAME),
         ignore_missing=True,
     )
     if _act_site_file.lines:
         act_site = _act_site_file.lines[0]
         if not self.__elo_obj:
             self.__elo_obj = sge_license_tools.ExternalLicenses(
                 sge_license_tools.BASE_DIR,
                 act_site,
                 self.log,
                 verbose=True,
             )
         lic_xml = self._update_lic(self.__elo_obj)
         if self._track_in_db:
             self._write_db_entries(act_site, self.__elo_obj, lic_xml)
     else:
         self.log("no actual site defined, no license tracking",
                  logging_tools.LOG_LEVEL_ERROR)
Ejemplo n.º 4
0
def _lic_addc(opts, act_conf):
    current_lics_file = sge_license_tools.LicenseTextFile(
        sge_license_tools.get_site_license_file_name(opts.base, opts.site),
        ignore_missing=True,
        strip_empty=False,
        strip_hash=False,
    )
    current_lics = sge_license_tools.parse_license_lines(
        current_lics_file.lines, opts.site)
    if not opts.complex_name or opts.complex_name in current_lics:
        print("complex name '{}' empty or already used".format(
            opts.complex_name))
        sys.exit(1)
    new_lic = sge_license_tools.SGELicense(opts.complex_name,
                                           license_type="complex",
                                           eval_str=opts.eval_str,
                                           site=opts.site,
                                           added=time.ctime())
    current_lics[new_lic.name] = new_lic
    current_lics_file.write(
        etree.tostring(sge_license_tools.build_license_xml(
            opts.site, current_lics),
                       pretty_print=True))
Ejemplo n.º 5
0
def main():
    # read current sites (to determine default site)
    _act_site_file = sge_license_tools.LicenseTextFile(
        os.path.join(sge_license_tools.BASE_DIR,
                     sge_license_tools.ACT_SITE_NAME),
        ignore_missing=True,
        content=[sge_license_tools.DEFAULT_SITE],
    )
    _act_site = _act_site_file.lines
    if _act_site:
        _def_site = _act_site[0]
    else:
        _def_site = sge_license_tools.DEFAULT_SITE
        print("setting '{}' as default site".format(_def_site))
        _act_site_file.write(content=[_def_site])
    parser = argparse.ArgumentParser()
    parser.add_argument("--verbose",
                        default=False,
                        action="store_true",
                        help="set verbose mode [%(default)s]")
    parser.add_argument("--base",
                        type=str,
                        default=sge_license_tools.BASE_DIR,
                        help="set basedir [%(default)s]")
    parser.add_argument(
        "--site",
        type=str,
        default=_def_site,
        help="select site [%(default)s], add if not already present")
    parser.add_argument("--set-as-default",
                        default=False,
                        action="store_true",
                        help="set site as default site [%(default)s]")
    parser.add_argument("--create",
                        default=False,
                        action="store_true",
                        help="create missing files [%(default)s]")
    _srv_group = parser.add_argument_group("server specs")
    _srv_group.add_argument("--port",
                            default=0,
                            type=int,
                            help="license server port to use [%(default)s]")
    _srv_group.add_argument("--host",
                            default="",
                            type=str,
                            help="license server to use [%(default)s]")
    parser.add_argument("--mode",
                        default="show",
                        choices=["config", "fetch", "addc", "show"],
                        help="operation mode [%(default)s]")
    _cmp_group = parser.add_argument_group("complex handling")
    _cmp_group.add_argument("--complex-name",
                            default="",
                            type=str,
                            help="name of new complex [%(default)s]")
    _cmp_group.add_argument(
        "--eval-str",
        default="1",
        type=str,
        help="evaluation string for new complex [%(default)s]")
    _query_group = parser.add_argument_group("query options")
    _query_group.add_argument("--list-sites",
                              default=False,
                              action="store_true",
                              help="list defined sites [%(default)s]")
    _query_group.add_argument("--show-config",
                              default=False,
                              action="store_true",
                              help="show config of choosen site [%(default)s]")
    opts = parser.parse_args()
    # print "*", opts
    _create_base_dir(opts)

    if opts.site != _def_site and opts.set_as_default:
        _act_site_file.write(content=[opts.site])

    valid_sites_file = sge_license_tools.LicenseTextFile(
        os.path.join(opts.base, sge_license_tools.SITE_CONF_NAME),
        create=opts.create,
        content=[sge_license_tools.DEFAULT_SITE],
    )
    valid_sites = valid_sites_file.lines
    if opts.site not in valid_sites:
        valid_sites.append(opts.site)
        print("adding '{}' to list of sites".format(opts.site))
        valid_sites_file.write(content=valid_sites)

    if opts.list_sites:
        print("{}:".format(
            logging_tools.get_plural("defined site", len(valid_sites))))
        for _site in valid_sites:
            print(" - {}".format(_site))

    act_conf_file = sge_license_tools.LicenseTextFile(
        sge_license_tools.get_site_config_file_name(opts.base, opts.site),
        content=sge_license_tools.DEFAULT_CONFIG,
        create=True,
    )
    act_conf = act_conf_file.dict

    if opts.port and opts.host and not act_conf["LICENSE_FILE"]:
        _new_lic = "{:d}@{}".format(opts.port, opts.host)
        print("set LICENSE_FILE of config for site '{}' to {}".format(
            opts.site, _new_lic))
        act_conf["LICENSE_FILE"] = _new_lic
        act_conf_file.write(act_conf)

    if opts.show_config:
        print("Config for site '{}' in {}:".format(
            opts.site,
            sge_license_tools.get_site_config_file_name(opts.base, opts.site),
        ))
        for _key in sorted(act_conf):
            print(" - {:<20} = '{}'".format(_key, act_conf[_key]))

    if opts.mode == "show":
        _lic_show(opts, act_conf)

    elif opts.mode == "fetch":
        _lic_fetch(opts, act_conf)

    elif opts.mode == "config":
        _lic_config(opts, act_conf)

    elif opts.mode == "addc":
        _lic_addc(opts, act_conf)
Ejemplo n.º 6
0
def main():
    # change IO-descriptors
    zmq_context = zmq.Context()
    log_template = logging_tools.get_logger(
        "loadsensor",
        get_log_path(icswLogHandleTypes.log_py),
        context=zmq_context,
    )
    base_dir = "/etc/sysconfig/licenses"
    my_pid = os.getpid()
    sge_dict = sge_license_tools.get_sge_environment()
    log_template.info("starting for pid {:d}, base_dir is {}".format(
        my_pid, base_dir))
    log_template.info(sge_license_tools.get_sge_log_line(sge_dict))
    _act_site_file = sge_license_tools.LicenseTextFile(
        os.path.join(base_dir, sge_license_tools.ACT_SITE_NAME), )
    act_site = _act_site_file.lines
    if not act_site:
        log_template.error("Actual site not defined, exiting...")
        sys.exit(1)
    act_site = act_site[0]

    act_conf = sge_license_tools.LicenseTextFile(
        sge_license_tools.get_site_config_file_name(base_dir, act_site), ).dict
    log_template.info("read config for actual site '{}'".format(act_site))
    log_template.info("{} in config:".format(
        logging_tools.get_plural("key", len(list(act_conf.keys())))))
    for key, value in act_conf.items():
        log_template.info(" - {:<20s} : {}".format(key, value))
    call_num = 0
    io_in_fd = sys.stdin.fileno()
    io_out_fd = sys.stdout.fileno()
    log_template.info(
        "starting up, input handle is {:d}, output handle is {:d}".format(
            io_in_fd, io_out_fd))
    actual_licenses, lic_read_time = ([], time.time())
    # read node_grouping file
    # ng_dict = sge_license_tools.read_ng_file(log_template)
    # license_check_dict
    lc_object = None
    # vector socket
    try:
        while True:
            in_lines = raw_read(io_in_fd)
            call_num += 1
            if in_lines == "quit":
                log_template.warning("call #{:d}, received '{}'".format(
                    call_num, in_lines))
                break
            else:
                log_template.info(
                    "starting reporting load values (call #{:d})".format(
                        call_num))
                start_time = time.time()
                site_lic_file_name = sge_license_tools.get_site_license_file_name(
                    base_dir, act_site)
                if os.path.isfile(site_lic_file_name):
                    file_time = os.stat(site_lic_file_name)[stat.ST_MTIME]
                    if not actual_licenses or file_time > lic_read_time:
                        log_template.info(
                            "reading license_file for site {}".format(
                                act_site))
                        actual_licenses = sge_license_tools.parse_license_lines(
                            sge_license_tools.LicenseTextFile(
                                sge_license_tools.get_site_license_file_name(
                                    base_dir, act_site),
                                ignore_missing=True,
                                strip_empty=False,
                                strip_hash=False,
                            ).lines, act_site)
                        lic_read_time = file_time
                    if not sge_license_tools.handle_license_policy(base_dir):
                        cur_used = {
                            _key: _value.used
                            for _key, _value in actual_licenses.items()
                        }
                        srv_result = parse_actual_license_usage(
                            log_template, actual_licenses, act_conf, lc_object)
                        sge_license_tools.update_usage(actual_licenses,
                                                       srv_result)
                        # [cur_lic.handle_node_grouping() for cur_lic in actual_licenses.itervalues()]
                        for log_line, log_level in sge_license_tools.handle_complex_licenses(
                                actual_licenses):
                            log_template.log(log_line, log_level)
                        sge_license_tools.calculate_usage(actual_licenses)
                        configured_licenses = [
                            _key for _key, _value in actual_licenses.items()
                            if _value.is_used
                        ]
                        sge_lines, rep_dict = build_sge_report_lines(
                            log_template, configured_licenses, actual_licenses,
                            cur_used)
                    else:
                        log_template.log(
                            "licenses are controlled via rms-server, reporting nothing to SGE",
                            logging_tools.LOG_LEVEL_WARN)
                        sge_lines, rep_dict = ([], None)
                    # report to SGE
                    print("\n".join(sge_lines))
                    end_time = time.time()
                    if rep_dict:
                        log_template.info(
                            "{} defined, {:d} configured, {:d} in use{}, ({:d} simple, {:d} complex), took {}"
                            .format(
                                logging_tools.get_plural(
                                    "license",
                                    len(list(actual_licenses.keys()))),
                                len(configured_licenses),
                                len(rep_dict["lics_in_use"]),
                                rep_dict["lics_in_use"] and " ({})".format(
                                    ", ".join(sorted(rep_dict["lics_in_use"])))
                                or "", rep_dict["simple_lics"],
                                rep_dict["complex_lics"],
                                logging_tools.get_diff_time_str(end_time -
                                                                start_time)))
                else:
                    log_template.warning(
                        "site_file for site {} not readable (base_dir is {})".
                        format(act_site, base_dir))
    except KeyboardInterrupt:
        log_template.warning(
            "proc {:d}: got KeyboardInterrupt, exiting ...".format(my_pid))
    except term_error:
        log_template.warning(
            "proc {:d}: got term-signal, exiting ...".format(my_pid))
    except stop_error:
        log_template.warning(
            "proc {:d}: got stop-signal, exiting ...".format(my_pid))
    except int_error:
        log_template.warning(
            "proc {:d}: got int-signal, exiting ...".format(my_pid))
    log_template.close()
    zmq_context.term()
    sys.exit(0)