harness-drone/cmd/drone-agent/updater.go

120 lines
2.6 KiB
Go
Raw Normal View History

2015-05-18 17:05:58 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
2015-05-18 17:05:58 +00:00
"net/http"
"net/url"
"strconv"
"time"
2015-05-18 17:05:58 +00:00
2015-05-22 18:37:40 +00:00
logs "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
2015-05-18 17:05:58 +00:00
common "github.com/drone/drone/pkg/types"
)
type updater struct{}
2015-05-18 17:05:58 +00:00
func (u *updater) SetBuild(user *common.User, r *common.Repo, b *common.Build) error {
path := fmt.Sprintf("/api/queue/push/%s", r.FullName)
return sendBackoff("POST", path, b, nil)
2015-05-18 17:05:58 +00:00
}
func (u *updater) SetJob(r *common.Repo, b *common.Build, j *common.Job) error {
path := fmt.Sprintf("/api/queue/push/%s/%v", r.FullName, b.Number)
2015-06-19 00:36:52 +00:00
return sendBackoff("POST", path, j, nil)
}
func (u *updater) SetLogs(r *common.Repo, b *common.Build, j *common.Job, rc io.ReadCloser) error {
path := fmt.Sprintf("/api/queue/push/%s/%v/%v", r.FullName, b.Number, j.Number)
return sendBackoff("POST", path, rc, nil)
}
func sendBackoff(method, path string, in, out interface{}) error {
var err error
var attempts int
for {
err = send(method, path, in, out)
if err == nil {
break
}
if attempts > 99 {
break
}
attempts++
time.Sleep(time.Second * 30)
2015-05-18 17:05:58 +00:00
}
return err
}
// do makes an http.Request and returns the response
func send(method, path string, in, out interface{}) error {
2015-05-18 17:05:58 +00:00
// create the URI
uri, err := url.Parse(addr + path)
2015-05-18 17:05:58 +00:00
if err != nil {
return err
}
if len(uri.Scheme) == 0 {
uri.Scheme = "http"
2015-05-18 17:05:58 +00:00
}
params := uri.Query()
params.Add("token", token)
uri.RawQuery = params.Encode()
// create the request
req, err := http.NewRequest(method, uri.String(), nil)
2015-05-18 17:05:58 +00:00
if err != nil {
return err
}
req.ProtoAtLeast(1, 1)
req.Close = true
req.ContentLength = 0
// If the data is a readCloser we can attach directly
// to the request body.
//
// Else we serialize the data input as JSON.
if rc, ok := in.(io.ReadCloser); ok {
req.Body = rc
2015-05-18 17:05:58 +00:00
} else if in != nil {
inJson, err := json.Marshal(in)
if err != nil {
return err
}
buf := bytes.NewBuffer(inJson)
req.Body = ioutil.NopCloser(buf)
req.ContentLength = int64(len(inJson))
req.Header.Set("Content-Length", strconv.Itoa(len(inJson)))
req.Header.Set("Content-Type", "application/json")
}
// make the request using the default http client
resp, err := http.DefaultClient.Do(req)
2015-05-18 17:05:58 +00:00
if err != nil {
logs.Errorf("Error posting request. %s", err)
2015-05-18 17:05:58 +00:00
return err
}
defer resp.Body.Close()
// Check for an http error status (ie not 200 StatusOK)
if resp.StatusCode > 300 {
logs.Errorf("Error status code %d", resp.StatusCode)
return fmt.Errorf(resp.Status)
}
// Decode the JSON response
if out != nil {
err = json.NewDecoder(resp.Body).Decode(out)
2015-05-18 17:05:58 +00:00
}
return err
2015-05-18 17:05:58 +00:00
}