Skip to content

Commit 8c147ec

Browse files
Leverage build scenario to figure out bucket and s3 path
1 parent a83b451 commit 8c147ec

File tree

4 files changed

+60
-84
lines changed

4 files changed

+60
-84
lines changed

.evergreen-functions.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,6 @@ functions:
490490
- rh_pyxis
491491
binary: scripts/dev/run_python.sh scripts/preflight_images.py --image ${image_name} --submit "${preflight_submit}"
492492

493-
# build_multi_cluster_binary:
494-
# - command: subprocess.exec
495-
# type: setup
496-
# params:
497-
# working_dir: src/github.com/mongodb/mongodb-kubernetes
498-
# binary: scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh
499-
500493
install_macnotary:
501494
- command: shell.exec
502495
type: setup
@@ -543,7 +536,7 @@ functions:
543536
MACOS_NOTARY_KEY: ${macos_notary_keyid}
544537
MACOS_NOTARY_SECRET: ${macos_notary_secret}
545538
working_dir: src/github.com/mongodb/mongodb-kubernetes
546-
binary: scripts/evergreen/run_python.sh scripts/release/kubectl-mongodb/python/build_kubectl_plugin.py
539+
binary: scripts/dev/run_python.sh scripts/release/kubectl-mongodb/python/build_kubectl_plugin.py
547540

548541
build_and_push_appdb_database:
549542
- command: subprocess.exec

build_info.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,14 @@
279279
"binaries": {
280280
"kubectl-mongodb": {
281281
"patch": {
282-
"s3-store": "s3://kubectl-mongodb/dev",
282+
"s3-store": "mongodb-kubernetes-dev",
283283
"platforms": [
284284
"linux/amd64"
285285
]
286286
},
287287
"staging": {
288-
"sign": true,
289-
"s3-store": "s3://kubectl-mongodb/staging",
288+
"sign": false,
289+
"s3-store": "mongodb-kubernetes-staging",
290290
"platforms": [
291291
"darwin/amd64",
292292
"darwin/arm64",
@@ -296,7 +296,7 @@
296296
},
297297
"release": {
298298
"sign": true,
299-
"s3-store": "s3://kubectl-mongodb/prod",
299+
"s3-store": "mongodb-kubernetes-release",
300300
"platforms": [
301301
"darwin/amd64",
302302
"darwin/arm64",

scripts/release/kubectl-mongodb/kubectl_mac_notarize.sh

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ if [[ -f "./dist/kubectl-mongodb_darwin_amd64_v1/kubectl-mongodb" && -f "./dist/
2929
-b com.mongodb.mongodb-kubectl-mongodb \
3030
-o ./dist/kubectl-mongodb_macos_signed.zip
3131

32-
echo "printing output zip file content, unzip -l ./dist/kubectl-mongodb_macos_signed.zip"
33-
unzip -l ./dist/kubectl-mongodb_macos_signed.zip
34-
echo "done"
35-
36-
echo "running unzip -t ./dist/kubectl-mongodb_macos_signed.zip to verify that the op zip file of macnotary is correct"
37-
unzip -t ./dist/kubectl-mongodb_macos_signed.zip
38-
echo "done"
39-
4032
echo "replacing original files"
4133
unzip -oj ./dist/kubectl-mongodb_macos_signed.zip dist/kubectl-mongodb_darwin_amd64_v1/kubectl-mongodb -d ./dist/kubectl-mongodb_darwin_amd64_v1/
4234
unzip -oj ./dist/kubectl-mongodb_macos_signed.zip dist/kubectl-mongodb_darwin_arm64/kubectl-mongodb -d ./dist/kubectl-mongodb_darwin_arm64/

scripts/release/kubectl-mongodb/python/build_kubectl_plugin.py

Lines changed: 55 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44

55
import boto3
66
from 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

1516
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
1919

2020
GORELEASER_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

2626
def 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

139124
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+
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

146137
if __name__ == "__main__":

0 commit comments

Comments
 (0)