Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update package command to work with new deployer #772

Merged
merged 4 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chalice/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,12 @@ def package(ctx, single_file, stage, out):
if single_file:
dirname = tempfile.mkdtemp()
try:
packager.package_app(config, dirname)
packager.package_app(config, dirname, stage)
create_zip_file(source_dir=dirname, outfile=out)
finally:
shutil.rmtree(dirname)
else:
packager.package_app(config, out)
packager.package_app(config, out, stage)


@cli.command('generate-pipeline')
Expand Down
78 changes: 49 additions & 29 deletions chalice/deploy/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,34 +235,11 @@ def create_default_deployer(session, config, ui):
# type: (Session, Config, UI) -> Deployer
client = TypedAWSClient(session)
osutils = OSUtils()
pip_runner = PipRunner(pip=SubprocessPip(osutils=osutils),
osutils=osutils)
dependency_builder = PipDependencyBuilder(
osutils=osutils,
pip_runner=pip_runner
)
return Deployer(
application_builder=ApplicationGraphBuilder(),
deps_builder=DependencyBuilder(),
build_stage=BuildStage(
steps=[
InjectDefaults(),
DeploymentPackager(
packager=LambdaDeploymentPackager(
osutils=osutils,
dependency_builder=dependency_builder,
ui=UI(),
),
),
PolicyGenerator(
policy_gen=AppPolicyGenerator(
osutils=osutils
),
),
SwaggerBuilder(
swagger_generator=TemplatedSwaggerGenerator(),
)
],
build_stage=create_build_stage(
osutils, UI(), TemplatedSwaggerGenerator(),
),
plan_stage=PlanStage(
osutils=osutils, remote_state=RemoteState(
Expand All @@ -274,6 +251,38 @@ def create_default_deployer(session, config, ui):
)


def create_build_stage(osutils, ui, swagger_gen):
# type: (OSUtils, UI, SwaggerGenerator) -> BuildStage
pip_runner = PipRunner(pip=SubprocessPip(osutils=osutils),
osutils=osutils)
dependency_builder = PipDependencyBuilder(
osutils=osutils,
pip_runner=pip_runner
)
build_stage = BuildStage(
steps=[
InjectDefaults(),
DeploymentPackager(
packager=LambdaDeploymentPackager(
osutils=osutils,
dependency_builder=dependency_builder,
ui=ui,
),
),
PolicyGenerator(
policy_gen=AppPolicyGenerator(
osutils=osutils
),
osutils=osutils,
),
SwaggerBuilder(
swagger_generator=swagger_gen,
)
],
)
return build_stage


def create_deletion_deployer(client, ui):
# type: (TypedAWSClient, UI) -> Deployer
return Deployer(
Expand Down Expand Up @@ -509,7 +518,7 @@ def _create_role_reference(self, config, stage_name, function_name):
return models.PreCreatedIAMRole(
role_arn=config.iam_role_arn,
)
policy = models.IAMPolicy()
policy = models.IAMPolicy(document=models.Placeholder.BUILD_STAGE)
if not config.autogen_policy:
resource_name = 'role-%s' % function_name
role_name = '%s-%s-%s' % (config.app_name, stage_name,
Expand All @@ -522,7 +531,8 @@ def _create_role_reference(self, config, stage_name, function_name):
filename = os.path.join(config.project_dir,
'.chalice',
'policy-%s.json' % stage_name)
policy = models.FileBasedIAMPolicy(filename=filename)
policy = models.FileBasedIAMPolicy(
filename=filename, document=models.Placeholder.BUILD_STAGE)
else:
resource_name = 'default-role'
role_name = '%s-%s' % (config.app_name, stage_name)
Expand Down Expand Up @@ -637,9 +647,19 @@ def handle_restapi(self, config, resource):


class PolicyGenerator(BaseDeployStep):
def __init__(self, policy_gen):
# type: (AppPolicyGenerator) -> None
def __init__(self, policy_gen, osutils):
# type: (AppPolicyGenerator, OSUtils) -> None
self._policy_gen = policy_gen
self._osutils = osutils

def handle_filebasediampolicy(self, config, resource):
# type: (Config, models.FileBasedIAMPolicy) -> None
try:
resource.document = json.loads(
self._osutils.get_file_contents(resource.filename))
except IOError as e:
raise RuntimeError("Unable to load IAM policy file %s: %s"
% (resource.filename, e))

def handle_autogeniampolicy(self, config, resource):
# type: (Config, models.AutoGenIAMPolicy) -> None
Expand Down
4 changes: 2 additions & 2 deletions chalice/deploy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class DeploymentPackage(Model):

@attrs
class IAMPolicy(Model):
pass
document = attrib()


@attrs
Expand All @@ -104,7 +104,7 @@ class FileBasedIAMPolicy(IAMPolicy):

@attrs
class AutoGenIAMPolicy(IAMPolicy):
document = attrib()
pass


@attrs
Expand Down
9 changes: 8 additions & 1 deletion chalice/deploy/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,21 @@ class DeploymentPackage(Model):


class IAMPolicy(Model):
pass
document = ... # type: DV[Dict[str, Any]]

def __init__(self,
document, # type: DV[Dict[str, Any]]
):
# type: (...) -> None
...


class FileBasedIAMPolicy(IAMPolicy):
filename = ... # type: str

def __init__(self,
filename, # type: str
document, # type: DV[Dict[str, Any]]
):
# type: (...) -> None
...
Expand Down
21 changes: 1 addition & 20 deletions chalice/deploy/planner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

from typing import List, Dict, Any, Optional, Union, Tuple, Set, cast # noqa
from typing import Sequence # noqa

Expand Down Expand Up @@ -249,7 +247,7 @@ def _plan_lambdafunction(self, resource):

def _plan_managediamrole(self, resource):
# type: (models.ManagedIAMRole) -> Sequence[_INSTRUCTION_MSG]
document = self._get_policy_document(resource.policy)
document = resource.policy.document
role_exists = self._remote_state.resource_exists(resource)
varname = '%s_role_arn' % resource.role_name
if not role_exists:
Expand Down Expand Up @@ -465,23 +463,6 @@ def _get_role_arn(self, resource):
# Make mypy happy.
raise RuntimeError("Unknown resource type: %s" % resource)

def _get_policy_document(self, resource):
# type: (models.IAMPolicy) -> Dict[str, Any]
if isinstance(resource, models.AutoGenIAMPolicy):
# mypy can't check this, but we assert that the
# placeholder values are filled in before we invoke
# any planners, so we can safely cast from
# Placholder[T] to T.
document = cast(Dict[str, Any], resource.document)
elif isinstance(resource, models.FileBasedIAMPolicy):
try:
document = json.loads(
self._osutils.get_file_contents(resource.filename))
except IOError as e:
raise RuntimeError("Unable to load IAM policy file %s: %s"
% (resource.filename, e))
return document


class NoopPlanner(PlanStage):
def __init__(self):
Expand Down
Loading