github.com/jerryclinesmith/packer@v0.3.7/packer/rpc/post_processor.go (about)

     1  package rpc
     2  
     3  import (
     4  	"github.com/mitchellh/packer/packer"
     5  	"net/rpc"
     6  )
     7  
     8  // An implementation of packer.PostProcessor where the PostProcessor is actually
     9  // executed over an RPC connection.
    10  type postProcessor struct {
    11  	client *rpc.Client
    12  }
    13  
    14  // PostProcessorServer wraps a packer.PostProcessor implementation and makes it
    15  // exportable as part of a Golang RPC server.
    16  type PostProcessorServer struct {
    17  	p packer.PostProcessor
    18  }
    19  
    20  type PostProcessorConfigureArgs struct {
    21  	Configs []interface{}
    22  }
    23  
    24  type PostProcessorProcessResponse struct {
    25  	Err        error
    26  	Keep       bool
    27  	RPCAddress string
    28  }
    29  
    30  func PostProcessor(client *rpc.Client) *postProcessor {
    31  	return &postProcessor{client}
    32  }
    33  func (p *postProcessor) Configure(raw ...interface{}) (err error) {
    34  	args := &PostProcessorConfigureArgs{Configs: raw}
    35  	if cerr := p.client.Call("PostProcessor.Configure", args, &err); cerr != nil {
    36  		err = cerr
    37  	}
    38  
    39  	return
    40  }
    41  
    42  func (p *postProcessor) PostProcess(ui packer.Ui, a packer.Artifact) (packer.Artifact, bool, error) {
    43  	server := rpc.NewServer()
    44  	RegisterArtifact(server, a)
    45  	RegisterUi(server, ui)
    46  
    47  	var response PostProcessorProcessResponse
    48  	if err := p.client.Call("PostProcessor.PostProcess", serveSingleConn(server), &response); err != nil {
    49  		return nil, false, err
    50  	}
    51  
    52  	if response.Err != nil {
    53  		return nil, false, response.Err
    54  	}
    55  
    56  	if response.RPCAddress == "" {
    57  		return nil, false, nil
    58  	}
    59  
    60  	client, err := rpc.Dial("tcp", response.RPCAddress)
    61  	if err != nil {
    62  		return nil, false, err
    63  	}
    64  
    65  	return Artifact(client), response.Keep, nil
    66  }
    67  
    68  func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply *error) error {
    69  	*reply = p.p.Configure(args.Configs...)
    70  	if *reply != nil {
    71  		*reply = NewBasicError(*reply)
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  func (p *PostProcessorServer) PostProcess(address string, reply *PostProcessorProcessResponse) error {
    78  	client, err := rpc.Dial("tcp", address)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	responseAddress := ""
    84  
    85  	artifact, keep, err := p.p.PostProcess(&Ui{client}, Artifact(client))
    86  	if err == nil && artifact != nil {
    87  		server := rpc.NewServer()
    88  		RegisterArtifact(server, artifact)
    89  		responseAddress = serveSingleConn(server)
    90  	}
    91  
    92  	if err != nil {
    93  		err = NewBasicError(err)
    94  	}
    95  
    96  	*reply = PostProcessorProcessResponse{
    97  		Err:        err,
    98  		Keep:       keep,
    99  		RPCAddress: responseAddress,
   100  	}
   101  
   102  	return nil
   103  }