From 0f790d61f33328cd106ae47dd93faed340335ab2 Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Thu, 22 Jul 2021 20:00:16 +0800 Subject: [PATCH] playground: ignore version check error in some cases (#1495) --- components/playground/main.go | 39 +++++++++++++++++++++++++++++ components/playground/playground.go | 16 ------------ pkg/repository/v1_repository.go | 21 ++++++++++------ 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/components/playground/main.go b/components/playground/main.go index c4ccbe86a5..ab8f083f1e 100644 --- a/components/playground/main.go +++ b/components/playground/main.go @@ -36,6 +36,7 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tiup/components/playground/instance" "github.com/pingcap/tiup/pkg/cluster/api" + "github.com/pingcap/tiup/pkg/cluster/spec" "github.com/pingcap/tiup/pkg/environment" "github.com/pingcap/tiup/pkg/localdata" "github.com/pingcap/tiup/pkg/logger/log" @@ -47,6 +48,7 @@ import ( "github.com/spf13/pflag" clientv3 "go.etcd.io/etcd/client/v3" "go.uber.org/zap" + "golang.org/x/mod/semver" "gopkg.in/yaml.v3" ) @@ -233,6 +235,43 @@ Examples: } }() + // expand version string + if !semver.IsValid(options.Version) { + var version utils.Version + var err error + // If any of the binpath arguments is set (which indicates the user is + // using a self build binary) and version number is not set, we assume + // it is a developer and use the latest release version by default. + // The platform string used to resolve the full version number is set + // to "linux-amd64" as this is the platform that every released version + // is available. + // For platforms lacks of support for some versions, e.g., darwin-amd64, + // specifically set a valid version for it, or use custom binpath for + // all components used. + // If none of the binpath arguments is set, use the platform of the + // playground binary itself. + if options.TiDB.BinPath != "" || options.TiKV.BinPath != "" || + options.PD.BinPath != "" || options.TiFlash.BinPath != "" || + options.TiCDC.BinPath != "" || options.Pump.BinPath != "" || + options.Drainer.BinPath != "" && options.Version == "" { + version, err = env.V1Repository().ResolveComponentVersionWithPlatform(spec.ComponentTiDB, options.Version, "linux-amd64") + } else { + version, err = env.V1Repository().ResolveComponentVersion(spec.ComponentTiDB, options.Version) + } + if err != nil { + return errors.Annotate(err, fmt.Sprintf("can not expand version %s to a valid semver string", options.Version)) + } + fmt.Println(color.YellowString(`Using the version %s for version constraint "%s". + +If you'd like to use a TiDB version other than %s, cancel and retry with the following arguments: + Specify version manually: tiup playground + Specify version range: tiup playground ^5 + The nightly version: tiup playground nightly +`, version, options.Version, version)) + + options.Version = version.String() + } + bootErr := p.bootCluster(ctx, env, options) if bootErr != nil { // always kill all process started and wait before quit. diff --git a/components/playground/playground.go b/components/playground/playground.go index 5be9d01ac7..a417908fc4 100644 --- a/components/playground/playground.go +++ b/components/playground/playground.go @@ -678,22 +678,6 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme return fmt.Errorf("all components count must be great than 0 (tikv=%v, pd=%v)", options.TiKV.Num, options.PD.Num) } - { - version, err := env.V1Repository().ResolveComponentVersion(spec.ComponentTiDB, options.Version) - if err != nil { - return err - } - fmt.Println(color.YellowString(`Using the version %s for version constraint "%s". - -If you'd like to use a TiDB version other than %s, cancel and retry with the following arguments: - Specify version manually: tiup playground - Specify version range: tiup playground ^5 - The nightly version: tiup playground nightly -`, version, options.Version, version)) - - options.Version = version.String() - } - if !utils.Version(options.Version).IsNightly() { if semver.Compare(options.Version, "v3.1.0") < 0 && options.TiFlash.Num != 0 { fmt.Println(color.YellowString("Warning: current version %s doesn't support TiFlash", options.Version)) diff --git a/pkg/repository/v1_repository.go b/pkg/repository/v1_repository.go index 3b20522774..4dd55290e6 100644 --- a/pkg/repository/v1_repository.go +++ b/pkg/repository/v1_repository.go @@ -750,8 +750,8 @@ func (r *V1Repository) ComponentVersion(id, ver string, includeYanked bool) (*v1 return vi, nil } -// ResolveComponentVersion resolves the latest version of a component that satisfies the constraint -func (r *V1Repository) ResolveComponentVersion(id, constraint string) (utils.Version, error) { +// ResolveComponentVersionWithPlatform resolves the latest version of a component that satisfies the constraint +func (r *V1Repository) ResolveComponentVersionWithPlatform(id, constraint, platform string) (utils.Version, error) { manifest, err := r.FetchComponentManifest(id, false) if err != nil { return "", err @@ -765,8 +765,8 @@ func (r *V1Repository) ResolveComponentVersion(id, constraint string) (utils.Ver } ver = v.String() case utils.NightlyVersionAlias: - if !manifest.HasNightly(r.PlatformString()) { - return "", errors.Annotatef(ErrUnknownVersion, "component %s does not have nightly on %s", id, r.PlatformString()) + if !manifest.HasNightly(platform) { + return "", errors.Annotatef(ErrUnknownVersion, "component %s does not have nightly on %s", id, platform) } ver = manifest.Nightly default: @@ -774,7 +774,7 @@ func (r *V1Repository) ResolveComponentVersion(id, constraint string) (utils.Ver if err != nil { return "", err } - versions := manifest.VersionList(r.PlatformString()) + versions := manifest.VersionList(platform) verList := make([]string, 0, len(versions)) for v := range versions { if v == manifest.Nightly { @@ -793,15 +793,20 @@ func (r *V1Repository) ResolveComponentVersion(id, constraint string) (utils.Ver } } if ver == "" { - return "", fmt.Errorf(`no version on %s for component %s satisfies constraint "%s"`, r.PlatformString(), id, constraint) + return "", fmt.Errorf(`no version on %s for component %s satisfies constraint "%s"`, platform, id, constraint) } - vi := manifest.VersionItem(r.PlatformString(), ver, false) + vi := manifest.VersionItem(platform, ver, false) if vi == nil { - return "", errors.Annotatef(ErrUnknownVersion, "version %s on %s for component %s not found", ver, r.PlatformString(), id) + return "", errors.Annotatef(ErrUnknownVersion, "version %s on %s for component %s not found", ver, platform, id) } return utils.Version(ver), nil } +// ResolveComponentVersion resolves the latest version of a component that satisfies the constraint +func (r *V1Repository) ResolveComponentVersion(id, constraint string) (utils.Version, error) { + return r.ResolveComponentVersionWithPlatform(id, constraint, r.PlatformString()) +} + // LatestNightlyVersion returns the latest nightly version of specific component func (r *V1Repository) LatestNightlyVersion(id string) (utils.Version, *v1manifest.VersionItem, error) { com, err := r.FetchComponentManifest(id, false)