Commit db8aeaba authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer, packer/rpc: Build/builders can return errors now

parent e101b031
...@@ -7,7 +7,7 @@ import "log" ...@@ -7,7 +7,7 @@ import "log"
type Build interface { type Build interface {
Name() string Name() string
Prepare(Ui) error Prepare(Ui) error
Run(Ui, Cache) Artifact Run(Ui, Cache) (Artifact, error)
Cancel() Cancel()
} }
...@@ -61,7 +61,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) { ...@@ -61,7 +61,7 @@ func (b *coreBuild) Prepare(ui Ui) (err error) {
} }
// Runs the actual build. Prepare must be called prior to running this. // Runs the actual build. Prepare must be called prior to running this.
func (b *coreBuild) Run(ui Ui, cache Cache) Artifact { func (b *coreBuild) Run(ui Ui, cache Cache) (Artifact, error) {
if !b.prepareCalled { if !b.prepareCalled {
panic("Prepare must be called first") panic("Prepare must be called first")
} }
......
...@@ -10,7 +10,7 @@ type Builder interface { ...@@ -10,7 +10,7 @@ type Builder interface {
Prepare(config interface{}) error Prepare(config interface{}) error
// Run is where the actual build should take place. It takes a Build and a Ui. // Run is where the actual build should take place. It takes a Build and a Ui.
Run(ui Ui, hook Hook, cache Cache) Artifact Run(ui Ui, hook Hook, cache Cache) (Artifact, error)
// Cancel cancels a possibly running Builder. This should block until // Cancel cancels a possibly running Builder. This should block until
// the builder actually cancels and cleans up after itself. // the builder actually cancels and cleans up after itself.
......
...@@ -16,12 +16,12 @@ func (tb *TestBuilder) Prepare(config interface{}) error { ...@@ -16,12 +16,12 @@ func (tb *TestBuilder) Prepare(config interface{}) error {
return nil return nil
} }
func (tb *TestBuilder) Run(ui Ui, h Hook, c Cache) Artifact { func (tb *TestBuilder) Run(ui Ui, h Hook, c Cache) (Artifact, error) {
tb.runCalled = true tb.runCalled = true
tb.runHook = h tb.runHook = h
tb.runUi = ui tb.runUi = ui
tb.runCache = c tb.runCache = c
return nil return nil, nil
} }
func (tb *TestBuilder) Cancel() { func (tb *TestBuilder) Cancel() {
......
...@@ -48,7 +48,7 @@ func (b *build) Prepare(ui packer.Ui) (err error) { ...@@ -48,7 +48,7 @@ func (b *build) Prepare(ui packer.Ui) (err error) {
return return
} }
func (b *build) Run(ui packer.Ui, cache packer.Cache) packer.Artifact { func (b *build) Run(ui packer.Ui, cache packer.Cache) (packer.Artifact, error) {
// Create and start the server for the UI // Create and start the server for the UI
server := rpc.NewServer() server := rpc.NewServer()
RegisterCache(server, cache) RegisterCache(server, cache)
...@@ -65,7 +65,7 @@ func (b *build) Run(ui packer.Ui, cache packer.Cache) packer.Artifact { ...@@ -65,7 +65,7 @@ func (b *build) Run(ui packer.Ui, cache packer.Cache) packer.Artifact {
panic(err) panic(err)
} }
return Artifact(client) return Artifact(client), nil
} }
func (b *build) Cancel() { func (b *build) Cancel() {
...@@ -95,7 +95,10 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error { ...@@ -95,7 +95,10 @@ func (b *BuildServer) Run(args *BuildRunArgs, reply *string) error {
return err return err
} }
artifact := b.build.Run(&Ui{client}, Cache(client)) artifact, err := b.build.Run(&Ui{client}, Cache(client))
if err != nil {
return err
}
// Wrap the artifact // Wrap the artifact
server := rpc.NewServer() server := rpc.NewServer()
......
...@@ -30,11 +30,11 @@ func (b *testBuild) Prepare(ui packer.Ui) error { ...@@ -30,11 +30,11 @@ func (b *testBuild) Prepare(ui packer.Ui) error {
return nil return nil
} }
func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) packer.Artifact { func (b *testBuild) Run(ui packer.Ui, cache packer.Cache) (packer.Artifact, error) {
b.runCalled = true b.runCalled = true
b.runCache = cache b.runCache = cache
b.runUi = ui b.runUi = ui
return testBuildArtifact return testBuildArtifact, nil
} }
func (b *testBuild) Cancel() { func (b *testBuild) Cancel() {
......
...@@ -30,6 +30,7 @@ type BuilderRunArgs struct { ...@@ -30,6 +30,7 @@ type BuilderRunArgs struct {
} }
type BuilderRunResponse struct { type BuilderRunResponse struct {
Err error
RPCAddress string RPCAddress string
} }
...@@ -46,7 +47,7 @@ func (b *builder) Prepare(config interface{}) (err error) { ...@@ -46,7 +47,7 @@ func (b *builder) Prepare(config interface{}) (err error) {
return return
} }
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact { func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
// Create and start the server for the Build and UI // Create and start the server for the Build and UI
server := rpc.NewServer() server := rpc.NewServer()
RegisterCache(server, cache) RegisterCache(server, cache)
...@@ -55,7 +56,7 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer ...@@ -55,7 +56,7 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer
// Create a server for the response // Create a server for the response
responseL := netListenerInRange(portRangeMin, portRangeMax) responseL := netListenerInRange(portRangeMin, portRangeMax)
artifactAddress := make(chan string) runResponseCh := make(chan *BuilderRunResponse)
go func() { go func() {
defer responseL.Close() defer responseL.Close()
...@@ -72,7 +73,7 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer ...@@ -72,7 +73,7 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer
log.Panic(err) log.Panic(err)
} }
artifactAddress <- response.RPCAddress runResponseCh <- &response
}() }()
args := &BuilderRunArgs{ args := &BuilderRunArgs{
...@@ -81,20 +82,24 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer ...@@ -81,20 +82,24 @@ func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer
} }
if err := b.client.Call("Builder.Run", args, new(interface{})); err != nil { if err := b.client.Call("Builder.Run", args, new(interface{})); err != nil {
panic(err) return nil, err
} }
address := <-artifactAddress response := <-runResponseCh
if address == "" { if response.Err != nil {
return nil return nil, response.Err
} }
client, err := rpc.Dial("tcp", address) if response.RPCAddress == "" {
return nil, nil
}
client, err := rpc.Dial("tcp", response.RPCAddress)
if err != nil { if err != nil {
panic(err) return nil, err
} }
return Artifact(client) return Artifact(client), nil
} }
func (b *builder) Cancel() { func (b *builder) Cancel() {
...@@ -132,17 +137,24 @@ func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error { ...@@ -132,17 +137,24 @@ func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
cache := Cache(client) cache := Cache(client)
hook := Hook(client) hook := Hook(client)
ui := &Ui{client} ui := &Ui{client}
artifact := b.builder.Run(ui, hook, cache) artifact, responseErr := b.builder.Run(ui, hook, cache)
responseAddress := "" responseAddress := ""
if artifact != nil { if responseErr == nil && artifact != nil {
// Wrap the artifact // Wrap the artifact
server := rpc.NewServer() server := rpc.NewServer()
RegisterArtifact(server, artifact) RegisterArtifact(server, artifact)
responseAddress = serveSingleConn(server) responseAddress = serveSingleConn(server)
} }
responseWriter.Encode(&BuilderRunResponse{responseAddress}) if responseErr != nil {
responseErr = NewBasicError(responseErr)
}
err := responseWriter.Encode(&BuilderRunResponse{responseErr, responseAddress})
if err != nil {
panic(err)
}
}() }()
return nil return nil
......
...@@ -2,6 +2,7 @@ package rpc ...@@ -2,6 +2,7 @@ package rpc
import ( import (
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
"errors"
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"net/rpc" "net/rpc"
"testing" "testing"
...@@ -18,6 +19,7 @@ type testBuilder struct { ...@@ -18,6 +19,7 @@ type testBuilder struct {
runUi packer.Ui runUi packer.Ui
cancelCalled bool cancelCalled bool
errRunResult bool
nilRunResult bool nilRunResult bool
} }
...@@ -27,16 +29,18 @@ func (b *testBuilder) Prepare(config interface{}) error { ...@@ -27,16 +29,18 @@ func (b *testBuilder) Prepare(config interface{}) error {
return nil return nil
} }
func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer.Artifact { func (b *testBuilder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
b.runCache = cache b.runCache = cache
b.runCalled = true b.runCalled = true
b.runHook = hook b.runHook = hook
b.runUi = ui b.runUi = ui
if !b.nilRunResult { if b.errRunResult {
return testBuilderArtifact return nil, errors.New("foo")
} else if b.nilRunResult {
return nil, nil
} else { } else {
return nil return testBuilderArtifact, nil
} }
} }
...@@ -70,7 +74,8 @@ func TestBuilderRPC(t *testing.T) { ...@@ -70,7 +74,8 @@ func TestBuilderRPC(t *testing.T) {
cache := new(testCache) cache := new(testCache)
hook := &testHook{} hook := &testHook{}
ui := &testUi{} ui := &testUi{}
artifact := bClient.Run(ui, hook, cache) artifact, err := bClient.Run(ui, hook, cache)
assert.Nil(err, "should have no error")
assert.True(b.runCalled, "runs hould be called") assert.True(b.runCalled, "runs hould be called")
if b.runCalled { if b.runCalled {
...@@ -89,8 +94,16 @@ func TestBuilderRPC(t *testing.T) { ...@@ -89,8 +94,16 @@ func TestBuilderRPC(t *testing.T) {
// Test run with nil result // Test run with nil result
b.nilRunResult = true b.nilRunResult = true
artifact = bClient.Run(ui, hook, cache) artifact, err = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil")
assert.Nil(err, "should have no error")
// Test with an error
b.errRunResult = true
b.nilRunResult = false
artifact, err = bClient.Run(ui, hook, cache)
assert.Nil(artifact, "should be nil") assert.Nil(artifact, "should be nil")
assert.NotNil(err, "should have error")
// Test Cancel // Test Cancel
bClient.Cancel() bClient.Cancel()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment