Пример #1
0
def generate_context(config_file_path):
    """
    Read the config.ini and generate context based on settings

    :param config_file_path: location of the config.ini file
    :return: dictionary containing configurations for jinja engine
    """
    # Read configuration file
    config_parser = ConfigParserUtil()
    config_parser.optionxform = str
    config_parser.read(os.path.join(PATH, config_file_path))
    configurations = config_parser.as_dictionary()

    # Reading the default values
    context = configurations[constants.CONFIG_PARAMS]
    settings = configurations[constants.CONFIG_SETTINGS]
    global PACK_LOCATION
    PACK_LOCATION = settings["REPOSITORY_CONF_DIRECTORY"]

    # if read_env_variables is true context will be generated from environment variables
    # if read_env_variables is not true context will be read from config.ini
    if settings["READ_FROM_ENVIRONMENT"] == "true":
        log.info("Reading from environment variables")
        for key, value in context.iteritems():
            # check if value exists for given key; use default if not exists
            context[key] = os.environ.get(key, context[key])

    # Converting Members to dictionary
    members = ast.literal_eval(context['STRATOS_MEMBERS']).split(",")
    context["STRATOS_MEMBERS"] = dict(s.split(':') for s in members)
    log.info("Context generated: %s", context)
    return context
Пример #2
0
def generate_context(config_file_path):
    """
    Read the config.ini and generate context based on settings

    :param config_file_path: location of the config.ini file
    :return: dictionary containing configurations for jinja engine
    """
    # Read configuration file
    config_parser = ConfigParserUtil()
    config_parser.read(config_file_path)
    configurations = config_parser.as_dictionary()

    # Reading the default values
    context = configurations[constants.CONFIG_PARAMS]
    settings = configurations[constants.CONFIG_SETTINGS]
    global PACK_LOCATION
    PACK_LOCATION = settings["distribution_file_path"]

    # if read_env_variables is true context will be generated from environment variables
    # if read_env_variables is not true context will be read from config.ini
    if settings["read_env_variables"] == "true":
        log.info("Reading from environment variables")
        for key, value in context.iteritems():
            # check if value exists for given key; use default if not exists
            context[key] = os.environ.get(key, context[key])

    # check whether members are available in context before conversion
    if 'members' in context:
        context['members'] = ast.literal_eval(context['members'])

    log.info("Context generated %s", context)
    return context
def generate_context(config_file_path):
    """
    Read the config.ini and generate context based on settings

    :param config_file_path: location of the module.ini file
    :return dictionary containing configurations loaded from module.ini
    """
    # Read configuration file
    config_parser = ConfigParserUtil()
    config_parser.optionxform = str
    config_parser.read(os.path.join(config_file_path))
    configurations = config_parser.as_dictionary()
    log.debug("Configurations loaded: %s", configurations)
    return configurations
Пример #4
0
def render_template(template_filename, default_context):
    """
    parse the xml file and return the content as a text

    :param template_filename: template filename path
    :param default_context: dictionary containing configurations read from module.ini
    :return: xml as a string
    """
    if READ_FROM_ENVIRONMENT == "true":
        template_source = \
            TEMPLATE_ENVIRONMENT.loader.get_source(TEMPLATE_ENVIRONMENT, template_filename)[0]
        parsed_content = TEMPLATE_ENVIRONMENT.parse(template_source)
        variables = meta.find_undeclared_variables(parsed_content)
        log.debug("Template variables : %s", variables)
        context = ConfigParserUtil.get_context_from_env(
            variables, default_context)
    else:
        context = default_context

    # Adding CARBON_HOME to context as it is required for registry db
    context[constants.CONFIG_SETTINGS_CARBON_HOME] = PACK_LOCATION
    log.info("Final context generated for rendering %s : %s ",
             os.path.basename(template_filename), context)
    log.info("Rendering template: %s \n", template_filename)
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
Пример #5
0
def generate_context(config_file_path):
    """
    Read the config.ini and generate context based on settings

    :param config_file_path: location of the config.ini file
    :return: dictionary containing configurations for jinja engine
    """
    # Read configuration file
    config_parser = ConfigParserUtil()
    config_parser.optionxform = str
    config_parser.read(os.path.join(PATH, config_file_path))
    configurations = config_parser.as_dictionary()
    log.debug("Configuration file content %s", configurations)
    settings = configurations[constants.CONFIG_SETTINGS]
    global PACK_LOCATION
    PACK_LOCATION = os.environ.get(constants.CONFIG_SETTINGS_CARBON_HOME,
                                   settings[constants.CONFIG_SETTINGS_CARBON_HOME])
    log.info("CARBON_HOME : %s" % PACK_LOCATION)
    context = configurations[constants.CONFIG_PARAMS]

    log.info("Context generated: %s", context)
    # if read_env_variables is true context will be generated from environment variables
    # if read_env_variables is not true context will be read from config.ini
    if settings["READ_FROM_ENVIRONMENT"] == "true":
        global READ_FROM_ENVIRONMENT
        READ_FROM_ENVIRONMENT = "true"
        log.info("Reading from environment")
    else:
        # Converting multi-valued params to dictionary
        context = ConfigParserUtil.get_multivalued_attributes_as_dictionary(context)
    return context
