123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- // Copyright 2016 The etcd Authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package command
- import (
- "fmt"
- "strconv"
- "strings"
- "github.com/spf13/cobra"
- )
- var memberPeerURLs string
- // NewMemberCommand returns the cobra command for "member".
- func NewMemberCommand() *cobra.Command {
- mc := &cobra.Command{
- Use: "member <subcommand>",
- Short: "Membership related commands",
- }
- mc.AddCommand(NewMemberAddCommand())
- mc.AddCommand(NewMemberRemoveCommand())
- mc.AddCommand(NewMemberUpdateCommand())
- mc.AddCommand(NewMemberListCommand())
- return mc
- }
- // NewMemberAddCommand returns the cobra command for "member add".
- func NewMemberAddCommand() *cobra.Command {
- cc := &cobra.Command{
- Use: "add <memberName> [options]",
- Short: "Adds a member into the cluster",
- Run: memberAddCommandFunc,
- }
- cc.Flags().StringVar(&memberPeerURLs, "peer-urls", "", "comma separated peer URLs for the new member.")
- return cc
- }
- // NewMemberRemoveCommand returns the cobra command for "member remove".
- func NewMemberRemoveCommand() *cobra.Command {
- cc := &cobra.Command{
- Use: "remove <memberID>",
- Short: "Removes a member from the cluster",
- Run: memberRemoveCommandFunc,
- }
- return cc
- }
- // NewMemberUpdateCommand returns the cobra command for "member update".
- func NewMemberUpdateCommand() *cobra.Command {
- cc := &cobra.Command{
- Use: "update <memberID> [options]",
- Short: "Updates a member in the cluster",
- Run: memberUpdateCommandFunc,
- }
- cc.Flags().StringVar(&memberPeerURLs, "peer-urls", "", "comma separated peer URLs for the updated member.")
- return cc
- }
- // NewMemberListCommand returns the cobra command for "member list".
- func NewMemberListCommand() *cobra.Command {
- cc := &cobra.Command{
- Use: "list",
- Short: "Lists all members in the cluster",
- Long: `When --write-out is set to simple, this command prints out comma-separated member lists for each endpoint.
- The items in the lists are ID, Status, Name, Peer Addrs, Client Addrs.
- `,
- Run: memberListCommandFunc,
- }
- return cc
- }
- // memberAddCommandFunc executes the "member add" command.
- func memberAddCommandFunc(cmd *cobra.Command, args []string) {
- if len(args) != 1 {
- ExitWithError(ExitBadArgs, fmt.Errorf("member name not provided."))
- }
- newMemberName := args[0]
- if len(memberPeerURLs) == 0 {
- ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
- }
- urls := strings.Split(memberPeerURLs, ",")
- ctx, cancel := commandCtx(cmd)
- cli := mustClientFromCmd(cmd)
- resp, err := cli.MemberAdd(ctx, urls)
- cancel()
- if err != nil {
- ExitWithError(ExitError, err)
- }
- newID := resp.Member.ID
- display.MemberAdd(*resp)
- if _, ok := (display).(*simplePrinter); ok {
- ctx, cancel = commandCtx(cmd)
- listResp, err := cli.MemberList(ctx)
- // make sure the member who served member list request has the latest member list.
- syncedMemberSet := make(map[uint64]struct{})
- syncedMemberSet[resp.Header.MemberId] = struct{}{} // the member who served member add is guaranteed to have the latest member list.
- for {
- if err != nil {
- ExitWithError(ExitError, err)
- }
- if _, ok := syncedMemberSet[listResp.Header.MemberId]; ok {
- break
- }
- // quorum get to sync cluster list
- gresp, gerr := cli.Get(ctx, "_")
- if gerr != nil {
- ExitWithError(ExitError, err)
- }
- syncedMemberSet[gresp.Header.MemberId] = struct{}{}
- listResp, err = cli.MemberList(ctx)
- }
- cancel()
- conf := []string{}
- for _, memb := range listResp.Members {
- for _, u := range memb.PeerURLs {
- n := memb.Name
- if memb.ID == newID {
- n = newMemberName
- }
- conf = append(conf, fmt.Sprintf("%s=%s", n, u))
- }
- }
- fmt.Print("\n")
- fmt.Printf("ETCD_NAME=%q\n", newMemberName)
- fmt.Printf("ETCD_INITIAL_CLUSTER=%q\n", strings.Join(conf, ","))
- fmt.Printf("ETCD_INITIAL_ADVERTISE_PEER_URLS=%q\n", memberPeerURLs)
- fmt.Printf("ETCD_INITIAL_CLUSTER_STATE=\"existing\"\n")
- }
- }
- // memberRemoveCommandFunc executes the "member remove" command.
- func memberRemoveCommandFunc(cmd *cobra.Command, args []string) {
- if len(args) != 1 {
- ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
- }
- id, err := strconv.ParseUint(args[0], 16, 64)
- if err != nil {
- ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
- }
- ctx, cancel := commandCtx(cmd)
- resp, err := mustClientFromCmd(cmd).MemberRemove(ctx, id)
- cancel()
- if err != nil {
- ExitWithError(ExitError, err)
- }
- display.MemberRemove(id, *resp)
- }
- // memberUpdateCommandFunc executes the "member update" command.
- func memberUpdateCommandFunc(cmd *cobra.Command, args []string) {
- if len(args) != 1 {
- ExitWithError(ExitBadArgs, fmt.Errorf("member ID is not provided"))
- }
- id, err := strconv.ParseUint(args[0], 16, 64)
- if err != nil {
- ExitWithError(ExitBadArgs, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err))
- }
- if len(memberPeerURLs) == 0 {
- ExitWithError(ExitBadArgs, fmt.Errorf("member peer urls not provided."))
- }
- urls := strings.Split(memberPeerURLs, ",")
- ctx, cancel := commandCtx(cmd)
- resp, err := mustClientFromCmd(cmd).MemberUpdate(ctx, id, urls)
- cancel()
- if err != nil {
- ExitWithError(ExitError, err)
- }
- display.MemberUpdate(id, *resp)
- }
- // memberListCommandFunc executes the "member list" command.
- func memberListCommandFunc(cmd *cobra.Command, args []string) {
- ctx, cancel := commandCtx(cmd)
- resp, err := mustClientFromCmd(cmd).MemberList(ctx)
- cancel()
- if err != nil {
- ExitWithError(ExitError, err)
- }
- display.MemberList(*resp)
- }
|