command_cluster_raft_remove.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, &commandRaftServerRemove{})
  11. }
  12. type commandRaftServerRemove struct {
  13. }
  14. func (c *commandRaftServerRemove) Name() string {
  15. return "cluster.raft.remove"
  16. }
  17. func (c *commandRaftServerRemove) Help() string {
  18. return `remove a server from the raft cluster
  19. Example:
  20. cluster.raft.remove -id <server_name>
  21. `
  22. }
  23. func (c *commandRaftServerRemove) HasTag(CommandTag) bool {
  24. return false
  25. }
  26. func (c *commandRaftServerRemove) 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. if err = raftServerAddCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. if *serverId == "" {
  33. return fmt.Errorf("empty server id")
  34. }
  35. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  36. _, err := client.RaftRemoveServer(context.Background(), &master_pb.RaftRemoveServerRequest{
  37. Id: *serverId,
  38. Force: true,
  39. })
  40. if err != nil {
  41. return fmt.Errorf("raft remove server: %v", err)
  42. }
  43. println("removed server", *serverId)
  44. return nil
  45. })
  46. return err
  47. }