2014-09-29 01:36:24 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2015-01-29 04:39:09 +00:00
|
|
|
"fmt"
|
2014-09-29 01:36:24 +00:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime/debug"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
|
|
|
"code.google.com/p/go.net/context"
|
|
|
|
"github.com/drone/drone/plugin/notify"
|
|
|
|
"github.com/drone/drone/server/blobstore"
|
|
|
|
"github.com/drone/drone/server/datastore"
|
2014-09-30 04:34:30 +00:00
|
|
|
"github.com/drone/drone/server/pubsub"
|
2014-09-29 01:36:24 +00:00
|
|
|
"github.com/drone/drone/server/worker"
|
|
|
|
"github.com/drone/drone/shared/build"
|
|
|
|
"github.com/drone/drone/shared/build/docker"
|
|
|
|
"github.com/drone/drone/shared/build/git"
|
|
|
|
"github.com/drone/drone/shared/build/repo"
|
|
|
|
"github.com/drone/drone/shared/build/script"
|
|
|
|
"github.com/drone/drone/shared/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const dockerKind = "docker"
|
|
|
|
|
|
|
|
type Docker struct {
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
Kind string `json:"type"`
|
|
|
|
Created int64 `json:"created"`
|
|
|
|
|
|
|
|
docker *docker.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *Docker {
|
|
|
|
return &Docker{
|
|
|
|
UUID: uuid.New(),
|
|
|
|
Kind: dockerKind,
|
|
|
|
Created: time.Now().UTC().Unix(),
|
|
|
|
docker: docker.New(),
|
2014-10-01 04:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHost(host string) *Docker {
|
|
|
|
return &Docker{
|
|
|
|
UUID: uuid.New(),
|
|
|
|
Kind: dockerKind,
|
|
|
|
Created: time.Now().UTC().Unix(),
|
|
|
|
docker: docker.NewHost(host),
|
2014-09-29 01:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 14:57:39 +00:00
|
|
|
func NewHostCertFile(host, cert, key string) *Docker {
|
|
|
|
docker_node, err := docker.NewHostCertFile(host, cert, key)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Docker{
|
|
|
|
UUID: uuid.New(),
|
|
|
|
Kind: dockerKind,
|
|
|
|
Created: time.Now().UTC().Unix(),
|
|
|
|
docker: docker_node,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
func (d *Docker) Do(c context.Context, r *worker.Work) {
|
|
|
|
|
|
|
|
// ensure that we can recover from any panics to
|
|
|
|
// avoid bringing down the entire application.
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
log.Printf("%s: %s", e, debug.Stack())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// mark the build as Started and update the database
|
|
|
|
r.Commit.Status = model.StatusStarted
|
|
|
|
r.Commit.Started = time.Now().UTC().Unix()
|
2014-11-30 05:39:13 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
datastore.PutCommit(c, r.Commit)
|
|
|
|
|
|
|
|
// notify all listeners that the build is started
|
2014-09-30 04:34:30 +00:00
|
|
|
commitc := pubsub.Register(c, "_global")
|
|
|
|
commitc.Publish(r)
|
|
|
|
stdoutc := pubsub.RegisterOpts(c, r.Commit.ID, pubsub.ConsoleOpts)
|
2014-10-24 21:25:56 +00:00
|
|
|
defer pubsub.Unregister(c, r.Commit.ID)
|
2014-09-29 01:36:24 +00:00
|
|
|
|
|
|
|
// create a special buffer that will also
|
|
|
|
// write to a websocket channel
|
2014-09-30 04:34:30 +00:00
|
|
|
buf := pubsub.NewBuffer(stdoutc)
|
2014-09-29 01:36:24 +00:00
|
|
|
|
|
|
|
// parse the parameters and build script. The script has already
|
|
|
|
// been parsed in the hook, so we can be confident it will succeed.
|
|
|
|
// that being said, we should clean this up
|
|
|
|
params, err := r.Repo.ParamMap()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing PARAMS for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
|
|
|
|
}
|
2014-10-12 20:02:53 +00:00
|
|
|
script, err := script.ParseBuild(script.Inject(r.Commit.Config, params))
|
2014-09-29 01:36:24 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing YAML for %s/%s, Err: %s", r.Repo.Owner, r.Repo.Name, err.Error())
|
|
|
|
}
|
|
|
|
|
2014-11-30 05:39:13 +00:00
|
|
|
// TODO: handle error better?
|
|
|
|
buildNumber, err := datastore.GetBuildNumber(c, r.Commit)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Unable to fetch build number, Err: %s", err.Error())
|
|
|
|
}
|
2015-01-29 04:39:09 +00:00
|
|
|
script.Env = append(script.Env, fmt.Sprintf("DRONE_BUILD_NUMBER=%d", buildNumber))
|
|
|
|
script.Env = append(script.Env, fmt.Sprintf("CI_BUILD_NUMBER=%d", buildNumber))
|
2014-11-30 05:39:13 +00:00
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
path := r.Repo.Host + "/" + r.Repo.Owner + "/" + r.Repo.Name
|
|
|
|
repo := &repo.Repo{
|
2015-07-16 14:42:34 +00:00
|
|
|
Name: path,
|
|
|
|
Path: r.Repo.CloneURL,
|
|
|
|
Branch: r.Commit.Branch,
|
|
|
|
Commit: r.Commit.Sha,
|
|
|
|
PR: r.Commit.PullRequest,
|
|
|
|
Private: r.Repo.Private,
|
2015-07-18 23:19:32 +00:00
|
|
|
Dir: filepath.ToSlash(filepath.Join("/var/cache/drone/src", git.GitPath(script.Git, path))),
|
2015-07-16 14:42:34 +00:00
|
|
|
Depth: git.GitDepth(script.Git),
|
|
|
|
}
|
|
|
|
|
|
|
|
// append private parameters to the environment
|
|
|
|
// variable section of the .drone.yml file, if
|
|
|
|
// this is trusted
|
|
|
|
if params != nil && repo.IsTrusted() {
|
|
|
|
for k, v := range params {
|
|
|
|
script.Env = append(script.Env, k+"="+v)
|
|
|
|
}
|
2014-09-29 01:36:24 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 18:25:14 +00:00
|
|
|
priorCommit, _ := datastore.GetCommitPrior(c, r.Commit)
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// send all "started" notifications
|
|
|
|
if script.Notifications == nil {
|
|
|
|
script.Notifications = ¬ify.Notification{}
|
|
|
|
}
|
2014-09-30 04:34:30 +00:00
|
|
|
script.Notifications.Send(&model.Request{
|
|
|
|
User: r.User,
|
|
|
|
Repo: r.Repo,
|
|
|
|
Commit: r.Commit,
|
|
|
|
Host: r.Host,
|
2014-12-30 18:25:14 +00:00
|
|
|
Prior: priorCommit,
|
2014-09-30 04:34:30 +00:00
|
|
|
})
|
2014-09-29 01:36:24 +00:00
|
|
|
|
|
|
|
// create an instance of the Docker builder
|
|
|
|
builder := build.New(d.docker)
|
|
|
|
builder.Build = script
|
|
|
|
builder.Repo = repo
|
2014-09-30 04:34:30 +00:00
|
|
|
builder.Stdout = buf
|
2014-09-29 01:36:24 +00:00
|
|
|
builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second
|
|
|
|
builder.Privileged = r.Repo.Privileged
|
|
|
|
|
2015-07-16 14:42:34 +00:00
|
|
|
if repo.IsTrusted() {
|
2014-12-30 18:35:44 +00:00
|
|
|
builder.Key = []byte(r.Repo.PrivateKey)
|
|
|
|
}
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// run the build
|
|
|
|
err = builder.Run()
|
|
|
|
|
|
|
|
// update the build status based on the results
|
|
|
|
// from the build runner.
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
r.Commit.Status = model.StatusError
|
|
|
|
log.Printf("Error building %s, Err: %s", r.Commit.Sha, err)
|
|
|
|
buf.WriteString(err.Error())
|
|
|
|
case builder.BuildState == nil:
|
|
|
|
r.Commit.Status = model.StatusFailure
|
|
|
|
case builder.BuildState.ExitCode != 0:
|
|
|
|
r.Commit.Status = model.StatusFailure
|
|
|
|
default:
|
|
|
|
r.Commit.Status = model.StatusSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
// calcualte the build finished and duration details and
|
|
|
|
// update the commit
|
|
|
|
r.Commit.Finished = time.Now().UTC().Unix()
|
|
|
|
r.Commit.Duration = (r.Commit.Finished - r.Commit.Started)
|
|
|
|
datastore.PutCommit(c, r.Commit)
|
|
|
|
blobstore.Put(c, filepath.Join(r.Repo.Host, r.Repo.Owner, r.Repo.Name, r.Commit.Branch, r.Commit.Sha), buf.Bytes())
|
|
|
|
|
|
|
|
// notify all listeners that the build is finished
|
2014-09-30 04:34:30 +00:00
|
|
|
commitc.Publish(r)
|
2014-09-29 01:36:24 +00:00
|
|
|
|
2014-12-30 18:25:14 +00:00
|
|
|
priorCommit, _ = datastore.GetCommitPrior(c, r.Commit)
|
|
|
|
|
2014-09-29 01:36:24 +00:00
|
|
|
// send all "finished" notifications
|
2014-09-30 04:34:30 +00:00
|
|
|
script.Notifications.Send(&model.Request{
|
|
|
|
User: r.User,
|
|
|
|
Repo: r.Repo,
|
|
|
|
Commit: r.Commit,
|
|
|
|
Host: r.Host,
|
2014-12-30 18:25:14 +00:00
|
|
|
Prior: priorCommit,
|
2014-09-30 04:34:30 +00:00
|
|
|
})
|
2014-09-29 01:36:24 +00:00
|
|
|
}
|