Esempio n. 1
0
    def run(self):

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

        guided_config = GuidedConfig(template_file=self.template_file,
                                     section=self.config_section)
        guided_config.read_config_showcase()

        self.guided_prompts(_parameter_override_keys)

        print_deploy_args(
            stack_name=self.guided_stack_name,
            s3_bucket=self.guided_s3_bucket,
            region=self.guided_region,
            capabilities=self._capabilities,
            parameter_overrides=self._parameter_overrides,
            confirm_changeset=self.confirm_changeset,
        )

        if self.save_to_config:
            guided_config.save_config(
                self._parameter_overrides,
                stack_name=self.guided_stack_name,
                s3_bucket=self.guided_s3_bucket,
                s3_prefix=self.guided_s3_prefix,
                region=self.guided_region,
                profile=self.guided_profile,
                confirm_changeset=self.confirm_changeset,
                capabilities=self._capabilities,
            )
Esempio n. 2
0
    def run(self):

        # Parse parameters
        with open(self.template_file, "r") as handle:
            template_str = handle.read()

        template_dict = yaml_parse(template_str)

        if not isinstance(template_dict, dict):
            raise deploy_exceptions.DeployFailedError(
                stack_name=self.stack_name, msg="{} not in required format".format(self.template_file)
            )

        parameters = self.merge_parameters(template_dict, self.parameter_overrides)

        template_size = os.path.getsize(self.template_file)
        if template_size > 51200 and not self.s3_bucket:
            raise deploy_exceptions.DeployBucketRequiredError()
        boto_config = get_boto_config_with_user_agent()
        cloudformation_client = boto3.client(
            "cloudformation", region_name=self.region if self.region else None, config=boto_config
        )

        s3_client = None
        if self.s3_bucket:
            s3_client = boto3.client("s3", region_name=self.region if self.region else None, config=boto_config)

            self.s3_uploader = S3Uploader(
                s3_client, self.s3_bucket, self.s3_prefix, self.kms_key_id, self.force_upload, self.no_progressbar
            )

        self.deployer = Deployer(cloudformation_client)

        region = s3_client._client_config.region_name if s3_client else self.region  # pylint: disable=W0212
        print_deploy_args(
            self.stack_name,
            self.s3_bucket,
            self.image_repository,
            region,
            self.capabilities,
            self.parameter_overrides,
            self.confirm_changeset,
            self.signing_profiles,
        )
        return self.deploy(
            self.stack_name,
            template_str,
            parameters,
            self.capabilities,
            self.no_execute_changeset,
            self.role_arn,
            self.notification_arns,
            self.s3_uploader,
            [{"Key": key, "Value": value} for key, value in self.tags.items()] if self.tags else [],
            region,
            self.fail_on_empty_changeset,
            self.confirm_changeset,
        )
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()