def generate_pipeline(ctx, codebuild_image, source, buildspec_file, filename): # type: (click.Context, str, str, str, str) -> None """Generate a cloudformation template for a starter CD pipeline. This command will write a starter cloudformation template to the filename you provide. It contains a CodeCommit repo, a CodeBuild stage for packaging your chalice app, and a CodePipeline stage to deploy your application using cloudformation. You can use any AWS SDK or the AWS CLI to deploy this stack. Here's an example using the AWS CLI: \b $ chalice generate-pipeline pipeline.json $ aws cloudformation deploy --stack-name mystack \b --template-file pipeline.json --capabilities CAPABILITY_IAM """ from chalice import pipeline factory = ctx.obj['factory'] # type: CLIFactory config = factory.create_config_obj() p = pipeline.CreatePipelineTemplate() params = pipeline.PipelineParameters( app_name=config.app_name, lambda_python_version=config.lambda_python_version, codebuild_image=codebuild_image, code_source=source, ) output = p.create_template(params) if buildspec_file: extractor = pipeline.BuildSpecExtractor() buildspec_contents = extractor.extract_buildspec(output) with open(buildspec_file, 'w') as f: f.write(buildspec_contents) with open(filename, 'w') as f: f.write(serialize_to_json(output))
def generate_pipeline(ctx, codebuild_image, source, buildspec_file, filename): # type: (click.Context, str, str, str, str) -> None """Generate a cloudformation template for a starter CD pipeline. This command will write a starter cloudformation template to the filename you provide. It contains a CodeCommit repo, a CodeBuild stage for packaging your chalice app, and a CodePipeline stage to deploy your application using cloudformation. You can use any AWS SDK or the AWS CLI to deploy this stack. Here's an example using the AWS CLI: \b $ chalice generate-pipeline pipeline.json $ aws cloudformation deploy --stack-name mystack \b --template-file pipeline.json --capabilities CAPABILITY_IAM """ from chalice import pipeline factory = ctx.obj['factory'] # type: CLIFactory config = factory.create_config_obj() p = pipeline.CreatePipelineTemplate() params = pipeline.PipelineParameters( app_name=config.app_name, lambda_python_version=config.lambda_python_version, codebuild_image=codebuild_image, code_source=source, ) output = p.create_template(params) if buildspec_file: extractor = pipeline.BuildSpecExtractor() buildspec_contents = extractor.extract_buildspec(output) with open(buildspec_file, 'w') as f: f.write(buildspec_contents) with open(filename, 'w') as f: f.write(serialize_to_json(output))
def gen_policy(ctx, filename): # type: (click.Context, str) -> None if filename is None: filename = os.path.join(ctx.obj['project_dir'], 'app.py') if not os.path.isfile(filename): click.echo("App file does not exist: %s" % filename, err=True) raise click.Abort() with open(filename) as f: contents = f.read() generated = policy.policy_from_source_code(contents) click.echo(serialize_to_json(generated))
def gen_policy(ctx, filename): # type: (click.Context, str) -> None from chalice import policy if filename is None: filename = os.path.join(ctx.obj['project_dir'], 'app.py') if not os.path.isfile(filename): click.echo("App file does not exist: %s" % filename, err=True) raise click.Abort() with open(filename) as f: contents = f.read() generated = policy.policy_from_source_code(contents) click.echo(serialize_to_json(generated))
def record_results(self, results, chalice_stage_name, project_dir): # type: (Any, str, str) -> None deployed_dir = self._osutils.joinpath(project_dir, '.chalice', 'deployed') deployed_filename = self._osutils.joinpath( deployed_dir, '%s.json' % chalice_stage_name) if not self._osutils.directory_exists(deployed_dir): self._osutils.makedirs(deployed_dir) serialized = serialize_to_json(results) self._osutils.set_file_contents(filename=deployed_filename, contents=serialized, binary=False)
def record_results(self, results, chalice_stage_name, project_dir): # type: (Any, str, str) -> None deployed_dir = self._osutils.joinpath( project_dir, '.chalice', 'deployed') deployed_filename = self._osutils.joinpath( deployed_dir, '%s.json' % chalice_stage_name) if not self._osutils.directory_exists(deployed_dir): self._osutils.makedirs(deployed_dir) serialized = serialize_to_json(results) self._osutils.set_file_contents( filename=deployed_filename, contents=serialized, binary=False )
def test_can_record_results_initial_deploy(self): expected_filename = os.path.join(self.deployed_dir, 'dev.json') self.osutils.file_exists.return_value = False self.osutils.directory_exists.return_value = False self.recorder.record_results( self.deployed_values, 'dev', '.', ) expected_contents = serialize_to_json(self.deployed_values) # Verify we created the deployed dir on an initial deploy. self.osutils.makedirs.assert_called_with(self.deployed_dir) self.osutils.set_file_contents.assert_called_with( filename=expected_filename, contents=expected_contents, binary=False )
def _create_role_from_source_code(self, config, role_name): # type: (Config, str) -> str app_policy = self._app_policy.generate_policy_from_app_source(config) if len(app_policy['Statement']) > 1: self._ui.write("The following execution policy will be used:\n") self._ui.write(serialize_to_json(app_policy)) self._ui.confirm("Would you like to continue? ", default=True, abort=True) role_arn = self._aws_client.create_role( name=role_name, trust_policy=LAMBDA_TRUST_POLICY, policy=app_policy) self._app_policy.record_policy(config, app_policy) return role_arn
def test_can_build_private_rest_api_custom_policy(self, tmpdir, sample_app): config = self.create_config(sample_app, app_name='rest-api-app', api_gateway_policy_file='foo.json', api_gateway_endpoint_type='PRIVATE', project_dir=str(tmpdir)) tmpdir.mkdir('.chalice').join('foo.json').write( serialize_to_json({ 'Version': '2012-10-17', 'Statement': [] })) application_builder = ApplicationGraphBuilder() build_stage = BuildStage( steps=[PolicyGenerator(osutils=OSUtils(), policy_gen=None)]) application = application_builder.build(config, stage_name='dev') build_stage.execute(config, application.resources) rest_api = application.resources[0] assert rest_api.policy.document == { 'Version': '2012-10-17', 'Statement': [] }
def generate_pipeline(ctx, filename): # type: (click.Context, str) -> None """Generate a cloudformation template for a starter CD pipeline. This command will write a starter cloudformation template to the filename you provide. It contains a CodeCommit repo, a CodeBuild stage for packaging your chalice app, and a CodePipeline stage to deploy your application using cloudformation. You can use any AWS SDK or the AWS CLI to deploy this stack. Here's an example using the AWS CLI: \b $ chalice generate-pipeline pipeline.json $ aws cloudformation deploy --stack-name mystack \b --template-file pipeline.json --capabilities CAPABILITY_IAM """ from chalice.pipeline import create_pipeline_template factory = ctx.obj['factory'] # type: CLIFactory config = factory.create_config_obj() output = create_pipeline_template(config) with open(filename, 'w') as f: f.write(serialize_to_json(output))
def create_new_project_skeleton(project_name, profile=None): # type: (str, Optional[str]) -> None chalice_dir = os.path.join(project_name, '.chalice') os.makedirs(chalice_dir) config = os.path.join(project_name, '.chalice', 'config.json') cfg = { 'version': CONFIG_VERSION, 'app_name': project_name, 'stages': { DEFAULT_STAGE_NAME: { 'api_gateway_stage': DEFAULT_APIGATEWAY_STAGE_NAME, } } } if profile is not None: cfg['profile'] = profile with open(config, 'w') as f: f.write(serialize_to_json(cfg)) with open(os.path.join(project_name, 'requirements.txt'), 'w'): pass with open(os.path.join(project_name, 'app.py'), 'w') as f: f.write(TEMPLATE_APP % project_name) with open(os.path.join(project_name, '.gitignore'), 'w') as f: f.write(GITIGNORE)
def create_new_project_skeleton(project_name, profile=None): # type: (str, Optional[str]) -> None chalice_dir = os.path.join(project_name, '.chalice') os.makedirs(chalice_dir) config = os.path.join(project_name, '.chalice', 'config.json') cfg = { 'version': CONFIG_VERSION, 'app_name': project_name, 'stages': { DEFAULT_STAGE_NAME: { 'api_gateway_stage': DEFAULT_APIGATEWAY_STAGE_NAME, } } } if profile is not None: cfg['profile'] = profile with open(config, 'w') as f: f.write(serialize_to_json(cfg)) with open(os.path.join(project_name, 'requirements.txt'), 'w'): pass with open(os.path.join(project_name, 'app.py'), 'w') as f: f.write(TEMPLATE_APP % project_name) with open(os.path.join(project_name, '.gitignore'), 'w') as f: f.write(GITIGNORE)
def test_serialize_json(): assert utils.serialize_to_json({'foo': 'bar'}) == ( '{\n' ' "foo": "bar"\n' '}\n' )
def record_policy(self, config, policy_document): # type: (Config, Dict[str, Any]) -> None policy_file = self._app_policy_file(config) policy_json = serialize_to_json(policy_document) self._osutils.set_file_contents(policy_file, policy_json, binary=False)
def serialize_template(self, contents): # type: (Dict[str, Any]) -> str return serialize_to_json(contents)
def record_policy(self, config, policy_document): # type: (Config, Dict[str, Any]) -> None policy_file = self._app_policy_file(config) policy_json = serialize_to_json(policy_document) self._osutils.set_file_contents(policy_file, policy_json, binary=False)
def _to_json(self, doc): # type: (Any) -> str return serialize_to_json(doc)
def _to_json(self, doc): # type: (Any) -> str return serialize_to_json(doc)
def test_serialize_json(): assert utils.serialize_to_json({'foo': 'bar'}) == ('{\n' ' "foo": "bar"\n' '}\n')