github.com/pingcap/tiup@v1.15.1/components/cluster/command/template.go (about)

     1  // Copyright 2021 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  	"bytes"
    18  	"fmt"
    19  	"path"
    20  	"text/template"
    21  
    22  	"github.com/pingcap/errors"
    23  	"github.com/pingcap/tiup/embed"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  // TemplateOptions contains the options for print topology template.
    28  type TemplateOptions struct {
    29  	Full    bool // print full template
    30  	MultiDC bool // print template for deploying to multiple data center
    31  	Local   bool // print and render local template
    32  }
    33  
    34  // LocalTemplate contains the variables for print local template.
    35  type LocalTemplate struct {
    36  	GlobalUser          string   // global.user in yaml template
    37  	GlobalGroup         string   // global.group in yaml template
    38  	GlobalSystemdMode   string   // global.systemd_mode in yaml template
    39  	GlobalSSHPort       int      // global.ssh_port in yaml template
    40  	GlobalDeployDir     string   // global.deploy_dir in yaml template
    41  	GlobalDataDir       string   // global.data_dir in yaml template
    42  	GlobalArch          string   // global.arch in yaml template
    43  	PDServers           []string // pd_servers in yaml template
    44  	TiDBServers         []string // tidb_servers in yaml template
    45  	TiKVServers         []string // tikv_servers in yaml template
    46  	TiFlashServers      []string // tiflash_servers in yaml template
    47  	MonitoringServers   []string // monitoring_servers in yaml template
    48  	GrafanaServers      []string // grafana_servers in yaml template
    49  	AlertManagerServers []string // alertmanager_servers in yaml template
    50  }
    51  
    52  // This is used to identify how many bool type options are set, so that an
    53  // error can be throw if more than one is given.
    54  func sumBool(b ...bool) int {
    55  	n := 0
    56  	for _, v := range b {
    57  		if v {
    58  			n++
    59  		}
    60  	}
    61  	return n
    62  }
    63  
    64  func newTemplateCmd() *cobra.Command {
    65  	opt := TemplateOptions{}
    66  	localOpt := LocalTemplate{}
    67  
    68  	cmd := &cobra.Command{
    69  		Use:   "template",
    70  		Short: "Print topology template",
    71  		RunE: func(cmd *cobra.Command, args []string) error {
    72  			if sumBool(opt.Full, opt.MultiDC, opt.Local) > 1 {
    73  				return errors.New("at most one of 'full', 'multi-dc', or 'local' can be specified")
    74  			}
    75  			name := "minimal.yaml"
    76  			switch {
    77  			case opt.Full:
    78  				name = "topology.example.yaml"
    79  			case opt.MultiDC:
    80  				name = "multi-dc.yaml"
    81  			case opt.Local:
    82  				name = "local.tpl"
    83  			}
    84  
    85  			fp := path.Join("examples", "cluster", name)
    86  			tpl, err := embed.ReadExample(fp)
    87  			if err != nil {
    88  				return err
    89  			}
    90  
    91  			if !opt.Local {
    92  				// print example yaml and return
    93  				fmt.Fprintln(cmd.OutOrStdout(), string(tpl))
    94  				return nil
    95  			}
    96  
    97  			// redner template
    98  
    99  			// validate arch
   100  			if localOpt.GlobalArch != "amd64" && localOpt.GlobalArch != "arm64" {
   101  				return fmt.Errorf(`supported values are "amd64" or "arm64" in global.arch`)
   102  			}
   103  
   104  			tmpl, err := template.New(name).Parse(string(tpl))
   105  			if err != nil {
   106  				return err
   107  			}
   108  
   109  			content := bytes.NewBufferString("")
   110  			if err := tmpl.Execute(content, &localOpt); err != nil {
   111  				return err
   112  			}
   113  
   114  			fmt.Fprintln(cmd.OutOrStdout(), content.String())
   115  			return nil
   116  		},
   117  	}
   118  
   119  	cmd.Flags().BoolVar(&opt.Full, "full", false, "Print the full topology template for TiDB cluster.")
   120  	cmd.Flags().BoolVar(&opt.MultiDC, "multi-dc", false, "Print template for deploying to multiple data center.")
   121  	cmd.Flags().BoolVar(&opt.Local, "local", false, "Print and render template for deploying a simple cluster locally.")
   122  
   123  	// template values for rendering
   124  	cmd.Flags().StringVar(&localOpt.GlobalUser, "user", "tidb", "The user who runs the tidb cluster.")
   125  	cmd.Flags().StringVar(&localOpt.GlobalGroup, "group", "", "group is used to specify the group name the user belong to if it's not the same as user.")
   126  	cmd.Flags().StringVar(&localOpt.GlobalSystemdMode, "systemd_mode", "system", "systemd_mode is used to select whether to use sudo permissions.")
   127  	cmd.Flags().IntVar(&localOpt.GlobalSSHPort, "ssh-port", 22, "SSH port of servers in the managed cluster.")
   128  	cmd.Flags().StringVar(&localOpt.GlobalDeployDir, "deploy-dir", "/tidb-deploy", "Storage directory for cluster deployment files, startup scripts, and configuration files.")
   129  	cmd.Flags().StringVar(&localOpt.GlobalDataDir, "data-dir", "/tidb-data", "TiDB Cluster data storage directory.")
   130  	cmd.Flags().StringVar(&localOpt.GlobalArch, "arch", "amd64", "Supported values: \"amd64\", \"arm64\".")
   131  	cmd.Flags().StringSliceVar(&localOpt.PDServers, "pd-servers", []string{"127.0.0.1"}, "List of PD servers")
   132  	cmd.Flags().StringSliceVar(&localOpt.TiDBServers, "tidb-servers", []string{"127.0.0.1"}, "List of TiDB servers")
   133  	cmd.Flags().StringSliceVar(&localOpt.TiKVServers, "tikv-servers", []string{"127.0.0.1"}, "List of TiKV servers")
   134  	cmd.Flags().StringSliceVar(&localOpt.TiFlashServers, "tiflash-servers", nil, "List of TiFlash servers")
   135  	cmd.Flags().StringSliceVar(&localOpt.MonitoringServers, "monitoring-servers", []string{"127.0.0.1"}, "List of monitor servers")
   136  	cmd.Flags().StringSliceVar(&localOpt.GrafanaServers, "grafana-servers", []string{"127.0.0.1"}, "List of grafana servers")
   137  	cmd.Flags().StringSliceVar(&localOpt.AlertManagerServers, "alertmanager-servers", nil, "List of alermanager servers")
   138  
   139  	return cmd
   140  }