packer-cn/packer/rpc/builder.go

66 lines
1.4 KiB
Go
Raw Normal View History

2013-05-04 16:47:11 -04:00
package rpc
import (
"github.com/mitchellh/packer/packer"
"net/rpc"
)
// An implementation of packer.Builder where the builder is actually executed
// over an RPC connection.
type builder struct {
2013-05-04 16:47:11 -04:00
client *rpc.Client
}
// BuilderServer wraps a packer.Builder implementation and makes it exportable
// as part of a Golang RPC server.
type BuilderServer struct {
builder packer.Builder
}
type BuilderPrepareArgs struct {
Config interface{}
}
type BuilderRunArgs struct {
RPCAddress string
}
func Builder(client *rpc.Client) *builder {
return &builder{client}
}
func (b *builder) Prepare(config interface{}) (err error) {
b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err)
return
2013-05-04 16:47:11 -04:00
}
func (b *builder) Run(build packer.Build, ui packer.Ui) {
2013-05-04 16:47:11 -04:00
// Create and start the server for the Build and UI
// TODO: Error handling
server := rpc.NewServer()
RegisterBuild(server, build)
RegisterUi(server, ui)
2013-05-04 16:47:11 -04:00
args := &BuilderRunArgs{serveSingleConn(server)}
2013-05-04 16:47:11 -04:00
b.client.Call("Builder.Run", args, new(interface{}))
}
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
*reply = b.builder.Prepare(args.Config)
2013-05-04 16:47:11 -04:00
return nil
}
func (b *BuilderServer) Run(args *BuilderRunArgs, reply *interface{}) error {
client, err := rpc.Dial("tcp", args.RPCAddress)
if err != nil {
return err
}
build := &Build{client}
ui := &Ui{client}
b.builder.Run(build, ui)
*reply = nil
return nil
}