client.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // Isoloate isolates the network of etcd
  34. Isolate() error
  35. // Status returns the status of etcd on the agent
  36. Status() (Status, error)
  37. }
  38. type agent struct {
  39. endpoint string
  40. rpcClient *rpc.Client
  41. }
  42. func NewAgent(endpoint string) (Agent, error) {
  43. c, err := rpc.DialHTTP("tcp", endpoint)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &agent{endpoint, c}, nil
  48. }
  49. func (a *agent) Start(args ...string) (int, error) {
  50. var pid int
  51. err := a.rpcClient.Call("Agent.RPCStart", args, &pid)
  52. if err != nil {
  53. return -1, err
  54. }
  55. return pid, nil
  56. }
  57. func (a *agent) Stop() error {
  58. return a.rpcClient.Call("Agent.RPCStop", struct{}{}, nil)
  59. }
  60. func (a *agent) Restart() (int, error) {
  61. var pid int
  62. err := a.rpcClient.Call("Agent.RPCRestart", struct{}{}, &pid)
  63. if err != nil {
  64. return -1, err
  65. }
  66. return pid, nil
  67. }
  68. func (a *agent) Cleanup() error {
  69. return a.rpcClient.Call("Agent.RPCCleanup", struct{}{}, nil)
  70. }
  71. func (a *agent) Terminate() error {
  72. return a.rpcClient.Call("Agent.RPCTerminate", struct{}{}, nil)
  73. }
  74. func (a *agent) Isolate() error {
  75. panic("not implemented")
  76. }
  77. func (a *agent) Status() (Status, error) {
  78. var s Status
  79. err := a.rpcClient.Call("Agent.RPCStatus", struct{}{}, &s)
  80. if err != nil {
  81. return s, err
  82. }
  83. return s, nil
  84. }
  85. func (a *agent) ID() uint64 {
  86. panic("not implemented")
  87. }