Skip to content

Commit

Permalink
Merge from aws/aws-sam-cli/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
aws-sam-cli-bot authored Sep 4, 2020
2 parents 79a6dc2 + 6828ba6 commit 70f485c
Show file tree
Hide file tree
Showing 44 changed files with 1,130 additions and 62 deletions.
Empty file added game.txt
Empty file.
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ dateparser~=0.7
python-dateutil~=2.6, <2.8.1
requests==2.23.0
serverlessrepo==0.1.9
aws_lambda_builders==1.0.0
aws_lambda_builders==1.1.0
tomlkit==0.5.8
8 changes: 4 additions & 4 deletions requirements/reproducible-linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ aws-sam-translator==1.26.0 \
--hash=sha256:3a200e6475f11726732b9b9c070ca4d58d2fe5ecc40e8fb629b09a053fba5640 \
--hash=sha256:de2f1b4efd83347639eb19fea37989e9da9a3c59da277320cf1e58a2f0ff6dd0 \
# via aws-sam-cli (setup.py)
aws_lambda_builders==1.0.0 \
--hash=sha256:757ed604e5d0948e0a3d0e444b24f5f4fff1016d82c5d02158ffacc5554acabd \
--hash=sha256:b9e7cd09b4f04574d96cdb880644e6c7fd1ad8bcc25c3aef3ed0268a5ada9a67 \
--hash=sha256:dc4501c641e0bc52605c0133e3d348770bc98aa9e2ac9937f717c0a658c4dbe8 \
aws_lambda_builders==1.1.0 \
--hash=sha256:2b40a0003c2c05143e1aa816fed758c7d78f3e5c8e115be681aa2478f2655056 \
--hash=sha256:2c8d710c6a96dfd4abd0c4450872deab71fd38c7fd79e1be84e0a760f3992f7f \
--hash=sha256:a5a68347fe348be7c79a48650a06685d6df690469e62a2e00c73e7ec5de8e69a \
# via aws-sam-cli (setup.py)
binaryornot==0.4.4 \
--hash=sha256:359501dfc9d40632edc9fac890e19542db1a287bbcfa58175b66658392018061 \
Expand Down
2 changes: 1 addition & 1 deletion samcli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
SAM CLI version
"""

__version__ = "1.1.0"
__version__ = "1.2.0"
16 changes: 16 additions & 0 deletions samcli/cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ def command_path(self):

return None

@property
def template_dict(self):
"""
Returns the template_dictionary from click context.
Returns
-------
dict
Template as dictionary
"""
click_core_ctx = click.get_current_context()
if click_core_ctx:
return click_core_ctx.template_dict

return None

@staticmethod
def get_current_context():
"""
Expand Down
13 changes: 10 additions & 3 deletions samcli/commands/_utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import click
from click.types import FuncParamType

from samcli.commands._utils.template import get_template_data, TemplateNotFoundException
from samcli.cli.types import CfnParameterOverridesType, CfnMetadataType, CfnTags
from samcli.commands._utils.custom_options.option_nargs import OptionNargs


_TEMPLATE_OPTION_DEFAULT_VALUE = "template.[yaml|yml]"
DEFAULT_STACK_NAME = "sam-app"


LOG = logging.getLogger(__name__)


Expand Down Expand Up @@ -53,6 +53,14 @@ def get_or_default_template_file_name(ctx, param, provided_value, include_build)
# sam configuration file should always be relative to the supplied original template and should not to be set
# to be .aws-sam/build/
setattr(ctx, "samconfig_dir", os.path.dirname(original_template_path))
try:
# FIX-ME: figure out a way to insert this directly to sam-cli context and not use click context.
template_data = get_template_data(result)
setattr(ctx, "template_dict", template_data)
except TemplateNotFoundException:
# Ignoring because there are certain cases where template file will not be available, eg: --help
pass

LOG.debug("Using SAM Template at %s", result)
return result

Expand Down Expand Up @@ -127,7 +135,6 @@ def docker_common_options(f):


