1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
"github.com/fatih/color"
"github.com/joomcode/errorx"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/components/dm/spec"
"github.com/pingcap/tiup/pkg/cluster/executor"
"github.com/pingcap/tiup/pkg/cluster/manager"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
cspec "github.com/pingcap/tiup/pkg/cluster/spec"
tiupmeta "github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/logger"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/proxy"
"github.com/pingcap/tiup/pkg/repository"
"github.com/pingcap/tiup/pkg/tui"
"github.com/pingcap/tiup/pkg/utils"
"github.com/pingcap/tiup/pkg/version"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var (
// nolint
errNS = errorx.NewNamespace("cmd")
rootCmd *cobra.Command
gOpt operator.Options
skipConfirm bool
log = logprinter.NewLogger("") // init default logger
)
var dmspec *cspec.SpecManager
var cm *manager.Manager
func init() {
logger.InitGlobalLogger()
tui.AddColorFunctionsForCobra()
cobra.EnableCommandSorting = false
nativeEnvVar := strings.ToLower(os.Getenv(localdata.EnvNameNativeSSHClient))
if nativeEnvVar == "true" || nativeEnvVar == "1" || nativeEnvVar == "enable" {
gOpt.NativeSSH = true
}
rootCmd = &cobra.Command{
Use: tui.OsArgs0(),
Short: "(EXPERIMENTAL) Deploy a DM cluster",
Long: `EXPERIMENTAL: This is an experimental feature, things may or may not work,
please backup your data before process.`,
SilenceUsage: true,
SilenceErrors: true,
Version: version.NewTiUPVersion().String(),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// populate logger
log.SetDisplayModeFromString(gOpt.DisplayMode)
var err error
var env *tiupmeta.Environment
if err = cspec.Initialize("dm"); err != nil {
return err
}
dmspec = spec.GetSpecManager()
logger.EnableAuditLog(cspec.AuditDir())
cm = manager.NewManager("dm", dmspec, log)
// Running in other OS/ARCH Should be fine we only download manifest file.
env, err = tiupmeta.InitEnv(repository.Options{
GOOS: "linux",
GOARCH: "amd64",
}, repository.MirrorOptions{})
if err != nil {
return err
}
tiupmeta.SetGlobalEnv(env)
if gOpt.NativeSSH {
gOpt.SSHType = executor.SSHTypeSystem
zap.L().Info("System ssh client will be used",
zap.String(localdata.EnvNameNativeSSHClient, os.Getenv(localdata.EnvNameNativeSSHClient)))
fmt.Println("The --native-ssh flag has been deprecated, please use --ssh=system")
}
err = proxy.MaybeStartProxy(
gOpt.SSHProxyHost,
gOpt.SSHProxyPort,
gOpt.SSHProxyUser,
gOpt.SSHProxyUsePassword,
gOpt.SSHProxyIdentity,
log,
)
if err != nil {
return perrs.Annotate(err, "start http-proxy")
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
proxy.MaybeStopProxy()
return tiupmeta.GlobalEnv().V1Repository().Mirror().Close()
},
}
tui.BeautifyCobraUsageAndHelp(rootCmd)
rootCmd.PersistentFlags().Uint64Var(&gOpt.SSHTimeout, "ssh-timeout", 5, "Timeout in seconds to connect host via SSH, ignored for operations that don't need an SSH connection.")
rootCmd.PersistentFlags().Uint64Var(&gOpt.OptTimeout, "wait-timeout", 120, "Timeout in seconds to wait for an operation to complete, ignored for operations that don't fit.")
rootCmd.PersistentFlags().BoolVarP(&skipConfirm, "yes", "y", false, "Skip all confirmations and assumes 'yes'")
rootCmd.PersistentFlags().BoolVar(&gOpt.NativeSSH, "native-ssh", gOpt.NativeSSH, "Use the SSH client installed on local system instead of the built-in one.")
rootCmd.PersistentFlags().StringVar((*string)(&gOpt.SSHType), "ssh", "", "The executor type: 'builtin', 'system', 'none'")
rootCmd.PersistentFlags().IntVarP(&gOpt.Concurrency, "concurrency", "c", 5, "max number of parallel tasks allowed")
rootCmd.PersistentFlags().StringVar(&gOpt.DisplayMode, "format", "default", "(EXPERIMENTAL) The format of output, available values are [default, json]")
rootCmd.PersistentFlags().StringVar(&gOpt.SSHProxyHost, "ssh-proxy-host", "", "The SSH proxy host used to connect to remote host.")
rootCmd.PersistentFlags().StringVar(&gOpt.SSHProxyUser, "ssh-proxy-user", utils.CurrentUser(), "The user name used to login the proxy host.")
rootCmd.PersistentFlags().IntVar(&gOpt.SSHProxyPort, "ssh-proxy-port", 22, "The port used to login the proxy host.")
rootCmd.PersistentFlags().StringVar(&gOpt.SSHProxyIdentity, "ssh-proxy-identity-file", path.Join(utils.UserHome(), ".ssh", "id_rsa"), "The identity file used to login the proxy host.")
rootCmd.PersistentFlags().BoolVar(&gOpt.SSHProxyUsePassword, "ssh-proxy-use-password", false, "Use password to login the proxy host.")
rootCmd.PersistentFlags().Uint64Var(&gOpt.SSHProxyTimeout, "ssh-proxy-timeout", 5, "Timeout in seconds to connect the proxy host via SSH, ignored for operations that don't need an SSH connection.")
_ = rootCmd.PersistentFlags().MarkHidden("native-ssh")
_ = rootCmd.PersistentFlags().MarkHidden("ssh-proxy-host")
_ = rootCmd.PersistentFlags().MarkHidden("ssh-proxy-user")
_ = rootCmd.PersistentFlags().MarkHidden("ssh-proxy-port")
_ = rootCmd.PersistentFlags().MarkHidden("ssh-proxy-identity-file")
_ = rootCmd.PersistentFlags().MarkHidden("ssh-proxy-use-password")
_ = rootCmd.PersistentFlags().MarkHidden("ssh-proxy-timeout")
rootCmd.AddCommand(
newDeployCmd(),
newStartCmd(),
newStopCmd(),
newRestartCmd(),
newListCmd(),
newDestroyCmd(),
newAuditCmd(),
newExecCmd(),
newEditConfigCmd(),
newDisplayCmd(),
newPruneCmd(),
newReloadCmd(),
newUpgradeCmd(),
newPatchCmd(),
newScaleOutCmd(),
newScaleInCmd(),
newImportCmd(),
newEnableCmd(),
newDisableCmd(),
newReplayCmd(),
newTemplateCmd(),
newMetaCmd(),
newRotateSSHCmd(),
)
}
func printErrorMessageForNormalError(err error) {
_, _ = tui.ColorErrorMsg.Fprintf(os.Stderr, "\nError: %s\n", err.Error())
}
func printErrorMessageForErrorX(err *errorx.Error) {
msg := ""
ident := 0
causeErrX := err
for causeErrX != nil {
if ident > 0 {
msg += strings.Repeat(" ", ident) + "caused by: "
}
currentErrMsg := causeErrX.Message()
if len(currentErrMsg) > 0 {
if ident == 0 {
// Print error code only for top level error
msg += fmt.Sprintf("%s (%s)\n", currentErrMsg, causeErrX.Type().FullName())
} else {
msg += fmt.Sprintf("%s\n", currentErrMsg)
}
ident++
}
cause := causeErrX.Cause()
if c := errorx.Cast(cause); c != nil {
causeErrX = c
} else {
if cause != nil {
if ident > 0 {
// Out most error may have empty message. In this case we treat it as a transparent error.
// Thus `ident == 0` can be possible.
msg += strings.Repeat(" ", ident) + "caused by: "
}
msg += fmt.Sprintf("%s\n", cause.Error())
}
break
}
}
_, _ = tui.ColorErrorMsg.Fprintf(os.Stderr, "\nError: %s", msg)
}
func extractSuggestionFromErrorX(err *errorx.Error) string {
cause := err
for cause != nil {
v, ok := cause.Property(utils.ErrPropSuggestion)
if ok {
if s, ok := v.(string); ok {
return s
}
}
cause = errorx.Cast(cause.Cause())
}
return ""
}
// Execute executes the root command
func Execute() {
zap.L().Info("Execute command", zap.String("command", tui.OsArgs()))
zap.L().Debug("Environment variables", zap.Strings("env", os.Environ()))
code := 0
err := rootCmd.Execute()
if err != nil {
code = 1
}
zap.L().Info("Execute command finished", zap.Int("code", code), zap.Error(err))
if err != nil {
switch strings.ToLower(gOpt.DisplayMode) {
case "json":
obj := struct {
Err string `json:"error"`
}{
Err: err.Error(),
}
data, err := json.Marshal(obj)
if err != nil {
fmt.Printf("{\"error\": \"%s\"}", err)
break
}
fmt.Fprintln(os.Stderr, string(data))
default:
if errx := errorx.Cast(err); errx != nil {
printErrorMessageForErrorX(errx)
} else {
printErrorMessageForNormalError(err)
}
if !errorx.HasTrait(err, utils.ErrTraitPreCheck) {
logger.OutputDebugLog("tiup-dm")
}
if errx := errorx.Cast(err); errx != nil {
if suggestion := extractSuggestionFromErrorX(errx); len(suggestion) > 0 {
_, _ = fmt.Fprintf(os.Stderr, "\n%s\n", suggestion)
}
}
}
}
err = logger.OutputAuditLogIfEnabled()
if err != nil {
zap.L().Warn("Write audit log file failed", zap.Error(err))
code = 1
}
color.Unset()
if code != 0 {
os.Exit(code)
}
}
|