Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdctl: verify pd address #528

Merged
merged 7 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 4 additions & 5 deletions cmd/pd-ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,11 @@ func loop() {
}
continue
}

if line == "exit" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split it to another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if InitPDClient meet error, it will return error directly without execute the command. so I move exit to here.

os.Exit(0)
}
args := strings.Split(strings.TrimSpace(line), " ")
args = append(args, "-u", url)
usage, err := pdctl.Start(args)
if err != nil {
fmt.Println(usage)
}
pdctl.Start(args)
}
}
28 changes: 28 additions & 0 deletions pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
var (
pdClient pd.Client
dailClient = &http.Client{}

pingPrifix = "pd/ping"
errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it")
)

func doRequest(cmd *cobra.Command, prefix string, method string) (string, error) {
Expand Down Expand Up @@ -74,6 +77,10 @@ func InitPDClient(cmd *cobra.Command) error {
if pdClient != nil {
return nil
}
err = validPDAddr(addr)
if err != nil {
return err
}
pdClient, err = pd.NewClient([]string{addr})
if err != nil {
return err
Expand Down Expand Up @@ -111,6 +118,27 @@ func printResponseError(r *http.Response) {
io.Copy(os.Stdout, r.Body)
}

func validPDAddr(pd string) error {
u, err := url.Parse(pd)
if err != nil {
return err
}
if u.Scheme == "" {
u.Scheme = "http"
}
addr := u.String()
reps, err := http.Get(fmt.Sprintf("%s/%s", addr, pingPrifix))
if err != nil {
return err
}
defer reps.Body.Close()
if reps.StatusCode != http.StatusOK {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

must read all response body and close

ioutil.ReadAll(reps.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to read it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not read, the connection FD will not close even you call body close later. I remember this is a pitfall before, but don't know go improves it or not.

Btw, We should call ioutil.ReadAll(reps.Body) before the status check.

return errInvalidAddr
}
return nil
}

// UsageTemplate will used to generate a help information
const UsageTemplate = `Usage:{{if .Runnable}}
{{if .HasAvailableFlags}}{{appendIfNotPresent .UseLine ""}}{{else}}{{.UseLine}}{{end}}{{end}}{{if .HasAvailableSubCommands}}
Expand Down
14 changes: 10 additions & 4 deletions pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package pdctl

import (
"fmt"
"os"

"github.com/pingcap/pd/pdctl/command"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -44,14 +47,17 @@ func init() {
}

// Start run Command
func Start(args []string) (string, error) {
func Start(args []string) {
rootCmd.SetArgs(args)
rootCmd.SilenceErrors = true
rootCmd.ParseFlags(args)
command.InitPDClient(rootCmd)
err := command.InitPDClient(rootCmd)
if err != nil {
fmt.Println(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should exit here directly?

os.Exit(1)
}
rootCmd.SetUsageTemplate(command.UsageTemplate)
if err := rootCmd.Execute(); err != nil {
return rootCmd.UsageString(), err
fmt.Println(rootCmd.UsageString())
}
return "", nil
}