harness-drone/drone/agent/exec.go

276 lines
7.1 KiB
Go
Raw Normal View History

2016-04-20 01:37:53 +00:00
package agent
import (
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
2016-04-20 01:37:53 +00:00
"time"
"github.com/Sirupsen/logrus"
"github.com/dchest/uniuri"
"github.com/drone/drone/client"
"github.com/drone/drone/engine/compiler"
"github.com/drone/drone/engine/compiler/builtin"
"github.com/drone/drone/engine/runner"
2016-04-22 00:10:19 +00:00
"github.com/drone/drone/engine/runner/docker"
2016-04-20 01:37:53 +00:00
"github.com/drone/drone/model"
"github.com/drone/drone/queue"
"github.com/drone/drone/yaml/expander"
2016-04-20 01:37:53 +00:00
"github.com/samalba/dockerclient"
"golang.org/x/net/context"
)
2016-04-22 00:10:19 +00:00
type config struct {
platform string
namespace string
whitelist []string
privileged []string
netrc []string
pull bool
}
type pipeline struct {
drone client.Client
docker dockerclient.Client
config config
2016-04-20 01:37:53 +00:00
}
2016-04-22 00:10:19 +00:00
func (r *pipeline) run() error {
w, err := r.drone.Pull("linux", "amd64")
2016-04-20 01:37:53 +00:00
if err != nil {
return err
}
logrus.Infof("Starting build %s/%s#%d.%d",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
w.Job.Status = model.StatusRunning
w.Job.Started = time.Now().Unix()
prefix := fmt.Sprintf("drone_%s", uniuri.New())
envs := toEnv(w)
w.Yaml = expander.ExpandString(w.Yaml, envs)
2016-04-21 08:18:20 +00:00
// inject the netrc file into the clone plugin if the repositroy is
// private and requires authentication.
2016-04-22 00:10:19 +00:00
var secrets []*model.Secret
if w.Verified {
secrets = append(secrets, w.Secrets...)
}
2016-04-21 08:18:20 +00:00
if w.Repo.IsPrivate {
2016-04-22 00:10:19 +00:00
secrets = append(secrets, &model.Secret{
2016-04-21 08:18:20 +00:00
Name: "DRONE_NETRC_USERNAME",
Value: w.Netrc.Login,
Images: []string{"git", "hg"}, // TODO(bradrydzewski) use the command line parameters here
Events: []string{model.EventDeploy, model.EventPull, model.EventPush, model.EventTag},
})
2016-04-22 00:10:19 +00:00
secrets = append(secrets, &model.Secret{
2016-04-21 08:18:20 +00:00
Name: "DRONE_NETRC_PASSWORD",
Value: w.Netrc.Password,
2016-04-22 00:10:19 +00:00
Images: []string{"git", "hg"},
2016-04-21 08:18:20 +00:00
Events: []string{model.EventDeploy, model.EventPull, model.EventPush, model.EventTag},
})
2016-04-22 00:10:19 +00:00
secrets = append(secrets, &model.Secret{
2016-04-21 08:18:20 +00:00
Name: "DRONE_NETRC_MACHINE",
Value: w.Netrc.Machine,
Images: []string{"git", "hg"},
Events: []string{model.EventDeploy, model.EventPull, model.EventPush, model.EventTag},
})
}
2016-04-20 01:37:53 +00:00
trans := []compiler.Transform{
2016-04-22 00:10:19 +00:00
builtin.NewCloneOp("plugins/git:latest", true),
2016-04-20 01:37:53 +00:00
builtin.NewCacheOp(
"plugins/cache:latest",
"/var/lib/drone/cache/"+w.Repo.FullName,
false,
),
2016-04-22 00:10:19 +00:00
builtin.NewSecretOp(w.Build.Event, secrets),
builtin.NewNormalizeOp(r.config.namespace),
builtin.NewWorkspaceOp("/drone", "/drone/src/github.com/"+w.Repo.FullName),
2016-04-20 01:37:53 +00:00
builtin.NewValidateOp(
w.Repo.IsTrusted,
2016-04-22 00:10:19 +00:00
r.config.whitelist,
2016-04-20 01:37:53 +00:00
),
builtin.NewEnvOp(envs),
2016-04-20 01:37:53 +00:00
builtin.NewShellOp(builtin.Linux_adm64),
builtin.NewArgsOp(),
2016-04-22 00:10:19 +00:00
builtin.NewEscalateOp(r.config.privileged),
2016-04-20 01:37:53 +00:00
builtin.NewPodOp(prefix),
builtin.NewAliasOp(prefix),
2016-04-22 00:10:19 +00:00
builtin.NewPullOp(r.config.pull),
2016-04-20 01:37:53 +00:00
builtin.NewFilterOp(
model.StatusSuccess, // TODO(bradrydzewski) please add the last build status here
2016-04-20 01:37:53 +00:00
w.Build.Branch,
w.Build.Event,
w.Build.Deploy,
w.Job.Environment,
2016-04-20 01:37:53 +00:00
),
}
compile := compiler.New()
compile.Transforms(trans)
spec, err := compile.CompileString(w.Yaml)
if err != nil {
w.Job.Error = err.Error()
w.Job.ExitCode = 255
w.Job.Finished = w.Job.Started
w.Job.Status = model.StatusError
pushRetry(r.drone, w)
return nil
2016-04-20 01:37:53 +00:00
}
pushRetry(r.drone, w)
2016-04-20 01:37:53 +00:00
conf := runner.Config{
2016-04-22 00:10:19 +00:00
Engine: docker.New(r.docker),
2016-04-20 01:37:53 +00:00
}
c := context.TODO()
c, timout := context.WithTimeout(c, time.Minute*time.Duration(w.Repo.Timeout))
c, cancel := context.WithCancel(c)
defer cancel()
defer timout()
2016-04-20 01:37:53 +00:00
run := conf.Runner(c, spec)
2016-04-20 01:37:53 +00:00
run.Run()
2016-04-22 00:10:19 +00:00
wait := r.drone.Wait(w.Job.ID)
defer wait.Cancel()
2016-04-20 01:37:53 +00:00
go func() {
if _, err := wait.Done(); err == nil {
2016-04-20 01:37:53 +00:00
logrus.Infof("Cancel build %s/%s#%d.%d",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
cancel()
}
}()
rc, wc := io.Pipe()
go func() {
// TODO(bradrydzewski) figure out how to resume upload on failure
2016-04-22 00:10:19 +00:00
err := r.drone.Stream(w.Job.ID, rc)
2016-04-20 01:37:53 +00:00
if err != nil && err != io.ErrClosedPipe {
logrus.Errorf("Error streaming build logs. %s", err)
}
}()
pipe := run.Pipe()
for {
line := pipe.Next()
if line == nil {
break
}
linejson, _ := json.Marshal(line)
wc.Write(linejson)
wc.Write([]byte{'\n'})
}
err = run.Wait()
pipe.Close()
wc.Close()
rc.Close()
// catch the build result
if err != nil {
w.Job.ExitCode = 255
}
if exitErr, ok := err.(*runner.ExitError); ok {
w.Job.ExitCode = exitErr.Code
}
w.Job.Finished = time.Now().Unix()
switch w.Job.ExitCode {
case 128, 130, 137:
2016-04-20 01:37:53 +00:00
w.Job.Status = model.StatusKilled
case 0:
w.Job.Status = model.StatusSuccess
default:
w.Job.Status = model.StatusFailure
}
logrus.Infof("Finished build %s/%s#%d.%d",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number)
pushRetry(r.drone, w)
return nil
}
func pushRetry(client client.Client, w *queue.Work) {
for {
err := client.Push(w)
if err == nil {
return
}
logrus.Errorf("Error updating %s/%s#%d.%d. Retry in 30s. %s",
w.Repo.Owner, w.Repo.Name, w.Build.Number, w.Job.Number, err)
logrus.Infof("Retry update in 30s")
time.Sleep(time.Second * 30)
}
2016-04-20 01:37:53 +00:00
}
func toEnv(w *queue.Work) map[string]string {
envs := map[string]string{
"CI": "drone",
"DRONE": "true",
"DRONE_ARCH": "linux_amd64",
"DRONE_REPO": w.Repo.FullName,
"DRONE_REPO_SCM": w.Repo.Kind,
"DRONE_REPO_OWNER": w.Repo.Owner,
"DRONE_REPO_NAME": w.Repo.Name,
"DRONE_REPO_LINK": w.Repo.Link,
"DRONE_REPO_AVATAR": w.Repo.Avatar,
"DRONE_REPO_BRANCH": w.Repo.Branch,
"DRONE_REPO_PRIVATE": fmt.Sprintf("%v", w.Repo.IsPrivate),
"DRONE_REPO_TRUSTED": fmt.Sprintf("%v", w.Repo.IsTrusted),
"DRONE_REMOTE_URL": w.Repo.Clone,
"DRONE_COMMIT_SHA": w.Build.Commit,
"DRONE_COMMIT_REF": w.Build.Ref,
"DRONE_COMMIT_BRANCH": w.Build.Branch,
"DRONE_COMMIT_LINK": w.Build.Link,
"DRONE_COMMIT_MESSAGE": w.Build.Message,
"DRONE_AUTHOR": w.Build.Author,
"DRONE_AUTHOR_EMAIL": w.Build.Email,
"DRONE_AUTHOR_AVATAR": w.Build.Avatar,
"DRONE_BUILD_NUMBER": fmt.Sprintf("%d", w.Build.Number),
"DRONE_BUILD_EVENT": w.Build.Event,
"DRONE_BUILD_CREATED": fmt.Sprintf("%d", w.Build.Created),
"DRONE_BUILD_STARTED": fmt.Sprintf("%d", w.Build.Started),
"DRONE_BUILD_FINISHED": fmt.Sprintf("%d", w.Build.Finished),
2016-04-22 00:10:19 +00:00
"DRONE_YAML_VERIFIED": fmt.Sprintf("%v", w.Verified),
"DRONE_YAML_SIGNED": fmt.Sprintf("%v", w.Signed),
"DRONE_BRANCH": w.Build.Branch,
"DRONE_COMMIT": w.Build.Commit,
}
if w.Build.Event == model.EventTag {
envs["DRONE_TAG"] = strings.TrimPrefix(w.Build.Ref, "refs/tags/")
}
if w.Build.Event == model.EventPull {
envs["DRONE_PULL_REQUEST"] = pullRegexp.FindString(w.Build.Ref)
}
if w.Build.Event == model.EventDeploy {
envs["DRONE_DEPLOY_TO"] = w.Build.Deploy
}
if w.BuildLast != nil {
envs["DRONE_PREV_BUILD_STATUS"] = w.BuildLast.Status
envs["DRONE_PREV_BUILD_NUMBER"] = fmt.Sprintf("%v", w.BuildLast.Number)
envs["DRONE_PREV_COMMIT_SHA"] = w.BuildLast.Commit
}
// inject matrix values as environment variables
for key, val := range w.Job.Environment {
envs[key] = val
}
return envs
}
var pullRegexp = regexp.MustCompile("\\d+")