etcdctl: impl members commands

This commit is contained in:
Brian Waldon 2014-10-29 14:37:24 -07:00
parent 8b12e1aa37
commit f6e242aa01
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,102 @@
package command
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/codegangsta/cli"
"github.com/coreos/etcd/client"
)
func NewMemberCommand() cli.Command {
return cli.Command{
Name: "member",
Subcommands: []cli.Command{
cli.Command{
Name: "list",
Usage: "enumerate existing cluster members",
Action: actionMemberList,
},
cli.Command{
Name: "add",
Usage: "add a new member to the etcd cluster",
Action: actionMemberAdd,
},
cli.Command{
Name: "remove",
Usage: "remove an existing member from the etcd cluster",
Action: actionMemberRemove,
},
},
}
}
func actionMemberList(c *cli.Context) {
if len(c.Args()) != 0 {
fmt.Fprintln(os.Stderr, "No arguments accepted")
os.Exit(1)
}
mAPI, err := client.NewMembersAPI(&http.Transport{}, "http://127.0.0.1:4001", client.DefaultRequestTimeout)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
members, err := mAPI.List()
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
for _, m := range members {
fmt.Printf("%s: name=%s peerURLs=%s clientURLs=%s\n", m.ID, m.Name, strings.Join(m.PeerURLs, ","), strings.Join(m.ClientURLs, ","))
}
}
func actionMemberAdd(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "Provide a single member peerURL")
os.Exit(1)
}
mAPI, err := client.NewMembersAPI(&http.Transport{}, "http://127.0.0.1:4001", client.DefaultRequestTimeout)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
url := args[0]
m, err := mAPI.Add(url)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Printf("Added member to cluster with ID %s", m.ID)
}
func actionMemberRemove(c *cli.Context) {
args := c.Args()
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "Provide a single member ID")
os.Exit(1)
}
mAPI, err := client.NewMembersAPI(&http.Transport{}, "http://127.0.0.1:4001", client.DefaultRequestTimeout)
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
mID := args[0]
if err := mAPI.Remove(mID); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
fmt.Printf("Removed member %s from cluster\n", mID)
}

View File

@ -33,6 +33,8 @@ func main() {
command.NewUpdateDirCommand(),
command.NewWatchCommand(),
command.NewExecWatchCommand(),
command.NewMemberCommand(),
}
app.Run(os.Args)
}