2015-05-11 07:45:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-09-09 01:55:03 +00:00
|
|
|
"os"
|
2015-05-11 07:45:31 +00:00
|
|
|
"strconv"
|
2015-09-09 01:55:03 +00:00
|
|
|
"strings"
|
2015-05-11 07:45:31 +00:00
|
|
|
"time"
|
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/gin-gonic/gin"
|
2015-05-17 18:42:56 +00:00
|
|
|
"github.com/drone/drone/pkg/queue"
|
2015-05-17 20:51:42 +00:00
|
|
|
common "github.com/drone/drone/pkg/types"
|
2015-09-02 23:14:25 +00:00
|
|
|
"github.com/drone/drone/pkg/utils/httputil"
|
2015-05-11 07:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetCommit accepts a request to retrieve a commit
|
|
|
|
// from the datastore for the given repository and
|
|
|
|
// commit sequence.
|
|
|
|
//
|
|
|
|
// GET /api/repos/:owner/:name/:number
|
|
|
|
//
|
2015-08-29 22:18:45 +00:00
|
|
|
func GetBuild(c *gin.Context) {
|
2015-05-11 07:45:31 +00:00
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, num)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
2015-08-03 06:02:44 +00:00
|
|
|
return
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build.Jobs, err = store.JobList(build)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
} else {
|
2015-06-23 03:45:08 +00:00
|
|
|
c.JSON(200, build)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommits accepts a request to retrieve a list
|
|
|
|
// of commits from the datastore for the given repository.
|
|
|
|
//
|
|
|
|
// GET /api/repos/:owner/:name/builds
|
|
|
|
//
|
2015-08-29 22:18:45 +00:00
|
|
|
func GetBuilds(c *gin.Context) {
|
2015-05-11 07:45:31 +00:00
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
2015-06-23 03:45:08 +00:00
|
|
|
builds, err := store.BuildList(repo, 20, 0)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
} else {
|
2015-06-23 03:45:08 +00:00
|
|
|
c.JSON(200, builds)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLogs 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 GetLogs(c *gin.Context) {
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
full, _ := strconv.ParseBool(c.Params.ByName("full"))
|
2015-06-23 03:45:08 +00:00
|
|
|
build, _ := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
job, _ := strconv.Atoi(c.Params.ByName("task"))
|
2015-05-11 07:45:31 +00:00
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
path := fmt.Sprintf("/logs/%s/%v/%v", repo.FullName, build, job)
|
2015-05-11 07:45:31 +00:00
|
|
|
r, err := store.GetBlobReader(path)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
2015-08-03 06:02:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer r.Close()
|
|
|
|
if full {
|
2015-05-11 07:45:31 +00:00
|
|
|
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
|
|
|
|
// }
|
|
|
|
// if err := store.SetBuildStatus(repo.Name, num, in); err != nil {
|
|
|
|
// c.Fail(400, err)
|
|
|
|
// } else {
|
|
|
|
// c.JSON(201, in)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// RunBuild accepts a request to restart an existing build.
|
|
|
|
//
|
|
|
|
// POST /api/builds/:owner/:name/builds/:number
|
|
|
|
//
|
|
|
|
func RunBuild(c *gin.Context) {
|
|
|
|
remote := ToRemote(c)
|
|
|
|
store := ToDatastore(c)
|
|
|
|
queue_ := ToQueue(c)
|
|
|
|
repo := ToRepo(c)
|
2015-06-08 00:04:57 +00:00
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, num)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build.Jobs, err = store.JobList(build)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := store.User(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// must not restart a running build
|
2015-06-23 03:45:08 +00:00
|
|
|
if build.Status == common.StatePending || build.Status == common.StateRunning {
|
2015-05-11 07:45:31 +00:00
|
|
|
c.AbortWithStatus(409)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
build.Status = common.StatePending
|
|
|
|
build.Started = 0
|
|
|
|
build.Finished = 0
|
|
|
|
for _, job := range build.Jobs {
|
2015-06-19 00:36:52 +00:00
|
|
|
job.Status = common.StatePending
|
|
|
|
job.Started = 0
|
|
|
|
job.Finished = 0
|
|
|
|
job.ExitCode = 0
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
err = store.SetBuild(build)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-02 03:42:18 +00:00
|
|
|
netrc, err := remote.Netrc(user, repo)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// featch the .drone.yml file from the database
|
2015-09-07 19:13:27 +00:00
|
|
|
raw, sec, err := remote.Script(user, repo, build)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-10 17:39:18 +00:00
|
|
|
// get the previous build so taht we can send
|
|
|
|
// on status change notifications
|
|
|
|
last, _ := store.BuildLast(repo, build.Commit.Branch)
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
c.JSON(202, build)
|
2015-05-11 07:45:31 +00:00
|
|
|
|
|
|
|
queue_.Publish(&queue.Work{
|
2015-09-10 17:39:18 +00:00
|
|
|
User: user,
|
|
|
|
Repo: repo,
|
|
|
|
Build: build,
|
|
|
|
BuildPrev: last,
|
|
|
|
Keys: repo.Keys,
|
|
|
|
Netrc: netrc,
|
|
|
|
Config: raw,
|
|
|
|
Secret: sec,
|
2015-09-02 23:14:25 +00:00
|
|
|
System: &common.System{
|
|
|
|
Link: httputil.GetURL(c.Request),
|
2015-09-09 01:55:03 +00:00
|
|
|
Plugins: strings.Split(os.Getenv("PLUGIN_FILTER"), " "),
|
|
|
|
Globals: strings.Split(os.Getenv("PLUGIN_PARAMS"), " "),
|
2015-09-02 23:14:25 +00:00
|
|
|
},
|
2015-05-11 07:45:31 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// KillBuild accepts a request to kill a running build.
|
|
|
|
//
|
|
|
|
// DELETE /api/builds/:owner/:name/builds/:number
|
|
|
|
//
|
|
|
|
func KillBuild(c *gin.Context) {
|
|
|
|
runner := ToRunner(c)
|
|
|
|
queue := ToQueue(c)
|
|
|
|
store := ToDatastore(c)
|
|
|
|
repo := ToRepo(c)
|
|
|
|
num, err := strconv.Atoi(c.Params.ByName("number"))
|
|
|
|
if err != nil {
|
|
|
|
c.Fail(400, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build, err := store.BuildNumber(repo, num)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
build.Jobs, err = store.JobList(build)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// must not restart a running build
|
2015-06-23 03:45:08 +00:00
|
|
|
if build.Status != common.StatePending && build.Status != common.StateRunning {
|
2015-05-11 07:45:31 +00:00
|
|
|
c.Fail(409, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove from the queue if exists
|
|
|
|
//
|
|
|
|
// TODO(bradrydzewski) this could yield a race condition
|
|
|
|
// because other threads may also be accessing these items.
|
|
|
|
for _, item := range queue.Items() {
|
2015-06-23 03:45:08 +00:00
|
|
|
if item.Repo.FullName == repo.FullName && item.Build.Number == build.Number {
|
2015-05-11 07:45:31 +00:00
|
|
|
queue.Remove(item)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
build.Status = common.StateKilled
|
|
|
|
build.Finished = time.Now().Unix()
|
|
|
|
if build.Started == 0 {
|
|
|
|
build.Started = build.Finished
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
for _, job := range build.Jobs {
|
2015-06-19 00:36:52 +00:00
|
|
|
if job.Status != common.StatePending && job.Status != common.StateRunning {
|
2015-05-11 07:45:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-06-19 00:36:52 +00:00
|
|
|
job.Status = common.StateKilled
|
2015-06-23 03:45:08 +00:00
|
|
|
job.Started = build.Started
|
|
|
|
job.Finished = build.Finished
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
err = store.SetBuild(build)
|
2015-05-11 07:45:31 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fail(500, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
for _, job := range build.Jobs {
|
2015-06-19 00:36:52 +00:00
|
|
|
runner.Cancel(job)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
// // get the agent from the repository so we can
|
|
|
|
// // notify the agent to kill the build.
|
|
|
|
// agent, err := store.BuildAgent(repo.FullName, build.Number)
|
|
|
|
// if err != nil {
|
|
|
|
// c.JSON(200, build)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// url_, _ := url.Parse("http://" + agent.Addr)
|
|
|
|
// url_.Path = fmt.Sprintf("/cancel/%s/%v", repo.FullName, build.Number)
|
|
|
|
// resp, err := http.Post(url_.String(), "application/json", nil)
|
|
|
|
// if err != nil {
|
|
|
|
// c.Fail(500, err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// defer resp.Body.Close()
|
|
|
|
|
2015-06-23 03:45:08 +00:00
|
|
|
c.JSON(200, build)
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|