|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
| 5 | +import boto3 |
| 6 | +from botocore.exceptions import ClientError, NoCredentialsError, PartialCredentialsError |
| 7 | + |
| 8 | +# from lib.base_logger import logger |
| 9 | + |
| 10 | +S3_BUCKET_NAME = "mongodb-kubernetes-dev" |
| 11 | +AWS_REGION = "eu-north-1" |
| 12 | +S3_BUCKET_KUBECTL_PLUGIN_SUBPATH = "kubectl-mongodb" |
| 13 | + |
| 14 | +COMMIT_SHA_ENV_VAR = "github_commit" |
| 15 | + |
| 16 | +GORELEASER_DIST_DIR = "dist" |
| 17 | + |
| 18 | + |
| 19 | +def run_goreleaser(): |
| 20 | + try: |
| 21 | + command = ["./goreleaser", "build", "--snapshot", "--clean"] |
| 22 | + |
| 23 | + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1) |
| 24 | + |
| 25 | + for log in iter(process.stdout.readline, ""): |
| 26 | + print(log, end="") |
| 27 | + |
| 28 | + process.stdout.close() |
| 29 | + exit_code = process.wait() |
| 30 | + |
| 31 | + if exit_code != 0: |
| 32 | + print(f"GoReleaser command failed with exit code {exit_code}.") |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + print("GoReleaser build completed successfully!") |
| 36 | + |
| 37 | + except FileNotFoundError: |
| 38 | + print("ERROR: 'goreleaser' command not found. Please ensure GoReleaser is installed and in your system's PATH.") |
| 39 | + sys.exit(1) |
| 40 | + except Exception as e: |
| 41 | + print(f"An unexpected error occurred while running `goreleaser build`: {e}") |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + |
| 45 | +def upload_artifacts_to_s3(): |
| 46 | + if not os.path.isdir(GORELEASER_DIST_DIR): |
| 47 | + print(f"ERROR: GoReleaser dist directory '{GORELEASER_DIST_DIR}' not found.") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + try: |
| 51 | + s3_client = boto3.client("s3", region_name=AWS_REGION) |
| 52 | + except (NoCredentialsError, PartialCredentialsError): |
| 53 | + print("ERROR: Failed to create S3 client. AWS credentials not found.") |
| 54 | + sys.exit(1) |
| 55 | + except Exception as e: |
| 56 | + print(f"An error occurred connecting to S3: {e}") |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | + uploaded_files = 0 |
| 60 | + for root, _, files in os.walk(GORELEASER_DIST_DIR): |
| 61 | + for filename in files: |
| 62 | + local_path = os.path.join(root, filename) |
| 63 | + s3_key = s3_path(local_path) |
| 64 | + |
| 65 | + print(f"Uploading artifact {local_path} to s3://{S3_BUCKET_NAME}/{s3_key}") |
| 66 | + |
| 67 | + try: |
| 68 | + s3_client.upload_file(local_path, S3_BUCKET_NAME, s3_key) |
| 69 | + print(f"Successfully uploaded the artifact {filename}") |
| 70 | + uploaded_files += 1 |
| 71 | + except Exception as e: |
| 72 | + print(f"ERROR: Failed to upload file {filename}: {e}") |
| 73 | + |
| 74 | + if uploaded_files > 0: |
| 75 | + print(f"Successfully uploaded {uploaded_files} kubectl-mongodb plugin artifacts to S3.") |
| 76 | + |
| 77 | + |
| 78 | +def s3_path(local_path: str): |
| 79 | + commit_sha = os.environ.get(COMMIT_SHA_ENV_VAR, "").strip() |
| 80 | + if commit_sha == "": |
| 81 | + print(f"Error: The commit sha environment variable {COMMIT_SHA_ENV_VAR} is not set. It's required to form the S3 Path.") |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + return f"{S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/{local_path}" |
| 85 | + |
| 86 | + |
| 87 | +def download_plugin_for_tests_image(): |
| 88 | + try: |
| 89 | + s3_client = boto3.client("s3", region_name=AWS_REGION) |
| 90 | + except Exception as e: |
| 91 | + print(f"An error occurred connecting to S3 to download kubectl plugin for tests image: {e}") |
| 92 | + return |
| 93 | + |
| 94 | + |
| 95 | + commit_sha = os.environ.get(COMMIT_SHA_ENV_VAR, "").strip() |
| 96 | + if commit_sha == "": |
| 97 | + print("Error: The commit sha environment variable is not set. It's required to form the S3 Path.") |
| 98 | + sys.exit(1) |
| 99 | + |
| 100 | + local_file_path = "docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_linux" |
| 101 | + |
| 102 | + bucket_path = f"{S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/dist/kubectl-mongodb_linux_amd64_v1/kubectl-mongodb" |
| 103 | + print(f"Downloading s3://{S3_BUCKET_NAME}/{bucket_path} to {local_file_path}") |
| 104 | + |
| 105 | + try: |
| 106 | + s3_client.download_file(S3_BUCKET_NAME, bucket_path, local_file_path) |
| 107 | + print(f"Successfully downloaded artifact to {local_file_path}") |
| 108 | + except ClientError as e: |
| 109 | + if e.response['Error']['Code'] == '404': |
| 110 | + print(f"ERROR: Artifact not found at s3://{S3_BUCKET_NAME}/{bucket_path} ") |
| 111 | + else: |
| 112 | + print(f"ERROR: Failed to download artifact. S3 Client Error: {e}") |
| 113 | + except Exception as e: |
| 114 | + print(f"An unexpected error occurred during download: {e}") |
| 115 | + |
| 116 | +def main(): |
| 117 | + run_goreleaser() |
| 118 | + upload_artifacts_to_s3() |
| 119 | + |
| 120 | + download_plugin_for_tests_image() |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + main() |
0 commit comments