-
Notifications
You must be signed in to change notification settings - Fork 309
/
root.go
353 lines (313 loc) · 9.86 KB
/
root.go
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// 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 (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/joomcode/errorx"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/executor"
"github.com/pingcap/tiup/pkg/cluster/manager"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/colorutil"
tiupmeta "github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/errutil"
"github.com/pingcap/tiup/pkg/flags"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/logger"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/repository"
"github.com/pingcap/tiup/pkg/telemetry"
"github.com/pingcap/tiup/pkg/version"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var (
errNS = errorx.NewNamespace("cmd")
rootCmd *cobra.Command
gOpt operator.Options
skipConfirm bool
reportEnabled bool // is telemetry report enabled
teleReport *telemetry.Report
clusterReport *telemetry.ClusterReport
teleNodeInfos []*telemetry.NodeInfo
teleTopology string
teleCommand []string
)
var tidbSpec *spec.SpecManager
var cm *manager.Manager
func scrubClusterName(n string) string {
// prepend the telemetry secret to cluster name, so that two installations
// of tiup with the same cluster name produce different hashes
return "cluster_" + telemetry.SaltedHash(n)
}
func getParentNames(cmd *cobra.Command) []string {
if cmd == nil {
return nil
}
p := cmd.Parent()
// always use 'cluster' as the root command name
if cmd.Parent() == nil {
return []string{"cluster"}
}
return append(getParentNames(p), cmd.Name())
}
func init() {
logger.InitGlobalLogger()
colorutil.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: cliutil.OsArgs0(),
Short: "Deploy a TiDB cluster for production",
SilenceUsage: true,
SilenceErrors: true,
Version: version.NewTiUPVersion().String(),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
var env *tiupmeta.Environment
if err = spec.Initialize("cluster"); err != nil {
return err
}
tidbSpec = spec.GetSpecManager()
cm = manager.NewManager("tidb", tidbSpec, spec.TiDBComponentVersion)
logger.EnableAuditLog(spec.AuditDir())
// Running in other OS/ARCH Should be fine we only download manifest file.
env, err = tiupmeta.InitEnv(repository.Options{
GOOS: "linux",
GOARCH: "amd64",
})
if err != nil {
return err
}
tiupmeta.SetGlobalEnv(env)
teleCommand = getParentNames(cmd)
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")
}
return nil
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
return tiupmeta.GlobalEnv().V1Repository().Mirror().Close()
},
}
cliutil.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.")
// the value of wait-timeout is also used for `systemctl` commands, as the default timeout of systemd for
// start/stop operations is 90s, the default value of this argument is better be longer than that
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, "(EXPERIMENTAL) Use the native SSH client installed on local system instead of the build-in one.")
rootCmd.PersistentFlags().StringVar((*string)(&gOpt.SSHType), "ssh", "", "(EXPERIMENTAL) The executor type: 'builtin', 'system', 'none'.")
_ = rootCmd.PersistentFlags().MarkHidden("native-ssh")
rootCmd.AddCommand(
newCheckCmd(),
newDeploy(),
newStartCmd(),
newStopCmd(),
newRestartCmd(),
newScaleInCmd(),
newScaleOutCmd(),
newDestroyCmd(),
newCleanCmd(),
newUpgradeCmd(),
newDisplayCmd(),
newPruneCmd(),
newListCmd(),
newAuditCmd(),
newImportCmd(),
newEditConfigCmd(),
newReloadCmd(),
newPatchCmd(),
newRenameCmd(),
newEnableCmd(),
newDisableCmd(),
newExecCmd(),
newPullCmd(),
newPushCmd(),
newTestCmd(), // hidden command for test internally
newTelemetryCmd(),
newReplayCmd(),
newTemplateCmd(),
)
}
func printErrorMessageForNormalError(err error) {
_, _ = colorutil.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 {
// The 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
}
}
_, _ = colorutil.ColorErrorMsg.Fprintf(os.Stderr, "\nError: %s", msg)
}
func extractSuggestionFromErrorX(err *errorx.Error) string {
cause := err
for cause != nil {
v, ok := cause.Property(errutil.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", cliutil.OsArgs()))
zap.L().Debug("Environment variables", zap.Strings("env", os.Environ()))
// Switch current work directory if running in TiUP component mode
if wd := os.Getenv(localdata.EnvNameWorkDir); wd != "" {
if err := os.Chdir(wd); err != nil {
zap.L().Warn("Failed to switch work directory", zap.String("working_dir", wd), zap.Error(err))
}
}
teleReport = new(telemetry.Report)
clusterReport = new(telemetry.ClusterReport)
teleReport.EventDetail = &telemetry.Report_Cluster{Cluster: clusterReport}
reportEnabled = telemetry.Enabled()
if reportEnabled {
eventUUID := os.Getenv(localdata.EnvNameTelemetryEventUUID)
if eventUUID == "" {
eventUUID = uuid.New().String()
}
teleReport.InstallationUUID = telemetry.GetUUID()
teleReport.EventUUID = eventUUID
teleReport.EventUnixTimestamp = time.Now().Unix()
teleReport.Version = telemetry.TiUPMeta()
}
start := time.Now()
code := 0
err := rootCmd.Execute()
if err != nil {
code = 1
}
zap.L().Info("Execute command finished", zap.Int("code", code), zap.Error(err))
if reportEnabled {
f := func() {
defer func() {
if r := recover(); r != nil {
if flags.DebugMode {
log.Debugf("Recovered in telemetry report: %v", r)
}
}
}()
clusterReport.ExitCode = int32(code)
clusterReport.Nodes = teleNodeInfos
if teleTopology != "" {
if data, err := telemetry.ScrubYaml(
[]byte(teleTopology),
map[string]struct{}{
"host": {},
"name": {},
"user": {},
"group": {},
"deploy_dir": {},
"data_dir": {},
"log_dir": {},
}, // fields to hash
map[string]struct{}{
"config": {},
"server_configs": {},
}, // fields to omit
telemetry.GetSecret(),
); err == nil {
clusterReport.Topology = (string(data))
}
}
clusterReport.TakeMilliseconds = uint64(time.Since(start).Milliseconds())
clusterReport.Command = strings.Join(teleCommand, " ")
tele := telemetry.NewTelemetry()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
err := tele.Report(ctx, teleReport)
if flags.DebugMode {
if err != nil {
log.Infof("report failed: %v", err)
}
fmt.Fprintf(os.Stderr, "report: %s\n", teleReport.String())
if data, err := json.Marshal(teleReport); err == nil {
log.Debugf("report: %s\n", string(data))
}
}
cancel()
}
f()
}
if err != nil {
if errx := errorx.Cast(err); errx != nil {
printErrorMessageForErrorX(errx)
} else {
printErrorMessageForNormalError(err)
}
if !errorx.HasTrait(err, errutil.ErrTraitPreCheck) {
logger.OutputDebugLog("tiup-cluster")
}
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)
}
}