phase 1 - add service targets
This commit is contained in:
parent
5fa84d9d7f
commit
6de73b074a
3 changed files with 1276 additions and 24 deletions
|
|
@ -271,23 +271,51 @@ def databuild_graph(name, jobs, lookup, visibility = None):
|
|||
jobs = jobs,
|
||||
visibility = visibility,
|
||||
)
|
||||
# Build deployment targets (renamed for hierarchical namespacing)
|
||||
tar(
|
||||
name = "%s.tar" % name,
|
||||
name = "%s.build.tar" % name,
|
||||
srcs = [":%s.build" % name],
|
||||
visibility = visibility,
|
||||
)
|
||||
oci_image(
|
||||
name = "%s.image" % name,
|
||||
name = "%s.build.image" % name,
|
||||
base = "@debian",
|
||||
cmd = ["/%s.build" % name],
|
||||
tars = [":%s.tar" % name],
|
||||
tars = [":%s.build.tar" % name],
|
||||
visibility = visibility,
|
||||
)
|
||||
oci_load(
|
||||
name = "%s.load" % name,
|
||||
image = ":%s.image" % name,
|
||||
name = "%s.build.image_load" % name,
|
||||
image = ":%s.build.image" % name,
|
||||
visibility = visibility,
|
||||
repo_tags = ["databuild_%s:latest" % name],
|
||||
repo_tags = ["databuild_%s_build:latest" % name],
|
||||
)
|
||||
|
||||
# Service targets
|
||||
_databuild_graph_service(
|
||||
name = "%s.service" % name,
|
||||
lookup = "%s.lookup" % name,
|
||||
jobs = jobs,
|
||||
graph_label = "//%s:%s" % (native.package_name(), name),
|
||||
visibility = visibility,
|
||||
)
|
||||
tar(
|
||||
name = "%s.service.tar" % name,
|
||||
srcs = [":%s.service" % name],
|
||||
visibility = visibility,
|
||||
)
|
||||
oci_image(
|
||||
name = "%s.service.image" % name,
|
||||
base = "@debian",
|
||||
cmd = ["/%s.service" % name],
|
||||
tars = [":%s.service.tar" % name],
|
||||
visibility = visibility,
|
||||
)
|
||||
oci_load(
|
||||
name = "%s.service.image_load" % name,
|
||||
image = ":%s.service.image" % name,
|
||||
visibility = visibility,
|
||||
repo_tags = ["databuild_%s_service:latest" % name],
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -651,3 +679,107 @@ _databuild_graph_build = rule(
|
|||
},
|
||||
executable = True,
|
||||
)
|
||||
|
||||
def _databuild_graph_service_impl(ctx):
|
||||
"""Implementation of the service target that runs the Build Graph Service."""
|
||||
script = ctx.actions.declare_file(ctx.label.name)
|
||||
|
||||
# Build job configurations mapping for DATABUILD_CANDIDATE_JOBS
|
||||
config_paths = {
|
||||
"//" + job.label.package + ":" + job.label.name:
|
||||
"$(rlocation _main/" + job[DataBuildJobInfo].configure.files_to_run.executable.short_path + ")"
|
||||
for job in ctx.attr.jobs
|
||||
}
|
||||
config_paths_str = "{" + ",".join(['\\"%s\\":\\"%s\\"' % (k, v) for k, v in config_paths.items()]) + "}"
|
||||
|
||||
# Default service configuration
|
||||
default_port = "8080"
|
||||
default_db = "sqlite:///tmp/%s.db" % ctx.label.name.replace(".", "_")
|
||||
|
||||
env_setup = """
|
||||
export DATABUILD_CANDIDATE_JOBS="{candidate_jobs}"
|
||||
export DATABUILD_JOB_LOOKUP_PATH=$(rlocation _main/{lookup_path})
|
||||
|
||||
# Default service arguments (can be overridden by user)
|
||||
DEFAULT_ARGS="--port {port} --event-log {db} --graph-label {graph_label} --job-lookup-path $(rlocation _main/{lookup_path})"
|
||||
|
||||
# Use user args if provided, otherwise use defaults
|
||||
if [ "$#" -eq 0 ]; then
|
||||
set -- $DEFAULT_ARGS
|
||||
fi
|
||||
""".format(
|
||||
candidate_jobs = config_paths_str,
|
||||
lookup_path = ctx.attr.lookup.files_to_run.executable.short_path,
|
||||
port = default_port,
|
||||
db = default_db,
|
||||
graph_label = ctx.attr.graph_label,
|
||||
)
|
||||
|
||||
ctx.actions.expand_template(
|
||||
template = ctx.file._template,
|
||||
output = script,
|
||||
substitutions = {
|
||||
"%{EXECUTABLE_PATH}": ctx.attr._service.files_to_run.executable.path,
|
||||
"%{RUNFILES_PREFIX}": RUNFILES_PREFIX,
|
||||
"%{PREFIX}": env_setup,
|
||||
},
|
||||
is_executable = True,
|
||||
)
|
||||
|
||||
# Gather all dependencies for runfiles
|
||||
configure_executables = [
|
||||
job[DataBuildJobInfo].configure.files_to_run.executable
|
||||
for job in ctx.attr.jobs
|
||||
]
|
||||
|
||||
runfiles = ctx.runfiles(
|
||||
files = [ctx.executable.lookup, ctx.executable._service] + configure_executables,
|
||||
).merge(ctx.attr.lookup.default_runfiles).merge(ctx.attr._service.default_runfiles).merge(
|
||||
ctx.attr._bash_runfiles.default_runfiles
|
||||
).merge_all([job.default_runfiles for job in ctx.attr.jobs])
|
||||
|
||||
# Merge runfiles from all configure targets
|
||||
for job in ctx.attr.jobs:
|
||||
configure_target = job[DataBuildJobInfo].configure
|
||||
runfiles = runfiles.merge(configure_target.default_runfiles)
|
||||
|
||||
return [
|
||||
DefaultInfo(
|
||||
executable = script,
|
||||
runfiles = runfiles,
|
||||
),
|
||||
]
|
||||
|
||||
_databuild_graph_service = rule(
|
||||
implementation = _databuild_graph_service_impl,
|
||||
attrs = {
|
||||
"lookup": attr.label(
|
||||
doc = "Target that implements job lookup for desired partition refs",
|
||||
mandatory = True,
|
||||
executable = True,
|
||||
cfg = "target",
|
||||
),
|
||||
"jobs": attr.label_list(
|
||||
doc = "The list of jobs that are candidates for building partitions in this databuild graph",
|
||||
allow_empty = False,
|
||||
),
|
||||
"graph_label": attr.string(
|
||||
doc = "The label of this graph for service identification",
|
||||
mandatory = True,
|
||||
),
|
||||
"_template": attr.label(
|
||||
default = "@databuild//databuild/runtime:simple_executable_wrapper.sh.tpl",
|
||||
allow_single_file = True,
|
||||
),
|
||||
"_bash_runfiles": attr.label(
|
||||
default = Label("@bazel_tools//tools/bash/runfiles"),
|
||||
allow_files = True,
|
||||
),
|
||||
"_service": attr.label(
|
||||
default = "@databuild//databuild:build_graph_service",
|
||||
executable = True,
|
||||
cfg = "target",
|
||||
),
|
||||
},
|
||||
executable = True,
|
||||
)
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in a new issue