woodpecker/engine/worker.go

116 lines
2.9 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package engine
import (
2016-01-22 20:01:23 +00:00
"fmt"
"io"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/shared/docker"
"github.com/samalba/dockerclient"
)
var (
// name of the build agent container.
DefaultAgent = "drone/drone-exec:latest"
// default name of the build agent executable
2015-09-02 23:52:59 +00:00
DefaultEntrypoint = []string{"/bin/drone-exec"}
// default argument to invoke build steps
2015-09-30 19:37:32 +00:00
DefaultBuildArgs = []string{"--pull", "--cache", "--clone", "--build", "--deploy"}
2015-05-16 02:35:33 +00:00
// default argument to invoke build steps
2015-09-30 19:37:32 +00:00
DefaultPullRequestArgs = []string{"--pull", "--cache", "--clone", "--build"}
2015-05-16 02:35:33 +00:00
// default arguments to invoke notify steps
2015-09-30 06:10:58 +00:00
DefaultNotifyArgs = []string{"--pull", "--notify"}
)
type worker struct {
2015-08-22 05:26:46 +00:00
client dockerclient.Client
build *dockerclient.ContainerInfo
notify *dockerclient.ContainerInfo
}
func newWorker(client dockerclient.Client) *worker {
2015-08-22 05:26:46 +00:00
return &worker{client: client}
}
// Build executes the clone, build and deploy steps.
2015-05-16 02:35:33 +00:00
func (w *worker) Build(name string, stdin []byte, pr bool) (_ int, err error) {
// the command line arguments passed into the
// build agent container.
args := DefaultBuildArgs
2015-05-16 02:35:33 +00:00
if pr {
args = DefaultPullRequestArgs
}
args = append(args, "--")
args = append(args, string(stdin))
conf := &dockerclient.ContainerConfig{
Image: DefaultAgent,
Entrypoint: DefaultEntrypoint,
Cmd: args,
HostConfig: dockerclient.HostConfig{
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
},
Volumes: map[string]struct{}{
2016-03-27 00:24:09 +00:00
"/var/run/docker.sock": {},
},
}
// TEMPORARY: always try to pull the new image for now
// since we'll be frequently updating the build image
// for the next few weeks
2015-09-30 06:10:58 +00:00
w.client.PullImage(conf.Image, nil)
2015-09-30 01:21:17 +00:00
w.build, err = docker.Run(w.client, conf, name)
if err != nil {
2015-05-06 07:56:06 +00:00
return 1, err
}
2016-01-22 20:01:23 +00:00
if w.build.State.OOMKilled {
return 1, fmt.Errorf("OOMKill received")
2016-01-22 20:01:23 +00:00
}
return w.build.State.ExitCode, err
}
// Notify executes the notification steps.
func (w *worker) Notify(stdin []byte) error {
args := DefaultNotifyArgs
args = append(args, "--")
args = append(args, string(stdin))
conf := &dockerclient.ContainerConfig{
Image: DefaultAgent,
Entrypoint: DefaultEntrypoint,
Cmd: args,
2015-09-30 01:21:17 +00:00
HostConfig: dockerclient.HostConfig{},
}
var err error
2015-09-30 01:21:17 +00:00
w.notify, err = docker.Run(w.client, conf, "")
return err
}
// Logs returns a multi-reader that fetches the logs
// from the build and deploy agents.
func (w *worker) Logs() (io.ReadCloser, error) {
if w.build == nil {
2015-09-30 01:21:17 +00:00
return nil, errLogging
}
return w.client.ContainerLogs(w.build.Id, logOpts)
}
// Remove stops and removes the build, deploy and
// notification agents created for the build task.
func (w *worker) Remove() {
if w.notify != nil {
w.client.KillContainer(w.notify.Id, "9")
w.client.RemoveContainer(w.notify.Id, true, true)
}
if w.build != nil {
w.client.KillContainer(w.build.Id, "9")
w.client.RemoveContainer(w.build.Id, true, true)
}
}