This repository was archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathregistry_utils.py
64 lines (50 loc) · 2.26 KB
/
registry_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import base64
import boto3
def get_ecr_login_info(region, repository_id):
ecr_client = boto3.client("ecr", region)
token = ecr_client.get_authorization_token(registryIds=[repository_id])
username, password = (
base64.b64decode(token["authorizationData"][0]["authorizationToken"])
.decode("utf-8")
.split(":")
)
registry_url = token["authorizationData"][0]["proxyEndpoint"]
return registry_url, username, password
def get_repository(ecr_client, repository_name):
result = ecr_client.describe_repositories(repositoryNames=[repository_name])
repository_id = result["repositories"][0]["registryId"]
repository_uri = result["repositories"][0]["repositoryUri"]
return repository_id, repository_uri
def create_ecr_repository_if_not_exists(region, repository_name):
ecr_client = boto3.client("ecr", region)
try:
result = ecr_client.describe_repositories(repositoryNames=[repository_name])
repository_id = result["repositories"][0]["registryId"]
repository_uri = result["repositories"][0]["repositoryUri"]
except ecr_client.exceptions.RepositoryNotFoundException:
result = ecr_client.create_repository(repositoryName=repository_name)
repository_id = result["repository"]["registryId"]
repository_uri = result["repository"]["repositoryUri"]
return repository_id, repository_uri
def create_repository(repository_name, operator_spec):
"""
Create ECR repository and return the information.
"""
repo_id, _ = create_ecr_repository_if_not_exists(
operator_spec["region"], repository_name
)
registry_url, username, password = get_ecr_login_info(
operator_spec["region"], repo_id
)
repository_url = f"{registry_url}/{repository_name}"
return repository_url, username, password
def delete_repository(repository_name, operator_spec):
"""
Delete the ECR repository created
"""
ecr_client = boto3.client("ecr", operator_spec.get("region"))
try:
get_repository(ecr_client, repository_name)
ecr_client.delete_repository(repositoryName=repository_name, force=True)
except ecr_client.exceptions.RepositoryNotFoundException:
print(f"Repository {repository_name} not found. Skipping registry cleanup")