2013-06-18 16:44:57 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/rpc"
|
2017-03-28 21:02:51 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-18 16:44:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.PostProcessor where the PostProcessor is actually
|
|
|
|
// executed over an RPC connection.
|
|
|
|
type postProcessor struct {
|
|
|
|
client *rpc.Client
|
2014-09-02 17:23:06 -04:00
|
|
|
mux *muxBroker
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostProcessorServer wraps a packer.PostProcessor implementation and makes it
|
|
|
|
// exportable as part of a Golang RPC server.
|
|
|
|
type PostProcessorServer struct {
|
2017-03-28 21:02:51 -04:00
|
|
|
mux *muxBroker
|
|
|
|
p packer.PostProcessor
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
type PostProcessorConfigureArgs struct {
|
|
|
|
Configs []interface{}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:44:57 -04:00
|
|
|
type PostProcessorProcessResponse struct {
|
2014-04-28 18:18:31 -04:00
|
|
|
Err *BasicError
|
2013-12-09 22:07:36 -05:00
|
|
|
Keep bool
|
|
|
|
StreamId uint32
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
func (p *postProcessor) Configure(raw ...interface{}) (err error) {
|
|
|
|
args := &PostProcessorConfigureArgs{Configs: raw}
|
2014-04-28 18:18:31 -04:00
|
|
|
if cerr := p.client.Call("PostProcessor.Configure", args, new(interface{})); cerr != nil {
|
2013-06-18 16:44:57 -04:00
|
|
|
err = cerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:30:39 -04:00
|
|
|
func (p *postProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) {
|
2013-12-09 22:07:36 -05:00
|
|
|
nextId := p.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(p.mux, nextId)
|
2013-12-09 22:07:36 -05:00
|
|
|
server.RegisterArtifact(a)
|
|
|
|
server.RegisterUi(ui)
|
|
|
|
go server.Serve()
|
2013-06-18 16:44:57 -04:00
|
|
|
|
|
|
|
var response PostProcessorProcessResponse
|
2013-12-09 22:07:36 -05:00
|
|
|
if err := p.client.Call("PostProcessor.PostProcess", nextId, &response); err != nil {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.Err != nil {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, response.Err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 22:07:36 -05:00
|
|
|
if response.StreamId == 0 {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(p.mux, response.StreamId)
|
2013-12-09 22:07:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Artifact(), response.Keep, nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2014-04-28 18:18:31 -04:00
|
|
|
func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply *interface{}) error {
|
|
|
|
err := p.p.Configure(args.Configs...)
|
|
|
|
return err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 22:07:36 -05:00
|
|
|
func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorProcessResponse) error {
|
2013-12-19 20:35:32 -05:00
|
|
|
client, err := newClientWithMux(p.mux, streamId)
|
2013-12-09 22:07:36 -05:00
|
|
|
if err != nil {
|
|
|
|
return NewBasicError(err)
|
2013-12-09 18:44:00 -05:00
|
|
|
}
|
2013-12-09 22:07:36 -05:00
|
|
|
defer client.Close()
|
2013-06-18 16:44:57 -04:00
|
|
|
|
2013-12-09 22:07:36 -05:00
|
|
|
streamId = 0
|
|
|
|
artifactResult, keep, err := p.p.PostProcess(client.Ui(), client.Artifact())
|
2013-12-09 18:44:00 -05:00
|
|
|
if err == nil && artifactResult != nil {
|
2013-12-09 22:07:36 -05:00
|
|
|
streamId = p.mux.NextId()
|
2013-12-19 20:35:32 -05:00
|
|
|
server := newServerWithMux(p.mux, streamId)
|
2013-12-09 22:07:36 -05:00
|
|
|
server.RegisterArtifact(artifactResult)
|
|
|
|
go server.Serve()
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
*reply = PostProcessorProcessResponse{
|
2014-04-28 18:18:31 -04:00
|
|
|
Err: NewBasicError(err),
|
2013-12-09 22:07:36 -05:00
|
|
|
Keep: keep,
|
|
|
|
StreamId: streamId,
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|