2022-09-05 09:06:08 +02:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2022-03-22 16:03:15 +01:00
package cmd
import (
"errors"
"fmt"
"io/fs"
"github.com/spf13/afero"
"github.com/spf13/cobra"
2023-10-16 15:05:29 +02:00
"github.com/spf13/pflag"
2022-03-22 16:03:15 +01:00
2022-09-21 13:47:57 +02:00
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/file"
2022-03-22 16:03:15 +01:00
)
2022-06-08 08:14:28 +02:00
// NewTerminateCmd returns a new cobra.Command for the terminate command.
func NewTerminateCmd ( ) * cobra . Command {
2022-03-22 16:03:15 +01:00
cmd := & cobra . Command {
Use : "terminate" ,
2022-05-09 17:02:47 +02:00
Short : "Terminate a Constellation cluster" ,
2023-01-17 14:01:56 +01:00
Long : "Terminate a Constellation cluster.\n\n" +
"The cluster can't be started again, and all persistent storage will be lost." ,
Args : cobra . NoArgs ,
RunE : runTerminate ,
2022-03-22 16:03:15 +01:00
}
2022-10-31 16:58:15 +01:00
cmd . Flags ( ) . BoolP ( "yes" , "y" , false , "terminate the cluster without further confirmation" )
2022-03-22 16:03:15 +01:00
return cmd
}
2023-10-16 15:05:29 +02:00
type terminateFlags struct {
rootFlags
yes bool
}
func ( f * terminateFlags ) parse ( flags * pflag . FlagSet ) error {
if err := f . rootFlags . parse ( flags ) ; err != nil {
return err
}
yes , err := flags . GetBool ( "yes" )
if err != nil {
return fmt . Errorf ( "getting 'yes' flag: %w" , err )
}
f . yes = yes
return nil
}
2022-03-22 16:03:15 +01:00
// runTerminate runs the terminate command.
2023-03-20 11:03:36 +01:00
func runTerminate ( cmd * cobra . Command , _ [ ] string ) error {
2023-02-16 15:43:19 +01:00
spinner , err := newSpinnerOrStderr ( cmd )
if err != nil {
return fmt . Errorf ( "creating spinner: %w" , err )
}
2022-10-07 19:35:07 +02:00
defer spinner . Stop ( )
2022-04-13 13:01:38 +02:00
terminator := cloudcmd . NewTerminator ( )
2023-10-16 15:05:29 +02:00
logger , err := newCLILogger ( cmd )
2022-10-31 16:58:15 +01:00
if err != nil {
2023-10-16 15:05:29 +02:00
return fmt . Errorf ( "creating logger: %w" , err )
}
t := & terminateCmd { log : logger , fileHandler : file . NewHandler ( afero . NewOsFs ( ) ) }
if err := t . flags . parse ( cmd . Flags ( ) ) ; err != nil {
return err
2022-10-31 16:58:15 +01:00
}
2023-10-16 15:05:29 +02:00
return t . terminate ( cmd , terminator , spinner )
}
type terminateCmd struct {
log debugLog
fileHandler file . Handler
flags terminateFlags
}
func ( t * terminateCmd ) terminate ( cmd * cobra . Command , terminator cloudTerminator , spinner spinnerInterf ) error {
if ! t . flags . yes {
2022-10-31 16:58:15 +01:00
cmd . Println ( "You are about to terminate a Constellation cluster." )
cmd . Println ( "All of its associated resources will be DESTROYED." )
cmd . Println ( "This action is irreversible and ALL DATA WILL BE LOST." )
ok , err := askToConfirm ( cmd , "Do you want to continue?" )
if err != nil {
return err
}
if ! ok {
cmd . Println ( "The termination of the cluster was aborted." )
return nil
}
}
2022-10-07 19:35:07 +02:00
spinner . Start ( "Terminating" , false )
2023-10-16 15:05:29 +02:00
err := terminator . Terminate ( cmd . Context ( ) , constants . TerraformWorkingDir , t . flags . tfLogLevel )
2022-10-04 19:17:05 +03:00
spinner . Stop ( )
if err != nil {
2022-06-09 14:10:42 +00:00
return fmt . Errorf ( "terminating Constellation cluster: %w" , err )
2022-03-22 16:03:15 +01:00
}
2022-05-04 09:13:46 +02:00
cmd . Println ( "Your Constellation cluster was terminated successfully." )
2022-03-22 16:03:15 +01:00
2023-02-07 15:19:59 +01:00
var removeErr error
2023-10-16 15:05:29 +02:00
if err := t . fileHandler . Remove ( constants . AdminConfFilename ) ; err != nil && ! errors . Is ( err , fs . ErrNotExist ) {
removeErr = errors . Join ( err , fmt . Errorf ( "failed to remove file: '%s', please remove it manually" , t . flags . pathPrefixer . PrefixPrintablePath ( constants . AdminConfFilename ) ) )
2022-03-22 16:03:15 +01:00
}
2022-03-29 11:38:14 +02:00
2023-10-16 15:05:29 +02:00
if err := t . fileHandler . Remove ( constants . StateFilename ) ; err != nil && ! errors . Is ( err , fs . ErrNotExist ) {
removeErr = errors . Join ( err , fmt . Errorf ( "failed to remove file: '%s', please remove it manually" , t . flags . pathPrefixer . PrefixPrintablePath ( constants . StateFilename ) ) )
2023-09-25 18:06:44 +02:00
}
2023-02-07 15:19:59 +01:00
return removeErr
2022-03-22 16:03:15 +01:00
}