github.com/pingcap/tiup@v1.15.1/components/cluster/command/display.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  	"errors"
    18  	"fmt"
    19  	"strings"
    20  	"time"
    21  
    22  	perrs "github.com/pingcap/errors"
    23  	"github.com/pingcap/tiup/pkg/cluster/manager"
    24  	"github.com/pingcap/tiup/pkg/cluster/spec"
    25  	"github.com/pingcap/tiup/pkg/meta"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  func newDisplayCmd() *cobra.Command {
    30  	var (
    31  		showDashboardOnly bool
    32  		showVersionOnly   bool
    33  		showTiKVLabels    bool
    34  		statusTimeout     uint64
    35  		dopt              manager.DisplayOption
    36  	)
    37  	cmd := &cobra.Command{
    38  		Use:   "display <cluster-name>",
    39  		Short: "Display information of a TiDB cluster",
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			if len(args) != 1 {
    42  				return cmd.Help()
    43  			}
    44  
    45  			gOpt.APITimeout = statusTimeout
    46  			dopt.ClusterName = args[0]
    47  			clusterReport.ID = scrubClusterName(dopt.ClusterName)
    48  			teleCommand = append(teleCommand, scrubClusterName(dopt.ClusterName))
    49  
    50  			exist, err := tidbSpec.Exist(dopt.ClusterName)
    51  			if err != nil {
    52  				return err
    53  			}
    54  
    55  			if !exist {
    56  				return perrs.Errorf("Cluster %s not found", dopt.ClusterName)
    57  			}
    58  
    59  			metadata, err := spec.ClusterMetadata(dopt.ClusterName)
    60  			if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) &&
    61  				!errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) {
    62  				return err
    63  			}
    64  
    65  			if showVersionOnly {
    66  				fmt.Println(metadata.Version)
    67  				return nil
    68  			}
    69  
    70  			if showDashboardOnly {
    71  				tlsCfg, err := metadata.Topology.TLSConfig(tidbSpec.Path(dopt.ClusterName, spec.TLSCertKeyDir))
    72  				if err != nil {
    73  					return err
    74  				}
    75  				return cm.DisplayDashboardInfo(dopt.ClusterName, time.Second*time.Duration(gOpt.APITimeout), tlsCfg)
    76  			}
    77  			if showTiKVLabels {
    78  				return cm.DisplayTiKVLabels(dopt, gOpt)
    79  			}
    80  			return cm.Display(dopt, gOpt)
    81  		},
    82  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    83  			switch len(args) {
    84  			case 0:
    85  				return shellCompGetClusterName(cm, toComplete)
    86  			default:
    87  				return nil, cobra.ShellCompDirectiveNoFileComp
    88  			}
    89  		},
    90  	}
    91  
    92  	cmd.Flags().StringSliceVarP(&gOpt.Roles, "role", "R", nil, "Only display specified roles")
    93  	cmd.Flags().StringSliceVarP(&gOpt.Nodes, "node", "N", nil, "Only display specified nodes")
    94  	cmd.Flags().BoolVar(&dopt.ShowUptime, "uptime", false, "Display with uptime")
    95  	cmd.Flags().BoolVar(&showDashboardOnly, "dashboard", false, "Only display TiDB Dashboard information")
    96  	cmd.Flags().BoolVar(&showVersionOnly, "version", false, "Only display TiDB cluster version")
    97  	cmd.Flags().BoolVar(&showTiKVLabels, "labels", false, "Only display labels of specified TiKV role or nodes")
    98  	cmd.Flags().BoolVar(&dopt.ShowProcess, "process", false, "display cpu and memory usage of nodes")
    99  	cmd.Flags().BoolVar(&dopt.ShowManageHost, "manage-host", false, "display manage host of nodes")
   100  	cmd.Flags().BoolVar(&dopt.ShowNuma, "numa", false, "display numa information of nodes")
   101  	cmd.Flags().BoolVar(&dopt.ShowVersions, "versions", false, "display component version of instances")
   102  	cmd.Flags().Uint64Var(&statusTimeout, "status-timeout", 10, "Timeout in seconds when getting node status")
   103  
   104  	return cmd
   105  }
   106  
   107  func shellCompGetClusterName(cm *manager.Manager, toComplete string) ([]string, cobra.ShellCompDirective) {
   108  	var result []string
   109  	clusters, _ := cm.GetClusterList()
   110  	for _, c := range clusters {
   111  		if strings.HasPrefix(c.Name, toComplete) {
   112  			result = append(result, c.Name)
   113  		}
   114  	}
   115  	return result, cobra.ShellCompDirectiveNoFileComp
   116  }