Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
bin/
*.tar
*.gz
Expand Down
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ package cmd

import (
"fmt"
"os"

"github.com/fatih/color"
"github.com/pingcap-incubator/tiup/pkg/meta"
"github.com/pingcap-incubator/tiup/pkg/repository"
"github.com/pingcap-incubator/tiup/pkg/version"
"github.com/spf13/cobra"
"os"
)

var rootCmd *cobra.Command
Expand All @@ -31,6 +30,7 @@ func init() {

var (
binary string
binPath string
tag string
rm bool
repoOpts repository.Options
Expand Down Expand Up @@ -75,6 +75,7 @@ the latest stable version will be downloaded from the repository.
// will be parsed correctly.
// e.g: tiup --tag mytag --rm playground --db 3 --pd 3 --kv 4
// => run "playground" with parameters "--db 3 --pd 3 --kv 4"
// tiup --tag mytag --binpath /xxx/tikv-server tikv
var transparentParams []string
componentSpec := args[0]
for i, arg := range os.Args {
Expand All @@ -83,7 +84,7 @@ the latest stable version will be downloaded from the repository.
break
}
}
return runComponent(tag, componentSpec, transparentParams, rm)
return runComponent(tag, componentSpec, binPath, transparentParams, rm)
}
return cmd.Help()
},
Expand All @@ -101,6 +102,7 @@ the latest stable version will be downloaded from the repository.
"and the latest version installed will be selected if no version specified")
rootCmd.Flags().StringVarP(&tag, "tag", "T", "", "Specify a tag for component instance")
rootCmd.Flags().BoolVar(&rm, "rm", false, "Remove the data directory when the component instance finishes its run")
rootCmd.Flags().StringVar(&binPath, "binpath", "", "Specify the binary path of component instance")

rootCmd.AddCommand(
newInstallCmd(),
Expand Down
35 changes: 21 additions & 14 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/pingcap/errors"
)

