|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import tarfile |
| 5 | + |
| 6 | +import boto3 |
| 7 | +import build_kubectl_plugin |
| 8 | +from botocore.exceptions import ClientError, NoCredentialsError, PartialCredentialsError |
| 9 | +from github import Github, GithubException |
| 10 | + |
| 11 | +GITHUB_REPO = "mongodb/mongodb-kubernetes" |
| 12 | +GITHUB_TOKEN = os.environ.get("GH_TOKEN") |
| 13 | + |
| 14 | +LOCAL_ARTIFACTS_DIR = "artifacts" |
| 15 | + |
| 16 | + |
| 17 | +def main(): |
| 18 | + parser = argparse.ArgumentParser() |
| 19 | + parser.add_argument( |
| 20 | + "--release_version", |
| 21 | + required=True, |
| 22 | + help="product release version, ideally should match with github tag/release.", |
| 23 | + ) |
| 24 | + parser.add_argument("--staging_commit", required=True, help="staging commit that we want to promote to release.") |
| 25 | + |
| 26 | + args = parser.parse_args() |
| 27 | + 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) |
| 32 | + |
| 33 | + |
| 34 | +def s3_artifacts_path_to_local_path(commit_sha: str): |
| 35 | + return { |
| 36 | + f"{build_kubectl_plugin.S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/dist/kubectl-mongodb_darwin_amd64_v1/": "kubectl-mongodb_darwin_amd64_v1", |
| 37 | + f"{build_kubectl_plugin.S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/dist/kubectl-mongodb_darwin_arm64/": "kubectl-mongodb_darwin_arm64", |
| 38 | + f"{build_kubectl_plugin.S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/dist/kubectl-mongodb_linux_amd64_v1/": "kubectl-mongodb_linux_amd64_v1", |
| 39 | + f"{build_kubectl_plugin.S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/dist/kubectl-mongodb_linux_arm64/": "kubectl-mongodb_linux_arm64", |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +def 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}") |
| 45 | + |
| 46 | + try: |
| 47 | + s3_client = boto3.client("s3", region_name=build_kubectl_plugin.AWS_REGION) |
| 48 | + except (NoCredentialsError, PartialCredentialsError): |
| 49 | + print("ERROR: AWS credentials were not set.") |
| 50 | + sys.exit(1) |
| 51 | + except Exception as e: |
| 52 | + print(f"An error occurred connecting to S3: {e}") |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + artifacts_to_promote = s3_artifacts_path_to_local_path(commit_sha) |
| 56 | + |
| 57 | + # Create the local temporary directory if it doesn't exist |
| 58 | + os.makedirs(LOCAL_ARTIFACTS_DIR, exist_ok=True) |
| 59 | + |
| 60 | + download_count = 0 |
| 61 | + 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} to {local_subdir}/") |
| 63 | + try: |
| 64 | + paginator = s3_client.get_paginator("list_objects_v2") |
| 65 | + pages = paginator.paginate(Bucket=build_kubectl_plugin.DEV_S3_BUCKET_NAME, Prefix=s3_artifact_dir) |
| 66 | + |
| 67 | + for page in pages: |
| 68 | + if "Contents" not in page: |
| 69 | + continue |
| 70 | + for obj in page["Contents"]: |
| 71 | + s3_key = obj["Key"] |
| 72 | + if s3_key.endswith("/"): |
| 73 | + continue |
| 74 | + |
| 75 | + # Get the path of the file relative to its S3 prefix, this would mostly be the object name itself |
| 76 | + # if s3_artifact_dir doesn't container directories and has just the objects. |
| 77 | + relative_path = os.path.relpath(s3_key, s3_artifact_dir) |
| 78 | + |
| 79 | + final_local_path = os.path.join(LOCAL_ARTIFACTS_DIR, local_subdir, relative_path) |
| 80 | + |
| 81 | + # Create the local directory structure if it doesn't exist |
| 82 | + os.makedirs(os.path.dirname(final_local_path), exist_ok=True) |
| 83 | + |
| 84 | + print(f"Downloading {s3_key} to {final_local_path}") |
| 85 | + s3_client.download_file(build_kubectl_plugin.DEV_S3_BUCKET_NAME, s3_key, final_local_path) |
| 86 | + download_count += 1 |
| 87 | + |
| 88 | + except ClientError as e: |
| 89 | + print(f"ERROR: Failed to list or download from prefix '{s3_artifact_dir}'. S3 Client Error: {e}") |
| 90 | + return False |
| 91 | + |
| 92 | + print("All the artifacts have been downloaded successfully.") |
| 93 | + return True |
| 94 | + |
| 95 | + |
| 96 | +def create_tarballs(): |
| 97 | + print(f"Creating archives for subdirectories in {LOCAL_ARTIFACTS_DIR}") |
| 98 | + created_archives = [] |
| 99 | + original_cwd = os.getcwd() |
| 100 | + try: |
| 101 | + os.chdir(LOCAL_ARTIFACTS_DIR) |
| 102 | + |
| 103 | + for dir_name in os.listdir("."): |
| 104 | + if os.path.isdir(dir_name): |
| 105 | + archive_name = f"{dir_name}.tar.gz" |
| 106 | + |
| 107 | + with tarfile.open(archive_name, "w:gz") as tar: |
| 108 | + tar.add(dir_name) |
| 109 | + |
| 110 | + full_archive_path = os.path.join(original_cwd, LOCAL_ARTIFACTS_DIR, archive_name) |
| 111 | + print(f"Successfully created archive at {full_archive_path}") |
| 112 | + created_archives.append(full_archive_path) |
| 113 | + |
| 114 | + except Exception as e: |
| 115 | + print(f"ERROR: Failed to create tar.gz archives: {e}") |
| 116 | + return [] |
| 117 | + finally: |
| 118 | + os.chdir(original_cwd) |
| 119 | + |
| 120 | + return created_archives |
| 121 | + |
| 122 | + |
| 123 | +def upload_assets_to_github_release(asset_paths, release_version: str): |
| 124 | + if not GITHUB_TOKEN: |
| 125 | + print("ERROR: GITHUB_TOKEN environment variable not set.") |
| 126 | + sys.exit(1) |
| 127 | + |
| 128 | + try: |
| 129 | + g = Github(GITHUB_TOKEN) |
| 130 | + repo = g.get_repo(GITHUB_REPO) |
| 131 | + except GithubException as e: |
| 132 | + print(f"ERROR: Could not connect to GitHub or find repository '{GITHUB_REPO}', Error {e}.") |
| 133 | + sys.exit(1) |
| 134 | + |
| 135 | + try: |
| 136 | + release = repo.get_release(release_version) |
| 137 | + except GithubException as e: |
| 138 | + print( |
| 139 | + f"ERROR: Could not find release with tag '{release_version}'. Please ensure release exists already. Error: {e}" |
| 140 | + ) |
| 141 | + return |
| 142 | + |
| 143 | + for asset_path in asset_paths: |
| 144 | + asset_name = os.path.basename(asset_path) |
| 145 | + print(f"Uploading artifact '{asset_name}' to github release as asset") |
| 146 | + try: |
| 147 | + release.upload_asset(path=asset_path, name=asset_name, content_type="application/gzip") |
| 148 | + except GithubException as e: |
| 149 | + print(f"ERROR: Failed to upload asset {asset_name}. Error: {e}") |
| 150 | + except Exception as e: |
| 151 | + print(f"An unexpected error occurred during upload of {asset_name}: {e}") |
| 152 | + |
| 153 | + |
| 154 | +if __name__ == "__main__": |
| 155 | + main() |
0 commit comments