2015-04-08 22:43:59 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2015-04-16 21:45:05 +00:00
|
|
|
"io"
|
2015-04-08 22:43:59 +00:00
|
|
|
"strconv"
|
2015-04-28 23:27:30 +00:00
|
|
|
"time"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-04-16 21:45:05 +00:00
|
|
|
"github.com/drone/drone/common"
|
2015-04-28 22:08:21 +00:00
|
|
|
"github.com/drone/drone/parser/inject"
|
2015-04-24 21:25:03 +00:00
|
|
|
"github.com/drone/drone/queue"
|
2015-04-08 22:43:59 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-16 21:45:05 +00:00
|
|
|
"github.com/gin-gonic/gin/binding"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetBuild accepts a request to retrieve a build
|
|
|
|
// from the datastore for the given repository and
|
|
|
|
// build number.
|
|
|
|
//
|
|
|
|
// GET /api/builds/:owner/:name/:number
|
|
|
|
//
|
|
|
|
func GetBuild(c *gin.Context) {
|
2015-04-15 05:04:38 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
repo := ToRepo(c)
|
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-15 05:04:38 +00:00
|
|
|
build, err := store.Build(repo.FullName, num)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, build)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 21:45:05 +00:00
|
|
|
// GetBuilds accepts a request to retrieve a list
|
2015-04-08 22:43:59 +00:00
|
|
|
// of builds from the datastore for the given repository.
|
|
|
|
//
|
|
|
|
// GET /api/builds/:owner/:name
|
|
|
|
//
|
|
|
|
func GetBuilds(c *gin.Context) {
|
2015-04-15 05:04:38 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-08 22:43:59 +00:00
|
|
|
repo := ToRepo(c)
|
2015-04-15 05:04:38 +00:00
|
|
|
builds, err := store.BuildList(repo.FullName)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(200, builds)
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 21:45:05 +00:00
|
|
|
|
|
|
|
// GetBuildLogs accepts a request to retrieve logs from the
|
|
|
|
// datastore for the given repository, build and task
|
|
|
|
// number.
|
|
|
|
//
|
|
|
|
// GET /api/repos/:owner/:name/logs/:number/:task
|
|
|
|
//
|
|
|
|
func GetBuildLogs(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
full, _ := strconv.ParseBool(c.Params.ByName("full"))
|
|
|
|
build, _ := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
task, _ := strconv.Atoi(c.Params.ByName("task"))
|
|
|
|
|
|
|
|
r, err := store.LogReader(repo.FullName, build, task)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
} else if full {
|
|
|
|
io.Copy(c.Writer, r)
|
|
|
|
} else {
|
|
|
|
io.Copy(c.Writer, io.LimitReader(r, 2000000))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostBuildStatus accepts a request to create a new build
|
|
|
|
// status. The created user status is returned in JSON
|
|
|
|
// format if successful.
|
|
|
|
//
|
|
|
|
// POST /api/repos/:owner/:name/status/:number
|
|
|
|
//
|
|
|
|
func PostBuildStatus(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
in := &common.Status{}
|
|
|
|
if !c.BindWith(in, binding.JSON) {
|
|
|
|
c.AbortWithStatus(400)
|
|
|
|
return
|
|
|
|
}
|
2015-04-23 18:15:17 +00:00
|
|
|
if err := store.SetBuildStatus(repo.Name, num, in); err != nil {
|
2015-04-16 21:45:05 +00:00
|
|
|
c.Fail(400, err)
|
|
|
|
} else {
|
|
|
|
c.JSON(201, in)
|
|
|
|
}
|
|
|
|
}
|
2015-04-17 05:07:13 +00:00
|
|
|
|
|
|
|
// RunBuild accepts a request to restart an existing build.
|
|
|
|
//
|
|
|
|
// POST /api/builds/:owner/:name/builds/:number
|
|
|
|
//
|
|
|
|
func RunBuild(c *gin.Context) {
|
2015-04-28 22:08:21 +00:00
|
|
|
remote := ToRemote(c)
|
2015-04-17 05:07:13 +00:00
|
|
|
store := ToDatastore(c)
|
2015-04-24 21:25:03 +00:00
|
|
|
queue_ := ToQueue(c)
|
2015-04-17 05:07:13 +00:00
|
|
|
repo := ToRepo(c)
|
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
build, err := store.Build(repo.FullName, num)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-24 21:25:03 +00:00
|
|
|
keys, err := store.RepoKeypair(repo.FullName)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := store.User(repo.User.Login)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-17 05:07:13 +00:00
|
|
|
// must not restart a running build
|
|
|
|
if build.State == common.StatePending || build.State == common.StateRunning {
|
2015-04-30 02:57:43 +00:00
|
|
|
c.AbortWithStatus(409)
|
2015-04-17 05:07:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
build.State = common.StatePending
|
|
|
|
build.Started = 0
|
|
|
|
build.Finished = 0
|
|
|
|
build.Duration = 0
|
|
|
|
build.Statuses = []*common.Status{}
|
|
|
|
for _, task := range build.Tasks {
|
|
|
|
task.State = common.StatePending
|
|
|
|
task.Started = 0
|
|
|
|
task.Finished = 0
|
|
|
|
task.ExitCode = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.SetBuild(repo.FullName, build)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:08:21 +00:00
|
|
|
netrc, err := remote.Netrc(user)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// featch the .drone.yml file from the database
|
|
|
|
raw, err := remote.Script(user, repo, build)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject any private parameters into the .drone.yml
|
|
|
|
params, _ := store.RepoParams(repo.FullName)
|
|
|
|
if params != nil && len(params) != 0 {
|
|
|
|
raw = []byte(inject.InjectSafe(string(raw), params))
|
|
|
|
}
|
2015-04-21 22:48:06 +00:00
|
|
|
|
2015-04-24 21:25:03 +00:00
|
|
|
queue_.Publish(&queue.Work{
|
|
|
|
User: user,
|
|
|
|
Repo: repo,
|
|
|
|
Build: build,
|
|
|
|
Keys: keys,
|
2015-04-28 22:08:21 +00:00
|
|
|
Netrc: netrc,
|
|
|
|
Yaml: raw,
|
2015-04-24 21:25:03 +00:00
|
|
|
})
|
2015-04-17 05:07:13 +00:00
|
|
|
|
|
|
|
c.JSON(202, build)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KillBuild accepts a request to kill a running build.
|
|
|
|
//
|
|
|
|
// DELETE /api/builds/:owner/:name/builds/:number
|
|
|
|
//
|
|
|
|
func KillBuild(c *gin.Context) {
|
2015-04-28 23:27:30 +00:00
|
|
|
queue := ToQueue(c)
|
2015-04-17 05:07:13 +00:00
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
build, err := store.Build(repo.FullName, num)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// must not restart a running build
|
|
|
|
if build.State != common.StatePending && build.State != common.StateRunning {
|
|
|
|
c.Fail(409, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-28 23:27:30 +00:00
|
|
|
// remove from the queue if exists
|
|
|
|
for _, item := range queue.Items() {
|
|
|
|
if item.Repo.FullName == repo.FullName && item.Build.Number == build.Number {
|
|
|
|
queue.Remove(item)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
build.State = common.StateKilled
|
|
|
|
build.Finished = time.Now().Unix()
|
|
|
|
if build.Started == 0 {
|
|
|
|
build.Started = build.Finished
|
|
|
|
}
|
|
|
|
build.Duration = build.Finished - build.Started
|
|
|
|
for _, task := range build.Tasks {
|
|
|
|
if task.State != common.StatePending && task.State != common.StateRunning {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
task.State = common.StateKilled
|
|
|
|
task.Started = build.Started
|
|
|
|
task.Finished = build.Finished
|
|
|
|
}
|
|
|
|
err = store.SetBuild(repo.FullName, build)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
2015-04-17 05:07:13 +00:00
|
|
|
|
2015-04-28 23:27:30 +00:00
|
|
|
c.JSON(200, build)
|
2015-04-17 05:07:13 +00:00
|
|
|
}
|