m: {cli,installer}: runfiles through datafile.MustGet
This implements datafile, a small library to more ergonomically resolve
Bazel runfiles:
1. It also works in cases where a tool doesn't run through `bazel run`.
2. It provides a MustGet wrapper which returns already read bytes and
removes some boilerplate at the callsite.
3. It allows us to extend the library in the future to prepare special
'self-contained' builds of some binaries, for example to bundle the
installer kernel in metroctl.
We then use this library to simplify the installer and installer tests.
In the installer, we technically remove the ability to specify arbitrary
kernels/bundles on the command line, but is this functionality actually
useful?
Change-Id: I46155b9951729c810e0d36930b470edfdfd82943
Reviewed-on: https://review.monogon.dev/c/monogon/+/484
Reviewed-by: Mateusz Zalega <mateusz@monogon.tech>
diff --git a/metropolis/cli/metroctl/BUILD.bazel b/metropolis/cli/metroctl/BUILD.bazel
index 6a311c8..f9b8b51 100644
--- a/metropolis/cli/metroctl/BUILD.bazel
+++ b/metropolis/cli/metroctl/BUILD.bazel
@@ -16,6 +16,7 @@
deps = [
"//metropolis/cli/metroctl/core:go_default_library",
"//metropolis/cli/pkg/context:go_default_library",
+ "//metropolis/cli/pkg/datafile:go_default_library",
"//metropolis/node:go_default_library",
"//metropolis/node/core/rpc:go_default_library",
"//metropolis/proto/api:go_default_library",
diff --git a/metropolis/cli/metroctl/install.go b/metropolis/cli/metroctl/install.go
index 10fc9f5..d2ca453 100644
--- a/metropolis/cli/metroctl/install.go
+++ b/metropolis/cli/metroctl/install.go
@@ -1,6 +1,7 @@
package main
import (
+ "bytes"
"crypto/ed25519"
"crypto/rand"
"encoding/pem"
@@ -12,6 +13,7 @@
"github.com/spf13/cobra"
"source.monogon.dev/metropolis/cli/metroctl/core"
+ "source.monogon.dev/metropolis/cli/pkg/datafile"
"source.monogon.dev/metropolis/proto/api"
)
@@ -20,57 +22,20 @@
Use: "install",
}
-// install flags
-var installer *string
-var bundle *string
-
var genusbCmd = &cobra.Command{
- Use: "genusb target --installer=inst.efi --bundle=bundle.bin",
+ Use: "genusb target",
Short: "Generates a Metropolis installer disk or image.",
- Example: "metroctl install genusb /dev/sdx --installer=installer_x86_64.efi --bundle=metropolis_dev_x86_64.tar.xz",
+ Example: "metroctl install genusb /dev/sdx",
Args: cobra.ExactArgs(1), // One positional argument: the target
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)
- }
- installerFileStat, err := installerFile.Stat()
- if err != nil {
- log.Fatalf("Failed to stat installer: %v", err)
- }
- var bundleFile *os.File
- var bundleFileStat os.FileInfo
- if bundle != nil && *bundle != "" {
- bundleFile, err = os.Open(*bundle)
- if err != nil {
- log.Fatalf("Failed to open bundle: %v", err)
- }
- bundleFileStat, err = bundleFile.Stat()
- if err != nil {
- log.Fatalf("Failed to stat bundle: %v", err)
- }
- }
+ installer := datafile.MustGet("metropolis/node/installer/kernel.efi")
+ bundle := datafile.MustGet("metropolis/node/node.zip")
// TODO(lorenz): Have a key management story for this
if err := os.MkdirAll(filepath.Join(xdg.ConfigHome, "metroctl"), 0700); err != nil {
@@ -118,16 +83,14 @@
installerImageArgs := core.MakeInstallerImageArgs{
TargetPath: args[0],
- Installer: installerFile,
- InstallerSize: uint64(installerFileStat.Size()),
+ Installer: bytes.NewBuffer(installer),
+ InstallerSize: uint64(len(installer)),
NodeParams: params,
+ Bundle: bytes.NewBuffer(bundle),
+ BundleSize: uint64(len(bundle)),
}
- if bundleFile != nil {
- installerImageArgs.Bundle = bundleFile
- installerImageArgs.BundleSize = uint64(bundleFileStat.Size())
- }
-
+ log.Printf("Generating installer image (this can take a while, see issues/92).")
if err := core.MakeInstallerImage(installerImageArgs); err != nil {
log.Fatalf("Failed to create installer: %v", err)
}
@@ -136,7 +99,4 @@
func init() {
rootCmd.AddCommand(installCmd)
installCmd.AddCommand(genusbCmd)
-
- bundle = installCmd.PersistentFlags().StringP("bundle", "b", "", "Metropolis bundle file to use")
- installer = installCmd.PersistentFlags().StringP("installer", "i", "", "Metropolis installer file to use")
}