Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
feat: write metadata to db, rename script to toolkit-builder.py
Browse files Browse the repository at this point in the history
  • Loading branch information
1hachem committed Mar 5, 2024
1 parent 83f8fbe commit 2f560ee
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: setup build_sdk remove_containers remove_registry_images lint format
.PHONY: setup build remove_containers remove_registry_images lint format

dir ?= hyko_toolkit
registry ?= registry.traefik.me
Expand All @@ -7,7 +7,7 @@ setup:
./scripts/setup.sh

build:
python scripts/sdk-builder.py --dir $(dir) --registry $(registry)
python scripts/toolkit_builder.py --dir $(dir) --registry $(registry)

remove_containers:
docker rm -f $$(docker ps -a | grep hyko_sdk | awk '{print $$1}')
Expand Down
40 changes: 26 additions & 14 deletions scripts/sdk-builder.py → scripts/toolkit-builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import sys
from dataclasses import dataclass

import httpx
from pydantic import ValidationError

from hyko_sdk.metadata import Category, MetaData, MetaDataBase
from hyko_sdk.utils import metadata_to_docker_label

SKIP_FOLDERS = ["__pycache__", "venv"]

Expand All @@ -25,7 +25,7 @@ class BuildError(BaseException):
failed_functions: list[BuildError] = []


def process_function_dir(path: str, dockerfile_path: str, registry_host: str):
def process_function_dir(path: str, dockerfile_path: str, host: str):
"""Path has to be a valid path with no spaces in it"""
path = os.path.normpath(path)
splitted = path.split(os.sep)
Expand Down Expand Up @@ -71,7 +71,7 @@ def process_function_dir(path: str, dockerfile_path: str, registry_host: str):
), "Probably an error happen in while catching stdout from metadata"
metadata = splitted[1]

image_name = f"{registry_host}/{category.lower()}/{task.lower()}/{function_name.lower()}:latest"
image_name = f"registry.{host}/{category.lower()}/{task.lower()}/{function_name.lower()}:latest"
try:
metadata = MetaDataBase(**json.loads(metadata))
metadata = MetaData(
Expand All @@ -91,7 +91,6 @@ def process_function_dir(path: str, dockerfile_path: str, registry_host: str):
build_cmd += f"--build-arg CATEGORY={category} "
build_cmd += f"--build-arg FUNCTION_NAME={function_name} "
build_cmd += f"-t {image_name} "
build_cmd += f"""--label metadata="{metadata_to_docker_label(metadata)}" """
build_cmd += f"-f ./{dockerfile_path} "
build_cmd += f"./{path}"
try:
Expand All @@ -108,18 +107,31 @@ def process_function_dir(path: str, dockerfile_path: str, registry_host: str):
except subprocess.CalledProcessError as e:
raise BuildError(
function_name,
f"Failed to push to docker registry {registry_host}",
f"Failed to push to docker registry registry.{host}",
) from e

if registry_host != "registry.traefik.me":
response = httpx.post(
f"https://api.{host}/toolkit/write",
content=metadata.model_dump_json(),
verify=False if host == "traefik.me" else True,
)

# Check if the request was successful
if response.status_code != 200:
raise BuildError(
function_name,
f"couldn't write metadata to database. error: {response.text}",
)

if host != "traefik.me":
print("Removing the image")
subprocess.run(f"docker rmi {image_name}".split(" "))

except BuildError as e:
failed_functions.append(e)


def walk_directory(path: str, registry_host: str, dockerfile_path: str):
def walk_directory(path: str, host: str, dockerfile_path: str):
ls = os.listdir(path)

if "Dockerfile" in ls:
Expand All @@ -128,7 +140,7 @@ def walk_directory(path: str, registry_host: str, dockerfile_path: str):

if all(f in ls for f in ["main.py", "metadata.py"]) and ".hykoignore" not in ls:
all_built_functions.append(path)
process_function_dir(path, dockerfile_path, registry_host)
process_function_dir(path, dockerfile_path, host)

else:
for sub_folder in ls:
Expand All @@ -140,7 +152,7 @@ def walk_directory(path: str, registry_host: str, dockerfile_path: str):
if not os.path.isdir(sub_folder_path):
continue

walk_directory(sub_folder_path, registry_host, dockerfile_path)
walk_directory(sub_folder_path, host, dockerfile_path)


def parse_args(args: list[str]) -> argparse.Namespace:
Expand All @@ -155,9 +167,9 @@ def parse_args(args: list[str]) -> argparse.Namespace:
type=str,
)
parser.add_argument(
"--registry",
default="registry.traefik.me",
help="Set a custom registry host",
"--host",
default="traefik.me",
help="Set a custom host name",
type=str,
)

Expand All @@ -167,7 +179,7 @@ def parse_args(args: list[str]) -> argparse.Namespace:
if __name__ == "__main__":
args = parse_args(sys.argv[1:])
directories = args.dir
registry_host = args.registry
host = args.host

print("build hyko_sdk image")

Expand All @@ -179,7 +191,7 @@ def parse_args(args: list[str]) -> argparse.Namespace:
)

for dir in directories:
walk_directory(dir, registry_host, dockerfile_path=".")
walk_directory(dir, host, dockerfile_path=".")

successful_count = len(all_built_functions) - len(failed_functions)

Expand Down

0 comments on commit 2f560ee

Please sign in to comment.