4
4
5
5
import boto3
6
6
from botocore .exceptions import ClientError , NoCredentialsError , PartialCredentialsError
7
- from mypy .types import ExtraAttrs
8
7
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
+ )
14
15
15
16
AWS_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
19
19
20
20
GORELEASER_DIST_DIR = "dist"
21
21
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"
24
24
25
25
26
26
def run_goreleaser ():
@@ -36,111 +36,102 @@ def run_goreleaser():
36
36
exit_code = process .wait ()
37
37
38
38
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 } ." )
40
40
sys .exit (1 )
41
41
42
- print ("GoReleaser build completed successfully!" )
42
+ logger . info ("GoReleaser build completed successfully!" )
43
43
44
44
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
+ )
46
48
sys .exit (1 )
47
49
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 } " )
49
51
sys .exit (1 )
50
52
51
53
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 ):
53
57
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." )
55
59
sys .exit (1 )
56
60
57
61
try :
58
62
s3_client = boto3 .client ("s3" , region_name = AWS_REGION )
59
63
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." )
61
65
sys .exit (1 )
62
66
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 } " )
64
68
sys .exit (1 )
65
69
66
70
uploaded_files = 0
71
+ # iterate over all the files generated by goreleaser in the dist directory and upload them to S3
67
72
for root , _ , files in os .walk (GORELEASER_DIST_DIR ):
68
73
for filename in files :
69
74
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 )
76
76
77
+ logger .info (f"Uploading artifact { local_path } to s3://{ s3_bucket } /{ s3_key } " )
77
78
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 } " )
87
81
uploaded_files += 1
88
82
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 } " )
90
84
91
85
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." )
93
87
94
88
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.
96
90
# For dev workflows it's going to be `kubectl-mongodb/{evg-patch-id}/{goreleaser-artifact}`,
97
91
# 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 } "
107
95
108
96
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 ):
110
100
try :
111
101
s3_client = boto3 .client ("s3" , region_name = AWS_REGION )
112
102
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 } " )
114
104
return
115
105
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"
122
107
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 } " )
124
109
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 )
128
113
129
- print (f"Successfully downloaded artifact to { LOCAL_FILE_PATH } " )
114
+ logger . info (f"Successfully downloaded artifact to { LOCAL_KUBECTL_PLUGIN_PATH } " )
130
115
except ClientError as e :
131
116
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 } " )
133
118
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 } " )
135
120
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 } " )
137
122
138
123
139
124
def 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
+
140
128
run_goreleaser ()
141
- upload_artifacts_to_s3 ()
142
129
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
+ )
144
135
145
136
146
137
if __name__ == "__main__" :
0 commit comments