Skip to content

Commit

Permalink
Evaluate absolute directory when user run playground (#620)
Browse files Browse the repository at this point in the history
Signed-off-by: Lonng <heng@lonng.org>
  • Loading branch information
lonng authored Jul 23, 2020
1 parent b191d55 commit 32db7e2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 16 deletions.
32 changes: 28 additions & 4 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -219,11 +220,34 @@ func hasDashboard(pdAddr string) bool {
return resp.StatusCode == 200
}

func getAbsolutePath(binPath string) string {
if !strings.HasPrefix(binPath, "/") && !strings.HasPrefix(binPath, "~") {
binPath = filepath.Join(os.Getenv(localdata.EnvNameWorkDir), binPath)
// getAbsolutePath returns the absolute path
func getAbsolutePath(path string) (string, error) {
if path == "" {
return "", nil
}
return binPath

if !filepath.IsAbs(path) && !strings.HasPrefix(path, "~/") {
wd := os.Getenv(localdata.EnvNameWorkDir)
if wd == "" {
return "", errors.New("playground running at non-tiup mode")
}
path = filepath.Join(wd, path)
}

if strings.HasPrefix(path, "~/") {
usr, err := user.Current()
if err != nil {
return "", errors.Annotatef(err, "retrieve user home failed")
}
path = filepath.Join(usr.HomeDir, path[2:])
}

absPath, err := filepath.Abs(path)
if err != nil {
return "", errors.AddStack(err)
}

return absPath, nil
}

func dumpPort(port int) error {
Expand Down
48 changes: 36 additions & 12 deletions components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (p *Playground) handleScaleIn(w io.Writer, pid int) error {
return nil
}

func (p *Playground) sanitizeConfig(boot instance.Config, cfg *instance.Config) {
func (p *Playground) sanitizeConfig(boot instance.Config, cfg *instance.Config) error {
if cfg.BinPath == "" {
cfg.BinPath = boot.BinPath
}
Expand All @@ -369,24 +369,31 @@ func (p *Playground) sanitizeConfig(boot instance.Config, cfg *instance.Config)
if cfg.Host == "" {
cfg.Host = boot.ConfigPath
}

path, err := getAbsolutePath(cfg.ConfigPath)
if err != nil {
return err
}
cfg.ConfigPath = path
return nil
}

func (p *Playground) sanitizeComponentConfig(cid string, cfg *instance.Config) {
func (p *Playground) sanitizeComponentConfig(cid string, cfg *instance.Config) error {
switch cid {
case "pd":
p.sanitizeConfig(p.bootOptions.pd, cfg)
return p.sanitizeConfig(p.bootOptions.pd, cfg)
case "tikv":
p.sanitizeConfig(p.bootOptions.tikv, cfg)
return p.sanitizeConfig(p.bootOptions.tikv, cfg)
case "tidb":
p.sanitizeConfig(p.bootOptions.tidb, cfg)
return p.sanitizeConfig(p.bootOptions.tidb, cfg)
case "tiflash":
p.sanitizeConfig(p.bootOptions.tiflash, cfg)
return p.sanitizeConfig(p.bootOptions.tiflash, cfg)
case "pump":
p.sanitizeConfig(p.bootOptions.pump, cfg)
return p.sanitizeConfig(p.bootOptions.pump, cfg)
case "drainer":
p.sanitizeConfig(p.bootOptions.drainer, cfg)
return p.sanitizeConfig(p.bootOptions.drainer, cfg)
default:
fmt.Printf("unknow %s in sanitizeConfig", cid)
return fmt.Errorf("unknow %s in sanitizeConfig", cid)
}
}

Expand Down Expand Up @@ -415,7 +422,10 @@ func (p *Playground) addWaitInstance(inst instance.Instance) {

func (p *Playground) handleScaleOut(w io.Writer, cmd *Command) error {
// Ignore Config.Num, alway one command as scale out one instance.
p.sanitizeComponentConfig(cmd.ComponentID, &cmd.Config)
err := p.sanitizeComponentConfig(cmd.ComponentID, &cmd.Config)
if err != nil {
return err
}
inst, err := p.addInstance(cmd.ComponentID, cmd.Config)
if err != nil {
return errors.AddStack(err)
Expand Down Expand Up @@ -549,11 +559,17 @@ func (p *Playground) enableBinlog() bool {

func (p *Playground) addInstance(componentID string, cfg instance.Config) (ins instance.Instance, err error) {
if cfg.BinPath != "" {
cfg.BinPath = getAbsolutePath(cfg.BinPath)
cfg.BinPath,err = getAbsolutePath(cfg.BinPath)
if err != nil {
return nil, err
}
}

if cfg.ConfigPath != "" {
cfg.ConfigPath = getAbsolutePath(cfg.ConfigPath)
cfg.ConfigPath,err = getAbsolutePath(cfg.ConfigPath)
if err != nil {
return nil, err
}
}

dataDir := os.Getenv(localdata.EnvNameInstanceDataDir)
Expand Down Expand Up @@ -610,6 +626,14 @@ func (p *Playground) addInstance(componentID string, cfg instance.Config) (ins i
}

func (p *Playground) bootCluster(env *environment.Environment, options *bootOptions) error {
for _, cfg := range []*instance.Config{&options.pd, &options.tidb, &options.tikv, &options.tiflash, &options.pump, &options.drainer} {
path, err := getAbsolutePath(cfg.ConfigPath)
if err != nil {
return errors.Annotatef(err, "cannot eval absolute directory: %s", cfg.ConfigPath)
}
cfg.ConfigPath = path
}

p.bootOptions = options

if options.pd.Num < 1 || options.tidb.Num < 1 || options.tikv.Num < 1 {
Expand Down
43 changes: 43 additions & 0 deletions components/playground/playground_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 main

import (
"os"
"os/user"
"path/filepath"
"testing"

"github.com/pingcap/tiup/pkg/localdata"
"github.com/tj/assert"
)

func TestPlaygroundAbsDir(t *testing.T) {
err := os.Setenv(localdata.EnvNameWorkDir, "/testing")
assert.Nil(t, err)

a, err := getAbsolutePath("./a")
assert.Nil(t, err)
assert.Equal(t, "/testing/a", a)

b, err := getAbsolutePath("../b")
assert.Nil(t, err)
assert.Equal(t, "/b", b)

u, err := user.Current()
assert.Nil(t, err)
c, err := getAbsolutePath("~/c/d/e")
assert.Nil(t, err)
assert.Equal(t, filepath.Join(u.HomeDir, "c/d/e"), c)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.5.1
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/tj/assert v0.0.0-20190920132354-ee03d75cd160
github.com/tj/go-termd v0.0.1
github.com/xo/usql v0.7.8
go.etcd.io/etcd v0.5.0-alpha.5.0.20191023171146-3cf2f69b5738
Expand Down

0 comments on commit 32db7e2

Please sign in to comment.