forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
484 lines (398 loc) · 11.3 KB
/
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package jenkins
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/httperror"
)
type Client struct {
API string
User string
Token string
CrumbHeader string
CrumbBody string
HTTPClient *http.Client
}
func New(api string, user string, token string, httpClient *http.Client) (*Client, error) {
c := &Client{
API: api,
User: user,
Token: token,
HTTPClient: httpClient,
}
if err := c.getCSRF(); err != nil {
return nil, err
}
return c, nil
}
func (c *Client) getCSRF() error {
getCrumbURL, err := url.Parse(c.API + GetCrumbURI)
if err != nil {
return err
}
req, _ := http.NewRequest(http.MethodGet, getCrumbURL.String(), nil)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, _ := ioutil.ReadAll(resp.Body)
Crumbs := strings.Split(string(data), ":")
if len(Crumbs) != 2 {
return errors.New("error get crumbs, Jenkins is probably not ready yet")
}
c.CrumbHeader = Crumbs[0]
c.CrumbBody = Crumbs[1]
return nil
}
//deleteBuild deletes the last build of a job
func (c *Client) deleteBuild(jobname string, buildNumber int) error {
deleteBuildURI := fmt.Sprintf(DeleteBuildURI, jobname, buildNumber)
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + deleteBuildURI)
if err != nil {
return err
}
req, _ := http.NewRequest(http.MethodPost, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "delete build")
}
func (c *Client) execScript(script string) (string, error) {
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + ScriptURI)
if err != nil {
return "", err
}
v := url.Values{}
v.Add("script", script)
req, _ := http.NewRequest(http.MethodPost, targetURL.String(), bytes.NewBufferString(v.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "exec script"); err != nil {
return "", err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(data), nil
}
func (c *Client) createJob(jobname string, content []byte) error {
createJobURL, err := url.Parse(c.API + CreateJobURI)
if err != nil {
return err
}
qry := createJobURL.Query()
qry.Add("name", jobname)
createJobURL.RawQuery = qry.Encode()
//send request part
req, _ := http.NewRequest(http.MethodPost, createJobURL.String(), bytes.NewReader(content))
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.Header.Set("Content-Type", "application/xml")
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "create job")
}
func (c *Client) updateJob(jobname string, content []byte) error {
updateJobURI := fmt.Sprintf(UpdateJobURI, jobname)
updateJobURL, err := url.Parse(c.API + updateJobURI)
if err != nil {
return err
}
//send request part
req, _ := http.NewRequest(http.MethodPost, updateJobURL.String(), bytes.NewReader(content))
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.Header.Set("Content-Type", "application/xml")
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "update job")
}
func (c *Client) buildJob(jobname string, params map[string]string) (string, error) {
buildURI := fmt.Sprintf(JenkinsJobBuildURI, jobname)
var targetURL *url.URL
targetURL, err := url.Parse(c.API + buildURI)
if err != nil {
return "", err
}
req, _ := http.NewRequest(http.MethodPost, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
return "", checkHTTPError(resp, "build job")
}
func (c *Client) getBuildInfo(jobname string) (*BuildInfo, error) {
buildInfoURI := fmt.Sprintf(JenkinsBuildInfoURI, jobname)
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + buildInfoURI)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodGet, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "get build info"); err != nil {
return nil, err
}
buildInfo := &BuildInfo{}
respBytes, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBytes, buildInfo)
if err != nil {
return nil, err
}
return buildInfo, nil
}
func (c *Client) getJobInfo(jobname string) (*JobInfo, error) {
jobInfoURI := fmt.Sprintf(JenkinsJobInfoURI, jobname)
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + jobInfoURI)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodGet, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "get job info"); err != nil {
return nil, err
}
jobInfo := &JobInfo{}
respBytes, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBytes, jobInfo)
if err != nil {
return nil, err
}
return jobInfo, nil
}
func (c *Client) getBuildRawOutput(jobname string, buildNumber int, startLine int) (string, error) {
buildRawOutputURI := fmt.Sprintf(JenkinsBuildLogURI, jobname, buildNumber)
if startLine > 0 {
buildRawOutputURI += "&startLine=" + strconv.Itoa(startLine)
}
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + buildRawOutputURI)
if err != nil {
return "", err
}
req, _ := http.NewRequest(http.MethodGet, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "get build output"); err != nil {
return "", err
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(respBytes), nil
}
func (c *Client) stopJob(jobname string, buildNumber int) error {
stopJobURI := fmt.Sprintf(StopJobURI, jobname, buildNumber)
targetURL, err := url.Parse(c.API + stopJobURI)
if err != nil {
return err
}
req, _ := http.NewRequest(http.MethodPost, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "stop job")
}
func (c *Client) cancelQueueItem(id int) error {
cancelQueueItemURI := fmt.Sprintf(CancelQueueItemURI, id)
targetURL, err := url.Parse(c.API + cancelQueueItemURI)
if err != nil {
return err
}
req, _ := http.NewRequest(http.MethodPost, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
//no redirect
return http.ErrUseLastResponse
},
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "cancel queue")
}
func (c *Client) getWFBuildInfo(jobname string) (*WFBuildInfo, error) {
buildInfoURI := fmt.Sprintf(JenkinsWFBuildInfoURI, jobname)
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + buildInfoURI)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodGet, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "get build info"); err != nil {
return nil, err
}
buildInfo := &WFBuildInfo{}
respBytes, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBytes, buildInfo)
if err != nil {
return nil, err
}
return buildInfo, nil
}
func (c *Client) getWFNodeInfo(jobname string, nodeID string) (*WFNodeInfo, error) {
nodeInfoURI := fmt.Sprintf(JenkinsWFNodeInfoURI, jobname, nodeID)
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + nodeInfoURI)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodGet, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "get WFNode info"); err != nil {
return nil, err
}
nodeInfo := &WFNodeInfo{}
respBytes, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBytes, nodeInfo)
if err != nil {
return nil, err
}
return nodeInfo, nil
}
func (c *Client) getWFNodeLog(jobname string, nodeID string) (*WFNodeLog, error) {
nodeLogURI := fmt.Sprintf(JenkinsWFNodeLogURI, jobname, nodeID)
var targetURL *url.URL
var err error
targetURL, err = url.Parse(c.API + nodeLogURI)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodGet, targetURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := checkHTTPError(resp, "get WFNode log"); err != nil {
return nil, err
}
nodeLog := &WFNodeLog{}
respBytes, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(respBytes, nodeLog)
if err != nil {
return nil, err
}
return nodeLog, nil
}
func (c *Client) createCredential(content []byte) error {
setCredURL, err := url.Parse(c.API + JenkinsSetCredURI)
if err != nil {
return err
}
req, _ := http.NewRequest(http.MethodPost, setCredURL.String(), bytes.NewReader(content))
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "create credential")
}
func (c *Client) getCredential(credentialID string) error {
getCredURI := fmt.Sprintf(JenkinsGetCredURI, credentialID)
getCredURL, err := url.Parse(c.API + getCredURI)
if err != nil {
return err
}
req, _ := http.NewRequest(http.MethodGet, getCredURL.String(), nil)
req.Header.Add(c.CrumbHeader, c.CrumbBody)
req.SetBasicAuth(c.User, c.Token)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return checkHTTPError(resp, "get credential")
}
func checkHTTPError(resp *http.Response, event string) error {
if resp == nil {
return nil
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
data, _ := ioutil.ReadAll(resp.Body)
return httperror.NewAPIErrorLong(resp.StatusCode, fmt.Sprintf("%s got %d", event, resp.StatusCode), string(data))
}
return nil
}