예제 #1
0
 def setUp(self):
     self.package_command_context = PackageContext(
         template_file="template-file",
         s3_bucket="s3-bucket",
         s3_prefix="s3-prefix",
         kms_key_id="kms-key-id",
         output_template_file=None,
         use_json=True,
         force_upload=True,
         metadata={},
         region=None,
         profile=None,
     )
 def setUp(self):
     self.package_command_context = PackageContext(
         template_file="template-file",
         s3_bucket="s3-bucket",
         s3_prefix="s3-prefix",
         image_repository="image-repo",
         image_repositories=None,
         kms_key_id="kms-key-id",
         output_template_file=None,
         use_json=True,
         force_upload=True,
         no_progressbar=False,
         metadata={},
         region=None,
         profile=None,
     )
예제 #3
0
파일: command.py 프로젝트: sky-big/fscli
def do_cli(
    template_file,
    s3_bucket,
    s3_prefix,
    kms_key_id,
    output_template_file,
    use_json,
    force_upload,
    metadata,
    region,
    profile,
):
    from samcli.commands.package.package_context import PackageContext

    with PackageContext(
            template_file=template_file,
            s3_bucket=s3_bucket,
            s3_prefix=s3_prefix,
            kms_key_id=kms_key_id,
            output_template_file=output_template_file,
            use_json=use_json,
            force_upload=force_upload,
            metadata=metadata,
            region=region,
            profile=profile,
    ) as package_context:
        package_context.run()
예제 #4
0
 def test_template_path_valid_no_json(self, patched_boto):
     with tempfile.NamedTemporaryFile(mode="w",
                                      delete=False) as temp_template_file:
         package_command_context = PackageContext(
             template_file=temp_template_file.name,
             s3_bucket="s3-bucket",
             s3_prefix="s3-prefix",
             kms_key_id="kms-key-id",
             output_template_file=None,
             use_json=False,
             force_upload=True,
             metadata={},
             region=None,
             profile=None,
         )
         package_command_context.run()
예제 #5
0
def do_cli(
    template_file,
    s3_bucket,
    image_repository,
    image_repositories,
    s3_prefix,
    kms_key_id,
    output_template_file,
    use_json,
    force_upload,
    no_progressbar,
    metadata,
    signing_profiles,
    region,
    profile,
    resolve_s3,
):
    """
    Implementation of the ``cli`` method
    """

    from samcli.commands.package.package_context import PackageContext

    if resolve_s3:
        s3_bucket = manage_stack(profile=profile, region=region)
        click.echo(f"\n\t\tManaged S3 bucket: {s3_bucket}")
        click.echo(
            "\t\tA different default S3 bucket can be set in samconfig.toml")
        click.echo("\t\tOr by specifying --s3-bucket explicitly.")

    with PackageContext(
            template_file=template_file,
            s3_bucket=s3_bucket,
            image_repository=image_repository,
            image_repositories=image_repositories,
            s3_prefix=s3_prefix,
            kms_key_id=kms_key_id,
            output_template_file=output_template_file,
            use_json=use_json,
            force_upload=force_upload,
            no_progressbar=no_progressbar,
            metadata=metadata,
            region=region,
            profile=profile,
            signing_profiles=signing_profiles,
    ) as package_context:
        package_context.run()
예제 #6
0
def do_cli(
    template_file,
    s3_bucket,
    s3_prefix,
    kms_key_id,
    output_template_file,
    use_json,
    force_upload,
    no_progressbar,
    metadata,
    signing_profiles,
    region,
    profile,
    resolve_s3,
):
    from samcli.commands.package.package_context import PackageContext
    from samcli.commands.package.exceptions import PackageResolveS3AndS3SetError, PackageResolveS3AndS3NotSetError

    if resolve_s3 and bool(s3_bucket):
        raise PackageResolveS3AndS3SetError()

    if not resolve_s3 and not bool(s3_bucket):
        raise PackageResolveS3AndS3NotSetError()

    if resolve_s3:
        s3_bucket = manage_stack(profile=profile, region=region)
        click.echo(f"\n\t\tManaged S3 bucket: {s3_bucket}")
        click.echo(
            "\t\tA different default S3 bucket can be set in samconfig.toml")
        click.echo("\t\tOr by specifying --s3-bucket explicitly.")

    with PackageContext(
            template_file=template_file,
            s3_bucket=s3_bucket,
            s3_prefix=s3_prefix,
            kms_key_id=kms_key_id,
            output_template_file=output_template_file,
            use_json=use_json,
            force_upload=force_upload,
            no_progressbar=no_progressbar,
            metadata=metadata,
            region=region,
            profile=profile,
            signing_profiles=signing_profiles,
    ) as package_context:
        package_context.run()
