packer: Build.Prepare takes a UI
This commit is contained in:
parent
fbcc6cb2b2
commit
cb91ca72ac
|
@ -64,7 +64,7 @@ func (Command) Run(env packer.Environment, args []string) int {
|
|||
// Prepare all the builds
|
||||
for _, b := range builds {
|
||||
log.Printf("Preparing build: %s", b.Name())
|
||||
err := b.Prepare()
|
||||
err := b.Prepare(buildUis[b.Name()])
|
||||
if err != nil {
|
||||
env.Ui().Error(err.Error())
|
||||
return 1
|
||||
|
|
|
@ -6,7 +6,7 @@ import "log"
|
|||
// building some machine image artifact. Builds are meant to be parallelized.
|
||||
type Build interface {
|
||||
Name() string
|
||||
Prepare() error
|
||||
Prepare(Ui) error
|
||||
Run(Ui) Artifact
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ func (b *coreBuild) Name() string {
|
|||
|
||||
// Prepare prepares the build by doing some initialization for the builder
|
||||
// and any hooks. This _must_ be called prior to Run.
|
||||
func (b *coreBuild) Prepare() (err error) {
|
||||
func (b *coreBuild) Prepare(ui Ui) (err error) {
|
||||
b.prepareCalled = true
|
||||
err = b.builder.Prepare(b.builderConfig)
|
||||
if err != nil {
|
||||
|
|
|
@ -51,7 +51,7 @@ func TestBuild_Prepare(t *testing.T) {
|
|||
build := testBuild()
|
||||
builder := build.(*coreBuild).builder.(*TestBuilder)
|
||||
|
||||
build.Prepare()
|
||||
build.Prepare(nil)
|
||||
assert.True(builder.prepareCalled, "prepare should be called")
|
||||
assert.Equal(builder.prepareConfig, 42, "prepare config should be 42")
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ func TestBuild_Run(t *testing.T) {
|
|||
ui := testUi()
|
||||
|
||||
build := testBuild()
|
||||
build.Prepare()
|
||||
build.Prepare(ui)
|
||||
build.Run(ui)
|
||||
|
||||
builder := build.(*coreBuild).builder.(*TestBuilder)
|
||||
|
|
|
@ -17,7 +17,9 @@ type BuildServer struct {
|
|||
build packer.Build
|
||||
}
|
||||
|
||||
type BuildPrepareArgs interface{}
|
||||
type BuildPrepareArgs struct {
|
||||
RPCAddress string
|
||||
}
|
||||
|
||||
type BuildRunArgs struct {
|
||||
UiRPCAddress string
|
||||
|
@ -32,8 +34,17 @@ func (b *build) Name() (result string) {
|
|||
return
|
||||
}
|
||||
|
||||
func (b *build) Prepare() (err error) {
|
||||
b.client.Call("Build.Prepare", new(interface{}), &err)
|
||||
func (b *build) Prepare(ui packer.Ui) (err error) {
|
||||
// Create and start the server for the UI
|
||||
// TODO: Error handling
|
||||
server := rpc.NewServer()
|
||||
RegisterUi(server, ui)
|
||||
args := &BuildPrepareArgs{serveSingleConn(server)}
|
||||
|
||||
if err := b.client.Call("Build.Prepare", args, &err); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -63,7 +74,12 @@ func (b *BuildServer) Name(args *interface{}, reply *string) error {
|
|||
}
|
||||
|
||||
func (b *BuildServer) Prepare(args *BuildPrepareArgs, reply *error) error {
|
||||
*reply = b.build.Prepare()
|
||||
client, err := rpc.Dial("tcp", args.RPCAddress)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*reply = b.build.Prepare(&Ui{client})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ var testBuildArtifact = &testArtifact{}
|
|||
type testBuild struct {
|
||||
nameCalled bool
|
||||
prepareCalled bool
|
||||
prepareUi packer.Ui
|
||||
runCalled bool
|
||||
runUi packer.Ui
|
||||
}
|
||||
|
@ -21,8 +22,9 @@ func (b *testBuild) Name() string {
|
|||
return "name"
|
||||
}
|
||||
|
||||
func (b *testBuild) Prepare() error {
|
||||
func (b *testBuild) Prepare(ui packer.Ui) error {
|
||||
b.prepareCalled = true
|
||||
b.prepareUi = ui
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -53,11 +55,12 @@ func TestBuildRPC(t *testing.T) {
|
|||
assert.True(b.nameCalled, "name should be called")
|
||||
|
||||
// Test Prepare
|
||||
bClient.Prepare()
|
||||
ui := new(testUi)
|
||||
bClient.Prepare(ui)
|
||||
assert.True(b.prepareCalled, "prepare should be called")
|
||||
|
||||
// Test Run
|
||||
ui := new(testUi)
|
||||
ui = new(testUi)
|
||||
bClient.Run(ui)
|
||||
assert.True(b.runCalled, "run should be called")
|
||||
|
||||
|
|
Loading…
Reference in New Issue