def generate_context(config_file_path):
    """
    Read the config.ini and generate context based on settings

    :param config_file_path: location of the config.ini file
    :return: dictionary containing configurations for jinja engine
    """
    # Read configuration file
    config_parser = ConfigParserUtil()
    config_parser.optionxform = str
    config_parser.read(os.path.join(PATH, config_file_path))
    configurations = config_parser.as_dictionary()
    log.debug("Configuration file content %s", configurations)
    settings = configurations[constants.CONFIG_SETTINGS]
    global PACK_LOCATION
    PACK_LOCATION = os.environ.get(constants.CONFIG_SETTINGS_CARBON_HOME,
                                   settings[constants.CONFIG_SETTINGS_CARBON_HOME])
    log.info("CARBON_HOME : %s" % PACK_LOCATION)
    context = configurations[constants.CONFIG_PARAMS]

    log.info("Context generated: %s", context)
    # if read_env_variables is true context will be generated from environment variables
    # if read_env_variables is not true context will be read from config.ini
    if settings["READ_FROM_ENVIRONMENT"] == "true":
        global READ_FROM_ENVIRONMENT
        READ_FROM_ENVIRONMENT = "true"
        log.info("Reading from environment")
    else:
        # Converting multi-valued params to dictionary
        context = ConfigParserUtil.get_multivalued_attributes_as_dictionary(context)
    return context
def render_template(template_filename, configuration_context):
    """
    parse the template file and return the content as a text

    :param template_filename: template filename path
    :param configuration_context: dictionary containing configurations loaded from module.ini
    :return populated template content
    """
    settings = configuration_context[constants.CONFIG_SETTINGS_KEY]
    # Converting multi-valued params to dictionary
    params_context = configuration_context[constants.CONFIG_PARAMS_KEY]

    if ConfigParserUtil.str_to_bool(settings[constants.CONFIG_READ_FROM_ENVIRONMENT_KEY]):
        log.info("READ_FROM_ENVIRONMENT is set to True. Reading template parameters from environment variables")
        template_source = template_environment.loader.get_source(template_environment, template_filename)[0]
        parsed_content = template_environment.parse(template_source)
        variables = meta.find_undeclared_variables(parsed_content)
        params_context_dict = ConfigParserUtil.get_context_from_env(variables, params_context)
    else:
        params_context_dict = ConfigParserUtil.get_multivalued_attributes_as_dictionary(params_context)

    log.info("Rendering template file: %s using template parameters: %s", template_filename, params_context_dict)
    return template_environment.get_template(template_filename).render(params_context_dict)
