Exemplo n.º 1
0
def _preprocess_file(path, params):
    """Render in place a jinja2 template."""
    if not _is_jinja2_template(path):
        return
    basedir, filename = os.path.split(path)
    env = jinja2.Environment(extensions=["jinja2.ext.with_"],
                             loader=jinja2.FileSystemLoader(basedir))
    # Add custom functions and filters
    utils.update_jinja2_env(env)
    result = env.get_template(filename).render(params)
    with open(path, 'w') as f:
        f.write(result)
Exemplo n.º 2
0
def _preprocess_file(path, params):
    """Renders in place a jinja2 template."""
    if not _is_jinja2_template(path):
        return
    basedir, filename = os.path.split(path)
    env = jinja2.Environment(
        extensions=["jinja2.ext.with_"],
        loader=jinja2.FileSystemLoader(basedir))
    # Add custom functions and filters
    utils.update_jinja2_env(env)
    result = env.get_template(filename).render(params)
    with open(path, 'w') as f:
        f.write(result)
Exemplo n.º 3
0
    def __init__(self,
                 yml_path,
                 logger=None,
                 stage=None,
                 vault_layer=None,
                 parameters=None):
        if logger is None:
            self.logger = logging.getLogger(__name__)
            # To prevent warnings
            self.logger.addHandler(logging.NullHandler())
        else:
            self.logger = logger
        self.__yml_path = yml_path
        self.stage = stage and stage.upper()
        basedir, envfile = os.path.split(yml_path)
        self.basedir = os.path.abspath(basedir)
        self._j2_env = j2.Environment(extensions=["jinja2.ext.with_"],
                                      loader=j2.FileSystemLoader(self.basedir))
        # Add custom functions and filters
        utils.update_jinja2_env(self._j2_env)
        if parameters is None:
            parameters = {}

        if "_default" in parameters:
            def_params = parameters.get("_default", {})
            del parameters["_default"]
            parameters.update(def_params)

        parameters.update(os.environ)
        with open(yml_path, 'r') as f:
            if os.path.splitext(yml_path)[1] == ".j2":
                template = self._j2_env.get_template(envfile)
                meta = yaml.load(template.render(stage=stage, **parameters))
            else:
                meta = yaml.load(f)

        self.name = list(meta.keys())[0]
        self.meta = meta.get(self.name)

        if len(self.meta) == 0:
            raise FileFormatError(yml_path,
                                  "Error getting environment name ",
                                  logger=self.logger)

        self.cf = Cloudformation(config.boto_config)
        self.sns_topic_arn = self.meta.get('sns-topic-arn', [])
        self.tags = self.meta.get('tags', {})
        self.tags['humilis:environment'] = self.name

        self.layers = []
        for layer in self.meta.get('layers', []):
            layer_name = layer.get('layer', None)
            if layer_name is None:
                msg = "Wrongly formatted layer: {}".format(layer)
                raise FileFormatError(yml_path, msg)
            if layer.get('disable', False):
                message = ("Layer {} is disabled by configuration. "
                           "Skipping.".format(layer.get('layer')))
                self.logger.warning(message)
                continue

            # Get the layer params provided in the environment spec
            layer_params = {k: v for k, v in layer.items() if k != 'layer'}
            layer_obj = Layer(self, layer_name, **layer_params)
            self.layers.append(layer_obj)

        self.vault_layer = self.get_layer(vault_layer or 'secrets-vault')
        self.__secrets_table_name = "{}-{}-secrets".format(
            self.name, self.stage)
        if self.stage:
            self.__keychain_namespace = "{}:{}".format(self.name,
                                                       self.stage.lower())
        else:
            self.__keychain_namespace = self.name

        self.__dynamodb = None