def do_cli(
    template_file,
    stack_name,
    s3_bucket,
    force_upload,
    s3_prefix,
    kms_key_id,
    parameter_overrides,
    capabilities,
    no_execute_changeset,
    role_arn,
    notification_arns,
    fail_on_empty_changeset,
    use_json,
    tags,
    metadata,
    guided,
    confirm_changeset,
    region,
    profile,
):
    from samcli.commands.package.package_context import PackageContext
    from samcli.commands.deploy.deploy_context import DeployContext
    from samcli.commands.deploy.guided_context import GuidedContext

    if guided:
        # Allow for a guided deploy to prompt and save those details.
        guided_context = GuidedContext(
            template_file=template_file,
            stack_name=stack_name,
            s3_bucket=s3_bucket,
            s3_prefix=s3_prefix,
            region=region,
            profile=profile,
            confirm_changeset=confirm_changeset,
            capabilities=capabilities,
            parameter_overrides=parameter_overrides,
            config_section=CONFIG_SECTION,
        )
        guided_context.run()

    print_deploy_args(
        stack_name=guided_context.guided_stack_name if guided else stack_name,
        s3_bucket=guided_context.guided_s3_bucket if guided else s3_bucket,
        region=guided_context.guided_region if guided else region,
        capabilities=guided_context.guided_capabilities
        if guided else capabilities,
        parameter_overrides=guided_context.guided_parameter_overrides
        if guided else parameter_overrides,
        confirm_changeset=guided_context.confirm_changeset
        if guided else confirm_changeset,
    )

    with osutils.tempfile_platform_independent() as output_template_file:

        with PackageContext(
                template_file=template_file,
                s3_bucket=guided_context.guided_s3_bucket
                if guided else s3_bucket,
                s3_prefix=guided_context.guided_s3_prefix
                if guided else s3_prefix,
                output_template_file=output_template_file.name,
                kms_key_id=kms_key_id,
                use_json=use_json,
                force_upload=force_upload,
                metadata=metadata,
                on_deploy=True,
                region=guided_context.guided_region if guided else region,
                profile=profile,
        ) as package_context:
            package_context.run()

        with DeployContext(
                template_file=output_template_file.name,
                stack_name=guided_context.guided_stack_name
                if guided else stack_name,
                s3_bucket=guided_context.guided_s3_bucket
                if guided else s3_bucket,
                force_upload=force_upload,
                s3_prefix=guided_context.guided_s3_prefix
                if guided else s3_prefix,
                kms_key_id=kms_key_id,
                parameter_overrides=sanitize_parameter_overrides(
                    guided_context.guided_parameter_overrides)
                if guided else parameter_overrides,
                capabilities=guided_context.guided_capabilities
                if guided else capabilities,
                no_execute_changeset=no_execute_changeset,
                role_arn=role_arn,
                notification_arns=notification_arns,
                fail_on_empty_changeset=fail_on_empty_changeset,
                tags=tags,
                region=guided_context.guided_region if guided else region,
                profile=profile,
                confirm_changeset=guided_context.confirm_changeset
                if guided else confirm_changeset,
        ) as deploy_context:
            deploy_context.run()
