From 9eed0d289689c49a1b49ce720bb9e2bcb353d8d8 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Tue, 10 Sep 2024 13:18:00 +0100 Subject: [PATCH 1/5] feat(run): create macro to encapsulate SSH logic --- e2e/docker/BUILD.bazel | 39 +++--------------------------- labgrid/run/BUILD.bazel | 1 + labgrid/run/defs.bzl | 5 ++++ labgrid/run/macro.bzl | 25 +++++++++++++++++++ {e2e/docker => labgrid/run}/ssh.py | 0 5 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 labgrid/run/BUILD.bazel create mode 100644 labgrid/run/defs.bzl create mode 100644 labgrid/run/macro.bzl rename {e2e/docker => labgrid/run}/ssh.py (100%) mode change 100755 => 100644 diff --git a/e2e/docker/BUILD.bazel b/e2e/docker/BUILD.bazel index f3a2562d..9f65c439 100644 --- a/e2e/docker/BUILD.bazel +++ b/e2e/docker/BUILD.bazel @@ -1,34 +1,9 @@ load("@rules_diff//diff/file/test:defs.bzl", "diff_file_test") load("@rules_labgrid//labgrid/config/toolchain:defs.bzl", "labgrid_config_toolchain") -load("@rules_labgrid//labgrid/genrule:defs.bzl", "labgrid_genrule") +load("@rules_labgrid//labgrid/run:defs.bzl", "labgrid_run") load("@rules_labgrid//labgrid/transition:defs.bzl", "labgrid_transition") load("@rules_python//python:defs.bzl", "py_binary") -# The goal of this test/example is to use the nut/bolts of `rules_labgrid` -# It exposes _all_ the steps to configure the `labgrid_genrule` -# Usually, higher-level macros are used -# These are _not_ hermetic to avoid implementation complexity. -# To run these rules, the following are needed on the host: -# - `docker` -# - `ssh` -# - `scp` -# The later targets are marked as "manual" so `bazel {build,test} //...` do not pick them up - -# This is *not* hermetic and requires access to `docker`/`ssh` on the host -# Useful as a test with `bazel run -- docker:ssh /path/to/target/binary argument` -# This is used later within `labgrid_genrule` -py_binary( - name = "ssh", - srcs = ["ssh.py"], - args = ["--lg-env=$(location local-ubuntu.16.04-gnu.yaml)"], - data = ["local-ubuntu.16.04-gnu.yaml"], - tags = ["manual"], - deps = [ - "@rules_labgrid//labgrid:pkg", - "@rules_labgrid//labgrid/config:deps", - ], -) - # A constraint to register the toolchain to constraint_setting(name = "device") @@ -65,21 +40,15 @@ py_binary( srcs = ["cat.py"], ) -# Run within the LabGrid environment -# This shows a few things: -# - It resolves the executor registered above to set up the `LG_ENV` enviroment variable -# - `:ssh` target uses `LG_ENV` to start the Docker container -# - `@ape//:cat` is provided to -labgrid_genrule( +labgrid_run( name = "echo", srcs = [":cat"], outs = ["stdout.log"], - cmd = "$(location :ssh) $(locations :cat) /etc/os-release > $@", + cmd = "$(locations :cat) /etc/os-release > $@", tags = ["manual"], - tools = [":ssh"], ) -# Transition the above `genrule` to the Docker platform +# Transition the above `run` to the Docker platform labgrid_transition( name = "transition", srcs = [":stdout.log"], diff --git a/labgrid/run/BUILD.bazel b/labgrid/run/BUILD.bazel new file mode 100644 index 00000000..5b744b6c --- /dev/null +++ b/labgrid/run/BUILD.bazel @@ -0,0 +1 @@ +exports_files(["ssh.py"]) diff --git a/labgrid/run/defs.bzl b/labgrid/run/defs.bzl new file mode 100644 index 00000000..ecbd6de7 --- /dev/null +++ b/labgrid/run/defs.bzl @@ -0,0 +1,5 @@ +load(":macro.bzl", run = "labgrid_run") + +visibility("public") + +labgrid_run = run diff --git a/labgrid/run/macro.bzl b/labgrid/run/macro.bzl new file mode 100644 index 00000000..1de54ad0 --- /dev/null +++ b/labgrid/run/macro.bzl @@ -0,0 +1,25 @@ +load("@rules_python//python:defs.bzl", "py_binary") +load("//labgrid/genrule:defs.bzl", "labgrid_genrule") + +visibility("//...") + +def labgrid_run(name, srcs, outs, cmd, **kwargs): + py_binary( + name = "{}.ssh".format(name), + srcs = [Label("ssh.py")], + main = "ssh.py", + tags = ["manual"], + deps = [ + Label("//labgrid/config:deps"), + Label("//labgrid:pkg"), + ], + ) + + labgrid_genrule( + name = name, + srcs = srcs, + outs = outs, + cmd = "$(location :{}.ssh) {}".format(name, cmd), + tools = [":{}.ssh".format(name)], + **kwargs + ) diff --git a/e2e/docker/ssh.py b/labgrid/run/ssh.py old mode 100755 new mode 100644 similarity index 100% rename from e2e/docker/ssh.py rename to labgrid/run/ssh.py -- GitLab From f47e6eb051849d9277189cd17980f511756bd57a Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Thu, 12 Sep 2024 10:13:38 +0100 Subject: [PATCH 2/5] refactor(run): make run binary static Previously, a new one was being created every time the macro was called. This also improves encapsulation, as we had to expose the .py before. --- labgrid/run/BUILD.bazel | 13 ++++++++++++- labgrid/run/macro.bzl | 17 +++-------------- labgrid/run/{ssh.py => run.py} | 0 3 files changed, 15 insertions(+), 15 deletions(-) rename labgrid/run/{ssh.py => run.py} (100%) diff --git a/labgrid/run/BUILD.bazel b/labgrid/run/BUILD.bazel index 5b744b6c..fe0048d7 100644 --- a/labgrid/run/BUILD.bazel +++ b/labgrid/run/BUILD.bazel @@ -1 +1,12 @@ -exports_files(["ssh.py"]) +load("@rules_python//python:defs.bzl", "py_binary") + +py_binary( + name = "run", + srcs = ["run.py"], + tags = ["manual"], + visibility = ["//visibility:public"], + deps = [ + "//labgrid:pkg", + "//labgrid/config:deps", + ], +) diff --git a/labgrid/run/macro.bzl b/labgrid/run/macro.bzl index 1de54ad0..ef18c596 100644 --- a/labgrid/run/macro.bzl +++ b/labgrid/run/macro.bzl @@ -1,25 +1,14 @@ -load("@rules_python//python:defs.bzl", "py_binary") load("//labgrid/genrule:defs.bzl", "labgrid_genrule") visibility("//...") def labgrid_run(name, srcs, outs, cmd, **kwargs): - py_binary( - name = "{}.ssh".format(name), - srcs = [Label("ssh.py")], - main = "ssh.py", - tags = ["manual"], - deps = [ - Label("//labgrid/config:deps"), - Label("//labgrid:pkg"), - ], - ) - + run = Label("//labgrid/run") labgrid_genrule( name = name, srcs = srcs, outs = outs, - cmd = "$(location :{}.ssh) {}".format(name, cmd), - tools = [":{}.ssh".format(name)], + cmd = "$(location {}) {}".format(run, cmd), + tools = [run], **kwargs ) diff --git a/labgrid/run/ssh.py b/labgrid/run/run.py similarity index 100% rename from labgrid/run/ssh.py rename to labgrid/run/run.py -- GitLab From 89cb785f05edf35276838092bb5c7fadb7ad4f07 Mon Sep 17 00:00:00 2001 From: Matthew Clarkson Date: Thu, 12 Sep 2024 14:23:48 +0000 Subject: [PATCH 3/5] refactor(run): keep run symbol private --- labgrid/run/defs.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/labgrid/run/defs.bzl b/labgrid/run/defs.bzl index ecbd6de7..011620d5 100644 --- a/labgrid/run/defs.bzl +++ b/labgrid/run/defs.bzl @@ -1,5 +1,5 @@ -load(":macro.bzl", run = "labgrid_run") +load(":macro.bzl", _run = "labgrid_run") visibility("public") -labgrid_run = run +labgrid_run = _run -- GitLab From 12dc5c657d625e2bd9e4d1d451e1d6ce3f6ffed3 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Tue, 17 Sep 2024 15:29:58 +0100 Subject: [PATCH 4/5] feat(genrule): accept optional platform attribute This will allow transitioning into the specified platform without the need to subsequently call `labgrid_transition`. --- labgrid/cfg/store.bzl | 2 +- labgrid/genrule/rule.bzl | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/labgrid/cfg/store.bzl b/labgrid/cfg/store.bzl index 15d63c40..bf810f3e 100644 --- a/labgrid/cfg/store.bzl +++ b/labgrid/cfg/store.bzl @@ -9,7 +9,7 @@ def _each(platform): } def implementation(settings, attr): - platform = settings["//command_line_option:platforms"] + platform = attr.platform or settings["//command_line_option:platforms"] if types.is_list(platform): return [_each(p) for p in platform] return _each(platform) diff --git a/labgrid/genrule/rule.bzl b/labgrid/genrule/rule.bzl index 2ba49880..9739dbdf 100644 --- a/labgrid/genrule/rule.bzl +++ b/labgrid/genrule/rule.bzl @@ -29,6 +29,10 @@ ATTRS = { mandatory = True, allow_empty = False, ), + "platform": attr.label( + doc = "Platform to transition to.", + providers = [platform_common.PlatformInfo], + ), "_executor": attr.label( doc = "The LabGrid environment executor", default = "//labgrid/executor", -- GitLab From 43294378b269348f898430e0cf80d3d260036854 Mon Sep 17 00:00:00 2001 From: Alex Tercete Date: Fri, 13 Sep 2024 14:15:59 +0100 Subject: [PATCH 5/5] feat(run): allow optional platform transition This will allow transitioning to a single platform by using the attribute or to multiple platforms by composing with `labgrid_transition`. --- e2e/docker/BUILD.bazel | 12 ++---------- labgrid/run/macro.bzl | 3 ++- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/e2e/docker/BUILD.bazel b/e2e/docker/BUILD.bazel index 9f65c439..dd0cf11a 100644 --- a/e2e/docker/BUILD.bazel +++ b/e2e/docker/BUILD.bazel @@ -1,7 +1,6 @@ load("@rules_diff//diff/file/test:defs.bzl", "diff_file_test") load("@rules_labgrid//labgrid/config/toolchain:defs.bzl", "labgrid_config_toolchain") load("@rules_labgrid//labgrid/run:defs.bzl", "labgrid_run") -load("@rules_labgrid//labgrid/transition:defs.bzl", "labgrid_transition") load("@rules_python//python:defs.bzl", "py_binary") # A constraint to register the toolchain to @@ -45,14 +44,7 @@ labgrid_run( srcs = [":cat"], outs = ["stdout.log"], cmd = "$(locations :cat) /etc/os-release > $@", - tags = ["manual"], -) - -# Transition the above `run` to the Docker platform -labgrid_transition( - name = "transition", - srcs = [":stdout.log"], - platforms = [":platform"], + platform = ":platform", tags = ["manual"], ) @@ -61,6 +53,6 @@ diff_file_test( name = "test", size = "small", a = ":version.txt", - b = ":transition", + b = ":stdout.log", tags = ["manual"], ) diff --git a/labgrid/run/macro.bzl b/labgrid/run/macro.bzl index ef18c596..be08399b 100644 --- a/labgrid/run/macro.bzl +++ b/labgrid/run/macro.bzl @@ -2,13 +2,14 @@ load("//labgrid/genrule:defs.bzl", "labgrid_genrule") visibility("//...") -def labgrid_run(name, srcs, outs, cmd, **kwargs): +def labgrid_run(name, srcs, outs, cmd, platform = None, **kwargs): run = Label("//labgrid/run") labgrid_genrule( name = name, srcs = srcs, outs = outs, cmd = "$(location {}) {}".format(run, cmd), + platform = platform, tools = [run], **kwargs ) -- GitLab