forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.go
215 lines (156 loc) · 4.94 KB
/
tasks.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
206
207
208
209
210
211
212
213
214
215
package director
import (
"fmt"
gourl "net/url"
"time"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type TaskImpl struct {
client Client
id int
startedAt time.Time
lastActivityAt time.Time
state string
user string
deploymentName string
description string
result string
}
func (t TaskImpl) ID() int { return t.id }
func (t TaskImpl) StartedAt() time.Time { return t.startedAt }
func (t TaskImpl) LastActivityAt() time.Time { return t.lastActivityAt }
func (t TaskImpl) State() string { return t.state }
func (t TaskImpl) IsError() bool {
return t.state == "error" || t.state == "timeout" || t.state == "cancelled"
}
func (t TaskImpl) User() string { return t.user }
func (t TaskImpl) DeploymentName() string { return t.deploymentName }
func (t TaskImpl) Description() string { return t.description }
func (t TaskImpl) Result() string { return t.result }
func (t TaskImpl) Cancel() error { return t.client.CancelTask(t.id) }
type TaskResp struct {
ID int // 165
StartedAt int64 `json:"started_at"` // 1440318199
LastActivityAt int64 `json:"timestamp"` // 1440318199
State string // e.g. "queued", "processing", "done", "error", "cancelled"
User string // e.g. "admin"
Deployment string
Description string // e.g. "create release"
Result string // e.g. "Created release `bosh-ui/0+dev.17'"
}
func NewTaskFromResp(client Client, r TaskResp) TaskImpl {
return TaskImpl{
client: client,
id: r.ID,
startedAt: time.Unix(r.StartedAt, 0).UTC(),
lastActivityAt: time.Unix(r.LastActivityAt, 0).UTC(),
state: r.State,
user: r.User,
deploymentName: r.Deployment,
description: r.Description,
result: r.Result,
}
}
func (d DirectorImpl) CurrentTasks(filter TasksFilter) ([]Task, error) {
tasks := []Task{}
taskResps, err := d.client.CurrentTasks(filter)
if err != nil {
return tasks, err
}
for _, r := range taskResps {
tasks = append(tasks, NewTaskFromResp(d.client, r))
}
return tasks, nil
}
func (d DirectorImpl) RecentTasks(limit int, filter TasksFilter) ([]Task, error) {
tasks := []Task{}
taskResps, err := d.client.RecentTasks(limit, filter)
if err != nil {
return tasks, err
}
for _, r := range taskResps {
tasks = append(tasks, NewTaskFromResp(d.client, r))
}
return tasks, nil
}
func (d DirectorImpl) FindTask(id int) (Task, error) {
taskResp, err := d.client.Task(id)
if err != nil {
return TaskImpl{}, err
}
return NewTaskFromResp(d.client, taskResp), nil
}
func (t TaskImpl) EventOutput(taskReporter TaskReporter) error {
return t.client.TaskOutput(t.id, "event", taskReporter)
}
func (t TaskImpl) CPIOutput(taskReporter TaskReporter) error {
return t.client.TaskOutput(t.id, "cpi", taskReporter)
}
func (t TaskImpl) DebugOutput(taskReporter TaskReporter) error {
return t.client.TaskOutput(t.id, "debug", taskReporter)
}
func (t TaskImpl) ResultOutput(taskReporter TaskReporter) error {
return t.client.TaskOutput(t.id, "result", taskReporter)
}
func (t TaskImpl) RawOutput(taskReporter TaskReporter) error {
return t.client.TaskOutput(t.id, "raw", taskReporter)
}
func (c Client) CurrentTasks(filter TasksFilter) ([]TaskResp, error) {
var tasks []TaskResp
query := gourl.Values{}
query.Add("state", "processing,cancelling,queued")
query.Add("verbose", c.taskVerbosity(filter.All))
if len(filter.Deployment) > 0 {
query.Add("deployment", filter.Deployment)
}
path := fmt.Sprintf("/tasks?%s", query.Encode())
err := c.clientRequest.Get(path, &tasks)
if err != nil {
return tasks, bosherr.WrapErrorf(err, "Finding current tasks")
}
return tasks, nil
}
func (c Client) RecentTasks(limit int, filter TasksFilter) ([]TaskResp, error) {
var tasks []TaskResp
query := gourl.Values{}
query.Add("limit", fmt.Sprintf("%d", limit))
query.Add("verbose", c.taskVerbosity(filter.All))
if len(filter.Deployment) > 0 {
query.Add("deployment", filter.Deployment)
}
path := fmt.Sprintf("/tasks?%s", query.Encode())
err := c.clientRequest.Get(path, &tasks)
if err != nil {
return tasks, bosherr.WrapErrorf(err, "Finding recent tasks")
}
return tasks, nil
}
func (c Client) taskVerbosity(includeAll bool) string {
if includeAll {
return "2"
}
return "1"
}
func (c Client) Task(id int) (TaskResp, error) {
var task TaskResp
err := c.clientRequest.Get(fmt.Sprintf("/tasks/%d", id), &task)
if err != nil {
return task, bosherr.WrapErrorf(err, "Finding task '%d'", id)
}
return task, nil
}
func (c Client) TaskOutput(id int, type_ string, taskReporter TaskReporter) error {
err := c.taskClientRequest.WaitForCompletion(id, type_, taskReporter)
if err != nil {
return bosherr.WrapErrorf(err, "Capturing task '%d' output", id)
}
return nil
}
func (c Client) CancelTask(id int) error {
path := fmt.Sprintf("/task/%d", id)
_, _, err := c.clientRequest.RawDelete(path)
if err != nil {
return bosherr.WrapErrorf(err, "Cancelling task '%d'", id)
}
return nil
}