Exemplo n.º 4
0
    def __init__(self, yml_path, logger=None, stage=None, vault_layer=None,
                 parameters=None):
        if logger is None:
            self.logger = logging.getLogger(__name__)
            # To prevent warnings
            self.logger.addHandler(logging.NullHandler())
        else:
            self.logger = logger
        self.__yml_path = yml_path
        self.stage = stage and stage.upper()
        self.basedir, envfile = os.path.split(yml_path)
        self._j2_env = j2.Environment(
            extensions=["jinja2.ext.with_"],
            loader=j2.FileSystemLoader(self.basedir))
        # Add custom functions and filters
        utils.update_jinja2_env(self._j2_env)
        if parameters is None:
            parameters = {}
        with open(yml_path, 'r') as f:
            if os.path.splitext(yml_path)[1] == ".j2":
                template = self._j2_env.get_template(envfile)
                meta = yaml.load(template.render(stage=stage, **parameters))
            else:
                meta = yaml.load(f)

        self.name = list(meta.keys())[0]
        self.meta = meta.get(self.name)

        if len(self.meta) == 0:
            raise FileFormatError(yml_path, logger=self.logger)

        self.cf = Cloudformation(config.boto_config)
        self.sns_topic_arn = self.meta.get('sns-topic-arn', [])
        self.tags = self.meta.get('tags', {})
        self.tags['humilis:environment'] = self.name

        self.layers = []
        for layer in self.meta.get('layers', []):
            layer_name = layer.get('layer', None)
            if layer_name is None:
                msg = "Wrongly formatted layer: {}".format(layer)
                raise FileFormatError(yml_path, msg)
            if layer.get('disable', False):
                message = ("Layer {} is disabled by configuration. "
                           "Skipping.".format(layer.get('layer')))
                self.logger.warning(message)
                continue

            # Get the layer params provided in the environment spec
            layer_params = {k: v for k, v in layer.items() if k != 'layer'}
            layer_obj = Layer(self, layer_name, **layer_params)
            self.layers.append(layer_obj)

        self.vault_layer = self.get_layer(vault_layer or 'secrets-vault')
        self.__secrets_table_name = "{}-{}-secrets".format(self.name,
                                                           self.stage)
        if self.stage:
            self.__keychain_namespace = "{}:{}".format(self.name,
                                                       self.stage.lower())
        else:
            self.__keychain_namespace = self.name

        self.__dynamodb = None
Exemplo n.º 5
0
    def __init__(self,
                 yml_path,
                 logger=None,
                 stage=None,
                 vault_layer=None,
                 parameters=None):
        if logger is None:
            self.logger = logging.getLogger(__name__)
            # To prevent warnings
            self.logger.addHandler(logging.NullHandler())
        else:
            self.logger = logger

        if stage is None:
            raise ValueError("stage can't be None")

        self.__yml_path = yml_path
        self.stage = stage and stage.upper()
        basedir, envfile = os.path.split(yml_path)
        self.basedir = os.path.abspath(basedir)
        self._j2_env = j2.Environment(loader=j2.FileSystemLoader(self.basedir))
        # Add custom functions and filters
        utils.update_jinja2_env(self._j2_env)

        parameters = self._preprocess_parameters(parameters)
        with open(yml_path, 'r') as f:
            if os.path.splitext(yml_path)[1] == ".j2":
                template = self._j2_env.get_template(envfile)
                meta = yaml.load(
                    template.render(
                        stage=stage,  # Backwards compatibility
                        __context={
                            'stage': stage,
                            'aws': {
                                'account_id':
                                boto3.client('sts').get_caller_identity().get(
                                    'Account')
                            }
                        },
                        __env=os.environ,
                        **parameters),
                    Loader=yaml.FullLoader)
            else:
                meta = yaml.load(f, Loader=yaml.FullLoader)

        self.name = list(meta.keys())[0]
        self.meta = meta.get(self.name)

        if len(self.meta) == 0:
            raise FileFormatError(yml_path,
                                  "Error getting environment name ",
                                  logger=self.logger)

        self.cf = Cloudformation(config.boto_config)
        self.sns_topic_arn = self.meta.get('sns-topic-arn', [])
        self.tags = self.meta.get('tags', {})
        self.tags['humilis:environment'] = self.name

        self.layers = []
        for layer in self.meta.get('layers', []):
            layer_name = layer.get('layer', None)
            if layer_name is None:
                msg = "Wrongly formatted layer: {}".format(layer)
                raise FileFormatError(yml_path, msg)
            if layer.get('disable', False):
                message = ("Layer {} is disabled by configuration. "
                           "Skipping.".format(layer.get('layer')))
                self.logger.warning(message)
                continue

            # Get the layer params provided in the environment spec
            layer_params = {k: v for k, v in layer.items() if k != 'layer'}
            layer_obj = Layer(self, layer_name, **layer_params)
            self.layers.append(layer_obj)

        self.vault_layer = self.get_layer(vault_layer or 'secrets-vault')
        self.__secrets_table_name = "{}-{}-secrets".format(
            self.name, self.stage)
        self.__keychain_namespace = "{}:{}".format(self.name,
                                                   self.stage.lower())
        self.__dynamodb = None