2013-06-18 16:44:57 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"net/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An implementation of packer.PostProcessor where the PostProcessor is actually
|
|
|
|
// executed over an RPC connection.
|
|
|
|
type postProcessor struct {
|
|
|
|
client *rpc.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostProcessorServer wraps a packer.PostProcessor implementation and makes it
|
|
|
|
// exportable as part of a Golang RPC server.
|
|
|
|
type PostProcessorServer struct {
|
|
|
|
p packer.PostProcessor
|
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
type PostProcessorConfigureArgs struct {
|
|
|
|
Configs []interface{}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:44:57 -04:00
|
|
|
type PostProcessorProcessResponse struct {
|
2013-06-18 19:29:29 -04:00
|
|
|
Err error
|
2013-07-01 14:30:39 -04:00
|
|
|
Keep bool
|
2013-06-18 16:44:57 -04:00
|
|
|
RPCAddress string
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostProcessor(client *rpc.Client) *postProcessor {
|
|
|
|
return &postProcessor{client}
|
|
|
|
}
|
2013-07-01 17:59:23 -04:00
|
|
|
func (p *postProcessor) Configure(raw ...interface{}) (err error) {
|
|
|
|
args := &PostProcessorConfigureArgs{Configs: raw}
|
|
|
|
if cerr := p.client.Call("PostProcessor.Configure", args, &err); 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-06-18 16:44:57 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterArtifact(server, a)
|
2013-06-18 23:38:21 -04:00
|
|
|
RegisterUi(server, ui)
|
2013-06-18 16:44:57 -04:00
|
|
|
|
|
|
|
var response PostProcessorProcessResponse
|
|
|
|
if err := p.client.Call("PostProcessor.PostProcess", serveSingleConn(server), &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
|
|
|
}
|
|
|
|
|
|
|
|
if response.RPCAddress == "" {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-09-18 20:15:48 -04:00
|
|
|
client, err := rpcDial(response.RPCAddress)
|
2013-06-18 16:44:57 -04:00
|
|
|
if err != nil {
|
2013-07-01 14:30:39 -04:00
|
|
|
return nil, false, err
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 14:30:39 -04:00
|
|
|
return Artifact(client), response.Keep, nil
|
2013-06-18 16:44:57 -04:00
|
|
|
}
|
|
|
|
|
2013-07-01 17:59:23 -04:00
|
|
|
func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply *error) error {
|
|
|
|
*reply = p.p.Configure(args.Configs...)
|
2013-06-19 00:04:33 -04:00
|
|
|
if *reply != nil {
|
|
|
|
*reply = NewBasicError(*reply)
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:44:57 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostProcessorServer) PostProcess(address string, reply *PostProcessorProcessResponse) error {
|
2013-09-18 20:15:48 -04:00
|
|
|
client, err := rpcDial(address)
|
2013-06-18 16:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
responseAddress := ""
|
|
|
|
|
2013-07-01 14:30:39 -04:00
|
|
|
artifact, keep, err := p.p.PostProcess(&Ui{client}, Artifact(client))
|
2013-06-18 16:44:57 -04:00
|
|
|
if err == nil && artifact != nil {
|
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterArtifact(server, artifact)
|
|
|
|
responseAddress = serveSingleConn(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = NewBasicError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*reply = PostProcessorProcessResponse{
|
2013-06-18 19:29:29 -04:00
|
|
|
Err: err,
|
2013-07-01 14:30:39 -04:00
|
|
|
Keep: keep,
|
2013-06-18 16:44:57 -04:00
|
|
|
RPCAddress: responseAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|