github.com/pingcap/tiup@v1.15.1/components/cluster/command/clean.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  	operator "github.com/pingcap/tiup/pkg/cluster/operation"
    18  	"github.com/spf13/cobra"
    19  )
    20  
    21  func newCleanCmd() *cobra.Command {
    22  	cleanOpt := operator.Options{}
    23  	cleanALl := false
    24  
    25  	cmd := &cobra.Command{
    26  		Use:   "clean <cluster-name>",
    27  		Short: "(EXPERIMENTAL) Cleanup a specified cluster",
    28  		Long: `EXPERIMENTAL: This is an experimental feature, things may or may not work,
    29  please backup your data before process.
    30  
    31  Cleanup a specified cluster without destroying it.
    32  You can retain some nodes and roles data when cleanup the cluster, eg:
    33      $ tiup cluster clean <cluster-name> --all
    34      $ tiup cluster clean <cluster-name> --log
    35      $ tiup cluster clean <cluster-name> --data
    36      $ tiup cluster clean <cluster-name> --audit-log
    37      $ tiup cluster clean <cluster-name> --all --ignore-role prometheus
    38      $ tiup cluster clean <cluster-name> --all --ignore-node 172.16.13.11:9000
    39      $ tiup cluster clean <cluster-name> --all --ignore-node 172.16.13.12`,
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			if len(args) != 1 {
    42  				return cmd.Help()
    43  			}
    44  
    45  			clusterName := args[0]
    46  			clusterReport.ID = scrubClusterName(clusterName)
    47  			teleCommand = append(teleCommand, scrubClusterName(clusterName))
    48  
    49  			if cleanALl {
    50  				cleanOpt.CleanupData = true
    51  				cleanOpt.CleanupLog = true
    52  			}
    53  
    54  			if !(cleanOpt.CleanupData || cleanOpt.CleanupLog || cleanOpt.CleanupAuditLog) {
    55  				return cmd.Help()
    56  			}
    57  
    58  			return cm.CleanCluster(clusterName, gOpt, cleanOpt, skipConfirm)
    59  		},
    60  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    61  			switch len(args) {
    62  			case 0:
    63  				return shellCompGetClusterName(cm, toComplete)
    64  			default:
    65  				return nil, cobra.ShellCompDirectiveNoFileComp
    66  			}
    67  		},
    68  	}
    69  
    70  	cmd.Flags().StringArrayVar(&cleanOpt.RetainDataNodes, "ignore-node", nil, "Specify the nodes or hosts whose data will be retained")
    71  	cmd.Flags().StringArrayVar(&cleanOpt.RetainDataRoles, "ignore-role", nil, "Specify the roles whose data will be retained")
    72  	cmd.Flags().BoolVar(&cleanOpt.CleanupData, "data", false, "Cleanup data")
    73  	cmd.Flags().BoolVar(&cleanOpt.CleanupLog, "log", false, "Cleanup log")
    74  	cmd.Flags().BoolVar(&cleanOpt.CleanupAuditLog, "audit-log", false, "Cleanup TiDB-server audit log")
    75  	cmd.Flags().BoolVar(&cleanALl, "all", false, "Cleanup both log and data (not include audit log)")
    76  
    77  	return cmd
    78  }