devops.go 12.7 KB
Newer Older
S
sunzhu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*

  Copyright 2019 The KubeSphere Authors.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

R
update  
runzexia 已提交
17
*/
S
sunzhu 已提交
18 19 20 21 22
package devops

import (
	"compress/gzip"
	"fmt"
Z
Zhuxiaoyang 已提交
23
	"github.com/PuerkitoBio/goquery"
S
sunzhu 已提交
24 25 26
	log "github.com/golang/glog"
	"io"
	"io/ioutil"
S
soulseen 已提交
27
	"kubesphere.io/kubesphere/pkg/gojenkins"
28
	"kubesphere.io/kubesphere/pkg/simple/client/admin_jenkins"
S
sunzhu 已提交
29 30 31 32 33
	"net/http"
	"net/url"
	"time"
)

S
soulseen 已提交
34
var jenkins *gojenkins.Jenkins
S
sunzhu 已提交
35

36
func PreCheckJenkins() {
S
soulseen 已提交
37
	jenkins = admin_jenkins.Client()
S
sunzhu 已提交
38 39
}

Z
Zhuxiaoyang 已提交
40
func GetPipeline(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
41
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipelineUrl, projectName, pipelineName)
S
sunzhu 已提交
42 43
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
44
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
45
	if err != nil {
S
soulseen 已提交
46
		log.Error(err)
S
sunzhu 已提交
47 48 49
		return nil, err
	}

Z
Zhuxiaoyang 已提交
50 51 52
	return res, err
}

