Ejemplo n.º 1
0
    def test_get_cloudformation_template_raises_exception_on_any_error(
            self, get_yaml_or_json_file_mock):
        get_yaml_or_json_file_mock.side_effect = Exception

        with self.assertRaises(TemplateErrorException):
            FileLoader.get_cloudformation_template(
                "s3://my-bucket/template.yml", None)
Ejemplo n.º 2
0
    def test_get_cloudformation_template_returns_template_with_transform_property(
            self, get_yaml_or_json_file_mock):
        expected = {
            'Conditions': {},
            'Mappings': {},
            'Metadata': {},
            'Resources': 'Foo',
            'Transform': 'transform-section',
            'Parameters': {},
            'Outputs': {},
            'AWSTemplateFormatVersion': '2010-09-09',
            'Description': ''
        }

        get_yaml_or_json_file_mock.return_value = {
            "Resources": "Foo",
            "Transform": "transform-section"
        }

        result = FileLoader.get_cloudformation_template(
            "s3://my-bucket/template.yml", None)

        self.assertEqual(expected, result.get_template_body_dict())
        get_yaml_or_json_file_mock.assert_called_once_with(
            's3://my-bucket/template.yml', None)
def upload_cfn_to_s3(project, logger):
    """
    This task is separate since they only work with Python2 >2.6 by
    the time being. Python3 support is underway.

    This means, when using Python<2.7, this task is not visible
    (see __init__.py).
    """
    from cfn_sphere.file_loader import FileLoader
    from cfn_sphere.template.transformer import CloudFormationTemplateTransformer

    for path, filename in project.get_property('template_files'):
        template = FileLoader.get_cloudformation_template(filename, path)
        transformed = CloudFormationTemplateTransformer.transform_template(
                template)
        output = transformed.get_template_json()

        bucket_name = project.get_property('bucket_name')
        key_prefix = project.get_property('template_key_prefix')
        filename = filename.replace('.yml', '.json')
        filename = filename.replace('.yaml', '.json')
        version_path = '{0}v{1}/{2}'.format(
                key_prefix, project.version, filename)
        # latest_path = '{0}latest/{1}'.format(key_prefix, filename)

        acl = project.get_property('template_file_access_control')
        check_acl_parameter_validity('template_file_access_control', acl)
        upload_helper(logger, bucket_name, version_path, output, acl)
Ejemplo n.º 4
0
def upload_cfn_to_s3(project, logger):
    """
    This task is separate since they only work with Python2 >2.6 by
    the time being. Python3 support is underway.

    This means, when using Python<2.7, this task is not visible
    (see __init__.py).
    """
    from cfn_sphere.file_loader import FileLoader
    from cfn_sphere.template.transformer import CloudFormationTemplateTransformer

    for path, filename in project.get_property('template_files'):
        template = FileLoader.get_cloudformation_template(filename, path)
        transformed = CloudFormationTemplateTransformer.transform_template(
            template)
        output = transformed.get_template_json()

        bucket_name = project.get_property('bucket_name')
        key_prefix = project.get_property('template_key_prefix')
        filename = filename.replace('.yml', '.json')
        filename = filename.replace('.yaml', '.json')
        version_path = '{0}v{1}/{2}'.format(key_prefix, project.version,
                                            filename)
        # latest_path = '{0}latest/{1}'.format(key_prefix, filename)

        acl = project.get_property('template_file_access_control')
        check_acl_parameter_validity('template_file_access_control', acl)
        upload_helper(logger, bucket_name, version_path, output, acl)
Ejemplo n.º 5
0
def render_template(template_file, confirm, yes):
    confirm = confirm or yes
    if not confirm:
        check_update_available()

    loader = FileLoader()
    template = loader.get_cloudformation_template(template_file, None)
    template = CloudFormationTemplateTransformer.transform_template(template)
    click.echo(template.get_pretty_template_json())
Ejemplo n.º 6
0
def render_template(template_file, confirm, yes):
    confirm = confirm or yes
    if not confirm:
        check_update_available()

    loader = FileLoader()
    template = loader.get_cloudformation_template(template_file, None)
    template = CloudFormationTemplateTransformer.transform_template(template)
    click.echo(template.get_pretty_template_json())
