Esempio n. 1
0
def load_config():
    """Parses the command line arguments and loads config files.
    """
    base_config_file = utils.determine_config_file(fname_base="base_sender")
    arguments = argument_parsing()

    if arguments.config_file is None:
        arguments.config_file = utils.determine_config_file(
            fname_base="datamanager").as_posix()
    else:
        arguments.config_file = Path(arguments.config_file).as_posix()

    # check if config_file exist
    utils.check_existence(base_config_file)
    utils.check_existence(arguments.config_file)

    # ------------------------------------------------------------------------
    # Get arguments from config file and command line
    # ------------------------------------------------------------------------

    config = utils.load_config(base_config_file)
    config_detailed = utils.load_config(arguments.config_file)

    # if config and yaml is mixed mapping has to take place before merging them
    config_type = "sender"
    config = utils.map_conf_format(config, config_type)
    config_detailed = utils.map_conf_format(config_detailed, config_type)
    arguments_dict = utils.map_conf_format(arguments,
                                           config_type,
                                           is_namespace=True)

    utils.update_dict(config_detailed, config)
    utils.update_dict(arguments_dict, config)

    return config
Esempio n. 2
0
def argument_parsing():
    """Parsing of command line arguments.
    """
    parser = argparse.ArgumentParser()

    parser.add_argument("--config_file",
                        type=str,
                        help="Location of the configuration file")
    parser.add_argument("--beamline",
                        type=str,
                        help="Beamline for which the HiDRA Server "
                        "(detector mode) should be started",
                        default="p00")
    parser.add_argument("--verbose",
                        help="More verbose output",
                        action="store_true")
    parser.add_argument("--onscreen",
                        type=str,
                        help="Display logging on screen "
                        "(options are CRITICAL, ERROR, WARNING, "
                        "INFO, DEBUG)",
                        default=False)

    arguments = parser.parse_args()

    config_file = arguments.config_file
    if config_file is None:
        config_file = utils.determine_config_file(
            fname_base="control_server.yaml")

    # convert to dict and map to config section
    arguments = {"controlserver": vars(arguments)}

    # ------------------------------------------------------------------------
    # Get arguments from config file and command line
    # ------------------------------------------------------------------------
    utils.check_existence(config_file)

    config = utils.load_config(config_file)
    utils.update_dict(arguments, config)

    # the configuration is now of the form:
    # {
    #   "controlserver": {...}
    #   "hidraconfig_static" : {...}
    #   "hidraconfig_variable" : {...}
    # }

    # TODO check config for required params

    return config
Esempio n. 3
0
def _merge_with_config(args, parser):
    """
    Takes the command line arguments and overwrites the parameter of the config
    file with it.
    """
    # convert to dict and map to config section
    args_dict = vars(args)

    # hidra config
    eventdetector = {
        "http_events": {
            "det_ip": args_dict["det"],
        }
    }
    if args_dict["detapi"] is not None:
        eventdetector["http_events"]["det_api_version"] = args_dict["detapi"]

    arguments = {"hidra": {"eventdetector": eventdetector}}

    del args_dict["det"]
    del args_dict["detapi"]

    if args_dict["beamline"] is None:
        del args_dict["beamline"]

    # general config
    arguments["general"] = args_dict

    # ------------------------------------------------------------------------
    # Get arguments from config file and command line
    # ------------------------------------------------------------------------
    config_file = _identify_config_file(arguments)
    config = utils.load_config(config_file)
    utils.update_dict(arguments, config)

    # check for beamline
    try:
        if config["general"]["beamline"] not in ALLOWED_BEAMLINES:
            raise parser.error(
                "argument --beamline: invalid choice: '{}' (choose from {})"
                .format(config["general"]["beamline"],
                        str(ALLOWED_BEAMLINES)[1:-1])
            )
    except KeyError:
        raise parser.error("the following arguments are required: --beamline")

    return config
