etcdctl: centralize getEndpoints logic
parent
4b555dba99
commit
f4ea274555
|
@ -20,7 +20,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -40,72 +39,29 @@ func dumpCURL(client *etcd.Client) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// createHttpPath attaches http scheme to the given address if needed
|
|
||||||
func createHttpPath(addr string) (string, error) {
|
|
||||||
u, err := url.Parse(addr)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if u.Scheme == "" {
|
|
||||||
u.Scheme = "http"
|
|
||||||
}
|
|
||||||
return u.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPeersFlagValue(c *cli.Context) []string {
|
|
||||||
peerstr := c.GlobalString("peers")
|
|
||||||
|
|
||||||
// Use an environment variable if nothing was supplied on the
|
|
||||||
// command line
|
|
||||||
if peerstr == "" {
|
|
||||||
peerstr = os.Getenv("ETCDCTL_PEERS")
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we still don't have peers, use a default
|
|
||||||
if peerstr == "" {
|
|
||||||
peerstr = "127.0.0.1:4001"
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Split(peerstr, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
// rawhandle wraps the command function handlers and sets up the
|
// rawhandle wraps the command function handlers and sets up the
|
||||||
// environment but performs no output formatting.
|
// environment but performs no output formatting.
|
||||||
func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) {
|
func rawhandle(c *cli.Context, fn handlerFunc) (*etcd.Response, error) {
|
||||||
sync := !c.GlobalBool("no-sync")
|
endpoints, err := getEndpoints(c)
|
||||||
|
if err != nil {
|
||||||
peers := getPeersFlagValue(c)
|
return nil, err
|
||||||
|
|
||||||
// If no sync, create http path for each peer address
|
|
||||||
if !sync {
|
|
||||||
revisedPeers := make([]string, 0)
|
|
||||||
for _, peer := range peers {
|
|
||||||
if revisedPeer, err := createHttpPath(peer); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Unsupported url %v: %v\n", peer, err)
|
|
||||||
} else {
|
|
||||||
revisedPeers = append(revisedPeers, revisedPeer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
peers = revisedPeers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
client := etcd.NewClient(peers)
|
client := etcd.NewClient(endpoints)
|
||||||
|
|
||||||
if c.GlobalBool("debug") {
|
if c.GlobalBool("debug") {
|
||||||
go dumpCURL(client)
|
go dumpCURL(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sync cluster.
|
// Sync cluster.
|
||||||
if sync {
|
if !c.GlobalBool("no-sync") {
|
||||||
if ok := client.SyncCluster(); !ok {
|
if ok := client.SyncCluster(); !ok {
|
||||||
handleError(FailedToConnectToHost, errors.New("Cannot sync with the cluster using peers "+strings.Join(peers, ", ")))
|
handleError(FailedToConnectToHost, errors.New("cannot sync with the cluster using endpoints "+strings.Join(endpoints, ", ")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.GlobalBool("debug") {
|
if c.GlobalBool("debug") {
|
||||||
fmt.Fprintf(os.Stderr, "Cluster-Peers: %s\n",
|
fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(client.GetCluster(), ", "))
|
||||||
strings.Join(client.GetCluster(), " "))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute handler function.
|
// Execute handler function.
|
||||||
|
|
|
@ -52,14 +52,13 @@ func NewMemberCommand() cli.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
|
func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
|
||||||
peers := getPeersFlagValue(c)
|
eps, err := getEndpoints(c)
|
||||||
for i, p := range peers {
|
if err != nil {
|
||||||
if !strings.HasPrefix(p, "http") && !strings.HasPrefix(p, "https") {
|
fmt.Fprintln(os.Stderr, err.Error())
|
||||||
peers[i] = fmt.Sprintf("http://%s", p)
|
os.Exit(1)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hc, err := client.NewHTTPClient(&http.Transport{}, peers)
|
hc, err := client.NewHTTPClient(&http.Transport{}, eps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err.Error())
|
fmt.Fprintln(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -75,6 +74,10 @@ func mustNewMembersAPI(c *cli.Context) client.MembersAPI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.GlobalBool("debug") {
|
||||||
|
fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
|
||||||
|
}
|
||||||
|
|
||||||
return client.NewMembersAPI(hc)
|
return client.NewMembersAPI(hc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,11 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -49,3 +53,45 @@ func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
|
||||||
}
|
}
|
||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func maybeAddScheme(maybeAddr string) (string, error) {
|
||||||
|
u, err := url.Parse(maybeAddr)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
if u.Scheme == "" {
|
||||||
|
u.Scheme = "http"
|
||||||
|
}
|
||||||
|
|
||||||
|
return u.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPeersFlagValue(c *cli.Context) []string {
|
||||||
|
peerstr := c.GlobalString("peers")
|
||||||
|
|
||||||
|
// Use an environment variable if nothing was supplied on the
|
||||||
|
// command line
|
||||||
|
if peerstr == "" {
|
||||||
|
peerstr = os.Getenv("ETCDCTL_PEERS")
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we still don't have peers, use a default
|
||||||
|
if peerstr == "" {
|
||||||
|
peerstr = "127.0.0.1:4001"
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.Split(peerstr, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEndpoints(c *cli.Context) ([]string, error) {
|
||||||
|
eps := getPeersFlagValue(c)
|
||||||
|
var err error
|
||||||
|
for i, ep := range eps {
|
||||||
|
eps[i], err = maybeAddScheme(ep)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eps, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue