blob: 986eeca9f7f674ae05d6b44eddcb85278d650022 [file] [log] [blame]
Lorenz Brune6573102021-11-02 14:15:37 +01001package main
2
3import (
Serge Bazanski97783222021-12-14 16:04:26 +01004 "bytes"
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +02005 "context"
Lorenz Brune6573102021-11-02 14:15:37 +01006 "crypto/ed25519"
Lorenz Brun7a510192022-07-04 15:31:38 +00007 _ "embed"
Lorenz Brun7a510192022-07-04 15:31:38 +00008 "io"
Lorenz Brune6573102021-11-02 14:15:37 +01009 "log"
10 "os"
Serge Bazanskibdc803b2023-03-27 10:55:09 +020011 "strings"
Lorenz Brune6573102021-11-02 14:15:37 +010012
Lorenz Brune6573102021-11-02 14:15:37 +010013 "github.com/spf13/cobra"
14
15 "source.monogon.dev/metropolis/cli/metroctl/core"
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020016 clicontext "source.monogon.dev/metropolis/cli/pkg/context"
Serge Bazanski97783222021-12-14 16:04:26 +010017 "source.monogon.dev/metropolis/cli/pkg/datafile"
Lorenz Brune6573102021-11-02 14:15:37 +010018 "source.monogon.dev/metropolis/proto/api"
Serge Bazanskibdc803b2023-03-27 10:55:09 +020019 cpb "source.monogon.dev/metropolis/proto/common"
Lorenz Brune6573102021-11-02 14:15:37 +010020)
21
22var installCmd = &cobra.Command{
Lorenz Brun7a510192022-07-04 15:31:38 +000023 Short: "Contains subcommands to install Metropolis via different media.",
Lorenz Brune6573102021-11-02 14:15:37 +010024 Use: "install",
25}
26
Lorenz Brun7a510192022-07-04 15:31:38 +000027var bundlePath = installCmd.PersistentFlags().StringP("bundle", "b", "", "Path to the Metropolis bundle to be installed")
28
Lorenz Brune6573102021-11-02 14:15:37 +010029var genusbCmd = &cobra.Command{
Serge Bazanski97783222021-12-14 16:04:26 +010030 Use: "genusb target",
Lorenz Brune6573102021-11-02 14:15:37 +010031 Short: "Generates a Metropolis installer disk or image.",
Lorenz Brun7a510192022-07-04 15:31:38 +000032 Example: "metroctl install --bundle=metropolis-v0.1.zip genusb /dev/sdx",
Lorenz Brune6573102021-11-02 14:15:37 +010033 Args: cobra.ExactArgs(1), // One positional argument: the target
34 Run: doGenUSB,
35}
36
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020037// bootstrap is a flag controlling node parameters included in the installer
38// image. If set, the installed node will bootstrap a new cluster. Otherwise,
39// it will try to connect to the cluster which endpoints were provided with
40// the --endpoints flag.
41var bootstrap bool
42
Serge Bazanskibdc803b2023-03-27 10:55:09 +020043var bootstrapTPMMode string
Tim Windelschmidtc09327c2023-06-14 17:48:56 +020044var bootstrapStorageSecurityPolicy string
Serge Bazanskibdc803b2023-03-27 10:55:09 +020045
Lorenz Brun7a510192022-07-04 15:31:38 +000046//go:embed metropolis/installer/kernel.efi
47var installer []byte
48
Lorenz Brune6573102021-11-02 14:15:37 +010049func doGenUSB(cmd *cobra.Command, args []string) {
Serge Bazanskibdc803b2023-03-27 10:55:09 +020050 var tpmMode cpb.ClusterConfiguration_TPMMode
51 switch strings.ToLower(bootstrapTPMMode) {
52 case "required", "require":
53 tpmMode = cpb.ClusterConfiguration_TPM_MODE_REQUIRED
54 case "best-effort", "besteffort":
55 tpmMode = cpb.ClusterConfiguration_TPM_MODE_BEST_EFFORT
56 case "disabled", "disable":
57 tpmMode = cpb.ClusterConfiguration_TPM_MODE_DISABLED
58 default:
59 log.Fatalf("Invalid --bootstrap-tpm-mode (must be one of: required, best-effort, disabled)")
60 }
61
Tim Windelschmidtc09327c2023-06-14 17:48:56 +020062 var bootstrapStorageSecurity cpb.ClusterConfiguration_StorageSecurityPolicy
63 switch strings.ToLower(bootstrapStorageSecurityPolicy) {
64 case "permissive":
65 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_PERMISSIVE
66 case "needs-encryption":
67 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION
68 case "needs-encryption-and-authentication":
69 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION_AND_AUTHENTICATION
70 case "needs-insecure":
71 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_INSECURE
72 default:
73
74 log.Fatalf("Invalid --bootstrap-storage-security (must be one of: permissive, needs-encryption, needs-encryption-and-authentication, needs-insecure)")
75 }
76
Lorenz Brun7a510192022-07-04 15:31:38 +000077 var bundleReader io.Reader
78 var bundleSize uint64
79 if bundlePath == nil || *bundlePath == "" {
80 // Attempt Bazel runfile bundle if not explicitly set
81 bundle, err := datafile.Get("metropolis/node/bundle.zip")
82 if err != nil {
83 log.Fatalf("No bundle specified and fallback to runfiles failed: %v", err)
84 }
85 bundleReader = bytes.NewReader(bundle)
86 bundleSize = uint64(len(bundle))
87 } else {
88 // Load bundle from specified path
89 bundle, err := os.Open(*bundlePath)
90 if err != nil {
91 log.Fatalf("Failed to open specified bundle: %v", err)
92 }
93 bundleStat, err := bundle.Stat()
94 if err != nil {
95 log.Fatalf("Failed to stat specified bundle: %v", err)
96 }
97 bundleReader = bundle
98 bundleSize = uint64(bundleStat.Size())
99 }
Lorenz Brune6573102021-11-02 14:15:37 +0100100
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200101 ctx := clicontext.WithInterrupt(context.Background())
102
Lorenz Brune6573102021-11-02 14:15:37 +0100103 // TODO(lorenz): Have a key management story for this
Mateusz Zalega8234c162022-07-08 17:05:50 +0200104 if err := os.MkdirAll(flags.configPath, 0700); err != nil && !os.IsExist(err) {
Lorenz Brune6573102021-11-02 14:15:37 +0100105 log.Fatalf("Failed to create config directory: %v", err)
106 }
Lorenz Brune6573102021-11-02 14:15:37 +0100107
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200108 var params *api.NodeParameters
109 if bootstrap {
Serge Bazanskicf23ebc2023-03-14 17:02:04 +0100110 priv, err := core.GetOrMakeOwnerKey(flags.configPath)
111 if err != nil {
112 log.Fatalf("Failed to generate or get owner key: %v", err)
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200113 }
Serge Bazanskicf23ebc2023-03-14 17:02:04 +0100114 pub := priv.Public().(ed25519.PublicKey)
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200115 params = &api.NodeParameters{
116 Cluster: &api.NodeParameters_ClusterBootstrap_{
117 ClusterBootstrap: &api.NodeParameters_ClusterBootstrap{
Serge Bazanskicf23ebc2023-03-14 17:02:04 +0100118 OwnerPublicKey: pub,
Serge Bazanskibdc803b2023-03-27 10:55:09 +0200119 InitialClusterConfiguration: &cpb.ClusterConfiguration{
Tim Windelschmidtc09327c2023-06-14 17:48:56 +0200120 StorageSecurityPolicy: bootstrapStorageSecurity,
121 TpmMode: tpmMode,
Serge Bazanskibdc803b2023-03-27 10:55:09 +0200122 },
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200123 },
Lorenz Brune6573102021-11-02 14:15:37 +0100124 },
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200125 }
126 } else {
Mateusz Zalegadb75e212022-08-04 17:31:34 +0200127 cc := dialAuthenticated(ctx)
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200128 mgmt := api.NewManagementClient(cc)
129 resT, err := mgmt.GetRegisterTicket(ctx, &api.GetRegisterTicketRequest{})
130 if err != nil {
131 log.Fatalf("While receiving register ticket: %v", err)
132 }
133 resI, err := mgmt.GetClusterInfo(ctx, &api.GetClusterInfoRequest{})
134 if err != nil {
135 log.Fatalf("While receiving cluster directory: %v", err)
136 }
137
138 params = &api.NodeParameters{
139 Cluster: &api.NodeParameters_ClusterRegister_{
140 ClusterRegister: &api.NodeParameters_ClusterRegister{
141 RegisterTicket: resT.Ticket,
142 ClusterDirectory: resI.ClusterDirectory,
143 CaCertificate: resI.CaCertificate,
144 },
145 },
146 }
Lorenz Brune6573102021-11-02 14:15:37 +0100147 }
148
149 installerImageArgs := core.MakeInstallerImageArgs{
150 TargetPath: args[0],
Lorenz Brun7a510192022-07-04 15:31:38 +0000151 Installer: bytes.NewReader(installer),
Serge Bazanski97783222021-12-14 16:04:26 +0100152 InstallerSize: uint64(len(installer)),
Lorenz Brune6573102021-11-02 14:15:37 +0100153 NodeParams: params,
Lorenz Brun7a510192022-07-04 15:31:38 +0000154 Bundle: bundleReader,
155 BundleSize: bundleSize,
Lorenz Brune6573102021-11-02 14:15:37 +0100156 }
157
Serge Bazanski97783222021-12-14 16:04:26 +0100158 log.Printf("Generating installer image (this can take a while, see issues/92).")
Lorenz Brune6573102021-11-02 14:15:37 +0100159 if err := core.MakeInstallerImage(installerImageArgs); err != nil {
160 log.Fatalf("Failed to create installer: %v", err)
161 }
162}
163
164func init() {
165 rootCmd.AddCommand(installCmd)
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200166
167 genusbCmd.Flags().BoolVar(&bootstrap, "bootstrap", false, "Create a bootstrap installer image.")
Serge Bazanskibdc803b2023-03-27 10:55:09 +0200168 genusbCmd.Flags().StringVar(&bootstrapTPMMode, "bootstrap-tpm-mode", "required", "TPM mode to set on cluster (required, best-effort, disabled)")
Tim Windelschmidtc09327c2023-06-14 17:48:56 +0200169 genusbCmd.Flags().StringVar(&bootstrapStorageSecurityPolicy, "bootstrap-storage-security", "needs-encryption-and-authentication", "Storage security policy to set on cluster (permissive, needs-encryption, needs-encryption-and-authentication, needs-insecure)")
Lorenz Brune6573102021-11-02 14:15:37 +0100170 installCmd.AddCommand(genusbCmd)
Lorenz Brune6573102021-11-02 14:15:37 +0100171}