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_yaml(output))
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_yaml(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.yml' % chalice_stage_name) if not self._osutils.directory_exists(deployed_dir): self._osutils.makedirs(deployed_dir) serialized = serialize_to_yaml(results) self._osutils.set_file_contents(filename=deployed_filename, contents=serialized, binary=False)
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.yml') 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_yaml(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_yaml(): assert utils.serialize_to_yaml({'foo': 'bar'}) == '---\nfoo: bar\n'
def _to_yaml(self, doc): # type: (Any) -> str return serialize_to_yaml(doc)