command_cluster_raft_add.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. "io"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandRaftServerAdd{})
  11. }
  12. type commandRaftServerAdd struct {
  13. }
  14. func (c *commandRaftServerAdd) Name() string {
  15. return "cluster.raft.add"
  16. }
  17. func (c *commandRaftServerAdd) Help() string {
  18. return `add a server to the raft cluster
  19. Example:
  20. cluster.raft.add -id <server_name> -address <server_host:port> -voter
  21. `
  22. }
  23. func (c *commandRaftServerAdd) HasTag(CommandTag) bool {
  24. return false
  25. }
  26. func (c *commandRaftServerAdd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. raftServerAddCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. serverId := raftServerAddCommand.String("id", "", "server id")
  29. serverAddress := raftServerAddCommand.String("address", "", "server grpc address")
  30. serverVoter := raftServerAddCommand.Bool("voter", true, "assign it a vote")
  31. if err = raftServerAddCommand.Parse(args); err != nil {
  32. return nil
  33. }
  34. if *serverId == "" || *serverAddress == "" {
  35. return fmt.Errorf("empty server id or address")
  36. }
  37. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  38. _, err := client.RaftAddServer(context.Background(), &master_pb.RaftAddServerRequest{
  39. Id: *serverId,
  40. Address: *serverAddress,
  41. Voter: *serverVoter,
  42. })
  43. if err != nil {
  44. return fmt.Errorf("raft add server: %v", err)
  45. }
  46. println("added server", *serverId)
  47. return nil
  48. })
  49. return err
  50. }