2022-10-07 09:38:43 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-12-08 16:27:04 +01:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constellation/state"
|
2022-10-07 09:38:43 +02:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newMiniDownCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "down",
|
2022-10-14 10:48:20 +02:00
|
|
|
Short: "Destroy a MiniConstellation cluster",
|
|
|
|
Long: "Destroy a MiniConstellation cluster.",
|
2022-10-07 09:38:43 +02:00
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
RunE: runDown,
|
|
|
|
}
|
2022-11-08 18:32:59 +01:00
|
|
|
cmd.Flags().BoolP("yes", "y", false, "terminate the cluster without further confirmation")
|
2022-10-07 09:38:43 +02:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDown(cmd *cobra.Command, args []string) error {
|
|
|
|
if err := checkForMiniCluster(file.NewHandler(afero.NewOsFs())); err != nil {
|
|
|
|
return fmt.Errorf("failed to destroy cluster: %w. Are you in the correct working directory?", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := runTerminate(cmd, args)
|
|
|
|
if removeErr := os.Remove(constants.MasterSecretFilename); removeErr != nil && !os.IsNotExist(removeErr) {
|
2023-02-07 12:56:25 +01:00
|
|
|
err = errors.Join(err, removeErr)
|
2022-10-07 09:38:43 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkForMiniCluster(fileHandler file.Handler) error {
|
2023-10-09 13:04:29 +02:00
|
|
|
stateFile, err := state.ReadFromFile(fileHandler, constants.StateFilename)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading state file: %w", err)
|
2022-10-07 09:38:43 +02:00
|
|
|
}
|
2023-10-09 13:04:29 +02:00
|
|
|
|
2023-11-20 12:10:16 +01:00
|
|
|
if stateFile.Infrastructure.Name != constants.MiniConstellationName {
|
2022-10-14 10:48:20 +02:00
|
|
|
return errors.New("cluster is not a MiniConstellation cluster")
|
2022-10-07 09:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|