github.com/pingcap/tiup@v1.15.1/components/cluster/command/destroy.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package command 15 16 import ( 17 perrs "github.com/pingcap/errors" 18 operator "github.com/pingcap/tiup/pkg/cluster/operation" 19 "github.com/pingcap/tiup/pkg/cluster/spec" 20 "github.com/pingcap/tiup/pkg/set" 21 "github.com/spf13/cobra" 22 ) 23 24 func newDestroyCmd() *cobra.Command { 25 destroyOpt := operator.Options{} 26 cmd := &cobra.Command{ 27 Use: "destroy <cluster-name>", 28 Short: "Destroy a specified cluster", 29 Long: `Destroy a specified cluster, which will clean the deployment binaries and data. 30 You can retain some nodes and roles data when destroy cluster, eg: 31 32 $ tiup cluster destroy <cluster-name> --retain-role-data prometheus 33 $ tiup cluster destroy <cluster-name> --retain-node-data 172.16.13.11:9000 34 $ tiup cluster destroy <cluster-name> --retain-node-data 172.16.13.12`, 35 RunE: func(cmd *cobra.Command, args []string) error { 36 if len(args) != 1 { 37 return cmd.Help() 38 } 39 40 clusterName := args[0] 41 clusterReport.ID = scrubClusterName(clusterName) 42 teleCommand = append(teleCommand, scrubClusterName(clusterName)) 43 44 // Validate the retained roles to prevent unexpected deleting data 45 if len(destroyOpt.RetainDataRoles) > 0 { 46 validRoles := set.NewStringSet(spec.AllComponentNames()...) 47 for _, role := range destroyOpt.RetainDataRoles { 48 if !validRoles.Exist(role) { 49 return perrs.Errorf("role name `%s` invalid", role) 50 } 51 } 52 } 53 54 return cm.DestroyCluster(clusterName, gOpt, destroyOpt, skipConfirm) 55 }, 56 ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 57 switch len(args) { 58 case 0: 59 return shellCompGetClusterName(cm, toComplete) 60 default: 61 return nil, cobra.ShellCompDirectiveNoFileComp 62 } 63 }, 64 } 65 66 cmd.Flags().StringArrayVar(&destroyOpt.RetainDataNodes, "retain-node-data", nil, "Specify the nodes or hosts whose data will be retained") 67 cmd.Flags().StringArrayVar(&destroyOpt.RetainDataRoles, "retain-role-data", nil, "Specify the roles whose data will be retained") 68 cmd.Flags().BoolVar(&destroyOpt.Force, "force", false, "Force will ignore remote error while destroy the cluster") 69 70 return cmd 71 }