diff --git a/skeleton/worker/BUILD.bazel b/skeleton/worker/BUILD.bazel index 03214c5e..766b48db 100644 --- a/skeleton/worker/BUILD.bazel +++ b/skeleton/worker/BUILD.bazel @@ -1,6 +1,5 @@ load("@rules_python//python:defs.bzl", "py_binary") -load("//tools/build_rules:cross_platform_image.bzl", "cross_platform_image") -load("//tools/build_rules:py_layer.bzl", "py_oci_image") +load("//tools/build_rules/worker:defs.bzl", "worker") py_binary( name = "binary", @@ -11,44 +10,9 @@ py_binary( deps = ["//skeleton:config_py"], ) -py_oci_image( - name = "base_image", - base = "@python3_image", - binaries = [ - "//scripts:wait_for_postgres", - ":binary", - ], - cmd = [ - "/" + package_name() + "/binary", - ], - entrypoint = [ - "/scripts/wait_for_postgres", - ], - env = { - "FLASK_APP": "app.py", - "FLASK_DEBUG": "True", - "API_PORT": "5000", - "FRONTEND_PROTOCOL": "http", - "FRONTEND_HOST": "frontend", - "FRONTEND_PORT": "5001", - "DB_HOST": "pg", - "DB_USERNAME": "admin", - "DB_PASSWORD": "development", - "DATABASE_NAME": "api_development", - "FITBIT_CLIENT_ID": "testing", - "FITBIT_CLIENT_SECRET": "testing", - "FITBIT_VERIFICATION_CODE": "testing", - "FLASK_SECRET_KEY": "testing", - }, - tags = ["manual"], -) - -# $ bazel run //skeleton/worker:image_tarball -# $ docker run --rm shaldengeki/skeleton-worker:latest -cross_platform_image( - name = "image", - image = ":base_image", +worker( + name = "worker", + binary = ":binary", + docker_hub_repository = "docker.io/shaldengeki/skeleton-worker", repo_tags = ["shaldengeki/skeleton-worker:latest"], - repository = "docker.io/shaldengeki/skeleton-worker", - visibility = ["//skeleton/worker:__subpackages__"], ) diff --git a/tools/build_rules/worker/BUILD.bazel b/tools/build_rules/worker/BUILD.bazel new file mode 100644 index 00000000..e69de29b diff --git a/tools/build_rules/worker/defs.bzl b/tools/build_rules/worker/defs.bzl new file mode 100644 index 00000000..927aeb23 --- /dev/null +++ b/tools/build_rules/worker/defs.bzl @@ -0,0 +1,9 @@ +""" +defs.bzl: public interfaces for worker rules. + +You should import rules exposed here. +""" + +load("//tools/build_rules/worker:worker.bzl", _worker = "worker") + +worker = _worker diff --git a/tools/build_rules/worker/worker.bzl b/tools/build_rules/worker/worker.bzl new file mode 100644 index 00000000..61c148a1 --- /dev/null +++ b/tools/build_rules/worker/worker.bzl @@ -0,0 +1,78 @@ +""" + +worker.bzl: Defines the standard worker application used across the monorepo. + +""" + +load("//tools/build_rules:cross_platform_image.bzl", "cross_platform_image") +load("//tools/build_rules:py_layer.bzl", "py_oci_image") + +def worker( + name, + binary, + repo_tags, + docker_hub_repository, + env = None, + visibility = None): + """ + Defines the standard worker application, including container images. + + Args: + name (str): Name to use as a prefix to generated rules. + binary (Label): py_binary target that should be the entrypoint of the worker. + repo_tags (list[str]): List of tags to apply to the container. See cross_platform_image. + docker_hub_repository (str): URL of the dockerhub repository to push to. See cross_platform_image. + env (dict[str, str]): Additional environment variables to set in the Python image. See py_oci_image. + visibility: The default visibility to set on the generated rules. Defaults to public. + """ + + binary = Label(binary) + + if env == None: + env = {} + + all_env = { + "FLASK_APP": "app.py", + "FLASK_DEBUG": "True", + "API_PORT": "5000", + "FRONTEND_PROTOCOL": "http", + "FRONTEND_HOST": "frontend", + "FRONTEND_PORT": "5001", + "DB_HOST": "pg", + "DB_USERNAME": "admin", + "DB_PASSWORD": "development", + "DATABASE_NAME": "api_development", + "FLASK_SECRET_KEY": "testing", + } + all_env.update(env) + + if visibility == None: + visibility = ["//visibility:public"] + + py_oci_image( + name = name + "_base_image", + base = "@python3_image", + binaries = [ + "//scripts:wait_for_postgres", + binary, + ], + cmd = [ + "/" + binary.package + "/" + binary.name, + ], + entrypoint = [ + "/scripts/wait_for_postgres", + ], + env = all_env, + visibility = visibility, + tags = ["manual"], + ) + + # $ bazel run //skeleton/worker:worker_image_tarball + # $ docker run --rm shaldengeki/skeleton-worker:latest + cross_platform_image( + name = name + "_image", + image = ":base_image", + repo_tags = repo_tags, + repository = docker_hub_repository, + visibility = visibility, + )