client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package client
  15. import "net/rpc"
  16. type Status struct {
  17. // TODO: gather more informations
  18. // TODO: memory usage, raft information, etc..
  19. State string
  20. }
  21. type Agent interface {
  22. ID() uint64
  23. // Start starts a new etcd with the given args on the agent machine.
  24. Start(args ...string) (int, error)
  25. // Stop stops the existing etcd the agent started.
  26. Stop() error
  27. // Restart restarts the existing etcd the agent stopped.
  28. Restart() (int, error)
  29. // Cleanup stops the exiting etcd the agent started, then archives log and its data dir.
  30. Cleanup() error
  31. // Terminate stops the exiting etcd the agent started and removes its data dir.
  32. Terminate() error
  33. // DropPort drops all network packets at the given port.
  34. DropPort(port int) error
  35. // RecoverPort stops dropping all network packets at the given port.
  36. RecoverPort(port int) error
  37. // Status returns the status of etcd on the agent
  38. Status() (Status, error)
  39. }
  40. type agent struct {
  41. endpoint string
  42. rpcClient *rpc.Client
  43. }
  44. func NewAgent(endpoint string) (Agent, error) {
  45. c, err := rpc.DialHTTP("tcp", endpoint)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &agent{endpoint, c}, nil
  50. }
  51. func (a *agent) Start(args ...string) (int, error) {
  52. var pid int
  53. err := a.rpcClient.Call("Agent.RPCStart", args, &pid)
  54. if err != nil {
  55. return -1, err
  56. }
  57. return pid, nil
  58. }
  59. func (a *agent) Stop() error {
  60. return a.rpcClient.Call("Agent.RPCStop", struct{}{}, nil)
  61. }
  62. func (a *agent) Restart() (int, error) {
  63. var pid int
  64. err := a.rpcClient.Call("Agent.RPCRestart", struct{}{}, &pid)
  65. if err != nil {
  66. return -1, err
  67. }
  68. return pid, nil
  69. }
  70. func (a *agent) Cleanup() error {
  71. return a.rpcClient.Call("Agent.RPCCleanup", struct{}{}, nil)
  72. }
  73. func (a *agent) Terminate() error {
  74. return a.rpcClient.Call("Agent.RPCTerminate", struct{}{}, nil)
  75. }
  76. func (a *agent) DropPort(port int) error {
  77. return a.rpcClient.Call("Agent.RPCDropPort", port, nil)
  78. }
  79. func (a *agent) RecoverPort(port int) error {
  80. return a.rpcClient.Call("Agent.RPCRecoverPort", port, nil)
  81. }
  82. func (a *agent) Status() (Status, error) {
  83. var s Status
  84. err := a.rpcClient.Call("Agent.RPCStatus", struct{}{}, &s)
  85. if err != nil {
  86. return s, err
  87. }
  88. return s, nil
  89. }
  90. func (a *agent) ID() uint64 {
  91. panic("not implemented")
  92. }