-
Notifications
You must be signed in to change notification settings - Fork 726
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
pd-ctl: add config replicate and show all config #573
Changes from 1 commit
22dd34d
c930dc8
dfc6089
42bd63b
44bf81c
85a93a1
956e1b1
c0ff6f5
66abfb5
55de956
0f38dfe
585673e
a97ec1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,15 +17,18 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
configPrefix = "pd/api/v1/config" | ||
schedulePrefix = "pd/api/v1/config/schedule" | ||
configPrefix = "pd/api/v1/config" | ||
schedulePrefix = "pd/api/v1/config/schedule" | ||
replicatePrefix = "pd/api/v1/config/replicate" | ||
) | ||
|
||
// NewConfigCommand return a config subcommand of rootCmd | ||
|
@@ -46,6 +49,17 @@ func NewShowConfigCommand() *cobra.Command { | |
Short: "show config of PD", | ||
Run: showConfigCommandFunc, | ||
} | ||
sc.AddCommand(NewShowAllConfigCommand()) | ||
return sc | ||
} | ||
|
||
// NewShowAllConfigCommand return a show all subcommand of show subcommand | ||
func NewShowAllConfigCommand() *cobra.Command { | ||
sc := &cobra.Command{ | ||
Use: "all", | ||
Short: "show all config of PD", | ||
Run: showAllConfigCommandFunc, | ||
} | ||
return sc | ||
} | ||
|
||
|
@@ -56,6 +70,17 @@ func NewSetConfigCommand() *cobra.Command { | |
Short: "set the option with value", | ||
Run: setConfigCommandFunc, | ||
} | ||
sc.AddCommand(NewSetReplicationCommand()) | ||
return sc | ||
} | ||
|
||
// NewSetReplicationCommand return a set replication subcommand of setCmd | ||
func NewSetReplicationCommand() *cobra.Command { | ||
sc := &cobra.Command{ | ||
Use: "replicate <option> <value>", | ||
Short: "set the replication option with value", | ||
Run: setReplicateConfigCommandFunc, | ||
} | ||
return sc | ||
} | ||
|
||
|
@@ -68,6 +93,15 @@ func showConfigCommandFunc(cmd *cobra.Command, args []string) { | |
fmt.Println(r) | ||
} | ||
|
||
func showAllConfigCommandFunc(cmd *cobra.Command, args []string) { | ||
r, err := doRequest(cmd, configPrefix, http.MethodGet) | ||
if err != nil { | ||
fmt.Printf("Failed to get config: %s", err) | ||
return | ||
} | ||
fmt.Println(r) | ||
} | ||
|
||
func setConfigCommandFunc(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
fmt.Println(cmd.UsageString()) | ||
|
@@ -115,3 +149,50 @@ func setConfigCommandFunc(cmd *cobra.Command, args []string) { | |
printResponseError(r) | ||
} | ||
} | ||
|
||
func setReplicateConfigCommandFunc(cmd *cobra.Command, args []string) { | ||
if len(args) != 2 { | ||
fmt.Println(cmd.UsageString()) | ||
return | ||
} | ||
|
||
var value interface{} | ||
data := make(map[string]interface{}) | ||
r, err := doRequest(cmd, replicatePrefix, http.MethodGet) | ||
if err != nil { | ||
fmt.Printf("Failed to set replication config:[%s]\n", err) | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use schedulePrefix? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the API setSchedule and getSchedule are not consistent. considering compatible I just adjust it in here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's ok to give up supporting old pd-server. |
||
err = json.Unmarshal([]byte(r), &data) | ||
if err != nil { | ||
fmt.Printf("Failed to set replication config:[%s]\n", err) | ||
return | ||
} | ||
value, err = strconv.ParseFloat(args[1], 64) | ||
if err != nil { | ||
value = args[1] | ||
if strings.Contains(args[1], ",") { | ||
value = strings.Split(args[1], ",") | ||
} | ||
} | ||
data[args[0]] = value | ||
reqData, err := json.Marshal(&data) | ||
if err != nil { | ||
fmt.Printf("Failed to set replication config:[%s]\n", err) | ||
return | ||
} | ||
url := getAddressFromCmd(cmd, replicatePrefix) | ||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(reqData)) | ||
if err != nil { | ||
fmt.Printf("Failed to set replication config:[%s]\n", err) | ||
} | ||
defer func() { | ||
ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
}() | ||
if resp.StatusCode == http.StatusOK { | ||
fmt.Println("Success!") | ||
} else { | ||
printResponseError(resp) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use
setConfigCommandFunc
directly? I think the implementation code is duplicated here.