1+ import json
12import os
23import zipfile
34import yaml
45import boto3
56from boto3 .session import Session
67from concurrent .futures import ThreadPoolExecutor , as_completed
78
9+ # Load index.json file
10+ with open ('index.json' ) as f :
11+ data = json .load (f )
12+
813# Specify your AWS S3 bucket from environment variable
914s3_bucket_name = os .environ .get ('S3_BUCKET_NAME' ) # Bucket name sourced from environment variables
1015
@@ -22,68 +27,61 @@ def upload_to_s3(file_path, s3_client, bucket_name, s3_key):
2227)
2328s3_client = session .client ('s3' )
2429
25- # Function to find all facets.yaml files recursively
26- def find_facets_yaml_files (base_dir ):
27- facets_paths = []
28- for root , _ , files in os .walk (base_dir ):
29- if 'facets.yaml' in files :
30- # Check if there are any .tf or .tf.json files in the directory
31- terraform_files = [file for file in files if file .endswith ('.tf' ) or file .endswith ('.tf.json' )]
32- if terraform_files :
33- facets_paths .append (root )
34- return facets_paths
35-
36- # Find all facets.yaml files in the modules directory
37- modules_dir = './modules'
38- facets_paths = find_facets_yaml_files (modules_dir )
39-
4030# Initialize a ThreadPoolExecutor for concurrent uploads
4131with ThreadPoolExecutor () as executor :
4232 # This will hold all future upload tasks
4333 upload_futures = []
44-
45- # Process each directory containing facets.yaml
46- for relative_path in facets_paths :
47- # Construct the path to facets.yaml
48- facets_yaml_path = os .path .join (relative_path , 'facets.yaml' )
49-
50- # Load facets.yaml file
51- with open (facets_yaml_path ) as yaml_file :
52- facets_data = yaml .safe_load (yaml_file )
53-
54- # Extract intent, flavor, and version
55- intent = facets_data .get ('intent' )
56- flavor = facets_data .get ('flavor' )
57- version = facets_data .get ('version' )
58-
59- # Check if all required fields are present
60- if intent and flavor and version :
61- print (f"Zipping intent { intent } flavor { flavor } version { version } " )
62-
63- # Create the target zip file path
64- zip_dir_path = os .path .join (str (zip_root_dir ), str (intent ), str (flavor ), str (version ))
65- os .makedirs (zip_dir_path , exist_ok = True )
66-
67- # Create zip file name
68- zip_file_name = os .path .join (zip_dir_path , 'module.zip' )
69-
70- # Create the zip file
71- with zipfile .ZipFile (zip_file_name , 'w' ) as zipf :
72- # Walk through the directory and add files
73- for root , _ , files in os .walk (relative_path ):
74- for file in files :
75- file_path = os .path .join (root , file )
76- # Write the file into the zip, preserving the original structure
77- zipf .write (file_path , os .path .relpath (file_path , os .path .dirname (relative_path )))
78-
79- # Prepare to upload the zip file to S3
80- s3_key = os .path .relpath (zip_file_name , zip_root_dir ) # Path within the S3 bucket
81- print (f"Uploading { s3_key } " )
82-
83- # Fire a thread to upload the zip file to S3
84- upload_future = executor .submit (upload_to_s3 , zip_file_name , s3_client , s3_bucket_name , s3_key )
85- upload_futures .append (upload_future )
86-
34+
35+ # Go through each entry in the JSON data
36+ for entry in data :
37+ relative_path = entry ['relativePath' ]
38+
39+ # Check if there are any .tf or .tf.json files in the relative path
40+ terraform_files = [file for file in os .listdir (relative_path ) if file .endswith ('.tf' ) or file .endswith ('.tf.json' )]
41+
42+ if terraform_files :
43+ # Construct the path to facets.yaml
44+ facets_yaml_path = os .path .join (relative_path , 'facets.yaml' )
45+
46+ # Check if facets.yaml exists
47+ if os .path .exists (facets_yaml_path ):
48+ # Load facets.yaml file
49+ with open (facets_yaml_path ) as yaml_file :
50+ facets_data = yaml .safe_load (yaml_file )
51+
52+ # Extract intent, flavor, and version
53+ intent = facets_data .get ('intent' )
54+ flavor = facets_data .get ('flavor' )
55+ version = facets_data .get ('version' )
56+
57+ # Check if all required fields are present
58+ if intent and flavor and version :
59+ print (f"Zipping intent { intent } flavor { flavor } version { version } " )
60+ # Create the target zip file path
61+ zip_dir_path = os .path .join (str (zip_root_dir ), str (intent ), str (flavor ), str (version ))
62+ os .makedirs (zip_dir_path , exist_ok = True )
63+
64+ # Create zip file name
65+ zip_file_name = os .path .join (zip_dir_path , 'module.zip' )
66+
67+ # Create the zip file
68+ with zipfile .ZipFile (zip_file_name , 'w' ) as zipf :
69+ # Walk through the directory and add files
70+ for root , _ , files in os .walk (relative_path ):
71+ for file in files :
72+ file_path = os .path .join (root , file )
73+ # Write the file into the zip, preserving the original structure
74+ zipf .write (file_path , os .path .relpath (file_path , os .path .dirname (relative_path )))
75+
76+ # Prepare to upload the zip file to S3
77+ s3_key = os .path .relpath (zip_file_name , zip_root_dir ) # Path within the S3 bucket
78+ print (f"Uploading { s3_key } " )
79+
80+ # Fire a thread to upload the zip file to S3
81+ upload_future = executor .submit (upload_to_s3 , zip_file_name , s3_client , s3_bucket_name , s3_key )
82+
83+ upload_futures .append (upload_future )
84+
8785 # Wait for all upload tasks to complete
8886 for future in as_completed (upload_futures ):
8987 future .result () # This will raise any exceptions that occurred during the upload
0 commit comments