Esempio n. 1
0
def auto_generate_config(c):
    p_log("Task: auto_generate_config")
    reform_root = settings.GetReformRoot()

    config = ConfigManager.ConfigManager(
        {"env": "defaults"}
    ).auto_generate_default_config()
Esempio n. 2
0
def get_config(c):
    """
    Fetches part of the config for use in a terraform map.
    Terraform can't handle multidimensional maps, this tool fetches a section of
    map and returns it as json.  Unlike other tasks, this tasks gets it's args
    from a json string sent to stdin.
    """
    p_log("Task: get_config")
    reform_root = settings.GetReformRoot()
    params = {}

    lines = [x.strip() for x in sys.stdin.readlines()]

    lines = list(filter(None, lines))
    if len(lines) != 0:
        params = json.loads(",".join(lines))

    c = {}

    if "cipher" in params and params["cipher"]:
        file = "%s/configs/%s/%s" % (
            reform_root,
            params["env"],
            ReformSettings.ReformSettings.reform_quadrant_secret_file,
        )
        if os.path.exists(file):
            with open(file, "r") as f:
                config = json.loads(f.read())
        else:
            p_log("Nested map not found: %s" % (file))
            exit(5)
    else:
        config = ConfigManager.ConfigManager({
            "env": params["env"]
        }).get_merge_configs()

    p_log("args: %s" % (params))

    if "swimlanes" in config:
        members = config["swimlanes"]
        # debug("Nested map found: %s"%(json.dumps(members)))
        if (params["client"] in members and params["service"]
                in members[params["client"]]["services"]):
            c = members[params["client"]]["services"][
                params["service"]]["configstore"]
    else:
        if params["client"] in config and params["service"] in config[
                params["client"]]:
            c = config[params["client"]][params["service"]]

    if "cipher" in params and params["cipher"]:
        c = SecretsManager.SecretsManager({
            "key": params["env"],
            "cipher": params["cipher"]
        }).secretDecoderRing(c)
    print(json.dumps(c))
Esempio n. 3
0
def config_delete_file(c, quadrant):
    """
    Delete/Truncate the whole config file
    """
    p_log("Task: config_delete_file")
    result = ConfigManager.ConfigManager({"env": quadrant}).delete_config()

    if result:
        print("ok")
        return True
    return False
Esempio n. 4
0
def config_delete(c, quadrant, attribute):
    """
    Delete an attribute in our terraform configs.
    """
    p_log("Task: config_delete")
    result = ConfigManager.ConfigManager(
        {"env": quadrant, "attribute": attribute}
    ).delete()

    if result:
        print("ok")
        return True
    return False
Esempio n. 5
0
def config_set(c, quadrant, attribute, value="", secure=False):
    """
    Set an attribute in our terraform configs.
    """
    p_log("Task: config_set")
    result = ConfigManager.ConfigManager(
        {"env": quadrant, "attribute": attribute, "value": value}
    ).upsert()

    if result:
        p_log("ok")
        return True
    return False
Esempio n. 6
0
def config_get(c, quadrant, attribute, cipher=None, output="text"):
    """
    Get an attribute from our configs.  If you set the cipher then it assumes you want config from a secret.
    """
    p_log("Task: config_get")
    result = ConfigManager.ConfigManager(
        {"env": quadrant, "attribute": attribute, "cipher": cipher}
    ).read()

    if output == "json":
        ret = json.dumps(result)
        print(ret)
        return ret
    else:
        print(result)
        return result
Esempio n. 7
0
def preform(c, quadrant):
    """
    A simple preprocessor for terraform that processes *\*.tf.tpl* files.
    This is how we work around terraforms lack of loops and conditionals.

    This is also how we seed our dynamic reform configs for state backend and and configs we've defined.
    """
    p_log("Start: Preform")
    projects_base_path = settings.GetReformRoot()

    # TODO Open this more to include modules
    work_dir = settings.GetReformRoot()
    modules_dir = "%s/modules" % (settings.GetReformRoot())
    projects_dir = "%s/projects" % (settings.GetReformRoot())
    template_suffix = ".tpl"
    env = Environment(loader=FileSystemLoader(work_dir), trim_blocks=True)

    # Custom Jinja Filters
    def is_list(value):
        return isinstance(value, list)

    def is_dict(value):
        return isinstance(value, dict)

    env.filters["is_list"] = is_list
    env.filters["is_dict"] = is_dict
    env.filters["jsonify"] = json.dumps

    config = ConfigManager.ConfigManager({"env": quadrant}).get_merge_configs()
    secret_manager = SecretsManager.SecretsManager(
        {"key": quadrant, "cipher": "RSA_AES"}
    )
    env_secret = secret_manager.getSecretPath(quadrant)
    secrets = secret_manager.decryptSecretFile(env_secret)
    # Handle modules dir
    for directory, subdirectories, files in os.walk(modules_dir):
        for file in files:
            if file.endswith(template_suffix):
                debug("Found template file: %s" % (file))
                full_file_path = os.path.join(directory, file)
                template = env.get_template(full_file_path.replace(work_dir, ""))
                new_full_file_path = re.sub(
                    template_suffix, "", os.path.join(directory, "preform_" + file)
                )

                debug("Generating file: %s" % (new_full_file_path))
                try:
                    with open(new_full_file_path, "w+") as outfile:
                        redered_template = template.render(
                            config=config,
                            project=os.path.basename(directory),
                            quadrant=quadrant,
                            secrets=secrets,
                        )
                        debug(redered_template)
                        outfile.write(
                            "##################################################\n"
                        )
                        outfile.write(
                            "# This file auto generated by preform, do not edit!\n"
                        )
                        outfile.write("# Instead edit \n")
                        outfile.write("# %s\n" % (full_file_path))
                        outfile.write(
                            "##################################################\n"
                        )
                        outfile.write("\n\n")
                        outfile.write(redered_template)
                        outfile.write("\n\n")
                    outfile.close()
                except:
                    pass

    # Handle projects dir
    for directory, subdirectories, files in os.walk(projects_dir):
        for file in files:
            if file.endswith(template_suffix):
                debug("Found template file: %s" % (file))
                full_file_path = os.path.join(directory, file)
                template = env.get_template(full_file_path.replace(work_dir, ""))
                new_full_file_path = re.sub(
                    template_suffix, "", os.path.join(directory, "preform_" + file)
                )

                debug("Generating file: %s" % (new_full_file_path))
                with open(new_full_file_path, "w+") as outfile:
                    redered_template = template.render(
                        config=config,
                        project=os.path.basename(directory),
                        quadrant=quadrant,
                        secrets=secrets,
                    )
                    debug(redered_template)
                    outfile.write(
                        "##################################################\n"
                    )
                    outfile.write(
                        "# This file auto generated by preform, do not edit!\n"
                    )
                    outfile.write("# Instead edit \n")
                    outfile.write("# %s\n" % (full_file_path))
                    outfile.write(
                        "##################################################\n"
                    )
                    outfile.write("\n\n")
                    outfile.write(redered_template)
                    outfile.write("\n\n")
                outfile.close()

    p_log("Complete: Preform")