Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro up worker in skeleton #330

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 5 additions & 41 deletions skeleton/worker/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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__"],
)
Empty file.
9 changes: 9 additions & 0 deletions tools/build_rules/worker/defs.bzl
Original file line number Diff line number Diff line change
@@ -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
78 changes: 78 additions & 0 deletions tools/build_rules/worker/worker.bzl
Original file line number Diff line number Diff line change
@@ -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,
)