woodpecker/pkg/runner/builtin/runner.go

309 lines
7.3 KiB
Go
Raw Normal View History

package builtin
import (
2015-05-05 10:25:08 +00:00
"bytes"
2015-08-19 01:28:35 +00:00
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
2015-05-05 10:25:08 +00:00
"io/ioutil"
"os"
"time"
2015-05-22 18:37:40 +00:00
"github.com/drone/drone/Godeps/_workspace/src/github.com/samalba/dockerclient"
2015-05-18 02:25:53 +00:00
"github.com/drone/drone/pkg/docker"
2015-05-17 18:42:56 +00:00
"github.com/drone/drone/pkg/queue"
2015-06-19 00:36:52 +00:00
"github.com/drone/drone/pkg/types"
2015-05-22 18:37:40 +00:00
log "github.com/drone/drone/Godeps/_workspace/src/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")
2015-07-13 19:26:36 +00:00
// Docker TLS variables
2015-08-06 16:22:31 +00:00
DockerHostCa = os.Getenv("DOCKER_CA")
DockerHostKey = os.Getenv("DOCKER_KEY")
2015-07-13 19:26:36 +00:00
DockerHostCert = os.Getenv("DOCKER_CERT")
)
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
2015-07-13 19:26:36 +00:00
var tlc *tls.Config
defer func() {
recover()
// ensures that all containers have been removed
// from the host machine.
for _, worker := range workers {
worker.Remove()
}
// if any part of the commit fails and leaves
// behind orphan sub-builds we need to cleanup
// after ourselves.
if w.Build.Status == types.StateRunning {
// if any tasks are running or pending
// we should mark them as complete.
for _, b := range w.Build.Jobs {
2015-06-19 00:36:52 +00:00
if b.Status == types.StateRunning {
b.Status = types.StateError
b.Finished = time.Now().UTC().Unix()
2015-05-17 00:46:29 +00:00
b.ExitCode = 255
}
2015-06-19 00:36:52 +00:00
if b.Status == types.StatePending {
b.Status = types.StateError
b.Started = time.Now().UTC().Unix()
b.Finished = time.Now().UTC().Unix()
2015-05-17 00:46:29 +00:00
b.ExitCode = 255
}
r.SetJob(w.Repo, w.Build, b)
}
2015-05-05 10:25:08 +00:00
// must populate build start
if w.Build.Started == 0 {
w.Build.Started = time.Now().UTC().Unix()
2015-05-05 10:25:08 +00:00
}
// mark the build as complete (with error)
w.Build.Status = types.StateError
w.Build.Finished = time.Now().UTC().Unix()
r.SetBuild(w.User, w.Repo, w.Build)
}
}()
// marks the build as running
w.Build.Started = time.Now().UTC().Unix()
w.Build.Status = types.StateRunning
err := r.SetBuild(w.User, w.Repo, w.Build)
if err != nil {
2015-07-13 19:26:36 +00:00
log.Errorf("failure to set build. %s", err)
return err
}
2015-07-13 19:26:36 +00:00
// create the Docket client TLS config
if len(DockerHostCert) > 0 && len(DockerHostKey) > 0 && len(DockerHostCa) > 0 {
cert, err := tls.LoadX509KeyPair(DockerHostCert, DockerHostKey)
if err != nil {
log.Errorf("failure to load SSL cert and key. %s", err)
}
caCert, err := ioutil.ReadFile(DockerHostCa)
if err != nil {
log.Errorf("failure to load SSL CA cert. %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlc = &tls.Config{
2015-08-06 16:22:31 +00:00
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
2015-07-13 19:26:36 +00:00
}
}
// 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.
2015-07-13 19:26:36 +00:00
client, err = dockerclient.NewDockerClient(DockerHost, tlc)
if err != nil {
2015-07-13 19:26:36 +00:00
log.Errorf("failure to connect to docker. %s", err)
return err
}
// loop through and execute the build and
2015-06-19 00:36:52 +00:00
// clone steps for each build job.
for _, job := range w.Build.Jobs {
// marks the task as running
2015-06-19 00:36:52 +00:00
job.Status = types.StateRunning
job.Started = time.Now().UTC().Unix()
err = r.SetJob(w.Repo, w.Build, job)
if err != nil {
2015-07-13 19:26:36 +00:00
log.Errorf("failure to set job. %s", err)
return err
}
work := &work{
System: w.System,
Workspace: &types.Workspace{Netrc: w.Netrc, Keys: w.Keys},
Repo: w.Repo,
Build: w.Build,
Job: job,
2015-09-07 19:13:27 +00:00
Secret: string(w.Secret),
Config: string(w.Config),
}
in, err := json.Marshal(work)
if err != nil {
2015-07-13 19:26:36 +00:00
log.Errorf("failure to marshalise work. %s", err)
return err
}
2015-05-16 00:19:15 +00:00
2015-08-22 05:26:46 +00:00
worker := newWorker(client)
workers = append(workers, worker)
2015-06-19 00:36:52 +00:00
cname := cname(job)
2015-09-04 06:56:52 +00:00
pullrequest := (w.Build.PullRequest != nil && w.Build.PullRequest.Number != 0)
2015-05-16 02:35:33 +00:00
state, builderr := worker.Build(cname, in, pullrequest)
switch {
2015-08-22 05:26:46 +00:00
case state == 128:
job.ExitCode = state
job.Status = types.StateKilled
case state == 130:
job.ExitCode = state
2015-06-19 00:36:52 +00:00
job.Status = types.StateKilled
2015-05-05 10:25:08 +00:00
case builderr != nil:
2015-06-19 00:36:52 +00:00
job.Status = types.StateError
case state != 0:
2015-06-19 00:36:52 +00:00
job.ExitCode = state
job.Status = types.StateFailure
default:
2015-06-19 00:36:52 +00:00
job.Status = types.StateSuccess
}
// send the logs to the datastore
2015-05-06 07:56:06 +00:00
var buf bytes.Buffer
rc, err := worker.Logs()
2015-05-05 10:25:08 +00:00
if err != nil && builderr != nil {
buf.WriteString("001 Error launching build")
2015-05-05 10:25:08 +00:00
buf.WriteString(builderr.Error())
} else if err != nil {
buf.WriteString("002 Error launching build")
2015-05-06 07:56:06 +00:00
buf.WriteString(err.Error())
return err
2015-05-06 07:56:06 +00:00
} else {
defer rc.Close()
2015-05-18 02:25:53 +00:00
docker.StdCopy(&buf, &buf, rc)
}
err = r.SetLogs(w.Repo, w.Build, job, ioutil.NopCloser(&buf))
if err != nil {
return err
}
// update the task in the datastore
2015-06-19 00:36:52 +00:00
job.Finished = time.Now().UTC().Unix()
err = r.SetJob(w.Repo, w.Build, job)
if err != nil {
return err
}
}
// update the build state if any of the sub-tasks
// had a non-success status
w.Build.Status = types.StateSuccess
for _, job := range w.Build.Jobs {
2015-06-19 00:36:52 +00:00
if job.Status != types.StateSuccess {
w.Build.Status = job.Status
break
}
}
err = r.SetBuild(w.User, w.Repo, w.Build)
if err != nil {
return err
}
// loop through and execute the notifications and
// the destroy all containers afterward.
for i, job := range w.Build.Jobs {
work := &work{
System: w.System,
Workspace: &types.Workspace{Netrc: w.Netrc, Keys: w.Keys},
Repo: w.Repo,
Build: w.Build,
Job: job,
2015-09-07 19:13:27 +00:00
Secret: string(w.Secret),
Config: string(w.Config),
}
in, err := json.Marshal(work)
if err != nil {
return err
}
workers[i].Notify(in)
break
}
return nil
}
2015-06-19 00:36:52 +00:00
func (r *Runner) Cancel(job *types.Job) error {
client, err := dockerclient.NewDockerClient(DockerHost, nil)
if err != nil {
return err
}
2015-06-19 00:36:52 +00:00
return client.StopContainer(cname(job), 30)
}
2015-06-19 00:36:52 +00:00
func (r *Runner) Logs(job *types.Job) (io.ReadCloser, error) {
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-06-19 00:36:52 +00:00
info, err := client.InspectContainer(cname(job))
2015-05-06 07:56:06 +00:00
if err != nil {
2015-09-07 22:33:55 +00:00
2015-08-24 19:29:08 +00:00
// add a small exponential backoff since there
// is a small window when the container hasn't
// been created yet, but the build is about to start
for i := 0; ; i++ {
time.Sleep(1 * time.Second)
info, err = client.InspectContainer(cname(job))
if err != nil && i == 5 {
return nil, err
}
2015-09-07 22:33:55 +00:00
if err == nil {
break
}
2015-08-24 19:29:08 +00:00
}
2015-05-06 07:56:06 +00:00
}
2015-05-06 07:56:06 +00:00
// 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
}
}
}
return client.ContainerLogs(info.Id, logOptsTail)
}
2015-06-19 00:36:52 +00:00
func cname(job *types.Job) string {
return fmt.Sprintf("drone-%d", job.ID)
}
func (r *Runner) Poll(q queue.Queue) {
for {
w := q.Pull()
q.Ack(w)
err := r.Run(w)
if err != nil {
log.Error(err)
}
}
}