packer-cn/packer/rpc/builder.go

114 lines
2.4 KiB
Go
Raw Normal View History

2013-05-04 16:47:11 -04:00
package rpc
import (
"github.com/mitchellh/packer/packer"
"log"
2013-05-04 16:47:11 -04:00
"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
2013-12-10 15:14:08 -05:00
mux *MuxConn
2013-05-04 16:47:11 -04:00
}
// BuilderServer wraps a packer.Builder implementation and makes it exportable
// as part of a Golang RPC server.
type BuilderServer struct {
builder packer.Builder
2013-12-10 15:14:08 -05:00
mux *MuxConn
2013-05-04 16:47:11 -04:00
}
type BuilderPrepareArgs struct {
Configs []interface{}
2013-05-04 16:47:11 -04:00
}
type BuilderPrepareResponse struct {
Warnings []string
Error error
}
func (b *builder) Prepare(config ...interface{}) ([]string, error) {
var resp BuilderPrepareResponse
cerr := b.client.Call("Builder.Prepare", &BuilderPrepareArgs{config}, &resp)
if cerr != nil {
return nil, cerr
}
return resp.Warnings, resp.Error
2013-05-04 16:47:11 -04:00
}
func (b *builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
2013-12-10 15:14:08 -05:00
nextId := b.mux.NextId()
server := NewServerWithMux(b.mux, nextId)
server.RegisterCache(cache)
server.RegisterHook(hook)
server.RegisterUi(ui)
go server.Serve()
var responseId uint32
if err := b.client.Call("Builder.Run", nextId, &responseId); err != nil {
return nil, err
}
2013-12-10 15:14:08 -05:00
if responseId == 0 {
return nil, nil
}
2013-12-10 15:14:08 -05:00
client, err := NewClientWithMux(b.mux, responseId)
if err != nil {
return nil, err
}
2013-12-10 15:14:08 -05:00
return client.Artifact(), nil
2013-05-04 16:47:11 -04:00
}
func (b *builder) Cancel() {
2013-06-03 18:30:09 -04:00
if err := b.client.Call("Builder.Cancel", new(interface{}), new(interface{})); err != nil {
log.Printf("Error cancelling builder: %s", err)
2013-06-03 18:30:09 -04:00
}
}
func (b *BuilderServer) Prepare(args *BuilderPrepareArgs, reply *BuilderPrepareResponse) error {
warnings, err := b.builder.Prepare(args.Configs...)
if err != nil {
err = NewBasicError(err)
}
*reply = BuilderPrepareResponse{
Warnings: warnings,
Error: err,
}
2013-05-04 16:47:11 -04:00
return nil
}
2013-12-10 15:14:08 -05:00
func (b *BuilderServer) Run(streamId uint32, reply *uint32) error {
client, err := NewClientWithMux(b.mux, streamId)
2013-05-04 16:47:11 -04:00
if err != nil {
2013-12-10 15:14:08 -05:00
return NewBasicError(err)
2013-05-04 16:47:11 -04:00
}
2013-12-10 15:14:08 -05:00
defer client.Close()
2013-05-04 16:47:11 -04:00
2013-12-10 15:14:08 -05:00
artifact, err := b.builder.Run(client.Ui(), client.Hook(), client.Cache())
if err != nil {
2013-12-10 15:14:08 -05:00
return NewBasicError(err)
}
2013-12-10 15:14:08 -05:00
*reply = 0
if artifact != nil {
streamId = b.mux.NextId()
server := NewServerWithMux(b.mux, streamId)
server.RegisterArtifact(artifact)
go server.Serve()
*reply = streamId
}
2013-05-04 16:47:11 -04:00
return nil
}
2013-06-03 18:30:09 -04:00
2013-06-04 01:32:04 -04:00
func (b *BuilderServer) Cancel(args *interface{}, reply *interface{}) error {
2013-06-03 18:30:09 -04:00
b.builder.Cancel()
return nil
}