2017-11-28 11:26:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/cluster"
|
2017-12-11 18:28:08 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2017-11-28 11:26:15 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RemoveCommand() cli.Command {
|
|
|
|
removeFlags := []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "Specify an alternate cluster YAML file",
|
|
|
|
Value: cluster.DefaultClusterConfig,
|
|
|
|
EnvVar: "RKE_CONFIG",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force",
|
|
|
|
Usage: "Force removal of the cluster",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return cli.Command{
|
|
|
|
Name: "remove",
|
|
|
|
Usage: "Teardown the cluster and clean cluster nodes",
|
|
|
|
Action: clusterRemoveFromCli,
|
|
|
|
Flags: removeFlags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 18:28:08 +00:00
|
|
|
func ClusterRemove(clusterFile string, customDialer hosts.Dialer) error {
|
2017-11-28 11:26:15 +00:00
|
|
|
logrus.Infof("Tearing down Kubernetes cluster")
|
2017-12-11 18:28:08 +00:00
|
|
|
kubeCluster, err := cluster.ParseConfig(clusterFile, customDialer)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = kubeCluster.TunnelHosts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Starting Cluster removal")
|
|
|
|
err = kubeCluster.ClusterRemove()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Cluster removed successfully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clusterRemoveFromCli(ctx *cli.Context) error {
|
|
|
|
force := ctx.Bool("force")
|
|
|
|
if !force {
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
fmt.Printf("Are you sure you want to remove Kubernetes cluster [y/n]: ")
|
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
input = strings.TrimSpace(input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if input != "y" && input != "Y" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clusterFile, err := resolveClusterFile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
|
|
|
}
|
2017-12-11 18:28:08 +00:00
|
|
|
return ClusterRemove(clusterFile, nil)
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|