github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/client.go (about)

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