func runComponent(tag, spec string, args []string, rm bool) error {
func runComponent(tag, spec, binPath string, args []string, rm bool) error {
component, version := meta.ParseCompVersion(spec)
if !isSupportedComponent(component) {
return fmt.Errorf("unkonwn component `%s` (see supported components via `tiup list --refresh`)", component)
Expand All @@ -41,7 +41,7 @@ func runComponent(tag, spec string, args []string, rm bool) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

p, err := launchComponent(ctx, component, version, tag, args)
p, err := launchComponent(ctx, component, version, binPath, tag, args)
// If the process has been launched, we must save the process info to meta directory
if err == nil || (p != nil && p.Pid != 0) {
defer cleanDataDir(rm, p.Dir)
Expand Down Expand Up @@ -134,19 +134,26 @@ func base62Tag() string {
return string(b)
}

func launchComponent(ctx context.Context, component string, version repository.Version, tag string, args []string) (*process, error) {
selectVer, err := meta.DownloadComponentIfMissing(component, version)
if err != nil {
return nil, err
}
binPath, err := meta.BinaryPath(component, selectVer)
if err != nil {
return nil, err
}
func launchComponent(ctx context.Context, component string, version repository.Version, binPath string, tag string, args []string) (*process, error) {
var selectVer repository.Version
var installPath string
var err error

installPath, err := meta.ComponentInstalledDir(component, selectVer)
if err != nil {
return nil, err
if binPath == "" {
selectVer, err = meta.DownloadComponentIfMissing(component, version)
if err != nil {
return nil, err
}

binPath, err = meta.BinaryPath(component, selectVer)
if err != nil {
return nil, err
}

installPath, err = meta.ComponentInstalledDir(component, selectVer)
if err != nil {
return nil, err
}
}

wd := os.Getenv(localdata.EnvNameInstanceDataDir)
Expand Down
2 changes: 1 addition & 1 deletion components/playground/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type instance struct {
// Instance represent running component
type Instance interface {
Pid() int
Start(ctx context.Context, version repository.Version) error
Start(ctx context.Context, version repository.Version, binPath string) error
Wait() error
}

Expand Down
5 changes: 3 additions & 2 deletions components/playground/instance/pd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ func (inst *PDInstance) Join(pds []*PDInstance) *PDInstance {
}

// Start calls set inst.cmd and Start
func (inst *PDInstance) Start(ctx context.Context, version repository.Version) error {
func (inst *PDInstance) Start(ctx context.Context, version repository.Version, binPath string) error {
if err := os.MkdirAll(inst.Dir, 0755); err != nil {
return err
}
uid := fmt.Sprintf("pd-%d", inst.ID)
args := []string{
"tiup", compVersion("pd", version),
"tiup", fmt.Sprintf("--binpath=%s", binPath),
compVersion("pd", version),
"--name=" + uid,
fmt.Sprintf("--data-dir=%s", filepath.Join(inst.Dir, "data")),
fmt.Sprintf("--peer-urls=http://%s:%d", inst.Host, inst.Port),
Expand Down
5 changes: 3 additions & 2 deletions components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewTiDBInstance(dir, host string, id int, pds []*PDInstance) *TiDBInstance
}

// Start calls set inst.cmd and Start
func (inst *TiDBInstance) Start(ctx context.Context, version repository.Version) error {
func (inst *TiDBInstance) Start(ctx context.Context, version repository.Version, binPath string) error {
if err := os.MkdirAll(inst.Dir, 0755); err != nil {
return err
}
Expand All @@ -58,7 +58,8 @@ func (inst *TiDBInstance) Start(ctx context.Context, version repository.Version)
endpoints = append(endpoints, fmt.Sprintf("%s:%d", inst.Host, pd.StatusPort))
}
args := []string{
"tiup", compVersion("tidb", version),
"tiup", fmt.Sprintf("--binpath=%s", binPath),
compVersion("tidb", version),
"-P", strconv.Itoa(inst.Port),
"--store=tikv",
fmt.Sprintf("--host=%s", inst.Host),
Expand Down
5 changes: 3 additions & 2 deletions components/playground/instance/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewTiKVInstance(dir, host string, id int, pds []*PDInstance) *TiKVInstance
}

// Start calls set inst.cmd and Start
func (inst *TiKVInstance) Start(ctx context.Context, version repository.Version) error {
func (inst *TiKVInstance) Start(ctx context.Context, version repository.Version, binPath string) error {
if err := os.MkdirAll(inst.Dir, 0755); err != nil {
return err
}
Expand All @@ -69,7 +69,8 @@ func (inst *TiKVInstance) Start(ctx context.Context, version repository.Version)
endpoints = append(endpoints, fmt.Sprintf("http://%s:%d", inst.Host, pd.StatusPort))
}
inst.cmd = exec.CommandContext(ctx,
"tiup", compVersion("tikv", version),
"tiup", fmt.Sprintf("--binpath=%s", binPath),
compVersion("tikv", version),
fmt.Sprintf("--addr=%s:%d", inst.Host, inst.Port),
fmt.Sprintf("--status-addr=%s:%d", inst.Host, inst.StatusPort),
fmt.Sprintf("--pd=%s", strings.Join(endpoints, ",")),
Expand Down
42 changes: 37 additions & 5 deletions components/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func execute() error {
pdNum := 1
host := "127.0.0.1"
monitor := false
tidbBinPath := ""
tikvBinPath := ""
pdBinPath := ""

rootCmd := &cobra.Command{
Use: "tiup playground [version]",
Expand All @@ -82,14 +85,15 @@ if you don't specified a version.
Examples:
$ tiup playground nightly # Start a TiDB nightly version local cluster
$ tiup playground v3.0.10 --db 3 --pd 3 --kv 3 # Start a local cluster with 10 nodes
$ tiup playground nightly --monitor # Start a local cluster with monitor system`,
$ tiup playground nightly --monitor # Start a local cluster with monitor system
$ tiup playground --db.binpath /xx/tidb-server # Start a local cluster with component binary path`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
version := ""
if len(args) > 0 {
version = args[0]
}
return bootCluster(version, pdNum, tidbNum, tikvNum, host, monitor)
return bootCluster(version, pdNum, tidbNum, tikvNum, host, monitor, tidbBinPath, tikvBinPath, pdBinPath)
},
}

Expand All @@ -98,6 +102,9 @@ Examples:
rootCmd.Flags().IntVarP(&pdNum, "pd", "", 1, "PD instance number")
rootCmd.Flags().StringVarP(&host, "host", "", host, "Playground cluster host")
rootCmd.Flags().BoolVar(&monitor, "monitor", false, "Start prometheus component")
rootCmd.Flags().StringVarP(&tidbBinPath, "db.binpath", "", tidbBinPath, "TiDB instance binary path")
rootCmd.Flags().StringVarP(&tikvBinPath, "kv.binpath", "", tikvBinPath, "TiKV instance binary path")
rootCmd.Flags().StringVarP(&pdBinPath, "pd.binpath", "", pdBinPath, "PD instance binary path")

return rootCmd.Execute()
}
Expand Down Expand Up @@ -147,19 +154,40 @@ func hasDashboard(pdAddr string) bool {
return false
}

func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monitor bool) error {
func getAbsolutePath(binPath string) string {
if !strings.HasPrefix(binPath, "/") && !strings.HasPrefix(binPath, "~") {
binPath = filepath.Join(os.Getenv(localdata.EnvNameWorkDir), binPath)
}
return binPath
}

func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monitor bool, tidbBinPath, tikvBinPath, pdBinPath string) error {
if pdNum < 1 || tidbNum < 1 || tikvNum < 1 {
return fmt.Errorf("all components count must be great than 0 (tidb=%v, tikv=%v, pd=%v)",
tidbNum, tikvNum, pdNum)
}

var pathMap = make(map[string]string)
Comment thread
fredchenbj marked this conversation as resolved.
if tidbBinPath != "" {
pathMap["tidb"] = getAbsolutePath(tidbBinPath)
}
if tikvBinPath != "" {
pathMap["tikv"] = getAbsolutePath(tikvBinPath)
}
if pdBinPath != "" {
pathMap["pd"] = getAbsolutePath(pdBinPath)
}

// Initialize the profile
profileRoot := os.Getenv(localdata.EnvNameHome)
if profileRoot == "" {
return fmt.Errorf("cannot read environment variable %s", localdata.EnvNameHome)
}
profile := localdata.NewProfile(profileRoot)
for _, comp := range []string{"pd", "tikv", "tidb"} {
if pathMap[comp] != "" {
continue
}
if err := installIfMissing(profile, comp, version); err != nil {
return err
}
Expand All @@ -171,6 +199,7 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
}

all := make([]instance.Instance, 0, pdNum+tikvNum+tidbNum)
allRole := make([]string, 0, pdNum+tikvNum+tidbNum)
pds := make([]*instance.PDInstance, 0, pdNum)
kvs := make([]*instance.TiKVInstance, 0, tikvNum)
dbs := make([]*instance.TiDBInstance, 0, tidbNum)
Expand All @@ -183,6 +212,7 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
inst := instance.NewPDInstance(dir, host, i)
pds = append(pds, inst)
all = append(all, inst)
allRole = append(allRole, "pd")
}
for _, pd := range pds {
pd.Join(pds)
Expand All @@ -193,13 +223,15 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
inst := instance.NewTiKVInstance(dir, host, i, pds)
kvs = append(kvs, inst)
all = append(all, inst)
allRole = append(allRole, "tikv")
}

for i := 0; i < tidbNum; i++ {
dir := filepath.Join(dataDir, fmt.Sprintf("tidb-%d", i))
inst := instance.NewTiDBInstance(dir, host, i, pds)
dbs = append(dbs, inst)
all = append(all, inst)
allRole = append(allRole, "tidb")
}

fmt.Println("Playground Bootstrapping...")
Expand Down Expand Up @@ -248,8 +280,8 @@ func bootCluster(version string, pdNum, tidbNum, tikvNum int, host string, monit
}()
}

for _, inst := range all {
if err := inst.Start(ctx, repository.Version(version)); err != nil {
for i, inst := range all {
if err := inst.Start(ctx, repository.Version(version), pathMap[allRole[i]]); err != nil {
return err
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/expected/tiup/tiup-h.output
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Available Components:
Flags:
-B, --binary <component>[:version] Print binary path of a specific version of a component <component>[:version]
and the latest version installed will be selected if no version specified
--binpath string Specify the binary path of component instance
-h, --help help for tiup
--rm Remove the data directory when the component instance finishes its run
--skip-version-check Skip the strict version check, by default a version must be a valid SemVer string
Expand Down
1 change: 1 addition & 0 deletions tests/expected/tiup/tiup-help.output
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Available Components:
Flags:
-B, --binary <component>[:version] Print binary path of a specific version of a component <component>[:version]
and the latest version installed will be selected if no version specified
--binpath string Specify the binary path of component instance
-h, --help help for tiup
--rm Remove the data directory when the component instance finishes its run
--skip-version-check Skip the strict version check, by default a version must be a valid SemVer string
Expand Down
1 change: 1 addition & 0 deletions tests/expected/tiup/tiup.output
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Available Components:
Flags:
-B, --binary <component>[:version] Print binary path of a specific version of a component <component>[:version]
and the latest version installed will be selected if no version specified
--binpath string Specify the binary path of component instance
-h, --help help for tiup
--rm Remove the data directory when the component instance finishes its run
--skip-version-check Skip the strict version check, by default a version must be a valid SemVer string
Expand Down