def docker_click_options():

return [
click.option(
"--skip-pull-image",
Expand Down
2 changes: 1 addition & 1 deletion samcli/commands/_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_template_data(template_file):
if not pathlib.Path(template_file).exists():
raise TemplateNotFoundException("Template file not found at {}".format(template_file))

with open(template_file, "r") as fp:
with open(template_file, "r", encoding="utf-8") as fp:
try:
return yaml_parse(fp.read())
except (ValueError, yaml.YAMLError) as ex:
Expand Down
19 changes: 19 additions & 0 deletions samcli/commands/deploy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from samcli.commands.deploy.utils import sanitize_parameter_overrides
from samcli.lib.telemetry.metrics import track_command
from samcli.lib.utils import osutils
from samcli.lib.bootstrap.bootstrap import manage_stack

SHORT_HELP = "Deploy an AWS SAM application."

Expand Down Expand Up @@ -127,6 +128,13 @@
help="Indicates whether to use JSON as the format for "
"the output AWS CloudFormation template. YAML is used by default.",
)
@click.option(
"--resolve-s3",
required=False,
is_flag=True,
help="Automatically resolve s3 bucket for non-guided deployments."
"Do not use --s3-guided parameter with this option.",
)
@metadata_override_option
@notification_arns_override_option
@tags_override_option
Expand Down Expand Up @@ -155,6 +163,7 @@ def cli(
metadata,
guided,
confirm_changeset,
resolve_s3,
):

# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing
Expand All @@ -178,6 +187,7 @@ def cli(
confirm_changeset,
ctx.region,
ctx.profile,
resolve_s3,
) # pragma: no cover


Expand All @@ -201,10 +211,12 @@ def do_cli(
confirm_changeset,
region,
profile,
resolve_s3,
):
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.
Expand All @@ -221,6 +233,13 @@ def do_cli(
config_section=CONFIG_SECTION,
)
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:

Expand Down
10 changes: 10 additions & 0 deletions samcli/commands/deploy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@ def __init__(self):
)

super(DeployBucketRequiredError, self).__init__(message=message_fmt)


class DeployResolveS3AndS3SetError(UserException):
def __init__(self):
message_fmt = (
"Cannot use both --resolve-s3 and --s3-bucket parameters in non-guided deployments."
" Please use only one or use the --guided option for a guided deployment."
)

