示例#1
0
    def load_config(self):
        """Load config data from project directory"""
        env_var_convertor = EnvVarConvertor()
        file_loader = FileLoader(file_path=self.get_config_file())
        file_loader.successor = env_var_convertor
        _, self._config = file_loader.process()

        self.validate()
示例#2
0
    def get_templates(self):
        """Get all the nested stacks"""
        # Create master template
        templates = {"tmp_dir": self._temp_dir, "master": None, "children": []}

        def yaml_tmp_ctor(loader, tag_suffix, node):
            if tag.suffix.startswith('!'):
                return node

        _, master_tpl = FileLoader(
            file_path=self._template_path,
            allowed_ext=FILE_LOADER_TEMPLATE_ALLOWED_EXT,
            yaml_replacements=self.YAML_TO).process()

        template_urls = []
        for name, resource in master_tpl['Resources'].iteritems():
            if resource['Type'] == 'AWS::CloudFormation::Stack':
                template_urls.append(resource['Properties']['TemplateURL'])

        if template_urls:
            for url in template_urls:
                found = NestedTemplateUrlConvertor.match(url)
                if len(found) > 0:
                    for fnd in found:
                        header, template_name = fnd.split("|")
                        templates['children'].append(
                            self.create_template(template_name))

        more_childen = self.get_child_template_from_param()
        if more_childen:
            templates['children'] += more_childen

        templates['master'] = self.create_template(self._template_path)

        return templates
示例#3
0
    def find_template(self, template_name):
        """Get list of params files"""
        findings = []
        if os.path.isdir(self._repo_path):
            findings = FileLoader.find_files_by_names(
                search_path=self._repo_path, only_files=[template_name])

        # Only return the first found
        return findings[0] if findings else None
示例#4
0
    def convert(self, data):
        """
        Convert all possible environment
        variable name to value
        """
        data_string, _ = data

        for key, value in self.replacement_data(data_string).iteritems():
            data_string = data_string.replace(key, value)

        return data_string, FileLoader.toJson(data_string)
    def convert(self, data):
        """
        Convert all lmdo template url format '$template|[template name]'
        to its value AWS format "https://s3.amazonaws.com/[bucket name]/[service id]/[template name]"
        """
        data_string, _ = data

        for key, value in self.replacement_data(data_string).iteritems():
            data_string = data_string.replace(key, value)

        return data_string, FileLoader.toJson(data_string)
示例#6
0
    def create_template(self, template_name):
        """Create shadow template for upload"""
        # Setup convertor chain
        env_var_convertor = EnvVarConvertor()
        stack_var_convertor = StackVarConvertor()
        nested_template_convertor = NestedTemplateUrlConvertor()

        env_var_convertor.successor = stack_var_convertor
        stack_var_convertor.successor = nested_template_convertor

        if not os.path.isfile(template_name):
            file_path = self.find_template(template_name)
        else:
            file_path = template_name

        if not file_path:
            Oprint.err('Cannot find template {} in {}'.format(
                template_name, self._repo_path))

        file_loader = FileLoader(file_path=file_path,
                                 allowed_ext=FILE_LOADER_TEMPLATE_ALLOWED_EXT,
                                 yaml_replacements=self.YAML_TO)
        file_loader.successor = env_var_convertor
        result, json_content = file_loader.process()

        # If it's yaml template, change ^ back to !
        # and dump back with yaml syntax
        if file_loader.isYaml():
            #result = yaml.safe_dump(result, default_flow_style=False, encoding=('utf-8'))
            for key, value in self.TO_YAML.iteritems():
                result = result.replace(key, value)

        template_name = os.path.basename(file_path)
        new_file_path = os.path.join(self._temp_dir, template_name)

        with open(new_file_path, 'w+') as f:
            f.write(result)
        f.close()

        return new_file_path
示例#7
0
    def get_child_template_from_param(self):
        """Check if we have nested templates from params"""
        if not self._params_path:
            return False

        _, child_tpls = FileLoader(
            file_path=self._params_path,
            allowed_ext=FILE_LOADER_TEMPLATE_ALLOWED_EXT).process()

        templates = []
        for key, value in child_tpls.iteritems():
            found = NestedTemplateUrlConvertor.match(value)
            if len(found) > 0:
                for fnd in found:
                    header, template_name = fnd.split("|")
                    templates.append(self.create_template(template_name))

        return templates if len(templates) > 0 else False
示例#8
0
文件: iam.py 项目: piwell/lmdo
    def create_lambda_role(self, role_name, role_policy):
        """
            Create role for Lambda, all policies
            store in the assume role policy doc
            so that we can do update
            If not set, will using default
            which enables Lambda for invoking
            and logging
        """
        try:
            Oprint.info(
                'Start creating role {} and policie for Lambda'.format(
                    role_name), 'iam')

            assume_roles = []
            if role_policy:
                assume_roles = role_policy.get('AssumeRoles')

            role_doc = self.get_lambda_default_assume_role_doc(
                extra_services=assume_roles)

            role = self.get_role(role_name)

            if not role:
                role = self._client.create_role(
                    RoleName=role_name,
                    AssumeRolePolicyDocument=json.dumps(role_doc))
            else:
                self._client.update_assume_role_policy(
                    RoleName=role_name, PolicyDocument=json.dumps(role_doc))

            to_replace = {
                "$region": self.get_region(),
                "$accountId": self.get_account_id()
            }

            policy_doc = []
            if role_policy and role_policy.get('PolicyDocument'):
                _, policy_doc = FileLoader(
                    file_path=role_policy.get('PolicyDocument')).process()
                policy_doc = policy_doc['Statement']

            policy_doc = self.get_lambda_default_policy_doc(
                extra_statement=policy_doc)

            policy_doc = update_template(json.dumps(policy_doc), to_replace)

            # Do inline policy so that when deleting the role, it'll be deleted
            self._client.put_role_policy(
                RoleName=role_name,
                PolicyName='{}-policy'.format(role_name),
                PolicyDocument=policy_doc)

            if role_policy and role_policy is dict and role_policy.get(
                    'ManagedPolicyArns'):
                for m_policy_arn in role_policy.get('ManagedPolicyArns'):
                    self._client.attach_role_policy(RoleName=role_name,
                                                    PolicyArn=m_policy_arn)

            Oprint.info(
                'Complete creating role {} and policie for Lambda'.format(
                    role_name), 'iam')
        except Exception as e:
            Oprint.err(e, 'iam')

        return role