2015-04-22 08:00:15 +00:00
|
|
|
package server
|
|
|
|
|
2015-05-18 22:47:13 +00:00
|
|
|
import (
|
2015-05-19 03:44:44 +00:00
|
|
|
"net"
|
2015-05-18 22:47:13 +00:00
|
|
|
"strconv"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin/binding"
|
2015-05-18 22:47:13 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
2015-05-18 22:47:13 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GET /queue/pull
|
|
|
|
func PollBuild(c *gin.Context) {
|
|
|
|
queue := ToQueue(c)
|
|
|
|
store := ToDatastore(c)
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
// extract the IP address from the agent that is
|
|
|
|
// polling for builds.
|
|
|
|
host := c.Request.RemoteAddr
|
|
|
|
addr, _, err := net.SplitHostPort(host)
|
|
|
|
if err != nil {
|
|
|
|
addr = host
|
|
|
|
}
|
|
|
|
addr = net.JoinHostPort(addr, "1999")
|
|
|
|
|
|
|
|
log.Infof("agent connected and polling builds at %s", addr)
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
// pull an item from the queue
|
|
|
|
work := queue.PullClose(c.Writer)
|
|
|
|
if work == nil {
|
|
|
|
c.AbortWithStatus(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:44:44 +00:00
|
|
|
// persist the relationship between agent and commit.
|
2015-06-23 03:45:08 +00:00
|
|
|
err = store.SetAgent(work.Build, addr)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
2015-05-19 03:44:44 +00:00
|
|
|
// note the we are ignoring and just logging the error here.
|
|
|
|
// we consider this an acceptible failure because it doesn't
|
|
|
|
// impact anything other than live-streaming output.
|
|
|
|
log.Errorf("unable to store the agent address %s for build %s %v",
|
2015-06-23 03:45:08 +00:00
|
|
|
addr, work.Repo.FullName, work.Build.Number)
|
2015-05-18 22:47:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, work)
|
|
|
|
|
|
|
|
// acknowledge work received by the client
|
|
|
|
queue.Ack(work)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /queue/push/:owner/:repo
|
|
|
|
func PushCommit(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
in := &common.Build{}
|
2015-05-18 22:47:13 +00:00
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user, err := store.User(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, in.Number)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
build.Started = in.Started
|
|
|
|
build.Finished = in.Finished
|
|
|
|
build.Status = in.Status
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
updater := ToUpdater(c)
|
2015-06-23 03:45:08 +00:00
|
|
|
err = updater.SetBuild(user, repo, build)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /queue/push/:owner/:repo/:commit
|
|
|
|
func PushBuild(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
cnum, _ := strconv.Atoi(c.Params.ByName("commit"))
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
in := &common.Job{}
|
2015-05-18 22:47:13 +00:00
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, cnum)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
job, err := store.JobNumber(build, in.Number)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
job.Started = in.Started
|
|
|
|
job.Finished = in.Finished
|
|
|
|
job.ExitCode = in.ExitCode
|
|
|
|
job.Status = in.Status
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
updater := ToUpdater(c)
|
2015-06-23 03:45:08 +00:00
|
|
|
err = updater.SetJob(repo, build, job)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST /queue/push/:owner/:repo/:comimt/:build/logs
|
|
|
|
func PushLogs(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
cnum, _ := strconv.Atoi(c.Params.ByName("commit"))
|
|
|
|
bnum, _ := strconv.Atoi(c.Params.ByName("build"))
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, cnum)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
job, err := store.JobNumber(build, bnum)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updater := ToUpdater(c)
|
2015-06-23 03:45:08 +00:00
|
|
|
err = updater.SetLogs(repo, build, job, c.Request.Body)
|
2015-05-18 22:47:13 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Writer.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetQueue(c *gin.Context) {
|
|
|
|
queue := ToQueue(c)
|
|
|
|
items := queue.Items()
|
|
|
|
c.JSON(200, items)
|
|
|
|
}
|