github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/common/version.go (about) 1 // Copyright 2019 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 common 15 16 import ( 17 "context" 18 "fmt" 19 "strings" 20 21 "github.com/coreos/go-semver/semver" 22 "github.com/pingcap/errors" 23 "go.uber.org/zap" 24 25 "github.com/pingcap/tidb-lightning/lightning/log" 26 ) 27 28 // Version information. 29 var ( 30 ReleaseVersion = "None" 31 BuildTS = "None" 32 GitHash = "None" 33 GitBranch = "None" 34 GoVersion = "None" 35 ) 36 37 // GetRawInfo do what its name tells 38 func GetRawInfo() string { 39 var info string 40 info += fmt.Sprintf("Release Version: %s\n", ReleaseVersion) 41 info += fmt.Sprintf("Git Commit Hash: %s\n", GitHash) 42 info += fmt.Sprintf("Git Branch: %s\n", GitBranch) 43 info += fmt.Sprintf("UTC Build Time: %s\n", BuildTS) 44 info += fmt.Sprintf("Go Version: %s\n", GoVersion) 45 return info 46 } 47 48 // PrintInfo prints some information of the app, like git hash, binary build time, etc. 49 func PrintInfo(app string, callback func()) { 50 oldLevel := log.SetLevel(zap.InfoLevel) 51 defer log.SetLevel(oldLevel) 52 53 log.L().Info("Welcome to "+app, 54 zap.String("Release Version", ReleaseVersion), 55 zap.String("Git Commit Hash", GitHash), 56 zap.String("Git Branch", GitBranch), 57 zap.String("UTC Build Time", BuildTS), 58 zap.String("Go Version", GoVersion), 59 ) 60 61 if callback != nil { 62 callback() 63 } 64 } 65 66 // FetchPDVersion get pd version 67 func FetchPDVersion(ctx context.Context, tls *TLS, pdAddr string) (*semver.Version, error) { 68 var rawVersion string 69 err := tls.WithHost(pdAddr).GetJSON(ctx, "/pd/api/v1/config/cluster-version", &rawVersion) 70 if err != nil { 71 return nil, errors.Trace(err) 72 } 73 74 return semver.NewVersion(rawVersion) 75 } 76 77 func ExtractTiDBVersion(version string) (*semver.Version, error) { 78 // version format: "5.7.10-TiDB-v2.1.0-rc.1-7-g38c939f" 79 // ^~~~~~~~~^ we only want this part 80 // version format: "5.7.10-TiDB-v2.0.4-1-g06a0bf5" 81 // ^~~~^ 82 // version format: "5.7.10-TiDB-v2.0.7" 83 // ^~~~^ 84 // version format: "5.7.25-TiDB-v3.0.0-beta-211-g09beefbe0-dirty" 85 // ^~~~~~~~~^ 86 // The version is generated by `git describe --tags` on the TiDB repository. 87 versions := strings.Split(strings.TrimSuffix(version, "-dirty"), "-") 88 end := len(versions) 89 switch end { 90 case 3, 4: 91 case 5, 6: 92 end -= 2 93 default: 94 return nil, errors.Errorf("not a valid TiDB version: %s", version) 95 } 96 rawVersion := strings.Join(versions[2:end], "-") 97 rawVersion = strings.TrimPrefix(rawVersion, "v") 98 return semver.NewVersion(rawVersion) 99 }