From 88d508893df4124d58347609a3db7fceb450ef00 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 11 Jun 2013 11:03:36 -0700 Subject: [PATCH] packer/plugin: client to Client so it can be used outside --- packer/plugin/builder.go | 2 +- packer/plugin/client.go | 29 ++++++++++++++++------------- packer/plugin/command.go | 2 +- packer/plugin/hook.go | 2 +- packer/plugin/provisioner.go | 2 +- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/packer/plugin/builder.go b/packer/plugin/builder.go index 00da4c99b..cb37b5bc2 100644 --- a/packer/plugin/builder.go +++ b/packer/plugin/builder.go @@ -7,7 +7,7 @@ import ( type cmdBuilder struct { builder packer.Builder - client *client + client *Client } func (b *cmdBuilder) Prepare(config interface{}) error { diff --git a/packer/plugin/client.go b/packer/plugin/client.go index 48cc2060d..8defc5a49 100644 --- a/packer/plugin/client.go +++ b/packer/plugin/client.go @@ -18,9 +18,12 @@ import ( // This is a slice of the "managed" clients which are cleaned up when // calling Cleanup -var managedClients = make([]*client, 0, 5) +var managedClients = make([]*Client, 0, 5) -type client struct { +// Client handles the lifecycle of a plugin application, determining its +// RPC address, and returning various types of packer interface implementations +// across the multi-process communication layer. +type Client struct { config *ClientConfig exited bool doneLogging bool @@ -81,7 +84,7 @@ func CleanupClients() { // the client is a managed client (created with NewManagedClient) you // can just call CleanupClients at the end of your program and they will // be properly cleaned. -func NewClient(config *ClientConfig) (c *client) { +func NewClient(config *ClientConfig) (c *Client) { if config.MinPort == 0 && config.MaxPort == 0 { config.MinPort = 10000 config.MaxPort = 25000 @@ -91,7 +94,7 @@ func NewClient(config *ClientConfig) (c *client) { config.StartTimeout = 1 * time.Minute } - c = &client{config: config} + c = &Client{config: config} if config.Managed { managedClients = append(managedClients, c) } @@ -100,13 +103,13 @@ func NewClient(config *ClientConfig) (c *client) { } // Tells whether or not the underlying process has exited. -func (c *client) Exited() bool { +func (c *Client) Exited() bool { return c.exited } // Returns a builder implementation that is communicating over this // client. If the client hasn't been started, this will start it. -func (c *client) Builder() (packer.Builder, error) { +func (c *Client) Builder() (packer.Builder, error) { client, err := c.rpcClient() if err != nil { return nil, err @@ -117,7 +120,7 @@ func (c *client) Builder() (packer.Builder, error) { // Returns a command implementation that is communicating over this // client. If the client hasn't been started, this will start it. -func (c *client) Command() (packer.Command, error) { +func (c *Client) Command() (packer.Command, error) { client, err := c.rpcClient() if err != nil { return nil, err @@ -128,7 +131,7 @@ func (c *client) Command() (packer.Command, error) { // Returns a hook implementation that is communicating over this // client. If the client hasn't been started, this will start it. -func (c *client) Hook() (packer.Hook, error) { +func (c *Client) Hook() (packer.Hook, error) { client, err := c.rpcClient() if err != nil { return nil, err @@ -139,7 +142,7 @@ func (c *client) Hook() (packer.Hook, error) { // Returns a provisioner implementation that is communicating over this // client. If the client hasn't been started, this will start it. -func (c *client) Provisioner() (packer.Provisioner, error) { +func (c *Client) Provisioner() (packer.Provisioner, error) { client, err := c.rpcClient() if err != nil { return nil, err @@ -154,7 +157,7 @@ func (c *client) Provisioner() (packer.Provisioner, error) { // This method blocks until the process successfully exits. // // This method can safely be called multiple times. -func (c *client) Kill() { +func (c *Client) Kill() { cmd := c.config.Cmd if cmd.Process == nil { @@ -182,7 +185,7 @@ func (c *client) Kill() { // This method is safe to call multiple times. Subsequent calls have no effect. // Once a client has been started once, it cannot be started again, even if // it was killed. -func (c *client) Start() (address string, err error) { +func (c *Client) Start() (address string, err error) { c.l.Lock() defer c.l.Unlock() @@ -269,7 +272,7 @@ func (c *client) Start() (address string, err error) { return } -func (c *client) logStderr(buf *bytes.Buffer) { +func (c *Client) logStderr(buf *bytes.Buffer) { for done := false; !done; { if c.Exited() { done = true @@ -291,7 +294,7 @@ func (c *client) logStderr(buf *bytes.Buffer) { c.doneLogging = true } -func (c *client) rpcClient() (*rpc.Client, error) { +func (c *Client) rpcClient() (*rpc.Client, error) { address, err := c.Start() if err != nil { return nil, err diff --git a/packer/plugin/command.go b/packer/plugin/command.go index 13c540ed7..4d6ccc490 100644 --- a/packer/plugin/command.go +++ b/packer/plugin/command.go @@ -7,7 +7,7 @@ import ( type cmdCommand struct { command packer.Command - client *client + client *Client } func (c *cmdCommand) Help() (result string) { diff --git a/packer/plugin/hook.go b/packer/plugin/hook.go index c90d4645a..61695d098 100644 --- a/packer/plugin/hook.go +++ b/packer/plugin/hook.go @@ -7,7 +7,7 @@ import ( type cmdHook struct { hook packer.Hook - client *client + client *Client } func (c *cmdHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) { diff --git a/packer/plugin/provisioner.go b/packer/plugin/provisioner.go index 92ad848a4..2bfa72405 100644 --- a/packer/plugin/provisioner.go +++ b/packer/plugin/provisioner.go @@ -7,7 +7,7 @@ import ( type cmdProvisioner struct { p packer.Provisioner - client *client + client *Client } func (c *cmdProvisioner) Prepare(configs ...interface{}) error {