forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_client.go
141 lines (115 loc) · 3.91 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
package http
import (
"fmt"
"time"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
boshretry "github.com/cloudfoundry/bosh-agent/retrystrategy"
bmac "github.com/cloudfoundry/bosh-micro-cli/deployment/agentclient"
bmas "github.com/cloudfoundry/bosh-micro-cli/deployment/applyspec"
bmhttpclient "github.com/cloudfoundry/bosh-micro-cli/deployment/httpclient"
)
type agentClient struct {
agentRequest agentRequest
getTaskDelay time.Duration
logger boshlog.Logger
logTag string
}
func NewAgentClient(
endpoint string,
directorID string,
getTaskDelay time.Duration,
httpClient bmhttpclient.HTTPClient,
logger boshlog.Logger,
) bmac.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 := NewAgentRequest(agentEndpoint, httpClient, directorID)
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 {
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.Errorf("Failed to start agent services with response: '%s'", response)
}
return nil
}
func (c *agentClient) GetState() (bmac.AgentState, error) {
var response StateResponse
err := c.agentRequest.Send("get_state", []interface{}{}, &response)
if err != nil {
return bmac.AgentState{}, bosherr.WrapError(err, "Sending get_state to the agent")
}
agentState := bmac.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 {
return c.sendAsyncTaskMessage("mount_disk", []interface{}{diskCID})
}
func (c *agentClient) UnmountDisk(diskCID string) error {
return c.sendAsyncTaskMessage("unmount_disk", []interface{}{diskCID})
}
func (c *agentClient) MigrateDisk() error {
return c.sendAsyncTaskMessage("migrate_disk", []interface{}{})
}
func (c *agentClient) sendAsyncTaskMessage(method string, arguments []interface{}) error {
var response TaskResponse
err := c.agentRequest.Send(method, arguments, &response)
if err != nil {
return bosherr.WrapErrorf(err, "Sending '%s' to the agent", method)
}
agentTaskID, err := response.TaskID()
if err != nil {
return 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")
}
taskState, err := response.TaskState()
if err != nil {
return false, bosherr.WrapError(err, "Getting task state")
}
if taskState != "running" {
return true, nil
}
return true, bosherr.Errorf("Task %s is still running", method)
})
getTaskRetryStrategy := boshretry.NewUnlimitedRetryStrategy(c.getTaskDelay, getTaskRetryable, c.logger)
return getTaskRetryStrategy.Try()
}