1// Copyright 2015 The etcd Authors 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 15package client 16 17import "net/rpc" 18 19type Status struct { 20 // State gives the human-readable status of an agent (e.g., "started" or "terminated") 21 State string 22 23 // TODO: gather more informations 24 // TODO: memory usage, raft information, etc.. 25} 26 27type Agent interface { 28 ID() uint64 29 // Start starts a new etcd with the given args on the agent machine. 30 Start(args ...string) (int, error) 31 // Stop stops the existing etcd the agent started. 32 Stop() error 33 // Restart restarts the existing etcd the agent stopped. 34 Restart() (int, error) 35 // Cleanup stops the exiting etcd the agent started, then archives log and its data dir. 36 Cleanup() error 37 // Terminate stops the exiting etcd the agent started and removes its data dir. 38 Terminate() error 39 // DropPort drops all network packets at the given port. 40 DropPort(port int) error 41 // RecoverPort stops dropping all network packets at the given port. 42 RecoverPort(port int) error 43 // SetLatency slows down network by introducing latency. 44 SetLatency(ms, rv int) error 45 // RemoveLatency removes latency introduced by SetLatency. 46 RemoveLatency() error 47 // Status returns the status of etcd on the agent 48 Status() (Status, error) 49} 50 51type agent struct { 52 endpoint string 53 rpcClient *rpc.Client 54} 55 56func NewAgent(endpoint string) (Agent, error) { 57 c, err := rpc.DialHTTP("tcp", endpoint) 58 if err != nil { 59 return nil, err 60 } 61 return &agent{endpoint, c}, nil 62} 63 64func (a *agent) Start(args ...string) (int, error) { 65 var pid int 66 err := a.rpcClient.Call("Agent.RPCStart", args, &pid) 67 if err != nil { 68 return -1, err 69 } 70 return pid, nil 71} 72 73func (a *agent) Stop() error { 74 return a.rpcClient.Call("Agent.RPCStop", struct{}{}, nil) 75} 76 77func (a *agent) Restart() (int, error) { 78 var pid int 79 err := a.rpcClient.Call("Agent.RPCRestart", struct{}{}, &pid) 80 if err != nil { 81 return -1, err 82 } 83 return pid, nil 84} 85 86func (a *agent) Cleanup() error { 87 return a.rpcClient.Call("Agent.RPCCleanup", struct{}{}, nil) 88} 89 90func (a *agent) Terminate() error { 91 return a.rpcClient.Call("Agent.RPCTerminate", struct{}{}, nil) 92} 93 94func (a *agent) DropPort(port int) error { 95 return a.rpcClient.Call("Agent.RPCDropPort", port, nil) 96} 97 98func (a *agent) RecoverPort(port int) error { 99 return a.rpcClient.Call("Agent.RPCRecoverPort", port, nil) 100} 101 102func (a *agent) SetLatency(ms, rv int) error { 103 return a.rpcClient.Call("Agent.RPCSetLatency", []int{ms, rv}, nil) 104} 105 106func (a *agent) RemoveLatency() error { 107 return a.rpcClient.Call("Agent.RPCRemoveLatency", struct{}{}, nil) 108} 109 110func (a *agent) Status() (Status, error) { 111 var s Status 112 err := a.rpcClient.Call("Agent.RPCStatus", struct{}{}, &s) 113 return s, err 114} 115 116func (a *agent) ID() uint64 { 117 panic("not implemented") 118} 119