github.com/pingcap/tiup@v1.15.1/components/dm/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  	"github.com/pingcap/tiup/components/dm/spec"
    19  	operator "github.com/pingcap/tiup/pkg/cluster/operation"
    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 DM cluster",
    29  		RunE: func(cmd *cobra.Command, args []string) error {
    30  			if len(args) != 1 {
    31  				return cmd.Help()
    32  			}
    33  
    34  			clusterName := args[0]
    35  
    36  			// Validate the retained roles to prevent unexpected deleting data
    37  			if len(destroyOpt.RetainDataRoles) > 0 {
    38  				validRoles := set.NewStringSet(spec.AllDMComponentNames()...)
    39  				for _, role := range destroyOpt.RetainDataRoles {
    40  					if !validRoles.Exist(role) {
    41  						return perrs.Errorf("role name `%s` invalid", role)
    42  					}
    43  				}
    44  			}
    45  
    46  			return cm.DestroyCluster(clusterName, gOpt, destroyOpt, skipConfirm)
    47  		},
    48  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    49  			switch len(args) {
    50  			case 0:
    51  				return shellCompGetClusterName(cm, toComplete)
    52  			default:
    53  				return nil, cobra.ShellCompDirectiveNoFileComp
    54  			}
    55  		},
    56  	}
    57  
    58  	cmd.Flags().StringArrayVar(&destroyOpt.RetainDataNodes, "retain-node-data", nil, "Specify the nodes or hosts whose data will be retained")
    59  	cmd.Flags().StringArrayVar(&destroyOpt.RetainDataRoles, "retain-role-data", nil, "Specify the roles whose data will be retained")
    60  	cmd.Flags().BoolVar(&destroyOpt.Force, "force", false, "Force will ignore remote error while destroy the cluster")
    61  
    62  	return cmd
    63  }