Merge pull request #777 from bradrydzewski/master

improved debugging when Docker errors
This commit is contained in:
Brad Rydzewski 2014-12-30 13:21:30 -08:00
commit 0a6227930d
4 changed files with 16 additions and 14 deletions

View file

@ -23,7 +23,7 @@ type Permstore interface {
// GetPerm retrieves the User's permission from // GetPerm retrieves the User's permission from
// the datastore for the given repository. // the datastore for the given repository.
func GetPerm(c context.Context, user *model.User, repo *model.Repo) (*model.Perm, error) { func GetPerm(c context.Context, user *model.User, repo *model.Repo) (*model.Perm, error) {
// if the user is a gues they should only be granted // if the user is a guest they should only be granted
// read access to public repositories. // read access to public repositories.
switch { switch {
case user == nil && repo.Private: case user == nil && repo.Private:
@ -43,8 +43,8 @@ func GetPerm(c context.Context, user *model.User, repo *model.Repo) (*model.Perm
// if the user is authenticated we'll retireive the // if the user is authenticated we'll retireive the
// permission details from the database. // permission details from the database.
perm, err := FromContext(c).GetPerm(user, repo) perm, err := FromContext(c).GetPerm(user, repo)
if err == nil && perm.ID != 0 { if perm.ID == 0 {
return perm, err perm.Guest = true
} }
switch { switch {
@ -53,12 +53,10 @@ func GetPerm(c context.Context, user *model.User, repo *model.Repo) (*model.Perm
perm.Read = true perm.Read = true
perm.Write = true perm.Write = true
perm.Admin = true perm.Admin = true
perm.Guest = true
// if the repo is public, grant read access only. // if the repo is public, grant read access only.
case repo.Private == false: case repo.Private == false:
perm.Read = true perm.Read = true
perm.Guest = true
} }
return perm, err return perm, err
} }

View file

@ -139,10 +139,13 @@ func (d *Docker) Do(c context.Context, r *worker.Work) {
builder.Build = script builder.Build = script
builder.Repo = repo builder.Repo = repo
builder.Stdout = buf builder.Stdout = buf
builder.Key = []byte(r.Repo.PrivateKey)
builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second builder.Timeout = time.Duration(r.Repo.Timeout) * time.Second
builder.Privileged = r.Repo.Privileged builder.Privileged = r.Repo.Privileged
if r.Repo.Private || len(r.Commit.PullRequest) == 0 {
builder.Key = []byte(r.Repo.PrivateKey)
}
// run the build // run the build
err = builder.Run() err = builder.Run()

View file

@ -238,7 +238,7 @@ func (b *Builder) setup() error {
if _, err := b.dockerClient.Images.Inspect(b.Build.Image); err == docker.ErrNotFound { if _, err := b.dockerClient.Images.Inspect(b.Build.Image); err == docker.ErrNotFound {
// download the image if it doesn't exist // download the image if it doesn't exist
if err := b.dockerClient.Images.Pull(b.Build.Image); err != nil { if err := b.dockerClient.Images.Pull(b.Build.Image); err != nil {
return err return fmt.Errorf("Error: Unable to pull image %s. %s", b.Build.Image, err)
} }
} else if err != nil { } else if err != nil {
log.Errf("failed to inspect image %s", b.Build.Image) log.Errf("failed to inspect image %s", b.Build.Image)
@ -392,7 +392,7 @@ func (b *Builder) run() error {
// create the container from the image // create the container from the image
run, err := b.dockerClient.Containers.Create(&conf) run, err := b.dockerClient.Containers.Create(&conf)
if err != nil { if err != nil {
return err return fmt.Errorf("Error: Failed to create build container. %s", err)
} }
// cache instance of docker.Run // cache instance of docker.Run
@ -407,7 +407,7 @@ func (b *Builder) run() error {
if err := b.dockerClient.Containers.Start(run.ID, &host); err != nil { if err := b.dockerClient.Containers.Start(run.ID, &host); err != nil {
b.BuildState.ExitCode = 1 b.BuildState.ExitCode = 1
b.BuildState.Finished = time.Now().UTC().Unix() b.BuildState.Finished = time.Now().UTC().Unix()
return err return fmt.Errorf("Error: Failed to start build container. %s", err)
} }
// wait for the container to stop // wait for the container to stop
@ -415,7 +415,7 @@ func (b *Builder) run() error {
if err != nil { if err != nil {
b.BuildState.ExitCode = 1 b.BuildState.ExitCode = 1
b.BuildState.Finished = time.Now().UTC().Unix() b.BuildState.Finished = time.Now().UTC().Unix()
return err return fmt.Errorf("Error: Failed to wait for build container. %s", err)
} }
// set completion time // set completion time
@ -503,7 +503,9 @@ func (b *Builder) writeBuildScript(dir string) error {
f.WriteHost(mapping) f.WriteHost(mapping)
} }
if len(b.Key) != 0 {
f.WriteFile("$HOME/.ssh/id_rsa", b.Key, 600) f.WriteFile("$HOME/.ssh/id_rsa", b.Key, 600)
}
// if the repository is remote then we should // if the repository is remote then we should
// add the commands to the build script to // add the commands to the build script to

View file

@ -422,8 +422,7 @@ func TestRunErrorCreate(t *testing.T) {
b.image = &docker.Image{ID: "c3ab8ff137"} b.image = &docker.Image{ID: "c3ab8ff137"}
b.Build = &script.Build{} b.Build = &script.Build{}
b.Repo = &repo.Repo{} b.Repo = &repo.Repo{}
if err := b.run(); err == nil || err.Error() != "Error: Failed to create build container. Bad Request" {
if err := b.run(); err != docker.ErrBadRequest {
t.Errorf("Expected error when trying to create build container") t.Errorf("Expected error when trying to create build container")
} }
} }
@ -456,7 +455,7 @@ func TestRunErrorStart(t *testing.T) {
b.Build = &script.Build{} b.Build = &script.Build{}
b.Repo = &repo.Repo{} b.Repo = &repo.Repo{}
if err := b.run(); err != docker.ErrBadRequest { if err := b.run(); err == nil || err.Error() != "Error: Failed to start build container. Bad Request" {
t.Errorf("Expected error when trying to start build container") t.Errorf("Expected error when trying to start build container")
} }