Esempio n. 1
0
 def __init__(self, msg="", hint_msg=""):
     if not msg:
         raise utils.handle_missing_param("msg")
     if hint_msg:
         super().__init__(f"User error: {msg}\nHint: {hint_msg}")
     else:
         super().__init__(f"User error: {msg}")
Esempio n. 2
0
    def __init__(self, ctx=None):

        if not ctx:
            raise utils.handle_missing_param(list(locals().keys()))

        self.data = {}
        self._ctx = ctx
        self._load_modules()
Esempio n. 3
0
    def __init__(self, ctx=None):

        if not ctx:
            raise utils.handle_missing_param(list(locals().keys()))

        self.env = {}
        self._ctx = ctx

        self._parse_minipresto_config()
        self._parse_user_env()
Esempio n. 4
0
    def get_section(self, section=""):
        """Gets and returns a section from the environment variables. If the
        section doesn't exist, an empty dict is returned.

        ### Parameters
        - `section`: The section to return, if it exists."""

        if not section:
            raise utils.handle_missing_param(list(locals().keys()))

        return self.env.get(section.upper(), {})
Esempio n. 5
0
    def get_var(self, key="", default=None):
        """Gets and returns a variable from a section of the environment
        variables. Since it is assumed there will not be duplicate environment
        variables between sections, the first-match is returned.

        ### Parameters
        - `key`: The key to search for.
        - `default` The default value to return if the key is not found."""

        if not key:
            raise utils.handle_missing_param(["key"])

        for section_k, section_v in self.env.items():
            if isinstance(section_v, dict):
                for section_dict_k, section_dict_v in section_v.items():
                    if section_dict_k.upper() == key.upper():
                        return section_dict_v
        return default
Esempio n. 6
0
def execute_container_bootstrap(ctx,
                                bootstrap="",
                                container_name="",
                                yaml_file=""):
    """Executes a single bootstrap inside a container. If the
    `/opt/minipresto/bootstrap_status.txt` file has the same checksum as the
    bootstrap script that is about to be executed, the boostrap script is
    skipped.

    Returns `False` if the script is not executed and `True` if it is."""

    if any((not bootstrap, not container_name, not yaml_file)):
        raise utils.handle_missing_param(list(locals().keys()))

    bootstrap_file = os.path.join(os.path.dirname(yaml_file), "resources",
                                  "bootstrap", bootstrap)
    if not os.path.isfile(bootstrap_file):
        raise err.UserError(
            f"Bootstrap file does not exist at location: {bootstrap_file}",
            "Check this module in the library to ensure the bootstrap script is present.",
        )

    # Add executable permissions to bootstrap
    st = os.stat(bootstrap_file)
    os.chmod(
        bootstrap_file,
        st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH,
    )

    bootstrap_checksum = hashlib.md5(open(bootstrap_file,
                                          "rb").read()).hexdigest()
    container = ctx.docker_client.containers.get(container_name)

    # Check if this script has already been executed
    output = ctx.cmd_executor.execute_commands(
        "cat /opt/minipresto/bootstrap_status.txt",
        suppress_output=True,
        container=container,
        trigger_error=False,
    )

    if f"{bootstrap_checksum}" in output[0].get("output", ""):
        ctx.logger.log(
            f"Bootstrap already executed in container '{container_name}'. Skipping.",
            level=ctx.logger.verbose,
        )
        return False

    ctx.logger.log(
        f"Executing bootstrap script in container '{container_name}'...",
        level=ctx.logger.verbose,
    )

    ctx.cmd_executor.execute_commands(
        f"docker cp {bootstrap_file} {container_name}:/tmp/")

    # Record executed file checksum
    ctx.cmd_executor.execute_commands(
        f"/tmp/{os.path.basename(bootstrap_file)}",
        f'bash -c "echo {bootstrap_checksum} >> /opt/minipresto/bootstrap_status.txt"',
        container=container,
    )

    ctx.logger.log(
        f"Successfully executed bootstrap script in container '{container_name}'.",
        level=ctx.logger.verbose,
    )

    return True
Esempio n. 7
0
 def __init__(self, msg=""):
     if not msg:
         raise utils.handle_missing_param(list(locals().keys()))
     super().__init__(msg)
     self.msg = msg
Esempio n. 8
0
    def __init__(self, ctx=None):

        if not ctx:
            raise utils.handle_missing_param(list(locals().keys()))

        self._ctx = ctx