def render_template(template_filename, context):
    """
    parse the xml file and return the content as a text

    :param template_filename: template filename path
    :param context: dictionary containing configurations
    :return: xml as a string
    """

    if not context:
        template_source = \
            TEMPLATE_ENVIRONMENT.loader.get_source(TEMPLATE_ENVIRONMENT, template_filename)[0]
        parsed_content = TEMPLATE_ENVIRONMENT.parse(template_source)
        variables = meta.find_undeclared_variables(parsed_content)
        log.debug("Template variables : %s",variables)
        context = ConfigParserUtil.get_context_from_env(variables)
        log.info("Context generated from env %s ", context)
    log.info("Rendering template: %s", template_filename)
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def render_template(template_filename, default_context):
    """
    parse the xml file and return the content as a text

    :param template_filename: template filename path
    :param default_context: dictionary containing configurations read from module.ini
    :return: xml as a string
    """
    if READ_FROM_ENVIRONMENT == "true":
        template_source = \
            TEMPLATE_ENVIRONMENT.loader.get_source(TEMPLATE_ENVIRONMENT, template_filename)[0]
        parsed_content = TEMPLATE_ENVIRONMENT.parse(template_source)
        variables = meta.find_undeclared_variables(parsed_content)
        log.debug("Template variables : %s", variables)
        context = ConfigParserUtil.get_context_from_env(variables, default_context)
    else:
        context = default_context
    log.info("Final context generated for rendering %s : %s ", os.path.basename(template_filename),
             context)
    log.info("Rendering template: %s \n", template_filename)
    return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def traverse(templates_dir, configuration_context):
    """
    traverse through the folder structure and generate xml files

    :param templates_dir: path to the template directory in template module
    :param configuration_context: dictionary containing configurations loaded from module.ini
    :return None
    """
    log.info("Scanning for templates in %s", templates_dir)
    carbon_home = ConfigParserUtil.get_carbon_home(configuration_context)
    for root, dirs, files in os.walk(templates_dir):
        for filename in files:
            # generating the relative path of the template
            template_file_path = os.path.join(root, filename)
            log.debug("Template file path: %s ", template_file_path)
            template_relative_path = os.path.splitext(os.path.relpath(os.path.join(root, filename), templates_dir))[0]
            log.debug("Teplate relative path: %s", template_relative_path)
            template_file_target = os.path.join(carbon_home, template_relative_path)
            log.debug("Template file target path: %s ", template_file_target)

            # populate the template and copy to target location
            generate_file_from_template(template_file_path, template_file_target, configuration_context)
def configure():
    """
    Main method
    :param cli_arguments: command line arguments
    :return None
    """

    log.info("Scanning the template module directory: %s" % template_module_parent_dir)
    # traverse through the template directory
    only_directories = [f for f in os.listdir(template_module_parent_dir) if
                        os.path.isdir(os.path.join(template_module_parent_dir, f))]
    if len(only_directories) == 0:
        log.info("No directories found at %s", template_module_parent_dir)

    for file_name in only_directories:
        module_settings_file_path = os.path.join(template_module_parent_dir, file_name, constants.CONFIG_FILE_NAME)
        templates_dir = os.path.join(template_module_parent_dir, file_name, constants.TEMPLATES_FOLDER_NAME)
        files_dir = os.path.join(template_module_parent_dir, file_name, constants.FILES_FOLDER_NAME)
        if os.path.isfile(module_settings_file_path):
            log.info("Module settings file found for template module: %s", file_name)
        else:
            log.warn("Could not find module settings file at: %s. Skipping directory...", module_settings_file_path)
            continue

        log.info("Populating templates at: %s", templates_dir)
        configuration_context = generate_context(module_settings_file_path)
        traverse(templates_dir, configuration_context)

        # copy files directory if it exists
        if os.path.exists(files_dir):
            log.info("Copying files at: %s", files_dir)
            target = ConfigParserUtil.get_carbon_home(configuration_context)
            copy_files_to_pack(files_dir, target)

        log.info("Configuration completed for template module: %s", file_name)

    log.info("End of WSO2 Private PaaS Configurator")
        log.info("Populating templates at: %s", templates_dir)
        configuration_context = generate_context(module_settings_file_path)
        traverse(templates_dir, configuration_context)

        # copy files directory if it exists
        if os.path.exists(files_dir):
            log.info("Copying files at: %s", files_dir)
            target = ConfigParserUtil.get_carbon_home(configuration_context)
            copy_files_to_pack(files_dir, target)

        log.info("Configuration completed for template module: %s", file_name)

    log.info("End of WSO2 Private PaaS Configurator")


if __name__ == "__main__":
    try:
        cli_arguments = sys.argv[1:]
        # read template module dir from environmental vars or default to configurator's path
        template_module_parent_dir = ConfigParserUtil.get_template_module_dir_from_cargs(cli_arguments)

        if template_module_parent_dir is None:
            template_module_parent_dir = os.environ.get(constants.TEMPLATE_MODULE_PATH_ENV_KEY,
                                                        os.path.join(PATH, constants.TEMPLATE_MODULE_FOLDER_NAME))
        log.info("Running WSO2 Private PaaS Configurator...")
        configure()
    except Exception as e:
        log.exception("Error while executing WSO2 Private PaaS Configurator: %s", e)
        sys.exit(1)