diff --git a/e2e/docker/BUILD.bazel b/e2e/docker/BUILD.bazel index dd0cf11a7ab83da1b3ca85c5db78487b857e1994..4c49090016f7ed991345903e77320f4743e5fe7f 100644 --- a/e2e/docker/BUILD.bazel +++ b/e2e/docker/BUILD.bazel @@ -26,6 +26,7 @@ labgrid_config_toolchain( name = "config", src = "local-ubuntu.16.04-gnu.yaml", manager = "@rules_labgrid//labgrid/manager:config", + state = "accessible", target_compatible_with = [ ":constraint", "@toolchain_utils//toolchain/constraint/os:linux", diff --git a/labgrid/config/BUILD.bazel b/labgrid/config/BUILD.bazel index 71e79285ac8056435f68b90f1beb405178c367bc..a131e87a37aaeea36a414c149a834f0fbe5a2304 100644 --- a/labgrid/config/BUILD.bazel +++ b/labgrid/config/BUILD.bazel @@ -1,5 +1,6 @@ -load(":src.bzl", "src") load(":deps.bzl", "deps") +load(":src.bzl", "src") +load(":state.bzl", "state") src( name = "src", @@ -12,6 +13,11 @@ alias( visibility = ["//visibility:public"], ) +state( + name = "state", + visibility = ["//visibility:public"], +) + deps( name = "deps", visibility = ["//visibility:public"], diff --git a/labgrid/config/providers.bzl b/labgrid/config/providers.bzl new file mode 100644 index 0000000000000000000000000000000000000000..6b694d3af313855e0469ea8b505c4fd60522b2b3 --- /dev/null +++ b/labgrid/config/providers.bzl @@ -0,0 +1,6 @@ +visibility("//...") + +StateInfo = provider( + doc = "A wrapper around the LabGrid state string value.", + fields = ["value"], +) diff --git a/labgrid/config/rule.bzl b/labgrid/config/rule.bzl index ced0a6f859f76db9ecdd0db06a6ec4a0f4813157..b78a34f5c37bc02c4ad2395448056470c68b61b5 100644 --- a/labgrid/config/rule.bzl +++ b/labgrid/config/rule.bzl @@ -9,6 +9,7 @@ Binds a LabGrid configuration file to the associated data required to setup, loa labgrid_config( name = "config", src = "docker.yaml", + state = "accessible", deps = ["@pip//:docker"], ) @@ -50,6 +51,9 @@ ATTRS = { doc = "The LabGrid configuration YAML.", allow_single_file = [".yml", ".yaml"], ), + "state": attr.string( + doc = "The state to transition the LabGrid strategy to.", + ), "deps": attr.label_list( doc = "LabGrid configuration Python dependencies.", providers = [[DefaultInfo, PyInfo]], @@ -67,6 +71,7 @@ def implementation(ctx): default = DefaultInfo(files = depset([ctx.file.src])) toolchain = platform_common.ToolchainInfo( src = ctx.file.src, + state = ctx.attr.state, deps = _flatten([_forward(d) for d in ctx.attr.deps]), ) diff --git a/labgrid/config/state.bzl b/labgrid/config/state.bzl new file mode 100644 index 0000000000000000000000000000000000000000..29f75ea653fc4eba241e3bc6d82bbc3d76427d0c --- /dev/null +++ b/labgrid/config/state.bzl @@ -0,0 +1,21 @@ +load("//labgrid/cfg:unstore.bzl", _cfg = "unstore") +load(":providers.bzl", "StateInfo") + +visibility("//labgrid/config/...") + +DOC = "Unpacks the `state` from the `//labgrid/toolchain/config:type` toolchain" + +ATTRS = {} + +def implementation(ctx): + toolchain = ctx.toolchains["//labgrid/toolchain/config:type"] + return StateInfo(value = toolchain.state) + +state = rule( + doc = DOC, + attrs = ATTRS, + implementation = implementation, + provides = [StateInfo], + toolchains = ["//labgrid/toolchain/config:type"], + cfg = _cfg, +) diff --git a/labgrid/config/toolchain/macro.bzl b/labgrid/config/toolchain/macro.bzl index 1ad3692dccea0a44eab8e477cbc6f8b2959c13fc..4b6221524967adcf9385195460f120dca303aabb 100644 --- a/labgrid/config/toolchain/macro.bzl +++ b/labgrid/config/toolchain/macro.bzl @@ -3,11 +3,12 @@ load("@rules_labgrid//labgrid/manager:defs.bzl", "labgrid_manager") visibility("//...") -def labgrid_config_toolchain(*, name, src, manager, target_compatible_with, deps = ()): +def labgrid_config_toolchain(*, name, src, state, manager, target_compatible_with, deps = ()): labgrid_config( name = "{}-config".format(name), src = native.package_relative_label(src), deps = [native.package_relative_label(d) for d in deps], + state = state, ) labgrid_manager( diff --git a/labgrid/executor/BUILD.bazel b/labgrid/executor/BUILD.bazel index 20db4415b09be48bfb8234422492c59671795726..992d1ac9d92625d251e38103d1d3f5228570cbac 100644 --- a/labgrid/executor/BUILD.bazel +++ b/labgrid/executor/BUILD.bazel @@ -1,11 +1,14 @@ load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test") +load(":args.bzl", "args") + +args( + name = "args", + visibility = ["//:__subpackages__"], +) py_binary( name = "executor", srcs = ["executor.py"], - env = { - "LABGRID_EXECUTOR_MANAGER": "$(location //labgrid/manager)", - }, tags = ["manual"], visibility = ["//:__subpackages__"], deps = [ @@ -31,24 +34,11 @@ py_test( args = [ "--manager", "labgrid.executor.host:manager", + "--state", + "any", "echo", "$${DATA}", ], main = "executor.py", deps = ["host"], ) - -py_test( - name = "envvar", - size = "small", - srcs = ["executor.py"], - args = [ - "echo", - "$${DATA}", - ], - env = { - "LABGRID_EXECUTOR_MANAGER": "labgrid.executor.host:manager", - }, - main = "executor.py", - deps = ["host"], -) diff --git a/labgrid/executor/args.bzl b/labgrid/executor/args.bzl new file mode 100644 index 0000000000000000000000000000000000000000..8cff45354101545142b19c9f22b1f4b05835e12a --- /dev/null +++ b/labgrid/executor/args.bzl @@ -0,0 +1,36 @@ +load("//labgrid/config:providers.bzl", "StateInfo") + +visibility("//labgrid/executor/...") + +DOC = "Creates an args file to be passed to `//labgrid/executor`" + +ATTRS = { + "_manager": attr.label( + allow_single_file = True, + doc = "The manager to use", + default = "//labgrid/manager:src", + cfg = "exec", + ), + "_state": attr.label( + doc = "The state to transition the LabGrid strategy to", + default = "//labgrid/config:state", + cfg = "exec", + ), +} + +def implementation(ctx): + output = ctx.actions.declare_file("executor-args.txt") + lines = [ + "--manager", + ctx.file._manager.short_path, + "--state", + ctx.attr._state[StateInfo].value, + ] + ctx.actions.write(output, "\n".join(lines)) + return DefaultInfo(files = depset([output])) + +args = rule( + doc = DOC, + attrs = ATTRS, + implementation = implementation, +) diff --git a/labgrid/executor/executor.py b/labgrid/executor/executor.py index 68f9cf7d86bbf15fdb6fbf3da4b766a29c61990a..ba2f1c93fcb687261f42718f77d7ad2e6ad95918 100644 --- a/labgrid/executor/executor.py +++ b/labgrid/executor/executor.py @@ -52,11 +52,10 @@ def arguments(prsr: ArgumentParser) -> None: "--manager", help="A manager to load from a Python module, of the form `module.to.load:Manager`.", type=load, - default=( - load(environ["LABGRID_EXECUTOR_MANAGER"]) - if "LABGRID_EXECUTOR_MANAGER" in environ - else None - ), + ) + prsr.add_argument( + "--state", + help="The state to transition the LabGrid strategy to.", ) @@ -85,6 +84,9 @@ def main(exe: Path, *args: str) -> int: with parsed.manager() as data: env = {k: v for k, v in data.env.items() if k != "RUNFILES_DIR"} + # TODO: Move this to a manager + if parsed.state is not None: + env |= {"LG_STATE": parsed.state} try: run(cmd, env=env, check=True) except CalledProcessError as e: diff --git a/labgrid/genrule/rule.bzl b/labgrid/genrule/rule.bzl index f692f85dff78f10a78d4496450a913a5098ed261..fd0a3de2bec336cd2f9328f3a17ea718326af269 100644 --- a/labgrid/genrule/rule.bzl +++ b/labgrid/genrule/rule.bzl @@ -1,5 +1,5 @@ -load("//labgrid/cfg:store.bzl", _cfg = "store") load("@bazel_skylib//lib:shell.bzl", "shell") +load("//labgrid/cfg:store.bzl", _cfg = "store") visibility("//...") @@ -19,7 +19,7 @@ ATTRS = { "srcs": attr.label_list( doc = "A list of inputs for this rule, such as files to process.", cfg = "target", - allow_files = True + allow_files = True, ), "tools": attr.label_list( doc = "A list of tools for this rule, that will run on the executor.", @@ -40,6 +40,12 @@ ATTRS = { executable = True, cfg = "exec", ), + "_executor_args": attr.label( + allow_single_file = True, + doc = "CLI args for the Labgrid environment executor", + default = "//labgrid/executor:args", + cfg = "exec", + ), "_sh": attr.label( doc = "The Shell interpreter", default = "@ape//:bash", @@ -79,6 +85,7 @@ def implementation(ctx): cmd = cmd.replace(key, value) args = ctx.actions.args() + args.add(ctx.file._executor_args, format = "@%s") args.add("--") args.add(ctx.executable._sh) args.add("-c") @@ -87,6 +94,7 @@ def implementation(ctx): ctx.actions.run( executable = ctx.executable._executor, arguments = [args], + inputs = [ctx.file._executor_args], outputs = ctx.outputs.outs, tools = [t.files_to_run for t in (ctx.attr.tools + ctx.attr.srcs)] + [ctx.executable._sh], env = ctx.attr._executor[RunEnvironmentInfo].environment, diff --git a/labgrid/run/run.py b/labgrid/run/run.py index 0c1ff803c8bee654ae07483eaa7ca8aab5e9effb..44e157bb68c9a2c237ad7fe4f49a9e117a7fbb3f 100644 --- a/labgrid/run/run.py +++ b/labgrid/run/run.py @@ -25,12 +25,16 @@ def arguments(prsr: ArgumentParser) -> None: "arguments", metavar="ARG", nargs="*", help="Command to run over SSH." ) prsr.add_argument( - "--lg-env", - help="The LabGrid environment configuration.", - dest="config", - default=PurePath(environ.get("LG_ENV", "config.yaml")), + "--config", + help="The LabGrid configuration.", + default=PurePath(environ["LG_ENV"]), type=PurePath, ) + prsr.add_argument( + "--state", + help="The state to transition the LabGrid strategy to", + default=environ["LG_STATE"], + ) def main(exe: Path, *args: str) -> int: @@ -45,8 +49,8 @@ def main(exe: Path, *args: str) -> int: # Start up Docker container with LabGrid env = Environment(str(parsed.config)) target = env.get_target() - strategy = target.get_driver("DockerStrategy") - strategy.transition("accessible") + strategy = target.get_driver("Strategy") + strategy.transition(parsed.state) # Retrieve the communication protocols shell = target.get_driver("CommandProtocol") diff --git a/labgrid/run_binary/rule.bzl b/labgrid/run_binary/rule.bzl index 4f682b976f6025338fa7b4c9bb1b92c4630de15f..f46d02925c9f14c583fa3b69eb7acad1399685d6 100644 --- a/labgrid/run_binary/rule.bzl +++ b/labgrid/run_binary/rule.bzl @@ -7,10 +7,10 @@ DOC = """Executes a binary on the host within a LabGrid environment.""" ATTRS = { "tool": attr.label( doc = "The tool to run in the action.\n\nMust be the label of a *_binary rule," + - " of a rule that generates an executable file, or of a file that can be" + - " executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary" + - " with executable permission on Linux). This label is available for" + - " `$(execpath)` and `$(location)` expansion in `args` and `env`.", + " of a rule that generates an executable file, or of a file that can be" + + " executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary" + + " with executable permission on Linux). This label is available for" + + " `$(execpath)` and `$(location)` expansion in `args` and `env`.", executable = True, allow_files = True, mandatory = True, @@ -18,23 +18,23 @@ ATTRS = { ), "env": attr.string_dict( doc = "Environment variables of the action.\n\nSubject to " + - " [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" + - " expansion.", + " [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" + + " expansion.", ), "srcs": attr.label_list( allow_files = True, doc = "Additional inputs of the action.\n\nThese labels are available for" + - " `$(execpath)` and `$(location)` expansion in `args` and `env`.", + " `$(execpath)` and `$(location)` expansion in `args` and `env`.", ), "outs": attr.output_list( mandatory = True, doc = "Output files generated by the action.\n\nThese labels are available for" + - " `$(execpath)` and `$(location)` expansion in `args` and `env`.", + " `$(execpath)` and `$(location)` expansion in `args` and `env`.", ), "args": attr.string_list( doc = "Command line arguments of the binary.\n\nSubject to" + - " [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" + - " expansion.", + " [`$(execpath)` and `$(location)`](https://bazel.build/reference/be/make-variables#predefined_label_variables)" + + " expansion.", ), "platform": attr.label( doc = "Platform to transition to.", @@ -45,7 +45,13 @@ ATTRS = { default = "//labgrid/executor", executable = True, cfg = "exec", - ) + ), + "_executor_args": attr.label( + allow_single_file = True, + doc = "CLI args for the Labgrid environment executor", + default = "//labgrid/executor:args", + cfg = "exec", + ), } def implementation(ctx): @@ -71,13 +77,14 @@ def implementation(ctx): } args = ctx.actions.args() + args.add(ctx.file._executor_args, format = "@%s") args.add("--") args.add(ctx.executable.tool) args.add_all(expanded_args) ctx.actions.run( outputs = ctx.outputs.outs, - inputs = ctx.files.srcs, + inputs = ctx.files.srcs + [ctx.file._executor_args], tools = [ctx.executable.tool], executable = ctx.executable._executor, arguments = [args],