github.com/kubernetes-incubator/kube-aws@v0.16.4/cmd/destroy.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bufio"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/kubernetes-incubator/kube-aws/core/root"
    12  	"github.com/kubernetes-incubator/kube-aws/logger"
    13  )
    14  
    15  var (
    16  	cmdDestroy = &cobra.Command{
    17  		Use:          "destroy",
    18  		Short:        "Destroy an existing Kubernetes cluster",
    19  		Long:         ``,
    20  		RunE:         runCmdDestroy,
    21  		SilenceUsage: true,
    22  	}
    23  	destroyOpts = root.DestroyOptions{}
    24  )
    25  
    26  func init() {
    27  	RootCmd.AddCommand(cmdDestroy)
    28  	cmdDestroy.Flags().StringVar(&destroyOpts.Profile, "profile", "", "The AWS profile to use from credentials file")
    29  	cmdDestroy.Flags().BoolVar(&destroyOpts.AwsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
    30  	cmdDestroy.Flags().BoolVar(&destroyOpts.Force, "force", false, "Don't ask for confirmation")
    31  }
    32  
    33  func runCmdDestroy(_ *cobra.Command, _ []string) error {
    34  	if !destroyOpts.Force && !destroyConfirmation() {
    35  		logger.Info("Operation Cancelled")
    36  		return nil
    37  	}
    38  
    39  	c, err := root.ClusterDestroyerFromFile(configPath, destroyOpts)
    40  	if err != nil {
    41  		return fmt.Errorf("error parsing config: %v", err)
    42  	}
    43  
    44  	if err := c.Destroy(); err != nil {
    45  		return fmt.Errorf("failed destroying cluster: %v", err)
    46  	}
    47  
    48  	logger.Info("CloudFormation stack is being destroyed. This will take several minutes")
    49  	return nil
    50  }
    51  
    52  func destroyConfirmation() bool {
    53  	reader := bufio.NewReader(os.Stdin)
    54  	fmt.Print("This operation will destroy the cluster. Are you sure? [y,n]: ")
    55  	text, _ := reader.ReadString('\n')
    56  	text = strings.TrimSuffix(strings.ToLower(text), "\n")
    57  
    58  	return text == "y" || text == "yes"
    59  }