github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/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 mux *MuxConn 13 } 14 15 // PostProcessorServer wraps a packer.PostProcessor implementation and makes it 16 // exportable as part of a Golang RPC server. 17 type PostProcessorServer struct { 18 client *rpc.Client 19 mux *MuxConn 20 p packer.PostProcessor 21 } 22 23 type PostProcessorConfigureArgs struct { 24 Configs []interface{} 25 } 26 27 type PostProcessorProcessResponse struct { 28 Err error 29 Keep bool 30 StreamId uint32 31 } 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 nextId := p.mux.NextId() 44 server := newServerWithMux(p.mux, nextId) 45 server.RegisterArtifact(a) 46 server.RegisterUi(ui) 47 go server.Serve() 48 49 var response PostProcessorProcessResponse 50 if err := p.client.Call("PostProcessor.PostProcess", nextId, &response); err != nil { 51 return nil, false, err 52 } 53 54 if response.Err != nil { 55 return nil, false, response.Err 56 } 57 58 if response.StreamId == 0 { 59 return nil, false, nil 60 } 61 62 client, err := newClientWithMux(p.mux, response.StreamId) 63 if err != nil { 64 return nil, false, err 65 } 66 67 return client.Artifact(), response.Keep, nil 68 } 69 70 func (p *PostProcessorServer) Configure(args *PostProcessorConfigureArgs, reply *error) error { 71 *reply = p.p.Configure(args.Configs...) 72 if *reply != nil { 73 *reply = NewBasicError(*reply) 74 } 75 76 return nil 77 } 78 79 func (p *PostProcessorServer) PostProcess(streamId uint32, reply *PostProcessorProcessResponse) error { 80 client, err := newClientWithMux(p.mux, streamId) 81 if err != nil { 82 return NewBasicError(err) 83 } 84 defer client.Close() 85 86 streamId = 0 87 artifactResult, keep, err := p.p.PostProcess(client.Ui(), client.Artifact()) 88 if err == nil && artifactResult != nil { 89 streamId = p.mux.NextId() 90 server := newServerWithMux(p.mux, streamId) 91 server.RegisterArtifact(artifactResult) 92 go server.Serve() 93 } 94 95 if err != nil { 96 err = NewBasicError(err) 97 } 98 99 *reply = PostProcessorProcessResponse{ 100 Err: err, 101 Keep: keep, 102 StreamId: streamId, 103 } 104 105 return nil 106 }