Commit 88d50889 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

packer/plugin: client to Client so it can be used outside

parent 7fe98e50
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
type cmdBuilder struct { type cmdBuilder struct {
builder packer.Builder builder packer.Builder
client *client client *Client
} }
func (b *cmdBuilder) Prepare(config interface{}) error { func (b *cmdBuilder) Prepare(config interface{}) error {
......
...@@ -18,9 +18,12 @@ import ( ...@@ -18,9 +18,12 @@ import (
// This is a slice of the "managed" clients which are cleaned up when // This is a slice of the "managed" clients which are cleaned up when
// calling Cleanup // 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 config *ClientConfig
exited bool exited bool
doneLogging bool doneLogging bool
...@@ -81,7 +84,7 @@ func CleanupClients() { ...@@ -81,7 +84,7 @@ func CleanupClients() {
// the client is a managed client (created with NewManagedClient) you // the client is a managed client (created with NewManagedClient) you
// can just call CleanupClients at the end of your program and they will // can just call CleanupClients at the end of your program and they will
// be properly cleaned. // be properly cleaned.
func NewClient(config *ClientConfig) (c *client) { func NewClient(config *ClientConfig) (c *Client) {
if config.MinPort == 0 && config.MaxPort == 0 { if config.MinPort == 0 && config.MaxPort == 0 {
config.MinPort = 10000 config.MinPort = 10000
config.MaxPort = 25000 config.MaxPort = 25000
...@@ -91,7 +94,7 @@ func NewClient(config *ClientConfig) (c *client) { ...@@ -91,7 +94,7 @@ func NewClient(config *ClientConfig) (c *client) {
config.StartTimeout = 1 * time.Minute config.StartTimeout = 1 * time.Minute
} }
c = &client{config: config} c = &Client{config: config}
if config.Managed { if config.Managed {
managedClients = append(managedClients, c) managedClients = append(managedClients, c)
} }
...@@ -100,13 +103,13 @@ func NewClient(config *ClientConfig) (c *client) { ...@@ -100,13 +103,13 @@ func NewClient(config *ClientConfig) (c *client) {
} }
// Tells whether or not the underlying process has exited. // Tells whether or not the underlying process has exited.
func (c *client) Exited() bool { func (c *Client) Exited() bool {
return c.exited return c.exited
} }
// Returns a builder implementation that is communicating over this // Returns a builder implementation that is communicating over this
// client. If the client hasn't been started, this will start it. // 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() client, err := c.rpcClient()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -117,7 +120,7 @@ func (c *client) Builder() (packer.Builder, error) { ...@@ -117,7 +120,7 @@ func (c *client) Builder() (packer.Builder, error) {
// Returns a command implementation that is communicating over this // Returns a command implementation that is communicating over this
// client. If the client hasn't been started, this will start it. // 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() client, err := c.rpcClient()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -128,7 +131,7 @@ func (c *client) Command() (packer.Command, error) { ...@@ -128,7 +131,7 @@ func (c *client) Command() (packer.Command, error) {
// Returns a hook implementation that is communicating over this // Returns a hook implementation that is communicating over this
// client. If the client hasn't been started, this will start it. // 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() client, err := c.rpcClient()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -139,7 +142,7 @@ func (c *client) Hook() (packer.Hook, error) { ...@@ -139,7 +142,7 @@ func (c *client) Hook() (packer.Hook, error) {
// Returns a provisioner implementation that is communicating over this // Returns a provisioner implementation that is communicating over this
// client. If the client hasn't been started, this will start it. // 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() client, err := c.rpcClient()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -154,7 +157,7 @@ func (c *client) Provisioner() (packer.Provisioner, error) { ...@@ -154,7 +157,7 @@ func (c *client) Provisioner() (packer.Provisioner, error) {
// This method blocks until the process successfully exits. // This method blocks until the process successfully exits.
// //
// This method can safely be called multiple times. // This method can safely be called multiple times.
func (c *client) Kill() { func (c *Client) Kill() {
cmd := c.config.Cmd cmd := c.config.Cmd
if cmd.Process == nil { if cmd.Process == nil {
...@@ -182,7 +185,7 @@ func (c *client) Kill() { ...@@ -182,7 +185,7 @@ func (c *client) Kill() {
// This method is safe to call multiple times. Subsequent calls have no effect. // 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 // Once a client has been started once, it cannot be started again, even if
// it was killed. // it was killed.
func (c *client) Start() (address string, err error) { func (c *Client) Start() (address string, err error) {
c.l.Lock() c.l.Lock()
defer c.l.Unlock() defer c.l.Unlock()
...@@ -269,7 +272,7 @@ func (c *client) Start() (address string, err error) { ...@@ -269,7 +272,7 @@ func (c *client) Start() (address string, err error) {
return return
} }
func (c *client) logStderr(buf *bytes.Buffer) { func (c *Client) logStderr(buf *bytes.Buffer) {
for done := false; !done; { for done := false; !done; {
if c.Exited() { if c.Exited() {
done = true done = true
...@@ -291,7 +294,7 @@ func (c *client) logStderr(buf *bytes.Buffer) { ...@@ -291,7 +294,7 @@ func (c *client) logStderr(buf *bytes.Buffer) {
c.doneLogging = true c.doneLogging = true
} }
func (c *client) rpcClient() (*rpc.Client, error) { func (c *Client) rpcClient() (*rpc.Client, error) {
address, err := c.Start() address, err := c.Start()
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
type cmdCommand struct { type cmdCommand struct {
command packer.Command command packer.Command
client *client client *Client
} }
func (c *cmdCommand) Help() (result string) { func (c *cmdCommand) Help() (result string) {
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
type cmdHook struct { type cmdHook struct {
hook packer.Hook hook packer.Hook
client *client client *Client
} }
func (c *cmdHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) { func (c *cmdHook) Run(name string, ui packer.Ui, comm packer.Communicator, data interface{}) {
......
...@@ -7,7 +7,7 @@ import ( ...@@ -7,7 +7,7 @@ import (
type cmdProvisioner struct { type cmdProvisioner struct {
p packer.Provisioner p packer.Provisioner
client *client client *Client
} }
func (c *cmdProvisioner) Prepare(configs ...interface{}) error { func (c *cmdProvisioner) Prepare(configs ...interface{}) error {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment