Beispiel #1
0
    def include_file(path: str,
                     direct: bool = False,
                     render: bool = True,
                     indent: int = 4,
                     skip=None,
                     escape=None):

        if not skip:
            skip = []

        if not escape:
            escape = []

        tmp_path = None
        if path.startswith('http'):
            tmp_path, _ = urllib.request.urlretrieve(path)
            env.loader.searchpath.append(os.path.dirname(tmp_path))
            path = os.path.basename(tmp_path)

        if render:
            try:
                data = env.get_template(path).render(**values)

            except jinja2.exceptions.TemplateNotFound as e:
                log and log.debug(
                    f'Used Jinja variables: {json.dumps(values)}')
                raise errors.RenderError(
                    f'Jinja template error: Template "{path}" not found.')
            except jinja2.exceptions.TemplateError as e:
                log and log.debug(
                    f'Used Jinja variables: {json.dumps(values)}')
                raise errors.RenderError(
                    f'Jinja template error in "{colors.bold(path)}": {e}')
        else:
            data, _, _ = env.loader.get_source(env, path)

        # Clean temporary stuff
        if tmp_path is not None:
            env.loader.searchpath = filter(
                lambda p: p != os.path.dirname(path), env.loader.searchpath)
            os.remove(tmp_path)

        if direct:
            prefix = '\n'
        else:
            prefix = '|\n'

        for token in skip:
            replace = ''
            if type(token) is tuple:
                token, replace = token
            data = data.replace(token, replace)
            prefix = prefix.replace(token, replace)

        for token in escape:
            data = data.replace(token, f'\\{token}')
            prefix = prefix.replace(token, f'\\{token}')

        return f'{prefix}{textwrap.indent(data, indent * " ")}'
Beispiel #2
0
    def create_secret(name: str = None,
                      use_pass: bool = True,
                      custom_cmd: bool = False,
                      as_ref: bool = False,
                      data: dict = None,
                      **kwargs):
        if not deployment:
            raise errors.RenderError('create_secret() cannot be used here')
        s = secret.GenericSecret(deployment, data or kwargs, name, use_pass,
                                 custom_cmd)
        if secret.Secret.register(s) and log:
            log.info(f'Registered generic secret "{colors.bold(s.name)}" '
                     f'for deployment "{colors.blue(deployment)} ...')

        if not as_ref:
            return s.name

        keys = (data or kwargs).keys()
        if len(keys) == 0:
            raise errors.RenderError(
                'You must specify at least a secret key if using create_secret() with key_ref = True'
            )

        return json.dumps({'name': s.name, 'key': list(keys)[0]})
Beispiel #3
0
 def create_docker_registry_secret(server: str,
                                   username: str,
                                   password: str,
                                   email: str = None,
                                   name: str = None,
                                   use_pass: bool = True,
                                   custom_cmd: bool = False):
     if not deployment:
         raise errors.RenderError(
             'create_docker_registry_secret() cannot be used here')
     s = secret.DockerRegistrySecret(deployment, server, username, password,
                                     email, name, use_pass, custom_cmd)
     if secret.Secret.register(s) and log:
         log.info(
             f'Registering docker registry secret "{colors.bold(s.name)}" '
             f'for deployment "{colors.blue(deployment)} ...')
     return s.name
Beispiel #4
0
 def create_tls_secret(cert: str,
                       key: str,
                       name: str = None,
                       use_pass: bool = True,
                       custom_cmd: bool = False):
     if not deployment:
         raise errors.RenderError('create_tls_secret() cannot be used here')
     s = secret.TlsSecret(deployment=deployment,
                          name=name,
                          cert=cert,
                          key=key,
                          use_pass=use_pass,
                          custom_cmd=custom_cmd)
     if secret.Secret.register(s) and log:
         log.info(f'Registering TLS secret "{colors.bold(s.name)}" '
                  f'for deployment "{colors.blue(deployment)} ...')
     return s.name
Beispiel #5
0
 def get_version(package: str):
     if not deployment:
         raise errors.RenderError(
             'get_version() or version() cannot be used here')
     return deployment.config.get('versions', {}).get(package, 'latest')