client.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Agent interface {
  17. ID() uint64
  18. // Start starts a new etcd with the given args on the agent machine.
  19. Start(args ...string) (int, error)
  20. // Stop stops the existing etcd the agent started.
  21. Stop() error
  22. // Restart restarts the existing etcd the agent stopped.
  23. Restart() (int, error)
  24. // Cleanup stops the exiting etcd the agent started, then archives log and its data dir.
  25. Cleanup() error
  26. // Terminate stops the exiting etcd the agent started and removes its data dir.
  27. Terminate() error
  28. // Isoloate isolates the network of etcd
  29. Isolate() error
  30. }
  31. type agent struct {
  32. endpoint string
  33. rpcClient *rpc.Client
  34. }
  35. func NewAgent(endpoint string) (Agent, error) {
  36. c, err := rpc.DialHTTP("tcp", endpoint)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &agent{endpoint, c}, nil
  41. }
  42. func (a *agent) Start(args ...string) (int, error) {
  43. var pid int
  44. err := a.rpcClient.Call("Agent.RPCStart", args, &pid)
  45. if err != nil {
  46. return -1, err
  47. }
  48. return pid, nil
  49. }
  50. func (a *agent) Stop() error {
  51. return a.rpcClient.Call("Agent.RPCStop", struct{}{}, nil)
  52. }
  53. func (a *agent) Restart() (int, error) {
  54. var pid int
  55. err := a.rpcClient.Call("Agent.RPCRestart", struct{}{}, &pid)
  56. if err != nil {
  57. return -1, err
  58. }
  59. return pid, nil
  60. }
  61. func (a *agent) Cleanup() error {
  62. return a.rpcClient.Call("Agent.RPCCleanup", struct{}{}, nil)
  63. }
  64. func (a *agent) Terminate() error {
  65. return a.rpcClient.Call("Agent.RPCTerminate", struct{}{}, nil)
  66. }
  67. func (a *agent) Isolate() error {
  68. panic("not implemented")
  69. }
  70. func (a *agent) ID() uint64 {
  71. panic("not implemented")
  72. }