github.com/pingcap/tiup@v1.15.1/components/dm/command/deploy.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  	"context"
    18  	"path"
    19  
    20  	"github.com/pingcap/errors"
    21  	"github.com/pingcap/tiup/pkg/cluster/manager"
    22  	operator "github.com/pingcap/tiup/pkg/cluster/operation"
    23  	"github.com/pingcap/tiup/pkg/cluster/spec"
    24  	"github.com/pingcap/tiup/pkg/cluster/task"
    25  	"github.com/pingcap/tiup/pkg/tidbver"
    26  	"github.com/pingcap/tiup/pkg/tui"
    27  	"github.com/pingcap/tiup/pkg/utils"
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  func newDeployCmd() *cobra.Command {
    32  	opt := manager.DeployOptions{
    33  		IdentityFile: path.Join(utils.UserHome(), ".ssh", "id_rsa"),
    34  	}
    35  	cmd := &cobra.Command{
    36  		Use:          "deploy <cluster-name> <version> <topology.yaml>",
    37  		Short:        "Deploy a DM cluster for production",
    38  		Long:         "Deploy a DM cluster for production. SSH connection will be used to deploy files, as well as creating system users for running the service.",
    39  		SilenceUsage: true,
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			shouldContinue, err := tui.CheckCommandArgsAndMayPrintHelp(cmd, args, 3)
    42  			if err != nil {
    43  				return err
    44  			}
    45  			if !shouldContinue {
    46  				return nil
    47  			}
    48  
    49  			clusterName := args[0]
    50  			version, err := utils.FmtVer(args[1])
    51  			if err != nil {
    52  				return err
    53  			}
    54  			topoFile := args[2]
    55  
    56  			if err := supportVersion(version); err != nil {
    57  				return err
    58  			}
    59  
    60  			return cm.Deploy(clusterName, version, topoFile, opt, postDeployHook, skipConfirm, gOpt)
    61  		},
    62  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    63  			switch len(args) {
    64  			case 2:
    65  				return nil, cobra.ShellCompDirectiveDefault
    66  			default:
    67  				return nil, cobra.ShellCompDirectiveNoFileComp
    68  			}
    69  		},
    70  	}
    71  
    72  	cmd.Flags().StringVarP(&opt.User, "user", "u", utils.CurrentUser(), "The user name to login via SSH. The user must has root (or sudo) privilege.")
    73  	cmd.Flags().StringVarP(&opt.IdentityFile, "identity_file", "i", opt.IdentityFile, "The path of the SSH identity file. If specified, public key authentication will be used.")
    74  	cmd.Flags().BoolVarP(&opt.UsePassword, "password", "p", false, "Use password of target hosts. If specified, password authentication will be used.")
    75  
    76  	return cmd
    77  }
    78  
    79  func supportVersion(vs string) error {
    80  	if !tidbver.DMSupportDeploy(vs) {
    81  		return errors.Errorf("Only support version not less than v2.0")
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func postDeployHook(builder *task.Builder, topo spec.Topology, gOpt operator.Options) {
    88  	enableTask := task.NewBuilder(builder.Logger).Func("Setting service auto start on boot", func(ctx context.Context) error {
    89  		return operator.Enable(ctx, topo, operator.Options{}, true)
    90  	}).BuildAsStep("Enable service").SetHidden(true)
    91  
    92  	builder.Parallel(false, enableTask)
    93  }