Skip to content

Commit

Permalink
*: remove v0manifest (#1078)
Browse files Browse the repository at this point in the history
* remove v0manifest from repository & env

* remove redudant code in repository

* remove v0manifest from profile

* remove v0manifest

* remove more redudant code

Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
AstroProfundis and ti-chi-bot authored Jan 20, 2021
1 parent d9609b5 commit 57e13bd
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 959 deletions.
22 changes: 1 addition & 21 deletions cmd/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,10 @@ func rebuildArgs(args []string) []string {
}

func usageTemplate(profile *localdata.Profile) string {
var installComps string
if repo := profile.Manifest(); repo != nil && len(repo.Components) > 0 {
installComps = `
Available Components:
`
var maxNameLen int
for _, comp := range repo.Components {
if len(comp.Name) > maxNameLen {
maxNameLen = len(comp.Name)
}
}

for _, comp := range repo.Components {
if !comp.Standalone {
continue
}
installComps += fmt.Sprintf(" %s%s %s\n", comp.Name, strings.Repeat(" ", maxNameLen-len(comp.Name)), comp.Desc)
}
} else {
installComps = `
installComps := `
Components Manifest:
use "tiup list" to fetch the latest components manifest
`
}

return `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if gt (len .Aliases) 0}}
Expand Down
6 changes: 2 additions & 4 deletions pkg/cluster/executor/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ func TestLocalExecuteWithQuotes(t *testing.T) {
`ls '/tmp'`,
}
for _, cmd := range cmds {
for _, sudo := range []bool{true, false} {
_, _, err = local.Execute(cmd, sudo)
assert.Nil(err)
}
_, _, err = local.Execute(cmd, false)
assert.Nil(err)
}
}
192 changes: 24 additions & 168 deletions pkg/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/localdata"
"github.com/pingcap/tiup/pkg/repository"
"github.com/pingcap/tiup/pkg/repository/v0manifest"
"github.com/pingcap/tiup/pkg/repository/v1manifest"
pkgver "github.com/pingcap/tiup/pkg/repository/version"
"github.com/pingcap/tiup/pkg/verbose"
"github.com/pingcap/tiup/pkg/version"
"golang.org/x/mod/semver"
)

// EnvNameV0 is the name of the env var used to direct TiUP to use old manifests.
const EnvNameV0 = "TIUP_USE_V0"

// Name of components
const (
TiUPName = "tiup"
tiupName = "tiup"
)

// Mirror return mirror of tiup.
Expand Down Expand Up @@ -77,12 +72,10 @@ type Environment struct {
profile *localdata.Profile
// repo represents the components repository of TiUP, it can be a
// local file system or a HTTP URL
repo *repository.Repository
v1Repo *repository.V1Repository
}

