Skip to content

Commit

Permalink
tiup/playground: refactor RunComponent (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexustar authored Feb 9, 2022
1 parent e622c23 commit 15cd08a
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 298 deletions.
19 changes: 15 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
"time"
Expand All @@ -27,7 +28,7 @@ import (
"github.com/google/uuid"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/exec"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/localdata"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/repository"
Expand Down Expand Up @@ -132,8 +133,12 @@ the latest stable version will be downloaded from the repository.`,
break
}
}
if len(transparentParams) > 0 && transparentParams[0] == "--" {
transparentParams = transparentParams[1:]
}

teleCommand = fmt.Sprintf("%s %s", cmd.CommandPath(), componentSpec)
return exec.RunComponent(env, tag, componentSpec, binPath, transparentParams)
return tiupexec.RunComponent(env, tag, componentSpec, binPath, transparentParams)
}
return cmd.Help()
},
Expand Down Expand Up @@ -221,8 +226,14 @@ func Execute() {

err := rootCmd.Execute()
if err != nil {
fmt.Println(color.RedString("Error: %v", err))
code = 1
// use exit code from component
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
code = exitErr.ExitCode()
} else {
fmt.Fprintln(os.Stderr, color.RedString("Error: %v", err))
code = 1
}
}

teleReport := new(telemetry.Report)
Expand Down
19 changes: 3 additions & 16 deletions components/playground/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/tiup/components/playground/instance"
"github.com/pingcap/tiup/pkg/environment"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)
Expand Down Expand Up @@ -199,23 +198,11 @@ http_port = %d
fmt.Sprintf("cfg:default.paths.logs=%s", path.Join(dir, "log")),
}

env := environment.GlobalEnv()
params := &tiupexec.PrepareCommandParams{
Ctx: ctx,
Component: "grafana",
Version: utils.Version(g.version),
InstanceDir: dir,
WD: dir,
Args: args,
SysProcAttr: instance.SysProcAttr,
Env: env,
}
cmd, err := tiupexec.PrepareCommand(params)
if err != nil {
var binPath string
if binPath, err = tiupexec.PrepareBinary("grafana", utils.Version(g.version), binPath); err != nil {
return err
}
cmd.Stdout = nil
cmd.Stderr = nil
cmd := instance.PrepareCommand(ctx, binPath, args, nil, dir)

g.cmd = cmd

Expand Down
6 changes: 4 additions & 2 deletions components/playground/instance/drainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"path/filepath"
"strings"

tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -84,10 +85,11 @@ func (d *Drainer) Start(ctx context.Context, version utils.Version) error {
}

var err error
if d.Process, err = NewComponentProcess(ctx, d.Dir, d.BinPath, "drainer", version, args...); err != nil {
if d.BinPath, err = tiupexec.PrepareBinary("drainer", version, d.BinPath); err != nil {
return err
}
logIfErr(d.Process.SetOutputFile(d.LogFile()))
d.Process = &process{cmd: PrepareCommand(ctx, d.BinPath, args, nil, d.Dir)}

logIfErr(d.Process.SetOutputFile(d.LogFile()))
return d.Process.Start()
}
6 changes: 4 additions & 2 deletions components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"

"github.com/pingcap/errors"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -98,11 +99,12 @@ func (inst *PDInstance) Start(ctx context.Context, version utils.Version) error
}

var err error
if inst.Process, err = NewComponentProcess(ctx, inst.Dir, inst.BinPath, "pd", version, args...); err != nil {
if inst.BinPath, err = tiupexec.PrepareBinary("pd", version, inst.BinPath); err != nil {
return err
}
logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, nil, inst.Dir)}

logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
return inst.Process.Start()
}

Expand Down
49 changes: 8 additions & 41 deletions components/playground/instance/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package instance

import (
"context"
"fmt"
"io"
"os"
"os/exec"
"sync"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/environment"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

// Process represent process to be run by playground
Expand Down Expand Up @@ -85,42 +81,13 @@ func (p *process) Cmd() *exec.Cmd {
return p.cmd
}

// NewComponentProcessWithEnvs create a Process instance with given environment variables.
func NewComponentProcessWithEnvs(ctx context.Context, dir, binPath, component string, version utils.Version, envs map[string]string, arg ...string) (Process, error) {
if dir == "" {
panic("dir must be set")
}
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}

env := environment.GlobalEnv()
params := &tiupexec.PrepareCommandParams{
Ctx: ctx,
Component: component,
Version: version,
BinPath: binPath,
InstanceDir: dir,
WD: dir,
Args: arg,
EnvVariables: make([]string, 0),
SysProcAttr: SysProcAttr,
Env: env,
}
for k, v := range envs {
pair := fmt.Sprintf("%s=%s", k, v)
params.EnvVariables = append(params.EnvVariables, pair)
}

cmd, err := tiupexec.PrepareCommand(params)
if err != nil {
return nil, err
}

return &process{cmd: cmd}, nil
}
// PrepareCommand return command for playground
func PrepareCommand(ctx context.Context, binPath string, args, envs []string, workDir string) *exec.Cmd {
c := exec.CommandContext(ctx, binPath, args...)

// NewComponentProcess create a Process instance.
func NewComponentProcess(ctx context.Context, dir, binPath, component string, version utils.Version, arg ...string) (Process, error) {
return NewComponentProcessWithEnvs(ctx, dir, binPath, component, version, nil, arg...)
c.Env = os.Environ()
c.Env = append(c.Env, envs...)
c.Dir = workDir
c.SysProcAttr = SysProcAttr
return c
}
6 changes: 4 additions & 2 deletions components/playground/instance/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"
"time"

tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -103,11 +104,12 @@ func (p *Pump) Start(ctx context.Context, version utils.Version) error {
}

var err error
if p.Process, err = NewComponentProcess(ctx, p.Dir, p.BinPath, "pump", version, args...); err != nil {
if p.BinPath, err = tiupexec.PrepareBinary("pump", version, p.BinPath); err != nil {
return err
}
logIfErr(p.Process.SetOutputFile(p.LogFile()))
p.Process = &process{cmd: PrepareCommand(ctx, p.BinPath, args, nil, p.Dir)}

logIfErr(p.Process.SetOutputFile(p.LogFile()))
return p.Process.Start()
}

Expand Down
8 changes: 5 additions & 3 deletions components/playground/instance/ticdc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"path/filepath"
"strings"

tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
"golang.org/x/mod/semver"
)
Expand Down Expand Up @@ -71,17 +72,18 @@ func (c *TiCDC) Start(ctx context.Context, version utils.Version) error {
}

var err error
if c.Process, err = NewComponentProcess(ctx, c.Dir, c.BinPath, "cdc", version, args...); err != nil {
if c.BinPath, err = tiupexec.PrepareBinary("cdc", version, c.BinPath); err != nil {
return err
}
logIfErr(c.Process.SetOutputFile(c.LogFile()))
c.Process = &process{cmd: PrepareCommand(ctx, c.BinPath, args, nil, c.Dir)}

logIfErr(c.Process.SetOutputFile(c.LogFile()))
return c.Process.Start()
}

// Component return component name.
func (c *TiCDC) Component() string {
return "ticdc"
return "cdc"
}

// LogFile return the log file.
Expand Down
6 changes: 4 additions & 2 deletions components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"
"strings"

tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -71,11 +72,12 @@ func (inst *TiDBInstance) Start(ctx context.Context, version utils.Version) erro
}

var err error
if inst.Process, err = NewComponentProcess(ctx, inst.Dir, inst.BinPath, "tidb", version, args...); err != nil {
if inst.BinPath, err = tiupexec.PrepareBinary("tidb", version, inst.BinPath); err != nil {
return err
}
logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, nil, inst.Dir)}

logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
return inst.Process.Start()
}

Expand Down
36 changes: 7 additions & 29 deletions components/playground/instance/tiflash.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"os/exec"
"path"
"path/filepath"
"runtime"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/environment"
"github.com/pingcap/tiup/pkg/repository"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -141,24 +139,8 @@ func (inst *TiFlashInstance) Start(ctx context.Context, version utils.Version) e
return err
}

// TiFlash needs to obtain absolute path of cluster_manager
if inst.BinPath == "" {
env, err := environment.InitEnv(repository.Options{
SkipVersionCheck: false,
GOOS: runtime.GOOS,
GOARCH: runtime.GOARCH,
DisableDecompress: false,
})
if err != nil {
return err
}
if version, err = env.DownloadComponentIfMissing("tiflash", version); err != nil {
return err
}
// version may be empty, we will use the latest stable version later in Start cmd.
if inst.BinPath, err = env.BinaryPath("tiflash", version); err != nil {
return err
}
if inst.BinPath, err = tiupexec.PrepareBinary("tiflash", version, inst.BinPath); err != nil {
return err
}

dirPath := filepath.Dir(inst.BinPath)
Expand All @@ -167,20 +149,16 @@ func (inst *TiFlashInstance) Start(ctx context.Context, version utils.Version) e
return err
}

if err = os.Setenv("LD_LIBRARY_PATH", fmt.Sprintf("%s:$LD_LIBRARY_PATH", dirPath)); err != nil {
return err
}

args := []string{
"server",
fmt.Sprintf("--config-file=%s", inst.ConfigPath),
}

if inst.Process, err = NewComponentProcess(ctx, inst.Dir, inst.BinPath, "tiflash", version, args...); err != nil {
return err
envs := []string{
fmt.Sprintf("LD_LIBRARY_PATH=%s:$LD_LIBRARY_PATH", dirPath),
}
logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, envs, inst.Dir)}

logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
return inst.Process.Start()
}

Expand Down
9 changes: 5 additions & 4 deletions components/playground/instance/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/pingcap/errors"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)

Expand Down Expand Up @@ -70,14 +71,14 @@ func (inst *TiKVInstance) Start(ctx context.Context, version utils.Version) erro
fmt.Sprintf("--log-file=%s", inst.LogFile()),
}

envs := []string{"MALLOC_CONF=prof:true,prof_active:false"}
var err error
envs := make(map[string]string)
envs["MALLOC_CONF"] = "prof:true,prof_active:false"
if inst.Process, err = NewComponentProcessWithEnvs(ctx, inst.Dir, inst.BinPath, "tikv", version, envs, args...); err != nil {
if inst.BinPath, err = tiupexec.PrepareBinary("tikv", version, inst.BinPath); err != nil {
return err
}
logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
inst.Process = &process{cmd: PrepareCommand(ctx, inst.BinPath, args, envs, inst.Dir)}

logIfErr(inst.Process.SetOutputFile(inst.LogFile()))
return inst.Process.Start()
}

Expand Down
17 changes: 3 additions & 14 deletions components/playground/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/pingcap/errors"
"github.com/pingcap/tiup/components/playground/instance"
"github.com/pingcap/tiup/pkg/environment"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)
Expand Down Expand Up @@ -136,21 +135,11 @@ scrape_configs:
fmt.Sprintf("--storage.tsdb.path=%s", filepath.Join(dir, "data")),
}

env := environment.GlobalEnv()
params := &tiupexec.PrepareCommandParams{
Ctx: ctx,
Component: "prometheus",
Version: utils.Version(version),
InstanceDir: dir,
WD: dir,
Args: args,
SysProcAttr: instance.SysProcAttr,
Env: env,
}
cmd, err := tiupexec.PrepareCommand(params)
if err != nil {
var binPath string
if binPath, err = tiupexec.PrepareBinary("prometheus", utils.Version(version), binPath); err != nil {
return nil, err
}
cmd := instance.PrepareCommand(ctx, binPath, args, nil, dir)

m.port = port
m.cmd = cmd
Expand Down
Loading

0 comments on commit 15cd08a

Please sign in to comment.