packer-cn/packer/rpc/builder.go

93 lines
1.8 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) {
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &err)
if cerr != nil {
err = cerr
}
return
2013-05-04 16:47:11 -04:00
}
2013-05-22 01:10:21 -04:00
func (b *builder) Run(ui packer.Ui, hook packer.Hook) packer.Artifact {
2013-05-04 16:47:11 -04:00
// Create and start the server for the Build and UI
// TODO: Error handling
server := rpc.NewServer()
RegisterUi(server, ui)
RegisterHook(server, hook)
2013-05-04 16:47:11 -04:00
args := &BuilderRunArgs{serveSingleConn(server)}
2013-05-22 01:10:21 -04:00
var reply string
if err := b.client.Call("Builder.Run", args, &reply); err != nil {
panic(err)
}
client, err := rpc.Dial("tcp", reply)
if err != nil {
panic(err)
}
return Artifact(client)
2013-05-04 16:47:11 -04:00
}
func (b *builder) Cancel() {
}
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *error) error {
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 *string) error {
2013-05-04 16:47:11 -04:00
client, err := rpc.Dial("tcp", args.RPCAddress)
if err != nil {
return err
}
hook := Hook(client)
2013-05-04 16:47:11 -04:00
ui := &Ui{client}
artifact := b.builder.Run(ui, hook)
// Wrap the artifact
server := rpc.NewServer()
RegisterArtifact(server, artifact)
2013-05-04 16:47:11 -04:00
*reply = serveSingleConn(server)
2013-05-04 16:47:11 -04:00
return nil
}