2015-05-05 08:04:20 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
2015-05-05 10:25:08 +00:00
|
|
|
"bytes"
|
2015-05-05 08:04:20 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-05-05 10:25:08 +00:00
|
|
|
"io/ioutil"
|
2015-05-05 08:04:20 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
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-05-05 08:04:20 +00:00
|
|
|
"github.com/samalba/dockerclient"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Defult docker host address
|
|
|
|
DefaultHost = "unix:///var/run/docker.sock"
|
|
|
|
|
|
|
|
// Docker host address from environment variable
|
|
|
|
DockerHost = os.Getenv("DOCKER_HOST")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// if the environment doesn't specify a DOCKER_HOST
|
|
|
|
// we should use the default Docker socket.
|
|
|
|
if len(DockerHost) == 0 {
|
|
|
|
DockerHost = DefaultHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Runner struct {
|
|
|
|
Updater
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) Run(w *queue.Work) error {
|
|
|
|
var workers []*worker
|
|
|
|
var client dockerclient.Client
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
|
|
|
|
// ensures that all containers have been removed
|
|
|
|
// from the host machine.
|
|
|
|
for _, worker := range workers {
|
|
|
|
worker.Remove()
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// if any part of the commit fails and leaves
|
2015-05-05 08:04:20 +00:00
|
|
|
// behind orphan sub-builds we need to cleanup
|
|
|
|
// after ourselves.
|
2015-05-11 07:45:31 +00:00
|
|
|
if w.Commit.State == common.StateRunning {
|
2015-05-05 08:04:20 +00:00
|
|
|
// if any tasks are running or pending
|
|
|
|
// we should mark them as complete.
|
2015-05-11 07:45:31 +00:00
|
|
|
for _, b := range w.Commit.Builds {
|
|
|
|
if b.State == common.StateRunning {
|
|
|
|
b.State = common.StateError
|
|
|
|
b.Finished = time.Now().UTC().Unix()
|
|
|
|
b.Duration = b.Finished - b.Started
|
2015-05-17 00:46:29 +00:00
|
|
|
b.ExitCode = 255
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
if b.State == common.StatePending {
|
|
|
|
b.State = common.StateError
|
|
|
|
b.Started = time.Now().UTC().Unix()
|
|
|
|
b.Finished = time.Now().UTC().Unix()
|
|
|
|
b.Duration = 0
|
2015-05-17 00:46:29 +00:00
|
|
|
b.ExitCode = 255
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
r.SetBuild(w.Repo, w.Commit, b)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-05-05 10:25:08 +00:00
|
|
|
// must populate build start
|
2015-05-11 07:45:31 +00:00
|
|
|
if w.Commit.Started == 0 {
|
|
|
|
w.Commit.Started = time.Now().UTC().Unix()
|
2015-05-05 10:25:08 +00:00
|
|
|
}
|
2015-05-05 08:04:20 +00:00
|
|
|
// mark the build as complete (with error)
|
2015-05-11 07:45:31 +00:00
|
|
|
w.Commit.State = common.StateError
|
|
|
|
w.Commit.Finished = time.Now().UTC().Unix()
|
|
|
|
r.SetCommit(w.User, w.Repo, w.Commit)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// marks the build as running
|
2015-05-11 07:45:31 +00:00
|
|
|
w.Commit.Started = time.Now().UTC().Unix()
|
|
|
|
w.Commit.State = common.StateRunning
|
|
|
|
err := r.SetCommit(w.User, w.Repo, w.Commit)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the Docker client. In this version of Drone (alpha)
|
|
|
|
// we do not spread builds across clients, but this can and
|
|
|
|
// (probably) will change in the future.
|
|
|
|
client, err = dockerclient.NewDockerClient(DockerHost, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through and execute the build and
|
|
|
|
// clone steps for each build task.
|
2015-05-11 07:45:31 +00:00
|
|
|
for _, task := range w.Commit.Builds {
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
// marks the task as running
|
|
|
|
task.State = common.StateRunning
|
|
|
|
task.Started = time.Now().UTC().Unix()
|
2015-05-11 07:45:31 +00:00
|
|
|
err = r.SetBuild(w.Repo, w.Commit, task)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
work := &work{
|
2015-05-11 07:45:31 +00:00
|
|
|
Repo: w.Repo,
|
|
|
|
Commit: w.Commit,
|
|
|
|
Keys: w.Keys,
|
|
|
|
Netrc: w.Netrc,
|
|
|
|
Yaml: w.Yaml,
|
|
|
|
Build: task,
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
in, err := json.Marshal(work)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-16 00:19:15 +00:00
|
|
|
|
|
|
|
worker := newWorkerTimeout(client, w.Repo.Timeout)
|
2015-05-05 08:04:20 +00:00
|
|
|
workers = append(workers, worker)
|
2015-05-11 07:45:31 +00:00
|
|
|
cname := cname(task)
|
2015-05-16 02:35:33 +00:00
|
|
|
pullrequest := (w.Commit.PullRequest != "")
|
|
|
|
state, builderr := worker.Build(cname, in, pullrequest)
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
switch {
|
2015-05-05 10:25:08 +00:00
|
|
|
case builderr == ErrTimeout:
|
2015-05-05 08:04:20 +00:00
|
|
|
task.State = common.StateKilled
|
2015-05-05 10:25:08 +00:00
|
|
|
case builderr != nil:
|
2015-05-05 08:04:20 +00:00
|
|
|
task.State = common.StateError
|
|
|
|
case state != 0:
|
|
|
|
task.ExitCode = state
|
|
|
|
task.State = common.StateFailure
|
|
|
|
default:
|
|
|
|
task.State = common.StateSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
// send the logs to the datastore
|
2015-05-06 07:56:06 +00:00
|
|
|
var buf bytes.Buffer
|
2015-05-05 08:04:20 +00:00
|
|
|
rc, err := worker.Logs()
|
2015-05-05 10:25:08 +00:00
|
|
|
if err != nil && builderr != nil {
|
2015-05-09 19:54:38 +00:00
|
|
|
buf.WriteString("001 Error launching build")
|
2015-05-05 10:25:08 +00:00
|
|
|
buf.WriteString(builderr.Error())
|
|
|
|
} else if err != nil {
|
2015-05-09 19:54:38 +00:00
|
|
|
buf.WriteString("002 Error launching build")
|
2015-05-06 07:56:06 +00:00
|
|
|
buf.WriteString(err.Error())
|
2015-05-05 08:04:20 +00:00
|
|
|
return err
|
2015-05-06 07:56:06 +00:00
|
|
|
} else {
|
|
|
|
defer rc.Close()
|
|
|
|
StdCopy(&buf, &buf, rc)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
err = r.SetLogs(w.Repo, w.Commit, task, ioutil.NopCloser(&buf))
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the task in the datastore
|
|
|
|
task.Finished = time.Now().UTC().Unix()
|
|
|
|
task.Duration = task.Finished - task.Started
|
2015-05-11 07:45:31 +00:00
|
|
|
err = r.SetBuild(w.Repo, w.Commit, task)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the build state if any of the sub-tasks
|
|
|
|
// had a non-success status
|
2015-05-11 07:45:31 +00:00
|
|
|
w.Commit.State = common.StateSuccess
|
|
|
|
for _, build := range w.Commit.Builds {
|
|
|
|
if build.State != common.StateSuccess {
|
|
|
|
w.Commit.State = build.State
|
2015-05-05 08:04:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
err = r.SetCommit(w.User, w.Repo, w.Commit)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through and execute the notifications and
|
|
|
|
// the destroy all containers afterward.
|
2015-05-11 07:45:31 +00:00
|
|
|
for i, build := range w.Commit.Builds {
|
2015-05-05 08:04:20 +00:00
|
|
|
work := &work{
|
2015-05-11 07:45:31 +00:00
|
|
|
Repo: w.Repo,
|
|
|
|
Commit: w.Commit,
|
|
|
|
Keys: w.Keys,
|
|
|
|
Netrc: w.Netrc,
|
|
|
|
Yaml: w.Yaml,
|
|
|
|
Build: build,
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
in, err := json.Marshal(work)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
workers[i].Notify(in)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func (r *Runner) Cancel(build *common.Build) error {
|
2015-05-05 08:04:20 +00:00
|
|
|
client, err := dockerclient.NewDockerClient(DockerHost, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-11 07:45:31 +00:00
|
|
|
return client.StopContainer(cname(build), 30)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func (r *Runner) Logs(build *common.Build) (io.ReadCloser, error) {
|
2015-05-05 08:04:20 +00:00
|
|
|
client, err := dockerclient.NewDockerClient(DockerHost, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-06 07:56:06 +00:00
|
|
|
// make sure this container actually exists
|
2015-05-11 07:45:31 +00:00
|
|
|
info, err := client.InspectContainer(cname(build))
|
2015-05-06 07:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// verify the container is running. if not we'll
|
|
|
|
// do an exponential backoff and attempt to wait
|
|
|
|
if !info.State.Running {
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
info, err = client.InspectContainer(info.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if info.State.Running {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == 5 {
|
|
|
|
return nil, dockerclient.ErrNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := client.ContainerLogs(info.Id, logOptsTail)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer rc.Close()
|
|
|
|
StdCopy(pw, pw, rc)
|
|
|
|
}()
|
|
|
|
return pr, nil
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
func cname(build *common.Build) string {
|
|
|
|
return fmt.Sprintf("drone-%d", build.ID)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) Poll(q queue.Queue) {
|
|
|
|
for {
|
|
|
|
w := q.Pull()
|
|
|
|
q.Ack(w)
|
|
|
|
err := r.Run(w)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|