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-04-13 07:01:38 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/cloud/cloudcmd"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/file"
|
2022-04-06 04:36:58 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newTerminateCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "terminate",
|
|
|
|
Short: "Terminate an existing Constellation.",
|
|
|
|
Long: "Terminate an existing Constellation. The Constellation can't be started again, and all persistent storage will be lost.",
|
|
|
|
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-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-05 03:11:45 -04:00
|
|
|
cmd.Println("Terminating ...")
|
|
|
|
|
2022-04-13 07:01:38 -04:00
|
|
|
if err := terminator.Terminate(cmd.Context(), stat); err != nil {
|
|
|
|
return err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Println("Your Constellation was terminated successfully.")
|
|
|
|
|
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 {
|
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file '%s', please remove 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) {
|
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file '%s', please remove 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) {
|
|
|
|
retErr = multierr.Append(err, fmt.Errorf("failed to remove file '%s', please remove manually", constants.WGQuickConfigFilename))
|
2022-03-29 05:38:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return retErr
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|