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.
|
2013-05-09 00:05:35 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-05-09 00:05:35 -04:00
|
|
|
func Builder(client *rpc.Client) *builder {
|
|
|
|
return &builder{client}
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:54:42 -04:00
|
|
|
func (b *builder) Prepare(config interface{}) (err error) {
|
2013-05-09 16:59:33 -04:00
|
|
|
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err)
|
|
|
|
if cerr != nil {
|
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:54:42 -04:00
|
|
|
return
|
2013-05-04 16:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-05-09 00:05:35 -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
|
2013-05-05 20:38:50 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterBuild(server, build)
|
|
|
|
RegisterUi(server, ui)
|
2013-05-04 16:47:11 -04:00
|
|
|
|
2013-05-05 20:38:50 -04:00
|
|
|
args := &BuilderRunArgs{serveSingleConn(server)}
|
2013-05-04 16:47:11 -04:00
|
|
|
b.client.Call("Builder.Run", args, new(interface{}))
|
|
|
|
}
|
|
|
|
|
2013-05-09 13:54:42 -04:00
|
|
|
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
|
2013-05-09 16:59:33 -04:00
|
|
|
err := b.builder.Prepare(args.Config)
|
|
|
|
if err != nil {
|
|
|
|
*reply = NewBasicError(err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|