super(DeployResolveS3AndS3SetError, self).__init__(message=message_fmt)
3 changes: 2 additions & 1 deletion samcli/commands/local/invoke/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def do_cli( # pylint: disable=R0914
from samcli.commands.local.cli_common.invoke_context import InvokeContext
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError, NoPrivilegeException
from samcli.local.docker.manager import DockerImagePullFailedException
from samcli.local.docker.lambda_debug_settings import DebuggingNotSupported

Expand Down Expand Up @@ -160,6 +160,7 @@ def do_cli( # pylint: disable=R0914
OverridesNotWellDefinedError,
InvalidLayerReference,
DebuggingNotSupported,
NoPrivilegeException,
) as ex:
raise UserException(str(ex), wrapped_from=ex.__class__.__name__)
except DockerImagePullFailedException as ex:
Expand Down
6 changes: 6 additions & 0 deletions samcli/commands/local/lib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ class OverridesNotWellDefinedError(Exception):
"""
Raised when the overrides file is invalid
"""


class NoPrivilegeException(Exception):
"""
Process does not have the required privilege to complete the action
"""
15 changes: 13 additions & 2 deletions samcli/commands/local/lib/local_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from samcli.local.lambdafn.env_vars import EnvironmentVariables
from samcli.local.lambdafn.config import FunctionConfig
from samcli.local.lambdafn.exceptions import FunctionNotFound
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError
from samcli.commands.local.lib.exceptions import OverridesNotWellDefinedError, NoPrivilegeException

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,7 +97,18 @@ def invoke(self, function_name, event, stdout=None, stderr=None):
config = self._get_invoke_config(function)

# Invoke the function
self.local_runtime.invoke(config, event, debug_context=self.debug_context, stdout=stdout, stderr=stderr)
try:
self.local_runtime.invoke(config, event, debug_context=self.debug_context, stdout=stdout, stderr=stderr)
except OSError as os_error:
# pylint: disable=no-member
if hasattr(os_error, "winerror") and os_error.winerror == 1314:
raise NoPrivilegeException(
"Administrator, Windows Developer Mode, or SeCreateSymbolicLinkPrivilege is required to create symbolic link for files: {}, {}".format(
os_error.filename, os_error.filename2
)
)

raise

def is_debugging(self):
"""
Expand Down
43 changes: 39 additions & 4 deletions samcli/commands/package/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"""
import click


from samcli.cli.cli_config_file import configuration_option, TomlProvider
from samcli.cli.main import pass_context, common_options, aws_creds_options
from samcli.commands._utils.options import metadata_override_option, template_click_option
from samcli.commands._utils.resources import resources_generator
from samcli.lib.telemetry.metrics import track_command
from samcli.lib.bootstrap.bootstrap import manage_stack
from samcli.lib.telemetry.metrics import track_command, track_template_warnings
from samcli.lib.warnings.sam_cli_warning import CodeDeployWarning

SHORT_HELP = "Package an AWS SAM application."

Expand Down Expand Up @@ -40,7 +41,7 @@ def resources_and_properties_help_string():
@template_click_option(include_build=True)
@click.option(
"--s3-bucket",
required=True,
required=False,
help="The name of the S3 bucket where this command uploads the artifacts that are referenced in your template.",
)
@click.option(
Expand Down Expand Up @@ -78,12 +79,31 @@ def resources_and_properties_help_string():
"in the S3 bucket. Specify this flag to upload artifacts even if they "
"match existing artifacts in the S3 bucket.",
)
@click.option(
"--resolve-s3",
required=False,
is_flag=True,
help="Automatically resolve s3 bucket for non-guided deployments."
"Do not use --s3-guided parameter with this option.",
)
@metadata_override_option
@common_options
@aws_creds_options
@pass_context
@track_command
def cli(ctx, template_file, s3_bucket, s3_prefix, kms_key_id, output_template_file, use_json, force_upload, metadata):
@track_template_warnings([CodeDeployWarning.__name__])
def cli(
ctx,
template_file,
s3_bucket,
s3_prefix,
kms_key_id,
output_template_file,
use_json,
force_upload,
metadata,
resolve_s3,
):

# All logic must be implemented in the ``do_cli`` method. This helps with easy unit testing

Expand All @@ -98,6 +118,7 @@ def cli(ctx, template_file, s3_bucket, s3_prefix, kms_key_id, output_template_fi
metadata,
ctx.region,
ctx.profile,
resolve_s3,
) # pragma: no cover


Expand All @@ -112,8 +133,22 @@ def do_cli(
metadata,
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,
Expand Down
14 changes: 14 additions & 0 deletions samcli/commands/package/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,17 @@ def __init__(self, **kwargs):
message_fmt = "\nS3 Bucket not specified, use --s3-bucket to specify a bucket name or run sam deploy --guided"

super(BucketNotSpecifiedError, self).__init__(message=message_fmt.format(**self.kwargs))


class PackageResolveS3AndS3SetError(UserException):
def __init__(self):
message_fmt = "Cannot use both --resolve-s3 and --s3-bucket parameters. Please use only one."

super(PackageResolveS3AndS3SetError, self).__init__(message=message_fmt)


class PackageResolveS3AndS3NotSetError(UserException):
def __init__(self):
message_fmt = "Cannot skip both --resolve-s3 and --s3-bucket parameters. Please provide one of these arguments."

super(PackageResolveS3AndS3NotSetError, self).__init__(message=message_fmt)
Loading

0 comments on commit 70f485c

Please sign in to comment.