2015-09-30 01:21:17 +00:00
|
|
|
package engine
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/shared/docker"
|
|
|
|
"github.com/samalba/dockerclient"
|
2015-05-05 08:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// name of the build agent container.
|
2015-09-02 23:14:25 +00:00
|
|
|
DefaultAgent = "drone/drone-exec:latest"
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
// default name of the build agent executable
|
2015-09-02 23:52:59 +00:00
|
|
|
DefaultEntrypoint = []string{"/bin/drone-exec"}
|
2015-05-05 08:04:20 +00:00
|
|
|
|
|
|
|
// default argument to invoke build steps
|
2015-09-30 06:10:58 +00:00
|
|
|
DefaultBuildArgs = []string{"--pull", "--cache", "--debug", "--clone", "--build", "--deploy"}
|
2015-05-05 08:04:20 +00:00
|
|
|
|
2015-05-16 02:35:33 +00:00
|
|
|
// default argument to invoke build steps
|
2015-09-30 01:21:17 +00:00
|
|
|
DefaultPullRequestArgs = []string{"--cache", "--clone", "--build"}
|
2015-05-16 02:35:33 +00:00
|
|
|
|
2015-05-05 08:04:20 +00:00
|
|
|
// default arguments to invoke notify steps
|
2015-09-30 06:10:58 +00:00
|
|
|
DefaultNotifyArgs = []string{"--pull", "--notify"}
|
2015-05-05 08:04:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type worker struct {
|
2015-08-22 05:26:46 +00:00
|
|
|
client dockerclient.Client
|
|
|
|
build *dockerclient.ContainerInfo
|
|
|
|
notify *dockerclient.ContainerInfo
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWorker(client dockerclient.Client) *worker {
|
2015-08-22 05:26:46 +00:00
|
|
|
return &worker{client: client}
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2015-05-05 08:04:20 +00:00
|
|
|
// the command line arguments passed into the
|
|
|
|
// build agent container.
|
|
|
|
args := DefaultBuildArgs
|
2015-05-16 02:35:33 +00:00
|
|
|
if pr {
|
|
|
|
args = DefaultPullRequestArgs
|
|
|
|
}
|
2015-05-05 08:04:20 +00:00
|
|
|
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{}{
|
|
|
|
"/var/run/docker.sock": struct{}{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-08-20 22:54:23 +00:00
|
|
|
// 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-08-20 22:54:23 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
w.build, err = docker.Run(w.client, conf, name)
|
2015-05-05 08:04:20 +00:00
|
|
|
if err != nil {
|
2015-05-06 07:56:06 +00:00
|
|
|
return 1, err
|
2015-05-05 08:04:20 +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{},
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2015-09-30 01:21:17 +00:00
|
|
|
w.notify, err = docker.Run(w.client, conf, "")
|
2015-05-05 08:04:20 +00:00
|
|
|
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
|
2015-05-05 08:04:20 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|