From 35c66c7b76aed5b0e98a0332f0a407b59d69c61c Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 15 May 2015 19:35:33 -0700 Subject: [PATCH] fixing many, many issues --- cmd/drone-build/client.go | 36 +++++++++++++++++++++++++----------- cmd/drone-build/main.go | 14 ++++++++++---- runner/builtin/runner.go | 3 ++- runner/builtin/worker.go | 8 +++++++- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/cmd/drone-build/client.go b/cmd/drone-build/client.go index bcce7c66..235df542 100644 --- a/cmd/drone-build/client.go +++ b/cmd/drone-build/client.go @@ -4,6 +4,7 @@ import ( "errors" "os" + log "github.com/Sirupsen/logrus" "github.com/samalba/dockerclient" ) @@ -98,14 +99,18 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo id, err := client.CreateContainer(conf, "") if err != nil { // and pull the image and re-create if that fails - client.PullImage(conf.Image, nil) + err = client.PullImage(conf.Image, nil) + if err != nil { + log.Errorf("Error pulling %s. %s\n", conf.Image, err) + return nil, err + } + id, err = client.CreateContainer(conf, "") // make sure the container is removed in // the event of a creation error. - if err != nil && len(id) != 0 { - client.RemoveContainer(id, true, true) - } if err != nil { + log.Errorf("Error starting %s. %s\n", conf.Image, err) + client.RemoveContainer(id, true, true) return nil, err } } @@ -120,6 +125,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo // fetches the container information. info, err := client.InspectContainer(id) if err != nil { + log.Errorf("Error inspecting %s. %s\n", conf.Image, err) client.RemoveContainer(id, true, true) return nil, err } @@ -133,6 +139,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo // starts the container err := client.StartContainer(id, &conf.HostConfig) if err != nil { + log.Errorf("Error starting %s. %s\n", conf.Image, err) errc <- err return } @@ -142,6 +149,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo // we could use the `wait` function instead rc, err := client.ContainerLogs(id, logOptsTail) if err != nil { + log.Errorf("Error tailing %s. %s\n", conf.Image, err) errc <- err return } @@ -151,6 +159,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo // fetches the container information info, err := client.InspectContainer(id) if err != nil { + log.Errorf("Error getting exit code for %s. %s\n", conf.Image, err) errc <- err return } @@ -178,14 +187,15 @@ func daemon(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull id, err := client.CreateContainer(conf, "") if err != nil { // and pull the image and re-create if that fails - client.PullImage(conf.Image, nil) - id, err = client.CreateContainer(conf, "") - // make sure the container is removed in - // the event of a creation error. - if err != nil && len(id) != 0 { - client.RemoveContainer(id, true, true) - } + err = client.PullImage(conf.Image, nil) if err != nil { + log.Errorf("Error pulling %s. %s\n", conf.Image, err) + return nil, err + } + id, err = client.CreateContainer(conf, "") + if err != nil { + log.Errorf("Error creating %s. %s\n", conf.Image, err) + client.RemoveContainer(id, true, true) return nil, err } } @@ -193,11 +203,15 @@ func daemon(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull // fetches the container information info, err := client.InspectContainer(id) if err != nil { + log.Errorf("Error inspecting %s. %s\n", conf.Image, err) client.RemoveContainer(id, true, true) return nil, err } // starts the container err = client.StartContainer(id, &conf.HostConfig) + if err != nil { + log.Errorf("Error starting daemon %s. %s\n", conf.Image, err) + } return info, err } diff --git a/cmd/drone-build/main.go b/cmd/drone-build/main.go index 810b4086..941bf50a 100644 --- a/cmd/drone-build/main.go +++ b/cmd/drone-build/main.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" + log "github.com/Sirupsen/logrus" "github.com/drone/drone/common" "github.com/samalba/dockerclient" ) @@ -20,14 +21,19 @@ var ( publish = flag.Bool("publish", false, "") deploy = flag.Bool("deploy", false, "") notify = flag.Bool("notify", false, "") + debug = flag.Bool("debug", false, "") ) func main() { flag.Parse() + if *debug { + log.SetLevel(log.DebugLevel) + } + ctx, err := parseContext() if err != nil { - fmt.Println("Error launching build container.", err) + log.Errorln("Error launching build container.", err) os.Exit(1) return } @@ -37,7 +43,7 @@ func main() { // linked Docker daemon docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil) if err != nil { - fmt.Println("Error connecting to build server.", err) + log.Errorln("Error connecting to build server.", err) os.Exit(1) return } @@ -46,7 +52,7 @@ func main() { // container to create a pod-like environment. client, err := newClient(docker) if err != nil { - fmt.Println("Error starting build server pod", err) + log.Errorln("Error starting build server pod", err) os.Exit(1) } ctx.client = client @@ -56,7 +62,7 @@ func main() { // prior to executing our build tasks. err = setup(ctx) if err != nil { - fmt.Println("Error processing .drone.yml file.", err) + log.Errorln("Error processing .drone.yml file.", err) client.Destroy() os.Exit(1) } diff --git a/runner/builtin/runner.go b/runner/builtin/runner.go index a8c3ef9c..70cc3fb8 100644 --- a/runner/builtin/runner.go +++ b/runner/builtin/runner.go @@ -124,7 +124,8 @@ func (r *Runner) Run(w *queue.Work) error { worker := newWorkerTimeout(client, w.Repo.Timeout) workers = append(workers, worker) cname := cname(task) - state, builderr := worker.Build(cname, in) + pullrequest := (w.Commit.PullRequest != "") + state, builderr := worker.Build(cname, in, pullrequest) switch { case builderr == ErrTimeout: diff --git a/runner/builtin/worker.go b/runner/builtin/worker.go index ce3eddb4..af82d077 100644 --- a/runner/builtin/worker.go +++ b/runner/builtin/worker.go @@ -41,6 +41,9 @@ var ( // default argument to invoke build steps DefaultBuildArgs = []string{"--build", "--clone", "--publish", "--deploy"} + // default argument to invoke build steps + DefaultPullRequestArgs = []string{"--build", "--clone"} + // default arguments to invoke notify steps DefaultNotifyArgs = []string{"--notify"} @@ -76,10 +79,13 @@ func newWorkerTimeout(client dockerclient.Client, timeout int64) *worker { } // Build executes the clone, build and deploy steps. -func (w *worker) Build(name string, stdin []byte) (_ int, err error) { +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 + if pr { + args = DefaultPullRequestArgs + } args = append(args, "--") args = append(args, string(stdin))