Ejemplo n.º 7
0
 def get_template(template_url, working_dir, region, package_bucket):
     template = FileLoader.get_cloudformation_template(
         template_url, working_dir)
     additional_stack_description = "Config repo url: {0}".format(
         get_git_repository_remote_url(working_dir))
     template = CloudFormationTemplateTransformer.transform_template(
         template, additional_stack_description)
     template = CloudFormationSamPackager.package(template_url, working_dir,
                                                  template, region,
                                                  package_bucket)
     return template
    def test_get_cloudformation_template_returns_template(self, get_yaml_or_json_file_mock):
        expected = {
            'Conditions': {},
            'Mappings': {},
            'Resources': 'Foo',
            'Parameters': {},
            'Outputs': {},
            'AWSTemplateFormatVersion': '2010-09-09',
            'Description': ''
        }

        get_yaml_or_json_file_mock.return_value = {"Resources": "Foo"}

        response = FileLoader.get_cloudformation_template("s3://my-bucket/template.yml", None)

        self.assertEqual(expected, response.get_template_body_dict())
        get_yaml_or_json_file_mock.assert_called_once_with('s3://my-bucket/template.yml', None)
Ejemplo n.º 9
0
    def create_or_update_stacks(self):
        existing_stacks = self.cfn.get_stack_names()
        desired_stacks = self.config.stacks
        stack_processing_order = DependencyResolver().get_stack_order(desired_stacks)

        if len(stack_processing_order) > 1:
            self.logger.info(
                "Will process stacks in the following order: {0}".format(", ".join(stack_processing_order)))

        for stack_name in stack_processing_order:
            stack_config = self.config.stacks.get(stack_name)

            template = FileLoader.get_cloudformation_template(stack_config.template_url,stack_config.working_dir)
            transformed_template = CloudFormationTemplateTransformer.transform_template(template)

            if stack_config.stack_policy_url:
                self.logger.info("Using stack policy from {0}".format(stack_config.stack_policy_url))
                stack_policy = FileLoader.get_yaml_or_json_file(stack_config.stack_policy_url, stack_config.working_dir)
            else:
                stack_policy = None

            parameters = self.parameter_resolver.resolve_parameter_values(stack_name, stack_config)
            merged_parameters = self.parameter_resolver.update_parameters_with_cli_parameters(
                parameters=parameters,
                cli_parameters=self.cli_parameters,
                stack_name=stack_name)

            self.logger.debug("Parameters after merging with cli options: {0}".format(merged_parameters))

            stack = CloudFormationStack(template=transformed_template,
                                        parameters=merged_parameters,
                                        tags=stack_config.tags,
                                        name=stack_name,
                                        region=self.config.region,
                                        timeout=stack_config.timeout,
                                        service_role=stack_config.service_role,
                                        stack_policy=stack_policy)

            if stack_name in existing_stacks:

                self.cfn.validate_stack_is_ready_for_action(stack)
                self.cfn.update_stack(stack)
            else:
                self.cfn.create_stack(stack)

            CustomResourceHandler.process_post_resources(stack)
Ejemplo n.º 10
0
def validate_template(template_file, confirm, yes):
    confirm = confirm or yes
    if not confirm:
        check_update_available()

    try:
        loader = FileLoader()
        template = loader.get_cloudformation_template(template_file, None)
        template = CloudFormationTemplateTransformer.transform_template(template)
        CloudFormation().validate_template(template)
        click.echo("Template is valid")
    except CfnSphereException as e:
        LOGGER.error(e)
        sys.exit(1)
    except Exception as e:
        LOGGER.error("Failed with unexpected error")
        LOGGER.exception(e)
        LOGGER.info("Please report at https://github.com/cfn-sphere/cfn-sphere/issues!")
        sys.exit(1)
Ejemplo n.º 11
0
def validate_template(template_file, confirm, yes):
    confirm = confirm or yes
    if not confirm:
        check_update_available()

    try:
        loader = FileLoader()
        template = loader.get_cloudformation_template(template_file, None)
        template = CloudFormationTemplateTransformer.transform_template(template)
        CloudFormation().validate_template(template)
        click.echo("Template is valid")
    except CfnSphereException as e:
        LOGGER.error(e)
        sys.exit(1)
    except Exception as e:
        LOGGER.error("Failed with unexpected error")
        LOGGER.exception(e)
        LOGGER.info("Please report at https://github.com/cfn-sphere/cfn-sphere/issues!")
        sys.exit(1)
Ejemplo n.º 12
0
    def test_get_cloudformation_template_raises_exception_on_any_error(self, get_yaml_or_json_file_mock):
        get_yaml_or_json_file_mock.side_effect = Exception

        with self.assertRaises(TemplateErrorException):
            FileLoader.get_cloudformation_template("s3://my-bucket/template.yml", None)
Ejemplo n.º 13
0
 def get_template(template_url, working_dir):
     template = FileLoader.get_cloudformation_template(template_url, working_dir)
     additional_stack_description = "Config repo url: {0}".format(get_git_repository_remote_url(working_dir))
     return CloudFormationTemplateTransformer.transform_template(template, additional_stack_description)