Z
Zhuxiaoyang 已提交
53
func SearchPipelines(req *http.Request) ([]byte, error) {
S
soulseen 已提交
54
	baseUrl := jenkins.Server + SearchPipelineUrl + req.URL.RawQuery
Z
Zhuxiaoyang 已提交
55 56
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
57
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
58 59 60 61 62 63 64 65
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
66
func SearchPipelineRuns(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
67
	baseUrl := fmt.Sprintf(jenkins.Server+SearchPipelineRunUrl+req.URL.RawQuery, projectName, pipelineName)
Z
Zhuxiaoyang 已提交
68 69
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
70
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
71 72 73 74 75 76 77 78
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
79
func GetBranchPipelineRun(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
80
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeBranchRunUrl, projectName, pipelineName, branchName, runId)
S
sunzhu 已提交
81 82
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
83
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
84
	if err != nil {
S
soulseen 已提交
85
		log.Error(err)
S
sunzhu 已提交
86 87 88
		return nil, err
	}

Z
Zhuxiaoyang 已提交
89 90 91
	return res, err
}

Z
Zhuxiaoyang 已提交
92
func GetPipelineRunNodesbyBranch(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
93
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
94 95
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
96
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
97 98 99 100 101 102 103 104
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
105
func GetStepLog(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
106
	baseUrl := fmt.Sprintf(jenkins.Server+GetStepLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
Z
Zhuxiaoyang 已提交
107 108
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
109
	resBody, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
110 111 112 113 114 115 116 117 118
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

func Validate(scmId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
119
	baseUrl := fmt.Sprintf(jenkins.Server+ValidateUrl, scmId)
S
sunzhu 已提交
120 121
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
122
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
123
	if err != nil {
S
soulseen 已提交
124
		log.Error(err)
S
sunzhu 已提交
125 126 127
		return nil, err
	}

Z
Zhuxiaoyang 已提交
128 129 130
	return resBody, err
}

Z
Zhuxiaoyang 已提交
131
func GetSCMOrg(scmId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
132
	baseUrl := fmt.Sprintf(jenkins.Server+GetSCMOrgUrl+req.URL.RawQuery, scmId)
Z
Zhuxiaoyang 已提交
133 134
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
135
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
136 137 138 139 140 141 142 143
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
144
func GetOrgRepo(scmId, organizationId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
145
	baseUrl := fmt.Sprintf(jenkins.Server+GetOrgRepoUrl+req.URL.RawQuery, scmId, organizationId)
S
sunzhu 已提交
146 147
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
148
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
149
	if err != nil {
S
soulseen 已提交
150
		log.Error(err)
S
sunzhu 已提交
151 152 153
		return nil, err
	}

Z
Zhuxiaoyang 已提交
154 155 156
	return res, err
}

Z
Zhuxiaoyang 已提交
157
func StopPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
158
	baseUrl := fmt.Sprintf(jenkins.Server+StopPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
159 160
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
161
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
162 163 164 165 166 167 168 169
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
170
func ReplayPipeline(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
171
	baseUrl := fmt.Sprintf(jenkins.Server+ReplayPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
S
sunzhu 已提交
172 173
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
174
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
175
	if err != nil {
S
soulseen 已提交
176
		log.Error(err)
S
sunzhu 已提交
177 178 179
		return nil, err
	}

Z
Zhuxiaoyang 已提交
180 181 182 183
	return res, err
}

func GetRunLog(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
184
	baseUrl := fmt.Sprintf(jenkins.Server+GetRunLogUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
185 186
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
187
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
188 189 190 191 192
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
193
	return res, err
Z
Zhuxiaoyang 已提交
194 195
}

Z
Zhuxiaoyang 已提交
196
func GetArtifacts(projectName, pipelineName, branchName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
197
	baseUrl := fmt.Sprintf(jenkins.Server+GetArtifactsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId)
Z
Zhuxiaoyang 已提交
198 199
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
200
	res, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
201 202 203 204 205 206 207 208
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
209
func GetPipeBranch(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
210
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeBranchUrl+req.URL.RawQuery, projectName, pipelineName)
Z
Zhuxiaoyang 已提交
211 212
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
213
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
214 215 216 217 218 219 220 221 222
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func CheckPipeline(projectName, pipelineName, branchName, runId, nodeId, stepId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
223
	baseUrl := fmt.Sprintf(jenkins.Server+CheckPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId, stepId)
S
sunzhu 已提交
224 225
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
226
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
sunzhu 已提交
227
	if err != nil {
S
soulseen 已提交
228 229 230 231 232 233 234
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

Z
Zhuxiaoyang 已提交
235
func GetConsoleLog(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
236
	baseUrl := fmt.Sprintf(jenkins.Server+GetConsoleLogUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
237 238
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
239
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
240 241
	if err != nil {
		log.Error(err)
S
sunzhu 已提交
242 243 244 245 246 247
		return nil, err
	}

	return resBody, err
}

Z
Zhuxiaoyang 已提交
248
func ScanBranch(projectName, pipelineName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
249
	baseUrl := fmt.Sprintf(jenkins.Server+ScanBranchUrl+req.URL.RawQuery, projectName, pipelineName)
S
soulseen 已提交
250 251
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
252
	resBody, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
253 254 255 256 257
	if err != nil {
		log.Error(err)
		return nil, err
	}

Z
Zhuxiaoyang 已提交
258 259 260
	return resBody, err
}

Z
Zhuxiaoyang 已提交
261
func RunPipeline(projectName, pipelineName, branchName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
262
	baseUrl := fmt.Sprintf(jenkins.Server+RunPipelineUrl+req.URL.RawQuery, projectName, pipelineName, branchName)
Z
Zhuxiaoyang 已提交
263 264
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
265
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
266 267 268 269 270 271 272 273
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
274
func GetStepsStatus(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
275
	baseUrl := fmt.Sprintf(jenkins.Server+GetStepsStatusUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId)
Z
Zhuxiaoyang 已提交
276 277
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
278
	res, err := sendJenkinsRequest(baseUrl, req)
S
soulseen 已提交
279 280 281 282 283 284 285 286
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
287
func GetCrumb(req *http.Request) ([]byte, error) {
S
soulseen 已提交
288
	baseUrl := fmt.Sprintf(jenkins.Server + GetCrumbUrl)
Z
Zhuxiaoyang 已提交
289 290
	log.Infof("Jenkins-url: " + baseUrl)

Z
Zhuxiaoyang 已提交
291
	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
292 293 294 295 296 297 298 299
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

Z
Zhuxiaoyang 已提交
300
func CheckScriptCompile(req *http.Request) ([]byte, error) {
S
soulseen 已提交
301
	baseUrl := jenkins.Server + CheckScriptCompileUrl
Z
Zhuxiaoyang 已提交
302
	log.Infof("Jenkins-url: " + baseUrl)
S
soulseen 已提交
303
	req.SetBasicAuth(jenkins.Requester.BasicAuth.Username, jenkins.Requester.BasicAuth.Password)
Z
Zhuxiaoyang 已提交
304 305 306 307 308 309 310 311 312 313 314

	resBody, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return resBody, err
}

func CheckCron(req *http.Request) (*CheckCronRes, error) {
S
soulseen 已提交
315
	baseUrl := jenkins.Server + CheckCronUrl + req.URL.RawQuery
Z
Zhuxiaoyang 已提交
316
	log.Infof("Jenkins-url: " + baseUrl)
S
soulseen 已提交
317
	req.SetBasicAuth(jenkins.Requester.BasicAuth.Username, jenkins.Requester.BasicAuth.Password)
Z
Zhuxiaoyang 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	var res = new(CheckCronRes)

	resp, err := http.Get(baseUrl)
	if err != nil {
		log.Error(err)
		return res, err
	}
	defer resp.Body.Close()

	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Error(err)
		return res, err
	}
	doc.Find("div").Each(func(i int, selection *goquery.Selection) {
		res.Message = selection.Text()
		res.Result, _ = selection.Attr("class")
	})
	return res, err
}

func GetPipelineRun(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
340
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipelineRunUrl, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
341 342 343
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
344 345
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
346
		return nil, err
Z
Zhuxiaoyang 已提交
347 348
	}

Z
Zhuxiaoyang 已提交
349 350 351
	return res, err
}

Z
Zhuxiaoyang 已提交
352
func GetBranchPipeline(projectName, pipelineName, branchName string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
353
	baseUrl := fmt.Sprintf(jenkins.Server+GetBranchPipeUrl, projectName, pipelineName, branchName)
Z
Zhuxiaoyang 已提交
354 355 356
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
Z
Zhuxiaoyang 已提交
357 358
	if err != nil {
		log.Error(err)
Z
Zhuxiaoyang 已提交
359
		return nil, err
Z
Zhuxiaoyang 已提交
360 361
	}

Z
Zhuxiaoyang 已提交
362 363 364
	return res, err
}

Z
Zhuxiaoyang 已提交
365
func GetPipelineRunNodes(projectName, pipelineName, runId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
366
	baseUrl := fmt.Sprintf(jenkins.Server+GetPipeRunNodesUrl+req.URL.RawQuery, projectName, pipelineName, runId)
Z
Zhuxiaoyang 已提交
367 368 369 370 371 372 373 374 375 376 377 378
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GetNodeSteps(projectName, pipelineName, branchName, runId, nodeId string, req *http.Request) ([]byte, error) {
S
soulseen 已提交
379
	baseUrl := fmt.Sprintf(jenkins.Server+GetNodeStepsUrl+req.URL.RawQuery, projectName, pipelineName, branchName, runId, nodeId)
Z
Zhuxiaoyang 已提交
380 381 382 383 384 385 386 387 388 389 390 391
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func ToJenkinsfile(req *http.Request) ([]byte, error) {
S
soulseen 已提交
392
	baseUrl := fmt.Sprintf(jenkins.Server + ToJenkinsfileUrl)
Z
Zhuxiaoyang 已提交
393 394 395 396 397 398 399 400 401 402 403 404
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func ToJson(req *http.Request) ([]byte, error) {
S
soulseen 已提交
405
	baseUrl := fmt.Sprintf(jenkins.Server + ToJsonUrl)
Z
Zhuxiaoyang 已提交
406 407 408 409 410 411 412 413 414 415 416 417
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GetNotifyCommit(req *http.Request) ([]byte, error) {
S
soulseen 已提交
418
	baseUrl := fmt.Sprintf(jenkins.Server + GetNotifyCommitUrl + req.URL.RawQuery)
Z
Zhuxiaoyang 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431
	log.Infof("Jenkins-url: " + baseUrl)
	req.Method = "GET"

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
}

func GithubWebhook(req *http.Request) ([]byte, error) {
S
soulseen 已提交
432
	baseUrl := fmt.Sprintf(jenkins.Server + GithubWebhookUrl + req.URL.RawQuery)
Z
Zhuxiaoyang 已提交
433 434 435 436 437 438 439 440 441
	log.Infof("Jenkins-url: " + baseUrl)

	res, err := sendJenkinsRequest(baseUrl, req)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	return res, err
Z
Zhuxiaoyang 已提交
442 443
}

Z
Zhuxiaoyang 已提交
444 445
// create jenkins request
func sendJenkinsRequest(baseUrl string, req *http.Request) ([]byte, error) {
S
sunzhu 已提交
446 447 448 449 450 451 452 453 454
	newReqUrl, err := url.Parse(baseUrl)
	if err != nil {
		log.Error(err)
		return nil, err
	}

	client := &http.Client{Timeout: 30 * time.Second}

	newRequest := &http.Request{
Z
Zhuxiaoyang 已提交
455 456 457 458 459 460
		Method:   req.Method,
		URL:      newReqUrl,
		Header:   req.Header,
		Body:     req.Body,
		Form:     req.Form,
		PostForm: req.PostForm,
S
sunzhu 已提交
461 462 463 464 465 466 467 468 469 470
	}

	resp, err := client.Do(newRequest)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	defer resp.Body.Close()

	resBody, _ := getRespBody(resp)
S
soulseen 已提交
471
	log.Info(string(resBody))
S
sunzhu 已提交
472 473
	if resp.StatusCode >= http.StatusBadRequest {
		jkerr := new(JkError)
Z
Zhuxiaoyang 已提交
474 475
		jkerr.Code = resp.StatusCode
		jkerr.Message = http.StatusText(resp.StatusCode)
S
sunzhu 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		return nil, jkerr
	}

	return resBody, err
}

// Decompress response.body of JenkinsAPIResponse
func getRespBody(resp *http.Response) ([]byte, error) {
	var reader io.ReadCloser
	if resp.Header.Get("Content-Encoding") == "gzip" {
		reader, _ = gzip.NewReader(resp.Body)
	} else {
		reader = resp.Body
	}
	resBody, err := ioutil.ReadAll(reader)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	return resBody, err

}