m/c/metroctl: use in-tree artifacts if called using bazel run

This allows for not specifying a bundle or installer and instead
taking them from the current build if running under bazel run.
So bazel run //metropolis/cli/metroctl -- install will automatically
build and use the bundle and installer. This just changes the defaults,
if you manually specify installer and/or bundle these are still
respected.

Change-Id: I0676c9b28308544712c06881ad6ace2dba4cab2c
Reviewed-on: https://review.monogon.dev/c/monogon/+/425
Reviewed-by: Leopold Schabel <leo@nexantic.com>
diff --git a/metropolis/cli/metroctl/BUILD.bazel b/metropolis/cli/metroctl/BUILD.bazel
index fbe2fea..eb09e96 100644
--- a/metropolis/cli/metroctl/BUILD.bazel
+++ b/metropolis/cli/metroctl/BUILD.bazel
@@ -6,6 +6,10 @@
         "install.go",
         "main.go",
     ],
+    data = [
+        "//metropolis/node",
+        "//metropolis/node/installer:kernel",
+    ],
     importpath = "source.monogon.dev/metropolis/cli/metroctl",
     visibility = ["//visibility:private"],
     deps = [
diff --git a/metropolis/cli/metroctl/install.go b/metropolis/cli/metroctl/install.go
index 3970e78..10fc9f5 100644
--- a/metropolis/cli/metroctl/install.go
+++ b/metropolis/cli/metroctl/install.go
@@ -32,10 +32,25 @@
 	Run:     doGenUSB,
 }
 
+// If useInTreeArtifacts is true metroctl should use a bundle and installer
+// directly from the build tree. It is automatically set to true if metroctl is
+// running under bazel run. Specifying either one manually still overrides
+// the in-tree artifacts.
+var useInTreeArtifacts = os.Getenv("BUILD_WORKSPACE_DIRECTORY") != ""
+
+var inTreeInstaller = "metropolis/node/installer/kernel.efi"
+var inTreeBundle = "metropolis/node/node.zip"
+
 // A PEM block type for a Metropolis initial owner private key
 const ownerKeyType = "METROPOLIS INITIAL OWNER PRIVATE KEY"
 
 func doGenUSB(cmd *cobra.Command, args []string) {
+	if useInTreeArtifacts && *installer == "" {
+		installer = &inTreeInstaller
+	}
+	if useInTreeArtifacts && *bundle == "" {
+		bundle = &inTreeBundle
+	}
 	installerFile, err := os.Open(*installer)
 	if err != nil {
 		log.Fatalf("Failed to open installer: %v", err)
diff --git a/metropolis/node/BUILD.bazel b/metropolis/node/BUILD.bazel
index 8e7ea95..e9a6f12 100644
--- a/metropolis/node/BUILD.bazel
+++ b/metropolis/node/BUILD.bazel
@@ -108,6 +108,7 @@
         ":kernel_efi",
         ":rootfs",
     ],
+    visibility = ["//visibility:public"],
 )
 
 genrule(