// InitEnv creates a new Environment object configured using env vars and defaults. Uses the EnvNameV0 env var to
// determine whether to use v0 or v1 manifests.
// InitEnv creates a new Environment object configured using env vars and defaults.
func InitEnv(options repository.Options) (*Environment, error) {
if env := GlobalEnv(); env != nil {
return env, nil
Expand All @@ -99,38 +92,19 @@ func InitEnv(options repository.Options) (*Environment, error) {
return nil, err
}

var repo *repository.Repository
var v1repo *repository.V1Repository
var err error

if env := os.Getenv(EnvNameV0); env == "" || env == "disable" || env == "false" {
var local v1manifest.LocalManifests
local, err = v1manifest.NewManifests(profile)
if err != nil {
return nil, errors.Annotatef(err, "initial repository from mirror(%s) failed", mirrorAddr)
}
v1repo = repository.NewV1Repo(mirror, options, local)
} else {
repo, err = repository.NewRepository(mirror, options)
if err != nil {
return nil, err
}
var local v1manifest.LocalManifests
local, err = v1manifest.NewManifests(profile)
if err != nil {
return nil, errors.Annotatef(err, "initial repository from mirror(%s) failed", mirrorAddr)
}
v1repo = repository.NewV1Repo(mirror, options, local)

verbose.Log("Initialize repository finished in %s", time.Since(initRepo))

return &Environment{profile, repo, v1repo}, nil
}

// NewV0 creates a new Environment with the provided data. Note that environments created with this function do not
// support v1 repositories.
func NewV0(profile *localdata.Profile, repo *repository.Repository) *Environment {
return &Environment{profile, repo, nil}
}

// Repository returns the initialized repository
func (env *Environment) Repository() *repository.Repository {
return env.repo
return &Environment{profile, v1repo}, nil
}

// V1Repository returns the initialized v1 repository
Expand All @@ -145,11 +119,8 @@ func (env *Environment) Profile() *localdata.Profile {

// Close release resource of env.
func (env *Environment) Close() error {
if env.v1Repo != nil {
return nil
}

return env.repo.Close()
// no need for v1manifest
return nil
}

// SetProfile exports for test
Expand All @@ -164,70 +135,30 @@ func (env *Environment) LocalPath(path ...string) string {

// UpdateComponents updates or installs all components described by specs.
func (env *Environment) UpdateComponents(specs []string, nightly, force bool) error {
if env.v1Repo != nil {
var v1specs []repository.ComponentSpec
for _, spec := range specs {
component, v := ParseCompVersion(spec)
if component == TiUPName {
continue
}
v1specs = append(v1specs, repository.ComponentSpec{ID: component, Version: v.String(), Force: force, Nightly: nightly})
}
return env.v1Repo.UpdateComponents(v1specs)
}

manifest, err := env.latestManifest()
if err != nil {
return err
}
var v1specs []repository.ComponentSpec
for _, spec := range specs {
component, v := ParseCompVersion(spec)
if component == TiUPName {
if component == tiupName {
continue
}
if nightly {
v = version.NightlyVersion
}
if !manifest.HasComponent(component) {
compInfo, found := manifest.FindComponent(component)
if !found {
return fmt.Errorf("component `%s` not found", component)
}

if !compInfo.IsSupport(env.PlatformString()) {
return fmt.Errorf("component `%s` does not support `%s`", component, env.PlatformString())
}
}

err := env.downloadComponent(component, v, v.IsNightly() || force)
if err != nil {
return err
}
v1specs = append(v1specs, repository.ComponentSpec{ID: component, Version: v.String(), Force: force, Nightly: nightly})
}
return nil
return env.v1Repo.UpdateComponents(v1specs)
}

// PlatformString returns a string identifying the current system.
func (env *Environment) PlatformString() string {
if env.v1Repo != nil {
return env.v1Repo.PlatformString()
}

return repository.PlatformString(env.repo.GOOS, env.repo.GOARCH)
return env.v1Repo.PlatformString()
}

// SelfUpdate updates TiUP.
func (env *Environment) SelfUpdate() error {
if env.v1Repo != nil {
if err := env.v1Repo.DownloadTiUP(env.LocalPath("bin")); err != nil {
return err
}

// Cover the root.json from tiup.bar.gz
return localdata.InitProfile().ResetMirror(Mirror(), "")
if err := env.v1Repo.DownloadTiUP(env.LocalPath("bin")); err != nil {
return err
}

return env.repo.DownloadTiUP(env.LocalPath("bin"))
// Cover the root.json from tiup.bar.gz
return localdata.InitProfile().ResetMirror(Mirror(), "")
}

func (env *Environment) downloadComponentv1(component string, version pkgver.Version, overwrite bool) error {
Expand All @@ -242,38 +173,7 @@ func (env *Environment) downloadComponentv1(component string, version pkgver.Ver

// downloadComponent downloads the specific version of a component from repository
func (env *Environment) downloadComponent(component string, version pkgver.Version, overwrite bool) error {
if env.v1Repo != nil {
return env.downloadComponentv1(component, version, overwrite)
}

versions, err := env.repo.ComponentVersions(component)
if err != nil {
return err
}
err = env.profile.SaveVersions(component, versions)
if err != nil {
return err
}
if version.IsNightly() && versions.Nightly == nil {
fmt.Printf("The component `%s` does not have a nightly version; skipped.\n", component)
return nil
}
if version.IsEmpty() {
version = versions.LatestVersion()
}
if !overwrite {
// Ignore if installed
found, err := env.profile.VersionIsInstalled(component, version.String())
if err != nil {
return err
}
if found {
fmt.Printf("The component `%s:%s` has been installed.\n", component, version)
return nil
}
}

return env.repo.DownloadComponent(env.LocalPath(localdata.ComponentParentDir), component, version)
return env.downloadComponentv1(component, version, overwrite)
}

// SelectInstalledVersion selects the installed versions and the latest release version
Expand Down Expand Up @@ -327,34 +227,18 @@ func (env *Environment) DownloadComponentIfMissing(component string, version pkg
return version, nil
}

// latestManifest returns the latest v0 component manifest and refresh the local cache
func (env *Environment) latestManifest() (*v0manifest.ComponentManifest, error) {
manifest, err := env.repo.Manifest()
if err != nil {
return nil, err
}
if err := env.profile.SaveManifest(manifest); err != nil {
return nil, err
}
return manifest, err
}

// GetComponentInstalledVersion return the installed version of component.
func (env *Environment) GetComponentInstalledVersion(component string, version pkgver.Version) (pkgver.Version, error) {
return env.profile.GetComponentInstalledVersion(component, version)
}

// BinaryPath return the installed binary path.
func (env *Environment) BinaryPath(component string, version pkgver.Version) (string, error) {
if env.v1Repo != nil {
installPath, err := env.profile.ComponentInstalledPath(component, version)
if err != nil {
return "", err
}
return env.v1Repo.BinaryPath(installPath, component, string(version))
installPath, err := env.profile.ComponentInstalledPath(component, version)
if err != nil {
return "", err
}

return env.profile.BinaryPathV0(component, version)
return env.v1Repo.BinaryPath(installPath, component, string(version))
}

// ParseCompVersion parses component part from <component>[:version] specification
Expand All @@ -365,31 +249,3 @@ func ParseCompVersion(spec string) (string, pkgver.Version) {
}
return spec, ""
}

// IsSupportedComponent return true if support if platform support the component.
func (env *Environment) IsSupportedComponent(component string) bool {
if env.v1Repo != nil {
// TODO
return true
}

// check local manifest
manifest := env.Profile().Manifest()
if manifest != nil && manifest.HasComponent(component) {
return true
}

manifest, err := env.Repository().Manifest()
if err != nil {
fmt.Println("Fetch latest manifest error:", err)
return false
}
if err := env.Profile().SaveManifest(manifest); err != nil {
fmt.Println("Save latest manifest error:", err)
}
comp, found := manifest.FindComponent(component)
if !found {
return false
}
return comp.IsSupport(env.PlatformString())
}
5 changes: 0 additions & 5 deletions pkg/exec/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
Expand All @@ -39,10 +38,6 @@ import (
// RunComponent start a component and wait it
func RunComponent(env *environment.Environment, tag, spec, binPath string, args []string) error {
component, version := environment.ParseCompVersion(spec)
if !env.IsSupportedComponent(component) {
return fmt.Errorf("component `%s` does not support `%s/%s` (see `tiup list`)", component, runtime.GOOS, runtime.GOARCH)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
Loading

0 comments on commit 57e13bd

Please sign in to comment.