github.com/pingcap/tiup@v1.15.1/components/dm/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/tiup/embed"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  // TemplateOptions contains the options for print topology template.
    27  type TemplateOptions struct {
    28  	Full  bool // print full template
    29  	Local bool // print and render local template
    30  }
    31  
    32  // LocalTemplate contains the variables for print local template.
    33  type LocalTemplate struct {
    34  	GlobalUser          string   // global.user in yaml template
    35  	GlobalGroup         string   // global.group in yaml template
    36  	GlobalSystemdMode   string   // global.systemd_mode in yaml template
    37  	GlobalSSHPort       int      // global.ssh_port in yaml template
    38  	GlobalDeployDir     string   // global.deploy_dir in yaml template
    39  	GlobalDataDir       string   // global.data_dir in yaml template
    40  	GlobalArch          string   // global.arch in yaml template
    41  	MasterServers       []string // master_servers in yaml template
    42  	WorkerServers       []string // worker_servers in yaml template
    43  	MonitoringServers   []string // monitoring_servers in yaml template
    44  	GrafanaServers      []string // grafana_servers in yaml template
    45  	AlertManagerServers []string // alertmanager_servers in yaml template
    46  }
    47  
    48  func newTemplateCmd() *cobra.Command {
    49  	opt := TemplateOptions{}
    50  	localOpt := LocalTemplate{}
    51  
    52  	cmd := &cobra.Command{
    53  		Use:   "template",
    54  		Short: "Print topology template",
    55  		RunE: func(cmd *cobra.Command, args []string) error {
    56  			name := "minimal.yaml"
    57  			switch {
    58  			case opt.Full:
    59  				name = "topology.example.yaml"
    60  			case opt.Local:
    61  				name = "local.tpl"
    62  			}
    63  
    64  			fp := path.Join("examples", "dm", name)
    65  			tpl, err := embed.ReadExample(fp)
    66  			if err != nil {
    67  				return err
    68  			}
    69  
    70  			if !opt.Local {
    71  				// print example yaml and return
    72  				fmt.Fprintln(cmd.OutOrStdout(), string(tpl))
    73  				return nil
    74  			}
    75  
    76  			// redner template
    77  
    78  			// validate arch
    79  			if localOpt.GlobalArch != "amd64" && localOpt.GlobalArch != "arm64" {
    80  				return fmt.Errorf(`supported values are "amd64" or "arm64" in global.arch`)
    81  			}
    82  
    83  			// validate number of masters and workers
    84  			if len(localOpt.MasterServers) < 3 {
    85  				return fmt.Errorf(
    86  					"at least 3 masters must be defined (given %d servers)",
    87  					len(localOpt.MasterServers),
    88  				)
    89  			}
    90  			if len(localOpt.WorkerServers) < 3 {
    91  				return fmt.Errorf(
    92  					"at least 3 workers must be defined (given %d servers)",
    93  					len(localOpt.WorkerServers),
    94  				)
    95  			}
    96  
    97  			tmpl, err := template.New(name).Parse(string(tpl))
    98  			if err != nil {
    99  				return err
   100  			}
   101  			content := bytes.NewBufferString("")
   102  			if err := tmpl.Execute(content, &localOpt); err != nil {
   103  				return err
   104  			}
   105  
   106  			fmt.Fprintln(cmd.OutOrStdout(), content.String())
   107  			return nil
   108  		},
   109  	}
   110  
   111  	cmd.Flags().BoolVar(&opt.Full, "full", false, "Print the full topology template for DM cluster.")
   112  	cmd.Flags().BoolVar(&opt.Local, "local", false, "Print and render template for deploying a simple DM cluster locally.")
   113  
   114  	// template values for rendering
   115  	cmd.Flags().StringVar(&localOpt.GlobalUser, "user", "tidb", "The user who runs the tidb cluster.")
   116  	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.")
   117  	cmd.Flags().StringVar(&localOpt.GlobalSystemdMode, "systemd_mode", "system", "systemd_mode is used to select whether to use sudo permissions.")
   118  	cmd.Flags().IntVar(&localOpt.GlobalSSHPort, "ssh-port", 22, "SSH port of servers in the managed cluster.")
   119  	cmd.Flags().StringVar(&localOpt.GlobalDeployDir, "deploy-dir", "/tidb-deploy", "Storage directory for cluster deployment files, startup scripts, and configuration files.")
   120  	cmd.Flags().StringVar(&localOpt.GlobalDataDir, "data-dir", "/tidb-data", "TiDB Cluster data storage directory.")
   121  	cmd.Flags().StringVar(&localOpt.GlobalArch, "arch", "amd64", "Supported values: \"amd64\", \"arm64\".")
   122  	cmd.Flags().StringSliceVar(&localOpt.MasterServers, "master-servers", []string{"172.19.0.101", "172.19.0.102", "172.19.0.103"}, "List of Master servers")
   123  	cmd.Flags().StringSliceVar(&localOpt.WorkerServers, "worker-servers", []string{"172.19.0.101", "172.19.0.102", "172.19.0.103"}, "List of Worker servers")
   124  	cmd.Flags().StringSliceVar(&localOpt.MonitoringServers, "monitoring-servers", []string{"172.19.0.101"}, "List of monitor servers")
   125  	cmd.Flags().StringSliceVar(&localOpt.GrafanaServers, "grafana-servers", []string{"172.19.0.101"}, "List of grafana servers")
   126  	cmd.Flags().StringSliceVar(&localOpt.AlertManagerServers, "alertmanager-servers", []string{"172.19.0.101"}, "List of alermanager servers")
   127  
   128  	return cmd
   129  }