Esempio n. 4
0
    def write_config(self, host_id):
        """
        Write the configuration into a file.

        Args:
            host_id: the host id the config belongs to.
        """

        try:
            self._check_config_complete(host_id)
        except utils.NotFoundError:
            return

        # if the requesting client has set parameters before these should be
        # taken. If this was not the case use the one from the previous
        # executed start
        if not self.client_dep_configs[host_id]["active"]:
            self.log.debug("Config parameters did not change since last start")
            self.log.debug("No need to write new config file")
            return

        # overwrites config_static entries
        utils.update_dict(self.client_dep_configs[host_id], self.config_static)

        # write configfile
        config_file = self.get_config_file_name()
        self.log.info("Writing config file: %s", config_file)
        utils.write_config(config_file, self.config_static, log=self.log)

        self.log.info(
            "Started with ext_ip: %s, event detector: %s, "
            "data fetcher: %s", self.config_static["general"]["ext_ip"],
            self.config_static["eventdetector"]["type"],
            self.config_static["datafetcher"]["type"])

        # store the dynamic config globally
        self.log.debug("config = %s", self.config_static)
        self.active_config = copy.deepcopy(self.config_static)
        # this information is unnecessary
        del self.active_config["active"]

        # mark local_config as inactive
        self.client_dep_configs[host_id]["active"] = False
Esempio n. 5
0
def argument_parsing():
    """Parses and checks the command line arguments used.
    """

    base_config_file = utils.determine_config_file(fname_base="base_receiver")

    # ------------------------------------------------------------------------
    # Get command line arguments
    # ------------------------------------------------------------------------

    parser = argparse.ArgumentParser()

    parser.add_argument("--config_file",
                        type=str,
                        help="Location of the configuration file")

    parser.add_argument("--log_path",
                        type=str,
                        help="Path where logfile will be created")
    parser.add_argument("--log_name",
                        type=str,
                        help="Filename used for logging")
    parser.add_argument("--log_size",
                        type=int,
                        help="File size before rollover in B (linux only)")
    parser.add_argument("--verbose",
                        help="More verbose output",
                        action="store_true")
    parser.add_argument("--onscreen",
                        type=str,
                        help="Display logging on screen "
                        "(options are CRITICAL, ERROR, WARNING, "
                        "INFO, DEBUG)",
                        default=False)

    parser.add_argument("--procname",
                        type=str,
                        help="Name with which the service should be running")

    parser.add_argument("--whitelist",
                        type=str,
                        help="List of hosts allowed to connect")
    parser.add_argument("--target_dir",
                        type=str,
                        help="Where incoming data will be stored to")
    parser.add_argument("--dirs_not_to_create",
                        type=str,
                        help="Subdirectories which should not be created when "
                        "data is stored")
    parser.add_argument("--data_stream_ip",
                        type=str,
                        help="Ip of dataStream-socket to pull new files from")
    parser.add_argument("--data_stream_port",
                        type=str,
                        help="Port number of dataStream-socket to pull new "
                        "files from")

    arguments = parser.parse_args()
    arguments.config_file = (
        arguments.config_file
        or utils.determine_config_file(fname_base="datareceiver"))

    # check if config_file exist
    utils.check_existence(arguments.config_file)

    # ------------------------------------------------------------------------
    # Get arguments from config file
    # ------------------------------------------------------------------------

    config = utils.load_config(base_config_file)
    config_detailed = utils.load_config(arguments.config_file)

    # if config and yaml is mixed mapping has to take place before merging them
    config_type = "receiver"
    config = utils.map_conf_format(config, config_type)
    config_detailed = utils.map_conf_format(config_detailed, config_type)
    arguments_dict = utils.map_conf_format(arguments,
                                           config_type,
                                           is_namespace=True)

    utils.update_dict(config_detailed, config)

    utils.update_dict(arguments_dict, config)

    # ------------------------------------------------------------------------
    # Check given arguments
    # ------------------------------------------------------------------------

    required_params = {
        "general": [
            "log_path", "log_name", "procname", "procname", "ldapuri",
            "dirs_not_to_create", "whitelist"
        ],
        "datareceiver": [
            "target_dir",
            "data_stream_ip",
            "data_stream_port",
        ]
    }

    # Check format of config
    check_passed, _ = utils.check_config(required_params, config, logging)
    if not check_passed:
        logging.error("Configuration check failed")
        raise utils.WrongConfiguration

    # check target directory for existence
    utils.check_existence(config["datareceiver"]["target_dir"])

    # check if logfile is writable
    config["general"]["log_file"] = os.path.join(config["general"]["log_path"],
                                                 config["general"]["log_name"])

    return config