예제 #8
0
def do_cli(
    template_file,
    stack_name,
    s3_bucket,
    force_upload,
    no_progressbar,
    s3_prefix,
    kms_key_id,
    parameter_overrides,
    capabilities,
    no_execute_changeset,
    role_arn,
    notification_arns,
    fail_on_empty_changeset,
    use_json,
    tags,
    metadata,
    guided,
    confirm_changeset,
    region,
    profile,
    signing_profiles,
    resolve_s3,
    config_file,
    config_env,
):
    from samcli.commands.package.package_context import PackageContext
    from samcli.commands.deploy.deploy_context import DeployContext
    from samcli.commands.deploy.guided_context import GuidedContext
    from samcli.commands.deploy.exceptions import DeployResolveS3AndS3SetError

    if guided:
        # Allow for a guided deploy to prompt and save those details.
        guided_context = GuidedContext(
            template_file=template_file,
            stack_name=stack_name,
            s3_bucket=s3_bucket,
            s3_prefix=s3_prefix,
            region=region,
            profile=profile,
            confirm_changeset=confirm_changeset,
            capabilities=capabilities,
            signing_profiles=signing_profiles,
            parameter_overrides=parameter_overrides,
            config_section=CONFIG_SECTION,
            config_env=config_env,
            config_file=config_file,
        )
        guided_context.run()
    elif resolve_s3 and bool(s3_bucket):
        raise DeployResolveS3AndS3SetError()
    elif resolve_s3:
        s3_bucket = manage_stack(profile=profile, region=region)
        click.echo(f"\n\t\tManaged S3 bucket: {s3_bucket}")
        click.echo(
            "\t\tA different default S3 bucket can be set in samconfig.toml")
        click.echo("\t\tOr by specifying --s3-bucket explicitly.")

    with osutils.tempfile_platform_independent() as output_template_file:

        with PackageContext(
                template_file=template_file,
                s3_bucket=guided_context.guided_s3_bucket
                if guided else s3_bucket,
                s3_prefix=guided_context.guided_s3_prefix
                if guided else s3_prefix,
                output_template_file=output_template_file.name,
                kms_key_id=kms_key_id,
                use_json=use_json,
                force_upload=force_upload,
                no_progressbar=no_progressbar,
                metadata=metadata,
                on_deploy=True,
                region=guided_context.guided_region if guided else region,
                profile=profile,
                signing_profiles=guided_context.signing_profiles
                if guided else signing_profiles,
        ) as package_context:
            package_context.run()

        with DeployContext(
                template_file=output_template_file.name,
                stack_name=guided_context.guided_stack_name
                if guided else stack_name,
                s3_bucket=guided_context.guided_s3_bucket
                if guided else s3_bucket,
                force_upload=force_upload,
                no_progressbar=no_progressbar,
                s3_prefix=guided_context.guided_s3_prefix
                if guided else s3_prefix,
                kms_key_id=kms_key_id,
                parameter_overrides=sanitize_parameter_overrides(
                    guided_context.guided_parameter_overrides)
                if guided else parameter_overrides,
                capabilities=guided_context.guided_capabilities
                if guided else capabilities,
                no_execute_changeset=no_execute_changeset,
                role_arn=role_arn,
                notification_arns=notification_arns,
                fail_on_empty_changeset=fail_on_empty_changeset,
                tags=tags,
                region=guided_context.guided_region if guided else region,
                profile=profile,
                confirm_changeset=guided_context.confirm_changeset
                if guided else confirm_changeset,
                signing_profiles=guided_context.signing_profiles
                if guided else signing_profiles,
        ) as deploy_context:
            deploy_context.run()
