github.com/rothwerx/packer@v0.9.0/packer/rpc/client.go (about) 1 package rpc 2 3 import ( 4 "io" 5 "log" 6 "net/rpc" 7 8 "github.com/mitchellh/packer/packer" 9 "github.com/ugorji/go/codec" 10 ) 11 12 // Client is the client end that communicates with a Packer RPC server. 13 // Establishing a connection is up to the user, the Client can just 14 // communicate over any ReadWriteCloser. 15 type Client struct { 16 mux *muxBroker 17 client *rpc.Client 18 closeMux bool 19 } 20 21 func NewClient(rwc io.ReadWriteCloser) (*Client, error) { 22 mux, err := newMuxBrokerClient(rwc) 23 if err != nil { 24 return nil, err 25 } 26 go mux.Run() 27 28 result, err := newClientWithMux(mux, 0) 29 if err != nil { 30 mux.Close() 31 return nil, err 32 } 33 34 result.closeMux = true 35 return result, err 36 } 37 38 func newClientWithMux(mux *muxBroker, streamId uint32) (*Client, error) { 39 clientConn, err := mux.Dial(streamId) 40 if err != nil { 41 return nil, err 42 } 43 44 h := &codec.MsgpackHandle{ 45 RawToString: true, 46 WriteExt: true, 47 } 48 clientCodec := codec.GoRpc.ClientCodec(clientConn, h) 49 50 return &Client{ 51 mux: mux, 52 client: rpc.NewClientWithCodec(clientCodec), 53 closeMux: false, 54 }, nil 55 } 56 57 func (c *Client) Close() error { 58 if err := c.client.Close(); err != nil { 59 return err 60 } 61 62 if c.closeMux { 63 log.Printf("[WARN] Client is closing mux") 64 return c.mux.Close() 65 } 66 67 return nil 68 } 69 70 func (c *Client) Artifact() packer.Artifact { 71 return &artifact{ 72 client: c.client, 73 endpoint: DefaultArtifactEndpoint, 74 } 75 } 76 77 func (c *Client) Build() packer.Build { 78 return &build{ 79 client: c.client, 80 mux: c.mux, 81 } 82 } 83 84 func (c *Client) Builder() packer.Builder { 85 return &builder{ 86 client: c.client, 87 mux: c.mux, 88 } 89 } 90 91 func (c *Client) Cache() packer.Cache { 92 return &cache{ 93 client: c.client, 94 } 95 } 96 97 func (c *Client) Communicator() packer.Communicator { 98 return &communicator{ 99 client: c.client, 100 mux: c.mux, 101 } 102 } 103 104 func (c *Client) Hook() packer.Hook { 105 return &hook{ 106 client: c.client, 107 mux: c.mux, 108 } 109 } 110 111 func (c *Client) PostProcessor() packer.PostProcessor { 112 return &postProcessor{ 113 client: c.client, 114 mux: c.mux, 115 } 116 } 117 118 func (c *Client) Provisioner() packer.Provisioner { 119 return &provisioner{ 120 client: c.client, 121 mux: c.mux, 122 } 123 } 124 125 func (c *Client) Ui() packer.Ui { 126 return &Ui{ 127 client: c.client, 128 endpoint: DefaultUiEndpoint, 129 } 130 }