fixing many, many issues

This commit is contained in:
Brad Rydzewski 2015-05-15 19:35:33 -07:00
parent 28061efeea
commit 35c66c7b76
4 changed files with 44 additions and 17 deletions

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"os" "os"
log "github.com/Sirupsen/logrus"
"github.com/samalba/dockerclient" "github.com/samalba/dockerclient"
) )
@ -98,14 +99,18 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo
id, err := client.CreateContainer(conf, "") id, err := client.CreateContainer(conf, "")
if err != nil { if err != nil {
// and pull the image and re-create if that fails // 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, "") id, err = client.CreateContainer(conf, "")
// make sure the container is removed in // make sure the container is removed in
// the event of a creation error. // the event of a creation error.
if err != nil && len(id) != 0 {
client.RemoveContainer(id, true, true)
}
if err != nil { if err != nil {
log.Errorf("Error starting %s. %s\n", conf.Image, err)
client.RemoveContainer(id, true, true)
return nil, err return nil, err
} }
} }
@ -120,6 +125,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo
// fetches the container information. // fetches the container information.
info, err := client.InspectContainer(id) info, err := client.InspectContainer(id)
if err != nil { if err != nil {
log.Errorf("Error inspecting %s. %s\n", conf.Image, err)
client.RemoveContainer(id, true, true) client.RemoveContainer(id, true, true)
return nil, err return nil, err
} }
@ -133,6 +139,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo
// starts the container // starts the container
err := client.StartContainer(id, &conf.HostConfig) err := client.StartContainer(id, &conf.HostConfig)
if err != nil { if err != nil {
log.Errorf("Error starting %s. %s\n", conf.Image, err)
errc <- err errc <- err
return return
} }
@ -142,6 +149,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo
// we could use the `wait` function instead // we could use the `wait` function instead
rc, err := client.ContainerLogs(id, logOptsTail) rc, err := client.ContainerLogs(id, logOptsTail)
if err != nil { if err != nil {
log.Errorf("Error tailing %s. %s\n", conf.Image, err)
errc <- err errc <- err
return return
} }
@ -151,6 +159,7 @@ func run(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull bo
// fetches the container information // fetches the container information
info, err := client.InspectContainer(id) info, err := client.InspectContainer(id)
if err != nil { if err != nil {
log.Errorf("Error getting exit code for %s. %s\n", conf.Image, err)
errc <- err errc <- err
return return
} }
@ -178,14 +187,15 @@ func daemon(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull
id, err := client.CreateContainer(conf, "") id, err := client.CreateContainer(conf, "")
if err != nil { if err != nil {
// and pull the image and re-create if that fails // and pull the image and re-create if that fails
client.PullImage(conf.Image, nil) err = 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)
}
if err != 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 return nil, err
} }
} }
@ -193,11 +203,15 @@ func daemon(client dockerclient.Client, conf *dockerclient.ContainerConfig, pull
// fetches the container information // fetches the container information
info, err := client.InspectContainer(id) info, err := client.InspectContainer(id)
if err != nil { if err != nil {
log.Errorf("Error inspecting %s. %s\n", conf.Image, err)
client.RemoveContainer(id, true, true) client.RemoveContainer(id, true, true)
return nil, err return nil, err
} }
// starts the container // starts the container
err = client.StartContainer(id, &conf.HostConfig) err = client.StartContainer(id, &conf.HostConfig)
if err != nil {
log.Errorf("Error starting daemon %s. %s\n", conf.Image, err)
}
return info, err return info, err
} }

View file

@ -10,6 +10,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
log "github.com/Sirupsen/logrus"
"github.com/drone/drone/common" "github.com/drone/drone/common"
"github.com/samalba/dockerclient" "github.com/samalba/dockerclient"
) )
@ -20,14 +21,19 @@ var (
publish = flag.Bool("publish", false, "") publish = flag.Bool("publish", false, "")
deploy = flag.Bool("deploy", false, "") deploy = flag.Bool("deploy", false, "")
notify = flag.Bool("notify", false, "") notify = flag.Bool("notify", false, "")
debug = flag.Bool("debug", false, "")
) )
func main() { func main() {
flag.Parse() flag.Parse()
if *debug {
log.SetLevel(log.DebugLevel)
}
ctx, err := parseContext() ctx, err := parseContext()
if err != nil { if err != nil {
fmt.Println("Error launching build container.", err) log.Errorln("Error launching build container.", err)
os.Exit(1) os.Exit(1)
return return
} }
@ -37,7 +43,7 @@ func main() {
// linked Docker daemon // linked Docker daemon
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil) docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
if err != nil { if err != nil {
fmt.Println("Error connecting to build server.", err) log.Errorln("Error connecting to build server.", err)
os.Exit(1) os.Exit(1)
return return
} }
@ -46,7 +52,7 @@ func main() {
// container to create a pod-like environment. // container to create a pod-like environment.
client, err := newClient(docker) client, err := newClient(docker)
if err != nil { if err != nil {
fmt.Println("Error starting build server pod", err) log.Errorln("Error starting build server pod", err)
os.Exit(1) os.Exit(1)
} }
ctx.client = client ctx.client = client
@ -56,7 +62,7 @@ func main() {
// prior to executing our build tasks. // prior to executing our build tasks.
err = setup(ctx) err = setup(ctx)
if err != nil { if err != nil {
fmt.Println("Error processing .drone.yml file.", err) log.Errorln("Error processing .drone.yml file.", err)
client.Destroy() client.Destroy()
os.Exit(1) os.Exit(1)
} }

View file

@ -124,7 +124,8 @@ func (r *Runner) Run(w *queue.Work) error {
worker := newWorkerTimeout(client, w.Repo.Timeout) worker := newWorkerTimeout(client, w.Repo.Timeout)
workers = append(workers, worker) workers = append(workers, worker)
cname := cname(task) cname := cname(task)
state, builderr := worker.Build(cname, in) pullrequest := (w.Commit.PullRequest != "")
state, builderr := worker.Build(cname, in, pullrequest)
switch { switch {
case builderr == ErrTimeout: case builderr == ErrTimeout:

View file

@ -41,6 +41,9 @@ var (
// default argument to invoke build steps // default argument to invoke build steps
DefaultBuildArgs = []string{"--build", "--clone", "--publish", "--deploy"} DefaultBuildArgs = []string{"--build", "--clone", "--publish", "--deploy"}
// default argument to invoke build steps
DefaultPullRequestArgs = []string{"--build", "--clone"}
// default arguments to invoke notify steps // default arguments to invoke notify steps
DefaultNotifyArgs = []string{"--notify"} DefaultNotifyArgs = []string{"--notify"}
@ -76,10 +79,13 @@ func newWorkerTimeout(client dockerclient.Client, timeout int64) *worker {
} }
// Build executes the clone, build and deploy steps. // 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 // the command line arguments passed into the
// build agent container. // build agent container.
args := DefaultBuildArgs args := DefaultBuildArgs
if pr {
args = DefaultPullRequestArgs
}
args = append(args, "--") args = append(args, "--")
args = append(args, string(stdin)) args = append(args, string(stdin))