2015-05-05 08:04:20 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
2015-05-05 10:25:08 +00:00
|
|
|
"bytes"
|
2015-08-19 01:28:35 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
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-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-05 08:04:20 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
log "github.com/drone/drone/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
2015-05-05 08:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
2015-05-05 08:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
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-06-23 03:45:08 +00:00
|
|
|
if w.Build.Status == types.StateRunning {
|
2015-05-05 08:04:20 +00:00
|
|
|
// if any tasks are running or pending
|
|
|
|
// we should mark them as complete.
|
2015-06-23 03:45:08 +00:00
|
|
|
for _, b := range w.Build.Jobs {
|
2015-06-19 00:36:52 +00:00
|
|
|
if b.Status == types.StateRunning {
|
|
|
|
b.Status = types.StateError
|
2015-05-11 07:45:31 +00:00
|
|
|
b.Finished = time.Now().UTC().Unix()
|
2015-05-17 00:46:29 +00:00
|
|
|
b.ExitCode = 255
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-06-19 00:36:52 +00:00
|
|
|
if b.Status == types.StatePending {
|
|
|
|
b.Status = types.StateError
|
2015-05-11 07:45:31 +00:00
|
|
|
b.Started = time.Now().UTC().Unix()
|
|
|
|
b.Finished = time.Now().UTC().Unix()
|
2015-05-17 00:46:29 +00:00
|
|
|
b.ExitCode = 255
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
r.SetJob(w.Repo, w.Build, b)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-05-05 10:25:08 +00:00
|
|
|
// must populate build start
|
2015-06-23 03:45:08 +00:00
|
|
|
if w.Build.Started == 0 {
|
|
|
|
w.Build.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-06-23 03:45:08 +00:00
|
|
|
w.Build.Status = types.StateError
|
|
|
|
w.Build.Finished = time.Now().UTC().Unix()
|
|
|
|
r.SetBuild(w.User, w.Repo, w.Build)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// marks the build as running
|
2015-06-23 03:45:08 +00:00
|
|
|
w.Build.Started = time.Now().UTC().Unix()
|
|
|
|
w.Build.Status = types.StateRunning
|
|
|
|
err := r.SetBuild(w.User, w.Repo, w.Build)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
2015-07-13 19:26:36 +00:00
|
|
|
log.Errorf("failure to set build. %s", err)
|
2015-05-05 08:04:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:04:20 +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)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
2015-07-13 19:26:36 +00:00
|
|
|
log.Errorf("failure to connect to docker. %s", err)
|
2015-05-05 08:04:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through and execute the build and
|
2015-06-19 00:36:52 +00:00
|
|
|
// clone steps for each build job.
|
2015-06-23 03:45:08 +00:00
|
|
|
for _, job := range w.Build.Jobs {
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
// marks the task as running
|
2015-06-19 00:36:52 +00:00
|
|
|
job.Status = types.StateRunning
|
|
|
|
job.Started = time.Now().UTC().Unix()
|
2015-06-23 03:45:08 +00:00
|
|
|
err = r.SetJob(w.Repo, w.Build, job)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
2015-07-13 19:26:36 +00:00
|
|
|
log.Errorf("failure to set job. %s", err)
|
2015-05-05 08:04:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
work := &work{
|
2015-05-20 06:19:59 +00:00
|
|
|
Repo: w.Repo,
|
2015-06-23 03:45:08 +00:00
|
|
|
Build: w.Build,
|
2015-05-20 06:19:59 +00:00
|
|
|
Keys: w.Keys,
|
|
|
|
Netrc: w.Netrc,
|
|
|
|
Yaml: w.Yaml,
|
2015-06-19 00:36:52 +00:00
|
|
|
Job: job,
|
2015-05-20 06:19:59 +00:00
|
|
|
Env: w.Env,
|
|
|
|
Plugins: w.Plugins,
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
in, err := json.Marshal(work)
|
|
|
|
if err != nil {
|
2015-07-13 19:26:36 +00:00
|
|
|
log.Errorf("failure to marshalise work. %s", err)
|
2015-05-05 08:04:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-16 00:19:15 +00:00
|
|
|
|
2015-08-22 05:26:46 +00:00
|
|
|
worker := newWorker(client)
|
2015-05-05 08:04:20 +00:00
|
|
|
workers = append(workers, worker)
|
2015-06-19 00:36:52 +00:00
|
|
|
cname := cname(job)
|
2015-06-23 03:45:08 +00:00
|
|
|
pullrequest := (w.Build.PullRequest != nil)
|
2015-05-16 02:35:33 +00:00
|
|
|
state, builderr := worker.Build(cname, in, pullrequest)
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
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
|
2015-05-05 08:04:20 +00:00
|
|
|
case state != 0:
|
2015-06-19 00:36:52 +00:00
|
|
|
job.ExitCode = state
|
|
|
|
job.Status = types.StateFailure
|
2015-05-05 08:04:20 +00:00
|
|
|
default:
|
2015-06-19 00:36:52 +00:00
|
|
|
job.Status = types.StateSuccess
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2015-05-18 02:25:53 +00:00
|
|
|
docker.StdCopy(&buf, &buf, rc)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
err = r.SetLogs(w.Repo, w.Build, job, ioutil.NopCloser(&buf))
|
2015-05-05 08:04:20 +00:00
|
|
|
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()
|
2015-06-23 03:45:08 +00:00
|
|
|
err = r.SetJob(w.Repo, w.Build, job)
|
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-06-23 03:45:08 +00:00
|
|
|
w.Build.Status = types.StateSuccess
|
|
|
|
for _, job := range w.Build.Jobs {
|
2015-06-19 00:36:52 +00:00
|
|
|
if job.Status != types.StateSuccess {
|
2015-06-23 03:45:08 +00:00
|
|
|
w.Build.Status = job.Status
|
2015-05-05 08:04:20 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2015-06-23 03:45:08 +00:00
|
|
|
err = r.SetBuild(w.User, w.Repo, w.Build)
|
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-06-23 03:45:08 +00:00
|
|
|
for i, job := range w.Build.Jobs {
|
2015-05-05 08:04:20 +00:00
|
|
|
work := &work{
|
2015-06-08 00:26:25 +00:00
|
|
|
Repo: w.Repo,
|
2015-06-23 03:45:08 +00:00
|
|
|
Build: w.Build,
|
2015-06-08 00:26:25 +00:00
|
|
|
Keys: w.Keys,
|
|
|
|
Netrc: w.Netrc,
|
|
|
|
Yaml: w.Yaml,
|
2015-06-19 00:36:52 +00:00
|
|
|
Job: job,
|
2015-06-08 00:26:25 +00:00
|
|
|
Env: w.Env,
|
|
|
|
Plugins: w.Plugins,
|
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-06-19 00:36:52 +00:00
|
|
|
func (r *Runner) Cancel(job *types.Job) error {
|
2015-05-05 08:04:20 +00:00
|
|
|
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-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
func (r *Runner) Logs(job *types.Job) (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-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-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
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2015-05-06 07:56:06 +00:00
|
|
|
}
|
2015-05-18 06:07:10 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 06:07:10 +00:00
|
|
|
return client.ContainerLogs(info.Id, logOptsTail)
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 00:36:52 +00:00
|
|
|
func cname(job *types.Job) string {
|
|
|
|
return fmt.Sprintf("drone-%d", job.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|