@@ -25,23 +25,91 @@ def main():
2525
2626    args  =  parser .parse_args ()
2727    download_artifacts_from_s3 (args .release_version , args .staging_commit )
28-     arcs  =  create_tarballs ()
29-     # promote to release s3 bucket 
30-     print (f"created archives are { arcs }  )
31-     upload_assets_to_github_release (arcs , args .release_version )
3228
29+     promote_to_release_bucket (args .staging_commit , args .release_version )
3330
34- def  s3_artifacts_path_to_local_path (commit_sha : str ):
31+     artifacts_tar  =  create_tarballs ()
32+ 
33+     upload_assets_to_github_release (artifacts_tar , args .release_version )
34+ 
35+ 
36+ def  artifacts_source_dir_s3 (commit_sha : str ):
37+     return  f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  
38+ 
39+ 
40+ def  artifacts_dest_dir_s3 (release_verion : str ):
41+     return  f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { release_verion }  
42+ 
43+ 
44+ def  promote_to_release_bucket (commit_sha : str , release_version : str ):
45+     try :
46+         s3_client  =  boto3 .client ("s3" , region_name = build_kubectl_plugin .AWS_REGION )
47+     except  (NoCredentialsError , PartialCredentialsError ):
48+         print ("ERROR: AWS credentials not found. Please configure AWS credentials." )
49+         sys .exit (1 )
50+     except  Exception  as  e :
51+         print (f"An error occurred connecting to S3: { e }  )
52+         sys .exit (1 )
53+ 
54+     copy_count  =  0 
55+     try :
56+         paginator  =  s3_client .get_paginator ("list_objects_v2" )
57+         pages  =  paginator .paginate (
58+             Bucket = build_kubectl_plugin .DEV_S3_BUCKET_NAME , Prefix = artifacts_source_dir_s3 (commit_sha )
59+         )
60+ 
61+         for  page  in  pages :
62+             if  "Contents"  not  in page :
63+                 break 
64+ 
65+             for  obj  in  page ["Contents" ]:
66+                 source_key  =  obj ["Key" ]
67+ 
68+                 if  source_key .endswith ("/" ):
69+                     continue 
70+ 
71+                 # Determine the new key for the destination 
72+                 relative_path  =  os .path .relpath (source_key , artifacts_source_dir_s3 (commit_sha ))
73+ 
74+                 destination_dir  =  artifacts_dest_dir_s3 (release_version )
75+                 destination_key  =  os .path .join (destination_dir , relative_path )
76+ 
77+                 # Ensure forward slashes for S3 compatibility 
78+                 destination_key  =  destination_key .replace (os .sep , "/" )
79+ 
80+                 print (f"Copying { source_key } { destination_key }  )
81+ 
82+                 # Prepare the source object for the copy operation 
83+                 copy_source  =  {"Bucket" : build_kubectl_plugin .DEV_S3_BUCKET_NAME , "Key" : source_key }
84+ 
85+                 # Perform the server-side copy 
86+                 s3_client .copy_object (
87+                     CopySource = copy_source , Bucket = build_kubectl_plugin .STAGING_S3_BUCKET_NAME , Key = destination_key 
88+                 )
89+                 copy_count  +=  1 
90+ 
91+     except  ClientError  as  e :
92+         print (f"ERROR: A client error occurred during the copy operation. Error: { e }  )
93+         sys .exit (1 )
94+     except  Exception  as  e :
95+         print (f"An unexpected error occurred: { e }  )
96+         sys .exit (1 )
97+ 
98+     if  copy_count  >  0 :
99+         print (f"Successfully copied { copy_count }  )
100+ 
101+ 
102+ def  s3_artifacts_path_to_local_path (release_version : str , commit_sha : str ):
35103    return  {
36-         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : "kubectl-mongodb_darwin_amd64_v1 " ,
37-         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : "kubectl-mongodb_darwin_arm64 " ,
38-         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : "kubectl-mongodb_linux_amd64_v1 " ,
39-         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : "kubectl-mongodb_linux_arm64 " ,
104+         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : f "kubectl-mongodb_ { release_version } _darwin_amd64 "
105+         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : f "kubectl-mongodb_ { release_version } _darwin_arm64 "
106+         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : f "kubectl-mongodb_ { release_version } _linux_amd64 "
107+         f"{ build_kubectl_plugin .S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } { commit_sha }  : f "kubectl-mongodb_ { release_version } _linux_arm64 "
40108    }
41109
42110
43111def  download_artifacts_from_s3 (release_version : str , commit_sha : str ):
44-     print (f"Starting  download of artifacts from S3 bucket: { build_kubectl_plugin .DEV_S3_BUCKET_NAME }  )
112+     print (f"\n Starting  download of artifacts from S3 bucket: { build_kubectl_plugin .DEV_S3_BUCKET_NAME }  )
45113
46114    try :
47115        s3_client  =  boto3 .client ("s3" , region_name = build_kubectl_plugin .AWS_REGION )
@@ -52,24 +120,26 @@ def download_artifacts_from_s3(release_version: str, commit_sha: str):
52120        print (f"An error occurred connecting to S3: { e }  )
53121        sys .exit (1 )
54122
55-     artifacts_to_promote  =  s3_artifacts_path_to_local_path (commit_sha )
123+     artifacts_to_promote  =  s3_artifacts_path_to_local_path (release_version ,  commit_sha )
56124
57125    # Create the local temporary directory if it doesn't exist 
58126    os .makedirs (LOCAL_ARTIFACTS_DIR , exist_ok = True )
59127
60128    download_count  =  0 
61129    for  s3_artifact_dir , local_subdir  in  artifacts_to_promote .items ():
62-         print (f"Copying from s3://{ build_kubectl_plugin .DEV_S3_BUCKET_NAME } { s3_artifact_dir } { local_subdir }  )
63130        try :
64131            paginator  =  s3_client .get_paginator ("list_objects_v2" )
65132            pages  =  paginator .paginate (Bucket = build_kubectl_plugin .DEV_S3_BUCKET_NAME , Prefix = s3_artifact_dir )
66133
67134            for  page  in  pages :
135+                 # "Contents" corresponds to the directory in the S3 bucket 
68136                if  "Contents"  not  in page :
69137                    continue 
70138                for  obj  in  page ["Contents" ]:
139+                     # obj is the S3 object in page["Contents"] directory 
71140                    s3_key  =  obj ["Key" ]
72141                    if  s3_key .endswith ("/" ):
142+                         # it's a directory 
73143                        continue 
74144
75145                    # Get the path of the file relative to its S3 prefix, this would mostly be the object name itself 
@@ -94,7 +164,7 @@ def download_artifacts_from_s3(release_version: str, commit_sha: str):
94164
95165
96166def  create_tarballs ():
97-     print (f"Creating  archives for subdirectories in { LOCAL_ARTIFACTS_DIR }  )
167+     print (f"\n Creating  archives for subdirectories in { LOCAL_ARTIFACTS_DIR }  )
98168    created_archives  =  []
99169    original_cwd  =  os .getcwd ()
100170    try :
@@ -114,8 +184,6 @@ def create_tarballs():
114184    except  Exception  as  e :
115185        print (f"ERROR: Failed to create tar.gz archives: { e }  )
116186        return  []
117-     finally :
118-         os .chdir (original_cwd )
119187
120188    return  created_archives 
121189
0 commit comments