示例#1
0
func (n *GitLabClient) GetBuild(config common.RunnerConfig) (*common.GetBuildResponse, bool) {
	request := common.GetBuildRequest{
		Info:  n.getRunnerVersion(config),
		Token: config.Token,
	}

	var response common.GetBuildResponse
	result, statusText, certificates := n.doJSON(config.RunnerCredentials, "POST", "builds/register.json", 201, &request, &response)

	switch result {
	case 201:
		config.Log().WithFields(logrus.Fields{
			"build":    strconv.Itoa(response.ID),
			"repo_url": response.RepoCleanURL(),
		}).Println("Checking for builds...", "received")
		response.TLSCAChain = certificates
		return &response, true
	case 403:
		config.Log().Errorln("Checking for builds...", "forbidden")
		return nil, false
	case 204, 404:
		config.Log().Debugln("Checking for builds...", "nothing")
		return nil, true
	case clientError:
		config.Log().WithField("status", statusText).Errorln("Checking for builds...", "error")
		return nil, false
	default:
		config.Log().WithField("status", statusText).Warningln("Checking for builds...", "failed")
		return nil, true
	}
}
示例#2
0
func (n *GitLabClient) UpdateBuild(config common.RunnerConfig, id int, state common.BuildState, trace *string) common.UpdateState {
	request := common.UpdateBuildRequest{
		Info:  n.getRunnerVersion(config),
		Token: config.Token,
		State: state,
		Trace: trace,
	}

	log := config.Log().WithField("build", id)

	result, statusText, _ := n.doJSON(config.RunnerCredentials, "PUT", fmt.Sprintf("builds/%d.json", id), 200, &request, nil)
	switch result {
	case 200:
		log.Debugln("Submitting build to coordinator...", "ok")
		return common.UpdateSucceeded
	case 404:
		log.Warningln("Submitting build to coordinator...", "aborted")
		return common.UpdateAbort
	case 403:
		log.WithField("status", statusText).Errorln("Submitting build to coordinator...", "forbidden")
		return common.UpdateAbort
	case clientError:
		log.WithField("status", statusText).Errorln("Submitting build to coordinator...", "error")
		return common.UpdateAbort
	default:
		log.WithField("status", statusText).Warningln("Submitting build to coordinator...", "failed")
		return common.UpdateFailed
	}
}
示例#3
0
func (c *clientBuildTrace) resendPatch(id int, config common.RunnerConfig, buildCredentials *common.BuildCredentials, tracePatch common.BuildTracePatch) (update common.UpdateState) {
	config.Log().Warningln(id, "Resending trace patch due to range missmatch")

	update = c.client.PatchTrace(config, buildCredentials, tracePatch)
	if update == common.UpdateRangeMissmatch {
		config.Log().Errorln(id, "Appending trace to coordinator...", "failed due to range missmatch")
		update = common.UpdateFailed
	}

	return
}
示例#4
0
func (n *GitLabClient) PatchTrace(config common.RunnerConfig, buildCredentials *common.BuildCredentials, tracePatch common.BuildTracePatch) common.UpdateState {
	id := buildCredentials.ID

	contentRange := fmt.Sprintf("%d-%d", tracePatch.Offset(), tracePatch.Limit())
	headers := make(http.Header)
	headers.Set("Content-Range", contentRange)
	headers.Set("BUILD-TOKEN", buildCredentials.Token)
	uri := fmt.Sprintf("builds/%d/trace.txt", id)
	request := bytes.NewReader(tracePatch.Patch())

	response, err := n.doRaw(config.RunnerCredentials, "PATCH", uri, request, "text/plain", headers)
	if err != nil {
		config.Log().Errorln("Appending trace to coordinator...", "error", err.Error())
		return common.UpdateFailed
	}

	defer response.Body.Close()
	defer io.Copy(ioutil.Discard, response.Body)

	remoteState := response.Header.Get("Build-Status")
	remoteRange := response.Header.Get("Range")
	log := config.Log().WithFields(logrus.Fields{
		"build":        id,
		"sent-log":     contentRange,
		"build-log":    remoteRange,
		"build-status": remoteState,
		"code":         response.StatusCode,
		"status":       response.Status,
	})

	if remoteState == "canceled" {
		log.Warningln("Appending trace to coordinator", "aborted")
		return common.UpdateAbort
	}

	switch response.StatusCode {
	case 202:
		log.Debugln("Appending trace to coordinator...", "ok")
		return common.UpdateSucceeded
	case 404:
		log.Warningln("Appending trace to coordinator...", "not-found")
		return common.UpdateNotFound
	case 406:
		log.Errorln("Appending trace to coordinator...", "forbidden")
		return common.UpdateAbort
	case 416:
		log.Warningln("Appending trace to coordinator...", "range missmatch")

		remoteRange := strings.Split(remoteRange, "-")
		newOffset, _ := strconv.Atoi(remoteRange[1])
		tracePatch.SetNewOffset(newOffset)

		return common.UpdateRangeMissmatch
	case clientError:
		log.Errorln("Appending trace to coordinator...", "error")
		return common.UpdateAbort
	default:
		log.Warningln("Appending trace to coordinator...", "failed")
		return common.UpdateFailed
	}
}