command_cluster_raft_add.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  24. raftServerAddCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  25. serverId := raftServerAddCommand.String("id", "", "server id")
  26. serverAddress := raftServerAddCommand.String("address", "", "server grpc address")
  27. serverVoter := raftServerAddCommand.Bool("voter", true, "assign it a vote")
  28. if err = raftServerAddCommand.Parse(args); err != nil {
  29. return nil
  30. }
  31. if *serverId == "" || *serverAddress == "" {
  32. return fmt.Errorf("empty server id or address")
  33. }
  34. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  35. _, err := client.RaftAddServer(context.Background(), &master_pb.RaftAddServerRequest{
  36. Id: *serverId,
  37. Address: *serverAddress,
  38. Voter: *serverVoter,
  39. })
  40. if err != nil {
  41. return fmt.Errorf("raft add server: %v", err)
  42. }
  43. println("added server", *serverId)
  44. return nil
  45. })
  46. return err
  47. }