|
1 | 1 | import json |
2 | 2 | import logging |
3 | 3 | import re |
| 4 | +import tempfile |
4 | 5 | from datetime import datetime |
5 | 6 | from os import getenv |
6 | 7 | from pathlib import Path |
| 8 | +from shutil import rmtree |
7 | 9 |
|
| 10 | +import boto3 |
8 | 11 | import dotenv |
9 | 12 | from podman import PodmanClient |
10 | 13 | from rq import get_current_job |
@@ -34,7 +37,13 @@ def build(req: dict, job=None): |
34 | 37 | Args: |
35 | 38 | request (dict): Contains all properties of requested image |
36 | 39 | """ |
37 | | - store_path = Path(req["public_path"]) / "store" |
| 40 | + if req["s3_server"]: |
| 41 | + temp_path = tempfile.TemporaryDirectory() |
| 42 | + store_path = Path(temp_path.name) |
| 43 | + else: |
| 44 | + temp_path = None |
| 45 | + store_path = Path(req["public_path"]) / "store" |
| 46 | + |
38 | 47 | store_path.mkdir(parents=True, exist_ok=True) |
39 | 48 | log.debug(f"Store path: {store_path}") |
40 | 49 |
|
@@ -326,6 +335,25 @@ def build(req: dict, job=None): |
326 | 335 |
|
327 | 336 | log.debug("JSON content %s", json_content) |
328 | 337 |
|
| 338 | + # Upload to S3 |
| 339 | + s3 = boto3.client( |
| 340 | + "s3", |
| 341 | + endpoint_url=req["s3_server"], |
| 342 | + aws_access_key_id=req["s3_access_key"], |
| 343 | + aws_secret_access_key=req["s3_secret_key"], |
| 344 | + ) |
| 345 | + for image in json_content["images"]: |
| 346 | + print(f"Uploading {image['name']} to S3") |
| 347 | + s3.upload_file( |
| 348 | + str(store_path / bin_dir / image["name"]), |
| 349 | + req["s3_bucket"], |
| 350 | + f"{req['request_hash']}/{image['name']}", |
| 351 | + ) |
| 352 | + |
| 353 | + if temp_path: |
| 354 | + temp_path.cleanup() |
| 355 | + rmtree(store_path, ignore_errors=True) |
| 356 | + |
329 | 357 | # Increment stats |
330 | 358 | job.connection.hincrby( |
331 | 359 | "stats:builds", |
|
0 commit comments