1
1
import io
2
+ import json
2
3
import os
3
4
import sys
4
5
import zipfile
@@ -57,8 +58,7 @@ def _create_layer(self, stack: Stack):
57
58
"-c" ,
58
59
rf"poetry export --with-credentials --format requirements.txt --output /tmp/requirements.txt &&\
59
60
pip install -r /tmp/requirements.txt -t { output_dir } &&\
60
- cp -R { input_dir } { output_dir } &&\
61
- find { output_dir } / -type d -name __pycache__ -prune -exec rm -rf {{}} \;" ,
61
+ cp -R { input_dir } { output_dir } " ,
62
62
],
63
63
),
64
64
),
@@ -69,7 +69,7 @@ def _find_handlers(self, directory: str) -> List:
69
69
for root , _ , files in os .walk (directory ):
70
70
return [os .path .join (root , filename ) for filename in files if filename .endswith (".py" )]
71
71
72
- def synthesize (self , handlers : List [str ]) -> Tuple [dict , str ]:
72
+ def synthesize (self , handlers : List [str ]) -> Tuple [dict , str , str ]:
73
73
integration_test_app = App ()
74
74
stack = Stack (integration_test_app , self .stack_name )
75
75
powertools_layer = self ._create_layer (stack )
@@ -98,10 +98,12 @@ def synthesize(self, handlers: List[str]) -> Tuple[dict, str]:
98
98
removal_policy = RemovalPolicy .DESTROY ,
99
99
)
100
100
CfnOutput (stack , f"{ filename } _arn" , value = function_python .function_arn )
101
- return (
102
- integration_test_app .synth ().get_stack_by_name (self .stack_name ).template ,
103
- integration_test_app .synth ().directory ,
104
- )
101
+ cloud_assembly = integration_test_app .synth ()
102
+ cf_template = cloud_assembly .get_stack_by_name (self .stack_name ).template
103
+ cloud_assembly_directory = cloud_assembly .directory
104
+ cloud_assembly_assets_manifest_path = cloud_assembly .get_stack_by_name (self .stack_name ).dependencies [0 ].file
105
+
106
+ return (cf_template , cloud_assembly_directory , cloud_assembly_assets_manifest_path )
105
107
106
108
def __call__ (self ) -> Tuple [dict , str ]:
107
109
handlers = self ._find_handlers (directory = self .handlers_dir )
@@ -124,8 +126,8 @@ def __init__(self, stack_name: str, handlers_dir: str, config: dict) -> None:
124
126
def deploy (self , Stack : Type [InfrastructureStackInterface ]) -> Dict [str , str ]:
125
127
126
128
stack = Stack (handlers_dir = self .handlers_dir , stack_name = self .stack_name , config = self .config )
127
- template , asset_root_dir = stack ()
128
- self ._upload_assets (template , asset_root_dir )
129
+ template , asset_root_dir , asset_manifest_file = stack ()
130
+ self ._upload_assets (asset_root_dir , asset_manifest_file )
129
131
130
132
response = self ._deploy_stack (self .stack_name , template )
131
133
@@ -134,25 +136,31 @@ def deploy(self, Stack: Type[InfrastructureStackInterface]) -> Dict[str, str]:
134
136
def delete (self ):
135
137
self .cf_client .delete_stack (StackName = self .stack_name )
136
138
137
- def _upload_assets (self , template : dict , asset_root_dir : str ):
139
+ def _upload_assets (self , asset_root_dir : str , asset_manifest_file : str ):
140
+
141
+ assets = self ._find_assets (asset_manifest_file , self .account_id , self .region )
138
142
139
- assets = self ._find_assets (template , self .account_id , self .region )
143
+ for s3_key , config in assets .items ():
144
+ print (config )
145
+ s3_bucket = self .s3_resource .Bucket (config ["bucket_name" ])
146
+
147
+ if config ["asset_packaging" ] != "zip" :
148
+ print ("Asset is not a zip file. Skipping upload" )
149
+ continue
140
150
141
- for s3_key , bucket in assets .items ():
142
- s3_bucket = self .s3_resource .Bucket (bucket )
143
151
if bool (list (s3_bucket .objects .filter (Prefix = s3_key ))):
144
152
print ("object exists, skipping" )
145
153
continue
146
154
147
155
buf = io .BytesIO ()
148
- asset_dir = f"{ asset_root_dir } /asset. { Path ( s3_key ). with_suffix ( '' ) } "
156
+ asset_dir = f"{ asset_root_dir } /{ config [ 'asset_path' ] } "
149
157
os .chdir (asset_dir )
150
158
asset_files = self ._find_files (directory = "." )
151
159
with zipfile .ZipFile (buf , "w" , compression = zipfile .ZIP_DEFLATED ) as zf :
152
160
for asset_file in asset_files :
153
161
zf .write (os .path .join (asset_file ))
154
162
buf .seek (0 )
155
- self .s3_client .upload_fileobj (Fileobj = buf , Bucket = bucket , Key = s3_key )
163
+ self .s3_client .upload_fileobj (Fileobj = buf , Bucket = config [ "bucket_name" ] , Key = s3_key )
156
164
157
165
def _find_files (self , directory : str ) -> List :
158
166
file_paths = []
@@ -174,22 +182,22 @@ def _deploy_stack(self, stack_name: str, template: dict):
174
182
response = self .cf_client .describe_stacks (StackName = stack_name )
175
183
return response
176
184
177
- def _find_assets (self , template : dict , account_id : str , region : str ):
185
+ def _find_assets (self , asset_template : str , account_id : str , region : str ):
178
186
assets = {}
179
- for _ , resource in template [ "Resources" ]. items () :
180
- bucket = None
181
- S3Key = None
182
-
183
- if resource [ "Properties" ]. get ( "Code" ):
184
- bucket = resource [ "Properties " ]["Code " ]["S3Bucket " ]
185
- S3Key = resource [ "Properties" ][ "Code" ][ "S3Key" ]
186
- elif resource [ "Properties" ]. get ( "Content" ):
187
- bucket = resource [ "Properties" ][ "Content" ][ "S3Bucket" ]
188
- S3Key = resource [ "Properties" ][ "Content" ][ "S3Key" ]
189
- if S3Key and bucket :
190
- assets [ S3Key ] = (
191
- bucket [ "Fn::Sub" ]. replace ( "${AWS::AccountId}" , account_id ). replace ( "${AWS::Region}" , region )
192
- )
187
+ with open ( asset_template , mode = "r" ) as template :
188
+ for _ , config in json . loads ( template . read ())[ "files" ]. items ():
189
+ asset_path = config [ "source" ][ "path" ]
190
+ asset_packaging = config [ "source" ][ "packaging" ]
191
+ bucket_name = config [ "destinations" ][ "current_account-current_region" ][ "bucketName" ]
192
+ object_key = config [ "destinations " ]["current_account-current_region " ]["objectKey " ]
193
+
194
+ assets [ object_key ] = {
195
+ "bucket_name" : bucket_name . replace ( "${AWS::AccountId}" , account_id ). replace (
196
+ "${AWS::Region}" , region
197
+ ),
198
+ "asset_path" : asset_path ,
199
+ "asset_packaging" : asset_packaging ,
200
+ }
193
201
194
202
return assets
195
203
0 commit comments