diff --git a/components/playground/main.go b/components/playground/main.go index 87df748b30..ed30625421 100644 --- a/components/playground/main.go +++ b/components/playground/main.go @@ -115,12 +115,17 @@ Examples: opt.version = args[0] } + dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) + if dataDir == "" { + return errors.Errorf("cannot read environment variable %s", localdata.EnvNameInstanceDataDir) + } + port, err := utils.GetFreePort("0.0.0.0", 9527) if err != nil { return errors.AddStack(err) } - err = dumpPort(port) - p := NewPlayground(port) + err = dumpPort(filepath.Join(dataDir, "port"), port) + p := NewPlayground(dataDir, port) if err != nil { return errors.AddStack(err) } @@ -321,8 +326,8 @@ func getAbsolutePath(path string) (string, error) { return absPath, nil } -func dumpPort(port int) error { - return ioutil.WriteFile("port", []byte(strconv.Itoa(port)), 0644) +func dumpPort(fname string, port int) error { + return ioutil.WriteFile(fname, []byte(strconv.Itoa(port)), 0644) } func loadPort(dir string) (port int, err error) { @@ -335,12 +340,12 @@ func loadPort(dir string) (port int, err error) { return } -func dumpDSN(dbs []*instance.TiDBInstance) { +func dumpDSN(fname string, dbs []*instance.TiDBInstance) { var dsn []string for _, db := range dbs { dsn = append(dsn, fmt.Sprintf("mysql://root@%s", db.Addr())) } - _ = ioutil.WriteFile("dsn", []byte(strings.Join(dsn, "\n")), 0644) + _ = ioutil.WriteFile(fname, []byte(strings.Join(dsn, "\n")), 0644) } func newEtcdClient(endpoint string) (*clientv3.Client, error) { diff --git a/components/playground/playground.go b/components/playground/playground.go index cb34cd7b83..0a49701d11 100755 --- a/components/playground/playground.go +++ b/components/playground/playground.go @@ -38,7 +38,6 @@ import ( "github.com/pingcap/tiup/pkg/cliutil/progress" "github.com/pingcap/tiup/pkg/cluster/api" "github.com/pingcap/tiup/pkg/environment" - "github.com/pingcap/tiup/pkg/localdata" "github.com/pingcap/tiup/pkg/repository/v0manifest" "github.com/pingcap/tiup/pkg/utils" "golang.org/x/mod/semver" @@ -50,7 +49,8 @@ const forceKillAfterDuration = time.Second * 10 // Playground represent the playground of a cluster. type Playground struct { - booted bool + dataDir string + booted bool // the latest receive signal curSig int32 bootOptions *bootOptions @@ -82,8 +82,9 @@ type MonitorInfo struct { } // NewPlayground create a Playground instance. -func NewPlayground(port int) *Playground { +func NewPlayground(dataDir string, port int) *Playground { return &Playground{ + dataDir: dataDir, port: port, idAlloc: make(map[string]int), } @@ -582,10 +583,7 @@ func (p *Playground) addInstance(componentID string, cfg instance.Config) (ins i } } - dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) - if dataDir == "" { - return nil, fmt.Errorf("cannot read environment variable %s", localdata.EnvNameInstanceDataDir) - } + dataDir := p.dataDir id := p.allocID(componentID) dir := filepath.Join(dataDir, fmt.Sprintf("%s-%d", componentID, id)) @@ -873,7 +871,7 @@ func (p *Playground) bootCluster(ctx context.Context, env *environment.Environme } } - dumpDSN(p.tidbs) + dumpDSN(filepath.Join(p.dataDir, "dsn"), p.tidbs) go func() { // fmt.Printf("serve at :%d\n", p.port) @@ -980,7 +978,7 @@ func (p *Playground) bootMonitor(ctx context.Context, env *environment.Environme options := p.bootOptions monitorInfo := &MonitorInfo{} - dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) + dataDir := p.dataDir promDir := filepath.Join(dataDir, "prometheus") monitor, err := newMonitor(ctx, options.version, options.host, promDir) @@ -1021,7 +1019,7 @@ func (p *Playground) bootGrafana(ctx context.Context, env *environment.Environme return nil, errors.AddStack(err) } - dataDir := os.Getenv(localdata.EnvNameInstanceDataDir) + dataDir := p.dataDir grafanaDir := filepath.Join(dataDir, "grafana") cmd := exec.Command("cp", "-r", installPath, grafanaDir) diff --git a/tests/tiup-playground/test_playground.sh b/tests/tiup-playground/test_playground.sh index 74340027bc..d22500f98d 100755 --- a/tests/tiup-playground/test_playground.sh +++ b/tests/tiup-playground/test_playground.sh @@ -15,6 +15,7 @@ curl https://tiup-mirrors.pingcap.com/root.json -o $TMP_DIR/home/bin/root.json rm -rf $TIUP_HOME/data mkdir -p $TIUP_HOME/data export TIUP_INSTANCE_DATA_DIR=$TIUP_HOME/data/test_play +mkdir $TIUP_INSTANCE_DATA_DIR mkdir -p $TEST_DIR/cover @@ -56,10 +57,6 @@ trap "kill_all > /dev/null 2>&1" EXIT # wait start cluster successfully timeout 300 grep -q "CLUSTER START SUCCESSFULLY" <(tail -f $outfile) -# playground dump this at the wd -sleep 3 -mv ./dsn $TIUP_INSTANCE_DATA_DIR/ -mv ./port $TIUP_INSTANCE_DATA_DIR/ tiup-playground display | grep -qv "exit" tiup-playground scale-out --db 2