44
55import boto3
66from botocore .exceptions import ClientError , NoCredentialsError , PartialCredentialsError
7- from mypy .types import ExtraAttrs
87
9- # from lib.base_logger import logger
10-
11- DEV_S3_BUCKET_NAME = "mongodb-kubernetes-dev"
12- STAGING_S3_BUCKET_NAME = "mongodb-kubernetes-staging"
13- RELEASE_S3_BUCKET_NAME = "mongodb-kubernetes-release"
8+ from lib .base_logger import logger
9+ from scripts .release .build .build_info import (
10+ load_build_info ,
11+ )
12+ from scripts .release .build .build_scenario import (
13+ BuildScenario ,
14+ )
1415
1516AWS_REGION = "eu-north-1"
16- S3_BUCKET_KUBECTL_PLUGIN_SUBPATH = "kubectl-mongodb"
17-
18- COMMIT_SHA_ENV_VAR = "github_commit"
17+ KUBECTL_PLUGIN_BINARY_NAME = "kubectl-mongodb"
18+ S3_BUCKET_KUBECTL_PLUGIN_SUBPATH = KUBECTL_PLUGIN_BINARY_NAME
1919
2020GORELEASER_DIST_DIR = "dist"
2121
22- # LOCAL_FILE_PATHis the full filename where tests image expects the kuebctl-mongodb binary to be available
23- LOCAL_FILE_PATH = "docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_linux"
22+ # LOCAL_KUBECTL_PLUGIN_PATH the full filename where tests image expects the kuebctl-mongodb binary to be available
23+ LOCAL_KUBECTL_PLUGIN_PATH = "docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_linux"
2424
2525
2626def run_goreleaser ():
@@ -36,111 +36,102 @@ def run_goreleaser():
3636 exit_code = process .wait ()
3737
3838 if exit_code != 0 :
39- print (f"GoReleaser command failed with exit code { exit_code } ." )
39+ logger . debug (f"GoReleaser command failed with exit code { exit_code } ." )
4040 sys .exit (1 )
4141
42- print ("GoReleaser build completed successfully!" )
42+ logger . info ("GoReleaser build completed successfully!" )
4343
4444 except FileNotFoundError :
45- print ("ERROR: 'goreleaser' command not found. Please ensure goreleaser is installed and in your system's PATH." )
45+ logger .debug (
46+ "ERROR: 'goreleaser' command not found. Please ensure goreleaser is installed and in your system's PATH."
47+ )
4648 sys .exit (1 )
4749 except Exception as e :
48- print (f"An unexpected error occurred while running `goreleaser build`: { e } " )
50+ logger . debug (f"An unexpected error occurred while running `goreleaser build`: { e } " )
4951 sys .exit (1 )
5052
5153
52- def upload_artifacts_to_s3 ():
54+ # upload_artifacts_to_s3 uploads the artifacts that are generated by goreleaser to S3 bucket at a specific path.
55+ # The S3 bucket and version are figured out and passed to this function based on BuildScenario.
56+ def upload_artifacts_to_s3 (s3_bucket : str , version : str ):
5357 if not os .path .isdir (GORELEASER_DIST_DIR ):
54- print (f"ERROR: GoReleaser dist directory '{ GORELEASER_DIST_DIR } ' not found." )
58+ logger . info (f"ERROR: GoReleaser dist directory '{ GORELEASER_DIST_DIR } ' not found." )
5559 sys .exit (1 )
5660
5761 try :
5862 s3_client = boto3 .client ("s3" , region_name = AWS_REGION )
5963 except (NoCredentialsError , PartialCredentialsError ):
60- print ("ERROR: Failed to create S3 client. AWS credentials not found." )
64+ logger . debug ("ERROR: Failed to create S3 client. AWS credentials not found." )
6165 sys .exit (1 )
6266 except Exception as e :
63- print (f"An error occurred connecting to S3: { e } " )
67+ logger . debug (f"An error occurred connecting to S3: { e } " )
6468 sys .exit (1 )
6569
6670 uploaded_files = 0
71+ # iterate over all the files generated by goreleaser in the dist directory and upload them to S3
6772 for root , _ , files in os .walk (GORELEASER_DIST_DIR ):
6873 for filename in files :
6974 local_path = os .path .join (root , filename )
70- s3_key = s3_path (local_path )
71-
72- print (f"Uploading artifact { local_path } to s3://{ DEV_S3_BUCKET_NAME } /{ s3_key } " )
73-
74- stat = os .stat (local_path )
75- permissions = str (oct (stat .st_mode )[- 3 :])
75+ s3_key = s3_path (local_path , version )
7676
77+ logger .info (f"Uploading artifact { local_path } to s3://{ s3_bucket } /{ s3_key } " )
7778 try :
78- s3_client .upload_file (
79- local_path ,
80- DEV_S3_BUCKET_NAME ,
81- s3_key ,
82- ExtraArgs = {
83- "Metadata" : {"posix-permissions" : permissions },
84- },
85- )
86- print (f"Successfully uploaded the artifact { filename } " )
79+ s3_client .upload_file (local_path , s3_bucket , s3_key )
80+ logger .info (f"Successfully uploaded the artifact { filename } " )
8781 uploaded_files += 1
8882 except Exception as e :
89- print (f"ERROR: Failed to upload file { filename } : { e } " )
83+ logger . debug (f"ERROR: Failed to upload file { filename } : { e } " )
9084
9185 if uploaded_files > 0 :
92- print (f"Successfully uploaded { uploaded_files } kubectl-mongodb plugin artifacts to S3." )
86+ logger . info (f"Successfully uploaded { uploaded_files } kubectl-mongodb plugin artifacts to S3." )
9387
9488
95- # s3_path returns the path where the artifacts should be uploaded to in S3 obect store.
89+ # s3_path returns the path where the artifacts should be uploaded to in S3 object store.
9690# For dev workflows it's going to be `kubectl-mongodb/{evg-patch-id}/{goreleaser-artifact}`,
9791# for staging workflows it would be `kubectl-mongodb/{commit-sha}/{goreleaser-artifact}`.
98- def s3_path (local_path : str ):
99- commit_sha = os .environ .get (COMMIT_SHA_ENV_VAR , "" ).strip ()
100- if commit_sha == "" :
101- print (
102- f"Error: The commit sha environment variable { COMMIT_SHA_ENV_VAR } is not set. It's required to form the S3 Path."
103- )
104- sys .exit (1 )
105-
106- return f"{ S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } /{ commit_sha } /{ local_path } "
92+ # The `version` string has the correct version (either patch id or commit sha), based on the BuildScenario.
93+ def s3_path (local_path : str , version : str ):
94+ return f"{ S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } /{ version } /{ local_path } "
10795
10896
109- def download_plugin_for_tests_image ():
97+ # download_plugin_for_tests_image downloads just the linux amd64 version of the binary and places it
98+ # at the location LOCAL_KUBECTL_PLUGIN_PATH.
99+ def download_plugin_for_tests_image (build_scenario : BuildScenario , s3_bucket : str , version : str ):
110100 try :
111101 s3_client = boto3 .client ("s3" , region_name = AWS_REGION )
112102 except Exception as e :
113- print (f"An error occurred connecting to S3 to download kubectl plugin for tests image: { e } " )
103+ logger . debug (f"An error occurred connecting to S3 to download kubectl plugin for tests image: { e } " )
114104 return
115105
116- commit_sha = os .environ .get (COMMIT_SHA_ENV_VAR , "" ).strip ()
117- if commit_sha == "" :
118- print ("Error: The commit sha environment variable is not set. It's required to form the S3 Path." )
119- sys .exit (1 )
120-
121- plugin_path = f"{ S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } /{ commit_sha } /dist/kubectl-mongodb_linux_amd64_v1/kubectl-mongodb"
106+ plugin_path = f"{ S3_BUCKET_KUBECTL_PLUGIN_SUBPATH } /{ version } /dist/kubectl-mongodb_linux_amd64_v1/kubectl-mongodb"
122107
123- print (f"Downloading s3://{ DEV_S3_BUCKET_NAME } /{ plugin_path } to { LOCAL_FILE_PATH } " )
108+ logger . info (f"Downloading s3://{ s3_bucket } /{ plugin_path } to { LOCAL_KUBECTL_PLUGIN_PATH } " )
124109 try :
125- s3_client .download_file (DEV_S3_BUCKET_NAME , plugin_path , LOCAL_FILE_PATH )
126- # change the file's permissions so that it can be executed
127- os .chmod (LOCAL_FILE_PATH , 0o755 )
110+ s3_client .download_file (s3_bucket , plugin_path , LOCAL_KUBECTL_PLUGIN_PATH )
111+ # change the file's permissions to make file executable
112+ os .chmod (LOCAL_KUBECTL_PLUGIN_PATH , 0o755 )
128113
129- print (f"Successfully downloaded artifact to { LOCAL_FILE_PATH } " )
114+ logger . info (f"Successfully downloaded artifact to { LOCAL_KUBECTL_PLUGIN_PATH } " )
130115 except ClientError as e :
131116 if e .response ["Error" ]["Code" ] == "404" :
132- print (f"ERROR: Artifact not found at s3://{ DEV_S3_BUCKET_NAME } /{ plugin_path } " )
117+ logger . debug (f"ERROR: Artifact not found at s3://{ s3_bucket } /{ plugin_path } " )
133118 else :
134- print (f"ERROR: Failed to download artifact. S3 Client Error: { e } " )
119+ logger . debug (f"ERROR: Failed to download artifact. S3 Client Error: { e } " )
135120 except Exception as e :
136- print (f"An unexpected error occurred during download: { e } " )
121+ logger . debug (f"An unexpected error occurred during download: { e } " )
137122
138123
139124def main ():
125+ build_scenario = BuildScenario .infer_scenario_from_environment ()
126+ kubectl_plugin_build_info = load_build_info (build_scenario ).binaries [KUBECTL_PLUGIN_BINARY_NAME ]
127+
140128 run_goreleaser ()
141- upload_artifacts_to_s3 ()
142129
143- download_plugin_for_tests_image ()
130+ upload_artifacts_to_s3 (kubectl_plugin_build_info .s3_store , kubectl_plugin_build_info .version )
131+
132+ download_plugin_for_tests_image (
133+ build_scenario , kubectl_plugin_build_info .s3_store , kubectl_plugin_build_info .version
134+ )
144135
145136
146137if __name__ == "__main__" :
0 commit comments