5
5
Known limitations: cannot transform CodeUri pointing at local directory.
6
6
7
7
Usage:
8
- sam-translate.py --input-file=sam-template.yaml [--output-file=<o>]
8
+ sam-translate.py --template-file=sam-template.yaml [--verbose] [--output-template=<o>]
9
+ sam-translate.py package --template-file=sam-template.yaml --s3-bucket=my-bucket [--verbose] [--output-template=<o>]
10
+ sam-translate.py deploy --template-file=sam-template.yaml --s3-bucket=my-bucket --capabilities=CAPABILITY_NAMED_IAM --stack-name=my-stack [--verbose] [--output-template=<o>]
9
11
10
12
Options:
11
- --input-file=<i> Location of SAM template to transform.
12
- --output-file=<o> Location to store resulting CloudFormation template [default: cfn-template.json].
13
+ --template-file=<i> Location of SAM template to transform [default: template.yaml].
14
+ --output-template=<o> Location to store resulting CloudFormation template [default: transformed-template.json].
15
+ --s3-bucket=<s> S3 bucket to use for SAM artifacts when using the `package` command
16
+ --capabilities=<c> Capabilities
17
+ --stack-name=<n> Unique name for your CloudFormation Stack
18
+ --verbose Enables verbose logging
13
19
14
20
"""
15
21
import json
22
+ import logging
16
23
import os
24
+ import platform
25
+ import subprocess
26
+ import sys
17
27
18
28
import boto3
19
29
from docopt import docopt
20
30
31
+ my_path = os .path .dirname (os .path .abspath (__file__ ))
32
+ sys .path .insert (0 , my_path + '/..' )
33
+
21
34
from samtranslator .public .translator import ManagedPolicyLoader
22
35
from samtranslator .translator .transform import transform
23
36
from samtranslator .yaml_helper import yaml_parse
24
37
from samtranslator .model .exceptions import InvalidDocumentException
25
38
26
-
39
+ LOG = logging . getLogger ( __name__ )
27
40
cli_options = docopt (__doc__ )
28
41
iam_client = boto3 .client ('iam' )
29
42
cwd = os .getcwd ()
30
43
44
+ if cli_options .get ('--verbose' ):
45
+ logging .basicConfig (level = logging .DEBUG )
46
+ else :
47
+ logging .basicConfig ()
48
+
49
+ def execute_command (command , args ):
50
+ try :
51
+ aws_cmd = 'aws' if platform .system ().lower () != 'windows' else 'aws.cmd'
52
+ command_with_args = [aws_cmd , 'cloudformation' , command ] + list (args )
53
+
54
+ LOG .debug ("Executing command: %s" , command_with_args )
55
+
56
+ subprocess .check_call (command_with_args )
57
+
58
+ LOG .debug ("Command successful" )
59
+ except subprocess .CalledProcessError as e :
60
+ # Underlying aws command will print the exception to the user
61
+ LOG .debug ("Exception: %s" , e )
62
+ sys .exit (e .returncode )
63
+
31
64
32
65
def get_input_output_file_paths ():
33
- input_file_option = cli_options .get ('--input -file' )
34
- output_file_option = cli_options .get ('--output-file ' )
66
+ input_file_option = cli_options .get ('--template -file' )
67
+ output_file_option = cli_options .get ('--output-template ' )
35
68
input_file_path = os .path .join (cwd , input_file_option )
36
69
output_file_path = os .path .join (cwd , output_file_option )
37
70
38
71
return input_file_path , output_file_path
39
72
40
73
41
- def main ():
42
- input_file_path , output_file_path = get_input_output_file_paths ()
74
+ def package (input_file_path , output_file_path ):
75
+ template_file = input_file_path
76
+ package_output_template_file = input_file_path + '._sam_packaged_.yaml'
77
+ s3_bucket = cli_options .get ('--s3-bucket' )
78
+ args = [
79
+ '--template-file' ,
80
+ template_file ,
81
+ '--output-template-file' ,
82
+ package_output_template_file ,
83
+ '--s3-bucket' ,
84
+ s3_bucket
85
+ ]
43
86
87
+ execute_command ('package' , args )
88
+
89
+ return package_output_template_file
90
+
91
+
92
+ def transform_template (input_file_path , output_file_path ):
44
93
with open (input_file_path , 'r' ) as f :
45
94
sam_template = yaml_parse (f )
46
95
@@ -56,10 +105,37 @@ def main():
56
105
print ('Wrote transformed CloudFormation template to: ' + output_file_path )
57
106
except InvalidDocumentException as e :
58
107
errorMessage = reduce (lambda message , error : message + ' ' + error .message , e .causes , e .message )
59
- print (errorMessage )
108
+ LOG . error (errorMessage )
60
109
errors = map (lambda cause : cause .message , e .causes )
61
- print (errors )
110
+ LOG .error (errors )
111
+
112
+
113
+ def deploy (template_file ):
114
+ capabilities = cli_options .get ('--capabilities' )
115
+ stack_name = cli_options .get ('--stack-name' )
116
+ args = [
117
+ '--template-file' ,
118
+ template_file ,
119
+ '--capabilities' ,
120
+ capabilities ,
121
+ '--stack-name' ,
122
+ stack_name
123
+ ]
124
+
125
+ execute_command ('deploy' , args )
126
+
127
+ return package_output_template_file
62
128
63
129
64
130
if __name__ == '__main__' :
65
- main ()
131
+ input_file_path , output_file_path = get_input_output_file_paths ()
132
+
133
+ if cli_options .get ('package' ):
134
+ package_output_template_file = package (input_file_path , output_file_path )
135
+ transform_template (package_output_template_file , output_file_path )
136
+ elif cli_options .get ('deploy' ):
137
+ package_output_template_file = package (input_file_path , output_file_path )
138
+ transform_template (package_output_template_file , output_file_path )
139
+ deploy (output_file_path )
140
+ else :
141
+ transform_template (input_file_path , output_file_path )
0 commit comments