-
Notifications
You must be signed in to change notification settings - Fork 309
/
root.go
306 lines (279 loc) · 9.34 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
// 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 cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/google/uuid"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/environment"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/localdata"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/repository"
"github.com/pingcap/tiup/pkg/repository/v1manifest"
"github.com/pingcap/tiup/pkg/telemetry"
"github.com/pingcap/tiup/pkg/version"
"github.com/spf13/cobra"
)
var (
rootCmd *cobra.Command
repoOpts repository.Options
reportEnabled bool // is telemetry report enabled
eventUUID = uuid.New().String()
teleCommand string
log = logprinter.NewLogger("") // use default logger
)
// arguments
var (
binary string
binPath string
tag string
printVersion bool // not using cobra.Command.Version to make it possible to show component versions
)
func init() {
cobra.EnableCommandSorting = false
_ = os.Setenv(localdata.EnvNameTelemetryEventUUID, eventUUID)
rootCmd = &cobra.Command{
Use: `tiup [flags] <command> [args...]
tiup [flags] <component> [args...]`,
Long: `TiUP is a command-line component management tool that can help to download and install
TiDB platform components to the local system. You can run a specific version of a component via
"tiup <component>[:version]". If no version number is specified, the latest version installed
locally will be used. If the specified component does not have any version installed locally,
the latest stable version will be downloaded from the repository.`,
SilenceErrors: true,
FParseErrWhitelist: cobra.FParseErrWhitelist{UnknownFlags: true},
Args: func(cmd *cobra.Command, args []string) error {
// Support `tiup <component>`
return nil
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
teleCommand = cmd.CommandPath()
if printVersion && len(args) == 0 {
return nil
}
switch cmd.Name() {
case "init",
"set":
if cmd.HasParent() && cmd.Parent().Name() == "mirror" {
// skip environment init
break
}
fallthrough
default:
e, err := environment.InitEnv(repoOpts)
if err != nil {
if errors.Is(perrs.Cause(err), v1manifest.ErrLoadManifest) {
log.Warnf("Please check for root manifest file, you may download one from the repository mirror, or try `tiup mirror set` to force reset it.")
}
return err
}
environment.SetGlobalEnv(e)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if printVersion && len(args) == 0 {
fmt.Println(version.NewTiUPVersion().String())
return nil
}
env := environment.GlobalEnv()
if binary != "" {
component, ver := environment.ParseCompVersion(binary)
selectedVer, err := env.SelectInstalledVersion(component, ver)
if err != nil {
return err
}
binaryPath, err := env.BinaryPath(component, selectedVer)
if err != nil {
return err
}
fmt.Println(binaryPath)
return nil
}
if len(args) > 0 {
// We assume the first unknown parameter is the component name and following
// parameters will be transparent passed because registered flags and subcommands
// will be parsed correctly.
// e.g: tiup --tag mytag --rm playground --db 3 --pd 3 --kv 4
// => run "playground" with parameters "--db 3 --pd 3 --kv 4"
// tiup --tag mytag --binpath /xxx/tikv-server tikv
var transparentParams []string
componentSpec := args[0]
for i, arg := range os.Args {
if arg == componentSpec {
transparentParams = os.Args[i+1:]
break
}
}
if len(transparentParams) > 0 && transparentParams[0] == "--" {
transparentParams = transparentParams[1:]
}
teleCommand = fmt.Sprintf("%s %s", cmd.CommandPath(), componentSpec)
return tiupexec.RunComponent(env, tag, componentSpec, binPath, transparentParams)
}
return cmd.Help()
},
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
if env := environment.GlobalEnv(); env != nil {
return env.Close()
}
return nil
},
SilenceUsage: true,
// implement auto completion for tiup components
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
env := environment.GlobalEnv()
if len(args) == 0 {
var result []string
installed, _ := env.Profile().InstalledComponents()
for _, comp := range installed {
if strings.HasPrefix(comp, toComplete) {
result = append(result, comp)
}
}
return result, cobra.ShellCompDirectiveNoFileComp
}
component, version := environment.ParseCompVersion(args[0])
selectedVer, err := env.SelectInstalledVersion(component, version)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
binaryPath, err := env.BinaryPath(component, selectedVer)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
argv := []string{binaryPath, "__complete"}
argv = append(append(argv, args[1:]...), toComplete)
_ = syscall.Exec(binaryPath, argv, os.Environ())
return nil, cobra.ShellCompDirectiveNoFileComp
},
}
rootCmd.PersistentFlags().BoolVarP(&repoOpts.SkipVersionCheck, "skip-version-check", "", false, "Skip the strict version check, by default a version must be a valid SemVer string")
rootCmd.Flags().StringVar(&binary, "binary", "", "Print binary path of a specific version of a component `<component>[:version]`\n"+
"and the latest version installed will be selected if no version specified")
rootCmd.Flags().StringVarP(&tag, "tag", "T", "", "[Deprecated] Specify a tag for component instance")
rootCmd.Flags().StringVar(&binPath, "binpath", "", "Specify the binary path of component instance")
rootCmd.Flags().BoolVarP(&printVersion, "version", "v", false, "Print the version of tiup")
rootCmd.AddCommand(
newInstallCmd(),
newListCmd(),
newUninstallCmd(),
newUpdateCmd(),
newStatusCmd(),
newCleanCmd(),
newMirrorCmd(),
newTelemetryCmd(),
newEnvCmd(),
)
originHelpFunc := rootCmd.HelpFunc()
rootCmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
cmd, _, _ = cmd.Root().Find(args)
if len(args) < 2 || cmd != rootCmd {
originHelpFunc(cmd, args)
return
}
env, _ := environment.InitEnv(repoOpts)
environment.SetGlobalEnv(env)
_ = cmd.RunE(cmd, args)
})
rootCmd.SetHelpCommand(newHelpCmd())
// If env is inited before, localdata.InitProfile() will return a valid profile
// or it will return an invalid one but still print usage
rootCmd.SetUsageTemplate(usageTemplate(localdata.InitProfile()))
}
// Execute parses the command line arguments and calls proper functions
func Execute() {
start := time.Now()
code := 0
err := rootCmd.Execute()
if err != nil {
// use exit code from component
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
code = exitErr.ExitCode()
} else {
fmt.Fprintln(os.Stderr, color.RedString("Error: %v", err))
code = 1
}
}
teleReport := new(telemetry.Report)
tiupReport := new(telemetry.TiUPReport)
teleReport.EventDetail = &telemetry.Report_Tiup{Tiup: tiupReport}
env := environment.GlobalEnv()
if env == nil {
// if the env is not initialized, skip telemetry upload
// as many info are read from the env.
// TODO: split pure meta information from env object and
// us a dedicated package for that
reportEnabled = false
} else {
teleMeta, _, err := telemetry.GetMeta(env)
if err == nil {
reportEnabled = teleMeta.Status == telemetry.EnableStatus
teleReport.InstallationUUID = teleMeta.UUID
} // default to false on errors
}
if teleCommand == "tiup __complete" {
reportEnabled = false
}
if reportEnabled {
teleReport.EventUUID = eventUUID
teleReport.EventUnixTimestamp = start.Unix()
teleReport.Version = telemetry.TiUPMeta()
teleReport.Version.TiUPVersion = version.NewTiUPVersion().SemVer()
tiupReport.Command = teleCommand
tiupReport.CustomMirror = env.Profile().Config.Mirror != repository.DefaultMirror
if tag != "" {
tiupReport.Tag = telemetry.SaltedHash(tag)
}
f := func() {
defer func() {
if r := recover(); r != nil {
if environment.DebugMode {
log.Debugf("Recovered in telemetry report: %v", r)
}
}
}()
tiupReport.ExitCode = int32(code)
tiupReport.TakeMilliseconds = uint64(time.Since(start).Milliseconds())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
tele := telemetry.NewTelemetry()
err := tele.Report(ctx, teleReport)
if environment.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()
}
color.Unset()
if code != 0 {
os.Exit(code)
}
}