forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_client.go
205 lines (171 loc) · 5.96 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package http
import (
"fmt"
"time"
biagentclient "github.com/cloudfoundry/bosh-init/deployment/agentclient"
bias "github.com/cloudfoundry/bosh-init/deployment/applyspec"
bihttpclient "github.com/cloudfoundry/bosh-init/deployment/httpclient"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshretry "github.com/cloudfoundry/bosh-utils/retrystrategy"
)
type agentClient struct {
agentRequest agentRequest
getTaskDelay time.Duration
logger boshlog.Logger
logTag string
}
func NewAgentClient(
endpoint string,
directorID string,
getTaskDelay time.Duration,
httpClient bihttpclient.HTTPClient,
logger boshlog.Logger,
) biagentclient.AgentClient {
// if this were NATS, we would need the agentID, but since it's http, the endpoint is unique to the agent
agentEndpoint := fmt.Sprintf("%s/agent", endpoint)
agentRequest := agentRequest{
directorID: directorID,
endpoint: agentEndpoint,
httpClient: httpClient,
}
return &agentClient{
agentRequest: agentRequest,
getTaskDelay: getTaskDelay,
logger: logger,
logTag: "httpAgentClient",
}
}
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 {
_, err := c.sendAsyncTaskMessage("stop", []interface{}{})
return err
}
func (c *agentClient) Apply(spec bias.ApplySpec) error {
_, err := c.sendAsyncTaskMessage("apply", []interface{}{spec})
return err
}
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.Errorf("Failed to start agent services with response: '%s'", response)
}
return nil
}
func (c *agentClient) GetState() (biagentclient.AgentState, error) {
var response StateResponse
err := c.agentRequest.Send("get_state", []interface{}{}, &response)
if err != nil {
return biagentclient.AgentState{}, bosherr.WrapError(err, "Sending get_state to the agent")
}
agentState := biagentclient.AgentState{
JobState: response.Value.JobState,
}
return agentState, nil
}
func (c *agentClient) ListDisk() ([]string, error) {
var response ListResponse
err := c.agentRequest.Send("list_disk", []interface{}{}, &response)
if err != nil {
return []string{}, bosherr.WrapError(err, "Sending 'list_disk' to the agent")
}
return response.Value, nil
}
func (c *agentClient) MountDisk(diskCID string) error {
_, err := c.sendAsyncTaskMessage("mount_disk", []interface{}{diskCID})
return err
}
func (c *agentClient) UnmountDisk(diskCID string) error {
_, err := c.sendAsyncTaskMessage("unmount_disk", []interface{}{diskCID})
return err
}
func (c *agentClient) MigrateDisk() error {
_, err := c.sendAsyncTaskMessage("migrate_disk", []interface{}{})
return err
}
func (c *agentClient) sendAsyncTaskMessage(method string, arguments []interface{}) (value map[string]interface{}, err error) {
var response TaskResponse
err = c.agentRequest.Send(method, arguments, &response)
if err != nil {
return value, bosherr.WrapErrorf(err, "Sending '%s' to the agent", method)
}
agentTaskID, err := response.TaskID()
if err != nil {
return value, bosherr.WrapError(err, "Getting agent task id")
}
getTaskRetryable := boshretry.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")
}
c.logger.Debug(c.logTag, "get_task response value: %#v", response.Value)
taskState, err := response.TaskState()
if err != nil {
return false, bosherr.WrapError(err, "Getting task state")
}
if taskState != "running" {
var ok bool
value, ok = response.Value.(map[string]interface{})
if !ok {
c.logger.Warn(c.logTag, "Unable to parse get_task response value: %#v", response.Value)
}
return true, nil
}
return true, bosherr.Errorf("Task %s is still running", method)
})
getTaskRetryStrategy := boshretry.NewUnlimitedRetryStrategy(c.getTaskDelay, getTaskRetryable, c.logger)
return value, getTaskRetryStrategy.Try()
}
func (c *agentClient) CompilePackage(packageSource biagentclient.BlobRef, compiledPackageDependencies []biagentclient.BlobRef) (compiledPackageRef biagentclient.BlobRef, err error) {
dependencies := make(map[string]BlobRef, len(compiledPackageDependencies))
for _, dependency := range compiledPackageDependencies {
dependencies[dependency.Name] = BlobRef{
Name: dependency.Name,
Version: dependency.Version,
SHA1: dependency.SHA1,
BlobstoreID: dependency.BlobstoreID,
}
}
args := []interface{}{
packageSource.BlobstoreID,
packageSource.SHA1,
packageSource.Name,
packageSource.Version,
dependencies,
}
responseValue, err := c.sendAsyncTaskMessage("compile_package", args)
if err != nil {
return biagentclient.BlobRef{}, bosherr.WrapError(err, "Sending 'compile_package' to the agent")
}
result, ok := responseValue["result"].(map[string]interface{})
if !ok {
return biagentclient.BlobRef{}, bosherr.Errorf("Unable to parse 'compile_package' response from the agent: %#v", responseValue)
}
sha1, ok := result["sha1"].(string)
if !ok {
return biagentclient.BlobRef{}, bosherr.Errorf("Unable to parse 'compile_package' response from the agent: %#v", responseValue)
}
blobstoreID, ok := result["blobstore_id"].(string)
if !ok {
return biagentclient.BlobRef{}, bosherr.Errorf("Unable to parse 'compile_package' response from the agent: %#v", responseValue)
}
compiledPackageRef = biagentclient.BlobRef{
Name: packageSource.Name,
Version: packageSource.Version,
SHA1: sha1,
BlobstoreID: blobstoreID,
}
return compiledPackageRef, nil
}