class TestPackageCommand(TestCase):
    def setUp(self):
        self.package_command_context = PackageContext(
            template_file="template-file",
            s3_bucket="s3-bucket",
            s3_prefix="s3-prefix",
            image_repository="image-repo",
            image_repositories=None,
            kms_key_id="kms-key-id",
            output_template_file=None,
            use_json=True,
            force_upload=True,
            no_progressbar=False,
            metadata={},
            region=None,
            profile=None,
        )

    @patch.object(Template, "export", MagicMock(sideeffect=OSError))
    @patch("boto3.Session")
    def test_template_permissions_error(self, patched_boto):
        with self.assertRaises(PackageFailedError):
            self.package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    def test_template_path_valid_with_output_template(self, patched_boto):
        with tempfile.NamedTemporaryFile(mode="w",
                                         delete=False) as temp_template_file:
            with tempfile.NamedTemporaryFile(
                    mode="w", delete=False) as temp_output_template_file:
                package_command_context = PackageContext(
                    template_file=temp_template_file.name,
                    s3_bucket="s3-bucket",
                    s3_prefix="s3-prefix",
                    image_repository="image-repo",
                    image_repositories=None,
                    kms_key_id="kms-key-id",
                    output_template_file=temp_output_template_file.name,
                    use_json=True,
                    force_upload=True,
                    no_progressbar=False,
                    metadata={},
                    region=None,
                    profile=None,
                )
                package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    def test_template_path_valid(self, patched_boto):
        with tempfile.NamedTemporaryFile(mode="w",
                                         delete=False) as temp_template_file:
            package_command_context = PackageContext(
                template_file=temp_template_file.name,
                s3_bucket="s3-bucket",
                s3_prefix="s3-prefix",
                image_repository="image-repo",
                image_repositories=None,
                kms_key_id="kms-key-id",
                output_template_file=None,
                use_json=True,
                force_upload=True,
                no_progressbar=False,
                metadata={},
                region=None,
                profile=None,
            )
            package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    def test_template_path_valid_no_json(self, patched_boto):
        with tempfile.NamedTemporaryFile(mode="w",
                                         delete=False) as temp_template_file:
            package_command_context = PackageContext(
                template_file=temp_template_file.name,
                s3_bucket="s3-bucket",
                s3_prefix="s3-prefix",
                image_repository="image-repo",
                image_repositories=None,
                kms_key_id="kms-key-id",
                output_template_file=None,
                use_json=False,
                force_upload=True,
                no_progressbar=False,
                metadata={},
                region=None,
                profile=None,
            )
            package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    @patch("boto3.client")
    @patch(
        "samcli.commands.package.package_context.get_boto_config_with_user_agent"
    )
    def test_boto_clients_created_with_config(self, patched_get_config,
                                              patched_boto_client,
                                              patched_boto_session):
        with self.assertRaises(PackageFailedError):
            self.package_command_context.run()

        patched_boto_client.assert_has_calls([call("s3", config=ANY)])
        patched_boto_client.assert_has_calls([call("ecr", config=ANY)])
        patched_boto_client.assert_has_calls([call("signer", config=ANY)])

        patched_get_config.assert_has_calls([
            call(region_name=ANY, signature_version=ANY),
            call(region_name=ANY),
            call(region_name=ANY)
        ])

        print("hello")
예제 #10
0
def do_cli(
    template_file,
    stack_name,
    s3_bucket,
    force_upload,
    s3_prefix,
    kms_key_id,
    parameter_overrides,
    capabilities,
    no_execute_changeset,
    role_arn,
    notification_arns,
    fail_on_empty_changeset,
    use_json,
    tags,
    metadata,
    guided,
    confirm_changeset,
    region,
    profile,
):
    from samcli.commands.package.package_context import PackageContext
    from samcli.commands.deploy.deploy_context import DeployContext

    # set capabilities and changeset decision to None, before guided gets input from the user
    changeset_decision = None
    _capabilities = None
    _parameter_overrides = None
    guided_stack_name = None
    guided_s3_bucket = None
    guided_s3_prefix = None
    guided_region = None

    if guided:

        try:
            _parameter_override_keys = get_template_parameters(template_file=template_file)
        except ValueError as ex:
            LOG.debug("Failed to parse SAM template", exc_info=ex)
            raise GuidedDeployFailedError(str(ex))

        read_config_showcase(template_file=template_file)

        guided_stack_name, guided_s3_bucket, guided_s3_prefix, guided_region, guided_profile, changeset_decision, _capabilities, _parameter_overrides, save_to_config = guided_deploy(
            stack_name, s3_bucket, region, profile, confirm_changeset, _parameter_override_keys, parameter_overrides
        )

        if save_to_config:
            save_config(
                template_file,
                stack_name=guided_stack_name,
                s3_bucket=guided_s3_bucket,
                s3_prefix=guided_s3_prefix,
                region=guided_region,
                profile=guided_profile,
                confirm_changeset=changeset_decision,
                capabilities=_capabilities,
                parameter_overrides=_parameter_overrides,
            )

    print_deploy_args(
        stack_name=guided_stack_name if guided else stack_name,
        s3_bucket=guided_s3_bucket if guided else s3_bucket,
        region=guided_region if guided else region,
        capabilities=_capabilities if guided else capabilities,
        parameter_overrides=_parameter_overrides if guided else parameter_overrides,
        confirm_changeset=changeset_decision if guided else confirm_changeset,
    )

    with osutils.tempfile_platform_independent() as output_template_file:

        with PackageContext(
            template_file=template_file,
            s3_bucket=guided_s3_bucket if guided else s3_bucket,
            s3_prefix=guided_s3_prefix if guided else s3_prefix,
            output_template_file=output_template_file.name,
            kms_key_id=kms_key_id,
            use_json=use_json,
            force_upload=force_upload,
            metadata=metadata,
            on_deploy=True,
            region=guided_region if guided else region,
            profile=profile,
        ) as package_context:
            package_context.run()

        with DeployContext(
            template_file=output_template_file.name,
            stack_name=guided_stack_name if guided else stack_name,
            s3_bucket=guided_s3_bucket if guided else s3_bucket,
            force_upload=force_upload,
            s3_prefix=guided_s3_prefix if guided else s3_prefix,
            kms_key_id=kms_key_id,
            parameter_overrides=sanitize_parameter_overrides(_parameter_overrides) if guided else parameter_overrides,
            capabilities=_capabilities if guided else capabilities,
            no_execute_changeset=no_execute_changeset,
            role_arn=role_arn,
            notification_arns=notification_arns,
            fail_on_empty_changeset=fail_on_empty_changeset,
            tags=tags,
            region=guided_region if guided else region,
            profile=profile,
            confirm_changeset=changeset_decision if guided else confirm_changeset,
        ) as deploy_context:
            deploy_context.run()
