blob: 39790f1ef7ce95eea5166459ad6e7017030bf49e [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 Brunad131882023-06-28 16:42:20 +020018 "source.monogon.dev/metropolis/pkg/blkio"
19 "source.monogon.dev/metropolis/pkg/fat32"
Lorenz Brune6573102021-11-02 14:15:37 +010020 "source.monogon.dev/metropolis/proto/api"
Serge Bazanskibdc803b2023-03-27 10:55:09 +020021 cpb "source.monogon.dev/metropolis/proto/common"
Lorenz Brune6573102021-11-02 14:15:37 +010022)
23
24var installCmd = &cobra.Command{
Lorenz Brun7a510192022-07-04 15:31:38 +000025 Short: "Contains subcommands to install Metropolis via different media.",
Lorenz Brune6573102021-11-02 14:15:37 +010026 Use: "install",
27}
28
Lorenz Brun7a510192022-07-04 15:31:38 +000029var bundlePath = installCmd.PersistentFlags().StringP("bundle", "b", "", "Path to the Metropolis bundle to be installed")
Serge Bazanski46a45f92023-07-19 17:09:52 +020030var installerPath = installCmd.PersistentFlags().StringP("installer", "i", "", "Path to the Metropolis installer to use when installing")
Lorenz Brun7a510192022-07-04 15:31:38 +000031
Lorenz Brune6573102021-11-02 14:15:37 +010032var genusbCmd = &cobra.Command{
Serge Bazanski97783222021-12-14 16:04:26 +010033 Use: "genusb target",
Lorenz Brune6573102021-11-02 14:15:37 +010034 Short: "Generates a Metropolis installer disk or image.",
Lorenz Brun7a510192022-07-04 15:31:38 +000035 Example: "metroctl install --bundle=metropolis-v0.1.zip genusb /dev/sdx",
Lorenz Brune6573102021-11-02 14:15:37 +010036 Args: cobra.ExactArgs(1), // One positional argument: the target
37 Run: doGenUSB,
38}
39
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +020040// bootstrap is a flag controlling node parameters included in the installer
41// image. If set, the installed node will bootstrap a new cluster. Otherwise,
42// it will try to connect to the cluster which endpoints were provided with
43// the --endpoints flag.
44var bootstrap bool
45
Serge Bazanskibdc803b2023-03-27 10:55:09 +020046var bootstrapTPMMode string
Tim Windelschmidtc09327c2023-06-14 17:48:56 +020047var bootstrapStorageSecurityPolicy string
Serge Bazanskibdc803b2023-03-27 10:55:09 +020048
Serge Bazanski46a45f92023-07-19 17:09:52 +020049type externalFile struct {
50 reader io.Reader
51 size uint64
52}
53
Lorenz Brunad131882023-06-28 16:42:20 +020054func external(name, datafilePath string, flag *string) fat32.SizedReader {
Serge Bazanski46a45f92023-07-19 17:09:52 +020055 if flag == nil || *flag == "" {
56 df, err := datafile.Get(datafilePath)
57 if err != nil {
58 log.Fatalf("No %s specified", name)
59 }
Lorenz Brunad131882023-06-28 16:42:20 +020060 return bytes.NewReader(df)
Serge Bazanski46a45f92023-07-19 17:09:52 +020061 }
62
Lorenz Brunad131882023-06-28 16:42:20 +020063 f, err := blkio.NewFileReader(*bundlePath)
Serge Bazanski46a45f92023-07-19 17:09:52 +020064 if err != nil {
65 log.Fatalf("Failed to open specified %s: %v", name, err)
66 }
Lorenz Brunad131882023-06-28 16:42:20 +020067
68 return f
Serge Bazanski46a45f92023-07-19 17:09:52 +020069}
Lorenz Brun7a510192022-07-04 15:31:38 +000070
Lorenz Brune6573102021-11-02 14:15:37 +010071func doGenUSB(cmd *cobra.Command, args []string) {
Serge Bazanskibdc803b2023-03-27 10:55:09 +020072 var tpmMode cpb.ClusterConfiguration_TPMMode
73 switch strings.ToLower(bootstrapTPMMode) {
74 case "required", "require":
75 tpmMode = cpb.ClusterConfiguration_TPM_MODE_REQUIRED
76 case "best-effort", "besteffort":
77 tpmMode = cpb.ClusterConfiguration_TPM_MODE_BEST_EFFORT
78 case "disabled", "disable":
79 tpmMode = cpb.ClusterConfiguration_TPM_MODE_DISABLED
80 default:
81 log.Fatalf("Invalid --bootstrap-tpm-mode (must be one of: required, best-effort, disabled)")
82 }
83
Tim Windelschmidtc09327c2023-06-14 17:48:56 +020084 var bootstrapStorageSecurity cpb.ClusterConfiguration_StorageSecurityPolicy
85 switch strings.ToLower(bootstrapStorageSecurityPolicy) {
86 case "permissive":
87 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_PERMISSIVE
88 case "needs-encryption":
89 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION
90 case "needs-encryption-and-authentication":
91 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION_AND_AUTHENTICATION
92 case "needs-insecure":
93 bootstrapStorageSecurity = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_INSECURE
94 default:
95
96 log.Fatalf("Invalid --bootstrap-storage-security (must be one of: permissive, needs-encryption, needs-encryption-and-authentication, needs-insecure)")
97 }
98
Serge Bazanski46a45f92023-07-19 17:09:52 +020099 bundle := external("bundle", "metropolis/node/bundle.zip", bundlePath)
100 installer := external("installer", "metropolis/installer/kernel.efi", installerPath)
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{
Lorenz Brunad131882023-06-28 16:42:20 +0200150 TargetPath: args[0],
151 Installer: installer,
152 NodeParams: params,
153 Bundle: bundle,
Lorenz Brune6573102021-11-02 14:15:37 +0100154 }
155
Serge Bazanski97783222021-12-14 16:04:26 +0100156 log.Printf("Generating installer image (this can take a while, see issues/92).")
Lorenz Brune6573102021-11-02 14:15:37 +0100157 if err := core.MakeInstallerImage(installerImageArgs); err != nil {
158 log.Fatalf("Failed to create installer: %v", err)
159 }
160}
161
162func init() {
163 rootCmd.AddCommand(installCmd)
Mateusz Zalegad5f2f7a2022-07-05 18:48:56 +0200164
165 genusbCmd.Flags().BoolVar(&bootstrap, "bootstrap", false, "Create a bootstrap installer image.")
Serge Bazanskibdc803b2023-03-27 10:55:09 +0200166 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 +0200167 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 +0100168 installCmd.AddCommand(genusbCmd)
Lorenz Brune6573102021-11-02 14:15:37 +0100169}