constellation/cli/internal/cmd/create.go

267 lines
9.7 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
package cmd
import (
"errors"
"fmt"
"io/fs"
2022-09-21 11:47:57 +00:00
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
2022-11-16 15:33:51 +00:00
"github.com/edgelesssys/constellation/v2/cli/internal/terraform"
2022-09-21 11:47:57 +00:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
"github.com/edgelesssys/constellation/v2/internal/config"
2022-09-21 11:47:57 +00:00
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
2022-04-13 11:01:38 +00:00
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
2022-06-08 06:14:28 +00:00
// NewCreateCmd returns a new cobra.Command for the create command.
func NewCreateCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Create instances on a cloud platform for your Constellation cluster",
2022-05-04 07:13:46 +00:00
Long: "Create instances on a cloud platform for your Constellation cluster.",
Args: cobra.ExactArgs(0),
RunE: runCreate,
}
2022-05-09 15:02:47 +00:00
cmd.Flags().BoolP("yes", "y", false, "create the cluster without further confirmation")
2022-05-04 16:41:24 +00:00
cmd.Flags().IntP("control-plane-nodes", "c", 0, "number of control-plane nodes (required)")
must(cobra.MarkFlagRequired(cmd.Flags(), "control-plane-nodes"))
2022-05-04 16:41:24 +00:00
cmd.Flags().IntP("worker-nodes", "w", 0, "number of worker nodes (required)")
must(cobra.MarkFlagRequired(cmd.Flags(), "worker-nodes"))
return cmd
}
type createCmd struct {
log debugLog
}
2022-04-13 11:01:38 +00:00
func runCreate(cmd *cobra.Command, args []string) error {
log, err := newCLILogger(cmd)
if err != nil {
return fmt.Errorf("creating logger: %w", err)
}
defer log.Sync()
spinner, err := newSpinnerOrStderr(cmd)
if err != nil {
return fmt.Errorf("creating spinner: %w", err)
}
2022-10-07 17:35:07 +00:00
defer spinner.Stop()
fileHandler := file.NewHandler(afero.NewOsFs())
creator := cloudcmd.NewCreator(spinner)
c := &createCmd{log: log}
return c.create(cmd, creator, fileHandler, spinner)
2022-04-13 11:01:38 +00:00
}
func (c *createCmd) create(cmd *cobra.Command, creator cloudCreator, fileHandler file.Handler, spinner spinnerInterf,
2022-10-07 17:35:07 +00:00
) (retErr error) {
flags, err := c.parseCreateFlags(cmd)
2022-04-13 11:01:38 +00:00
if err != nil {
return err
}
c.log.Debugf("Using flags: %+v", flags)
if err := c.checkDirClean(fileHandler); err != nil {
2022-04-13 11:01:38 +00:00
return err
}
c.log.Debugf("Loading configuration file from %q", flags.configPath)
conf, err := config.New(fileHandler, flags.configPath, flags.force)
var configValidationErr *config.ValidationError
if errors.As(err, &configValidationErr) {
cmd.PrintErrln(configValidationErr.LongMessage())
}
2022-04-13 11:01:38 +00:00
if err != nil {
return err
2022-04-13 11:01:38 +00:00
}
c.log.Debugf("Checking configuration for warnings")
2022-09-06 11:05:49 +00:00
var printedAWarning bool
if !conf.IsReleaseImage() {
cmd.PrintErrln("Configured image doesn't look like a released production image. Double check image before deploying to production.")
2022-09-06 11:05:49 +00:00
printedAWarning = true
}
if conf.IsDebugCluster() {
cmd.PrintErrln("WARNING: Creating a debug cluster. This cluster is not secure and should only be used for debugging purposes.")
cmd.PrintErrln("DO NOT USE THIS CLUSTER IN PRODUCTION.")
2022-09-06 11:05:49 +00:00
printedAWarning = true
}
if conf.IsAzureNonCVM() {
cmd.PrintErrln("Disabling Confidential VMs is insecure. Use only for evaluation purposes.")
2022-09-06 11:05:49 +00:00
printedAWarning = true
if conf.EnforcesIDKeyDigest() {
cmd.PrintErrln("Your config asks for enforcing the idkeydigest. This is only available on Confidential VMs. It will not be enforced.")
}
}
2022-09-06 11:05:49 +00:00
// Print an extra new line later to separate warnings from the prompt message of the create command
if printedAWarning {
cmd.PrintErrln("")
2022-09-06 11:05:49 +00:00
}
provider := conf.GetProvider()
2022-08-31 15:35:33 +00:00
var instanceType string
switch provider {
case cloudprovider.AWS:
c.log.Debugf("Configuring instance type for AWS")
instanceType = conf.Provider.AWS.InstanceType
2022-08-31 15:35:33 +00:00
case cloudprovider.Azure:
c.log.Debugf("Configuring instance type for Azure")
instanceType = conf.Provider.Azure.InstanceType
2022-08-31 15:35:33 +00:00
case cloudprovider.GCP:
c.log.Debugf("Configuring instance type for GCP")
instanceType = conf.Provider.GCP.InstanceType
case cloudprovider.QEMU:
c.log.Debugf("Configuring instance type for QEMU")
cpus := conf.Provider.QEMU.VCPUs
instanceType = fmt.Sprintf("%d-vCPU", cpus)
2022-08-31 15:35:33 +00:00
}
c.log.Debugf("Configured with instance type %q", instanceType)
2022-08-31 15:35:33 +00:00
2022-04-13 11:01:38 +00:00
if !flags.yes {
// Ask user to confirm action.
2022-05-04 07:13:46 +00:00
cmd.Printf("The following Constellation cluster will be created:\n")
cmd.Printf("%d control-plane node%s of type %s will be created.\n", flags.controllerCount, isPlural(flags.controllerCount), instanceType)
cmd.Printf("%d worker node%s of type %s will be created.\n", flags.workerCount, isPlural(flags.workerCount), instanceType)
2022-05-04 07:13:46 +00:00
ok, err := askToConfirm(cmd, "Do you want to create this cluster?")
2022-04-13 11:01:38 +00:00
if err != nil {
return err
}
if !ok {
2022-05-04 07:13:46 +00:00
cmd.Println("The creation of the cluster was aborted.")
2022-04-13 11:01:38 +00:00
return nil
}
}
2022-10-07 17:35:07 +00:00
spinner.Start("Creating", false)
idFile, err := creator.Create(cmd.Context(), provider, conf, instanceType, flags.controllerCount, flags.workerCount)
c.log.Debugf("Successfully created the cloud resources for the cluster")
spinner.Stop()
2022-04-13 11:01:38 +00:00
if err != nil {
2022-11-16 15:33:51 +00:00
return translateCreateErrors(cmd, err)
2022-04-13 11:01:38 +00:00
}
2022-10-11 10:24:33 +00:00
if err := fileHandler.WriteJSON(constants.ClusterIDsFileName, idFile, file.OptNone); err != nil {
2022-07-29 08:01:10 +00:00
return err
}
2022-05-04 07:13:46 +00:00
cmd.Println("Your Constellation cluster was created successfully.")
2022-04-13 11:01:38 +00:00
return nil
}
// parseCreateFlags parses the flags of the create command.
func (c *createCmd) parseCreateFlags(cmd *cobra.Command) (createFlags, error) {
controllerCount, err := cmd.Flags().GetInt("control-plane-nodes")
if err != nil {
return createFlags{}, fmt.Errorf("parsing number of control-plane nodes: %w", err)
}
c.log.Debugf("Control-plane nodes flag is %d", controllerCount)
if controllerCount < constants.MinControllerCount {
return createFlags{}, fmt.Errorf("number of control-plane nodes must be at least %d", constants.MinControllerCount)
}
workerCount, err := cmd.Flags().GetInt("worker-nodes")
if err != nil {
return createFlags{}, fmt.Errorf("parsing number of worker nodes: %w", err)
}
c.log.Debugf("Worker nodes flag is %d", workerCount)
if workerCount < constants.MinWorkerCount {
return createFlags{}, fmt.Errorf("number of worker nodes must be at least %d", constants.MinWorkerCount)
}
2022-04-13 11:01:38 +00:00
yes, err := cmd.Flags().GetBool("yes")
if err != nil {
return createFlags{}, fmt.Errorf("%w; Set '-yes' without a value to automatically confirm", err)
2022-04-13 11:01:38 +00:00
}
c.log.Debugf("Yes flag is %t", yes)
configPath, err := cmd.Flags().GetString("config")
2022-04-13 11:01:38 +00:00
if err != nil {
return createFlags{}, fmt.Errorf("parsing config path argument: %w", err)
2022-04-13 11:01:38 +00:00
}
c.log.Debugf("Configuration path flag is %q", configPath)
2022-04-13 11:01:38 +00:00
force, err := cmd.Flags().GetBool("force")
if err != nil {
return createFlags{}, fmt.Errorf("parsing force argument: %w", err)
}
c.log.Debugf("force flag is %t", force)
2022-04-13 11:01:38 +00:00
return createFlags{
controllerCount: controllerCount,
workerCount: workerCount,
configPath: configPath,
force: force,
yes: yes,
2022-04-13 11:01:38 +00:00
}, nil
}
// createFlags contains the parsed flags of the create command.
type createFlags struct {
controllerCount int
workerCount int
configPath string
force bool
yes bool
}
// checkDirClean checks if files of a previous Constellation are left in the current working dir.
func (c *createCmd) checkDirClean(fileHandler file.Handler) error {
c.log.Debugf("Checking admin configuration file")
2022-04-06 08:36:58 +00:00
if _, err := fileHandler.Stat(constants.AdminConfFilename); !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("file '%s' already exists in working directory, run 'constellation terminate' before creating a new one", constants.AdminConfFilename)
}
c.log.Debugf("Checking master secrets file")
2022-04-06 08:36:58 +00:00
if _, err := fileHandler.Stat(constants.MasterSecretFilename); !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("file '%s' already exists in working directory. Constellation won't overwrite previous master secrets. Move it somewhere or delete it before creating a new cluster", constants.MasterSecretFilename)
}
c.log.Debugf("Checking cluster IDs file")
2022-07-29 08:01:10 +00:00
if _, err := fileHandler.Stat(constants.ClusterIDsFileName); !errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("file '%s' already exists in working directory. Constellation won't overwrite previous cluster IDs. Move it somewhere or delete it before creating a new cluster", constants.ClusterIDsFileName)
}
return nil
}
2022-04-13 11:01:38 +00:00
2022-11-16 15:33:51 +00:00
func translateCreateErrors(cmd *cobra.Command, err error) error {
switch {
case errors.Is(err, terraform.ErrTerraformWorkspaceDifferentFiles):
cmd.PrintErrln("\nYour current working directory contains an existing Terraform workspace which does not match the expected state.")
cmd.PrintErrln("This can be due to a mix up between providers, versions or an otherwise corrupted workspace.")
cmd.PrintErrln("Before creating a new cluster, try \"constellation terminate\".")
cmd.PrintErrf("If this does not work, either move or delete the directory %q.\n", constants.TerraformWorkingDir)
cmd.PrintErrln("Please only delete the directory if you made sure that all created cloud resources have been terminated.")
return err
case errors.Is(err, terraform.ErrTerraformWorkspaceExistsWithDifferentVariables):
cmd.PrintErrln("\nYour current working directory contains an existing Terraform workspace which was initiated with different input variables.")
cmd.PrintErrln("This can be the case if you have tried to create a cluster before with different options which did not complete, or the workspace is corrupted.")
cmd.PrintErrln("Before creating a new cluster, try \"constellation terminate\".")
cmd.PrintErrf("If this does not work, either move or delete the directory %q.\n", constants.TerraformWorkingDir)
cmd.PrintErrln("Please only delete the directory if you made sure that all created cloud resources have been terminated.")
return err
default:
return err
}
}
func isPlural(count int) string {
if count == 1 {
return ""
}
return "s"
}
2022-06-08 06:14:28 +00:00
func must(err error) {
if err != nil {
panic(err)
}
}