class TestPackageCommand(TestCase):
    def setUp(self):
        self.package_command_context = PackageContext(
            template_file="template-file",
            s3_bucket="s3-bucket",
            s3_prefix="s3-prefix",
            kms_key_id="kms-key-id",
            output_template_file=None,
            use_json=True,
            force_upload=True,
            no_progressbar=False,
            metadata={},
            region=None,
            profile=None,
        )

    @patch.object(Template, "export", MagicMock(sideeffect=OSError))
    @patch("boto3.Session")
    def test_template_permissions_error(self, patched_boto):
        with self.assertRaises(PackageFailedError):
            self.package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    def test_template_path_valid_with_output_template(self, patched_boto):
        with tempfile.NamedTemporaryFile(mode="w",
                                         delete=False) as temp_template_file:
            with tempfile.NamedTemporaryFile(
                    mode="w", delete=False) as temp_output_template_file:
                package_command_context = PackageContext(
                    template_file=temp_template_file.name,
                    s3_bucket="s3-bucket",
                    s3_prefix="s3-prefix",
                    kms_key_id="kms-key-id",
                    output_template_file=temp_output_template_file.name,
                    use_json=True,
                    force_upload=True,
                    no_progressbar=False,
                    metadata={},
                    region=None,
                    profile=None,
                )
                package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    def test_template_path_valid(self, patched_boto):
        with tempfile.NamedTemporaryFile(mode="w",
                                         delete=False) as temp_template_file:
            package_command_context = PackageContext(
                template_file=temp_template_file.name,
                s3_bucket="s3-bucket",
                s3_prefix="s3-prefix",
                kms_key_id="kms-key-id",
                output_template_file=None,
                use_json=True,
                force_upload=True,
                no_progressbar=False,
                metadata={},
                region=None,
                profile=None,
            )
            package_command_context.run()

    @patch.object(Template, "export", MagicMock(return_value={}))
    @patch("boto3.Session")
    def test_template_path_valid_no_json(self, patched_boto):
        with tempfile.NamedTemporaryFile(mode="w",
                                         delete=False) as temp_template_file:
            package_command_context = PackageContext(
                template_file=temp_template_file.name,
                s3_bucket="s3-bucket",
                s3_prefix="s3-prefix",
                kms_key_id="kms-key-id",
                output_template_file=None,
                use_json=False,
                force_upload=True,
                no_progressbar=False,
                metadata={},
                region=None,
                profile=None,
            )
            package_command_context.run()