-
Notifications
You must be signed in to change notification settings - Fork 4
/
image_builder.py
72 lines (58 loc) · 2.15 KB
/
image_builder.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
65
66
67
68
69
70
71
72
import base64
import logging
import os
import boto3
import botocore
import docker
import yaml
from stactools_pipelines.models.pipeline import Pipeline
logging.basicConfig(level=logging.DEBUG)
pipeline_name = os.environ["PIPELINE"]
def build_and_push(dockerfile: str, tag: str, pipeline_id: str):
image, build_logs = client.images.build(
path="./",
dockerfile=dockerfile,
tag=tag,
buildargs={
"pipeline": pipeline_id,
},
)
for chunk in build_logs:
if "stream" in chunk:
for line in chunk["stream"].splitlines():
logging.debug(line)
ecr_client = boto3.client("ecr")
try:
ecr_client.create_repository(repositoryName=tag)
except botocore.exceptions.ClientError as error:
if error.response["Error"]["Code"] == "RepositoryAlreadyExistsException":
logging.debug("Repository already exists")
else:
raise error
ecr_credentials = ecr_client.get_authorization_token()["authorizationData"][0]
ecr_password = (
base64.b64decode(ecr_credentials["authorizationToken"])
.replace(b"AWS:", b"")
.decode("utf-8")
)
ecr_url = ecr_credentials["proxyEndpoint"]
client.login(username="AWS", password=ecr_password, registry=ecr_url)
ecr_repo_name = "{}/{}".format(ecr_url.replace("https://", ""), tag)
image.tag(ecr_repo_name, tag="latest")
push_log = client.images.push(ecr_repo_name, tag="latest")
logging.debug(push_log)
with open(f"./stactools_pipelines/pipelines/{pipeline_name}/config.yaml") as f:
config = yaml.safe_load(f)
pipeline = Pipeline(**config)
client = docker.from_env()
if pipeline.compute == "awslambda":
dockerfile = "./lambda.collection.Dockerfile"
tag = f"{pipeline.id}-collection"
build_and_push(dockerfile, tag, pipeline.id)
dockerfile = "./lambda.Dockerfile"
tag = pipeline.id
build_and_push(dockerfile, tag, pipeline.id)
if pipeline.inventory_location:
dockerfile = "./lambda.historic.Dockerfile"
tag = f"{pipeline.id}-historic"
build_and_push(dockerfile, tag, pipeline.id)