forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_client.go
122 lines (100 loc) · 3.15 KB
/
agent_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package agentclient
import (
"fmt"
"time"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
bmas "github.com/cloudfoundry/bosh-micro-cli/deployer/applyspec"
bmhttpclient "github.com/cloudfoundry/bosh-micro-cli/deployer/httpclient"
bmretrystrategy "github.com/cloudfoundry/bosh-micro-cli/retrystrategy"
)
type AgentClient interface {
Ping() (string, error)
Stop() error
Apply(bmas.ApplySpec) error
Start() error
GetState() (State, error)
}
type agentClient struct {
agentRequest agentRequest
getTaskDelay time.Duration
logger boshlog.Logger
logTag string
}
func NewAgentClient(
endpoint string,
uuid string,
getTaskDelay time.Duration,
httpClient bmhttpclient.HTTPClient,
logger boshlog.Logger,
) AgentClient {
agentEndpoint := fmt.Sprintf("%s/agent", endpoint)
agentRequest := NewAgentRequest(agentEndpoint, httpClient, uuid)
return &agentClient{
agentRequest: agentRequest,
getTaskDelay: getTaskDelay,
logger: logger,
logTag: "agentClient",
}
}
func (c *agentClient) Ping() (string, error) {
var response SimpleTaskResponse
err := c.agentRequest.Send("ping", []interface{}{}, &response)
if err != nil {
return "", bosherr.WrapError(err, "Sending ping to the agent")
}
return response.Value, nil
}
func (c *agentClient) Stop() error {
return c.sendAsyncTaskMessage("stop", []interface{}{})
}
func (c *agentClient) Apply(spec bmas.ApplySpec) error {
return c.sendAsyncTaskMessage("apply", []interface{}{spec})
}
func (c *agentClient) Start() error {
var response SimpleTaskResponse
err := c.agentRequest.Send("start", []interface{}{}, &response)
if err != nil {
return bosherr.WrapError(err, "Starting agent services")
}
if response.Value != "started" {
return bosherr.New("Failed to start agent services with response: '%s'", response)
}
return nil
}
func (c *agentClient) GetState() (State, error) {
var response StateResponse
err := c.agentRequest.Send("get_state", []interface{}{}, &response)
if err != nil {
return State{}, bosherr.WrapError(err, "Sending get_state to the agent")
}
return response.Value, nil
}
func (c *agentClient) sendAsyncTaskMessage(method string, arguments []interface{}) error {
var response TaskResponse
err := c.agentRequest.Send(method, arguments, &response)
if err != nil {
return bosherr.WrapError(err, "Sending '%s' to the agent", method)
}
agentTaskID, err := response.TaskID()
if err != nil {
return bosherr.WrapError(err, "Getting agent task id")
}
getTaskRetryable := bmretrystrategy.NewRetryable(func() (bool, error) {
var response TaskResponse
err = c.agentRequest.Send("get_task", []interface{}{agentTaskID}, &response)
if err != nil {
return false, bosherr.WrapError(err, "Sending 'get_task' to the agent")
}
taskState, err := response.TaskState()
if err != nil {
return false, bosherr.WrapError(err, "Getting task state")
}
if taskState != "running" {
return true, nil
}
return true, bosherr.New("Task %s is still running", method)
})
getTaskRetryStrategy := bmretrystrategy.NewUnlimitedRetryStrategy(c.getTaskDelay, getTaskRetryable, c.logger)
return getTaskRetryStrategy.Try()
}