2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
2022-03-29 05:38:14 -04:00
|
|
|
"go.uber.org/multierr"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/state"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
// NewTerminateCmd returns a new cobra.Command for the terminate command.
|
|
|
|
func NewTerminateCmd() *cobra.Command {
|
2022-03-22 11:03:15 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "terminate",
|
2022-05-09 11:02:47 -04:00
|
|
|
Short: "Terminate a Constellation cluster",
|
|
|
|
Long: "Terminate a Constellation cluster. The cluster can't be started again, and all persistent storage will be lost.",
|
2022-03-22 11:03:15 -04:00
|
|
|
Args: cobra.NoArgs,
|
|
|
|
RunE: runTerminate,
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// runTerminate runs the terminate command.
|
|
|
|
func runTerminate(cmd *cobra.Command, args []string) error {
|
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2022-04-13 07:01:38 -04:00
|
|
|
terminator := cloudcmd.NewTerminator()
|
|
|
|
|
|
|
|
return terminate(cmd, terminator, fileHandler)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
func terminate(cmd *cobra.Command, terminator cloudTerminator, fileHandler file.Handler) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
var stat state.ConstellationState
|
2022-04-06 04:36:58 -04:00
|
|
|
if err := fileHandler.ReadJSON(constants.StateFilename, &stat); err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return fmt.Errorf("reading Constellation state: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-10-06 05:20:01 -04:00
|
|
|
spinner := newSpinner(cmd, "Terminating", false)
|
2022-10-04 12:17:05 -04:00
|
|
|
spinner.Start()
|
|
|
|
err := terminator.Terminate(cmd.Context(), stat)
|
|
|
|
spinner.Stop()
|
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return fmt.Errorf("terminating Constellation cluster: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-04 03:13:46 -04:00
|
|
|
cmd.Println("Your Constellation cluster was terminated successfully.")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-03-29 05:38:14 -04:00
|
|
|
var retErr error
|
2022-04-06 04:36:58 -04:00
|
|
|
if err := fileHandler.Remove(constants.StateFilename); err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file: '%s', please remove it manually", constants.StateFilename))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-06 04:36:58 -04:00
|
|
|
if err := fileHandler.Remove(constants.AdminConfFilename); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2022-06-09 10:10:42 -04:00
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file: '%s', please remove it manually", constants.AdminConfFilename))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-03-29 05:38:14 -04:00
|
|
|
|
2022-04-06 04:36:58 -04:00
|
|
|
if err := fileHandler.Remove(constants.WGQuickConfigFilename); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
2022-06-09 10:10:42 -04:00
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file: '%s', please remove it manually", constants.WGQuickConfigFilename))
|
2022-03-29 05:38:14 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 07:59:46 -04:00
|
|
|
if err := fileHandler.Remove(constants.ClusterIDsFileName); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file: '%s', please remove it manually", constants.ClusterIDsFileName))
|
|
|
|
}
|
|
|
|
|
2022-03-29 05:38:14 -04:00
|
|
|
return retErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|