Class: Ridley::HostCommander

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Logging
Defined in:
lib/ridley-connectors/host_commander.rb

Constant Summary collapse

PORT_CHECK_TIMEOUT =
3
RETRY_COUNT =
3
DEFAULT_WINDOWS_CONNECTOR =
"winrm"
DEFAULT_LINUX_CONNECTOR =
"ssh"
VALID_CONNECTORS =
[ DEFAULT_WINDOWS_CONNECTOR, DEFAULT_LINUX_CONNECTOR ]
CONNECTOR_PORT_ERRORS =
[
  Errno::ETIMEDOUT, Timeout::Error, SocketError, 
  Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::EADDRNOTAVAIL,
  Resolv::ResolvError
]

Instance Method Summary collapse

Constructor Details

#initialize(connector_pool_size = nil) ⇒ HostCommander

Returns a new instance of HostCommander.



46
47
48
49
50
# File 'lib/ridley-connectors/host_commander.rb', line 46

def initialize(connector_pool_size=nil)
  connector_pool_size ||= 1
  @connector_registry   = Celluloid::Registry.new
  @connector_supervisor = ConnectorSupervisor.new_link(@connector_registry, connector_pool_size)
end

Instance Method Details

#bootstrap(host, options = {}) ⇒ HostConnector::Response

Bootstrap a node

Parameters:

  • host (String)

    the host to perform the action on

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ssh (Hash)
    • :user (String) a shell user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of key(s) to authenticate the ssh user with instead of a password

    • :timeout (Float) timeout value for SSH bootstrap (5.0)

    • :sudo (Boolean) run as sudo

  • :winrm (Hash)
    • :user (String) a user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the user that will perform the bootstrap (required)

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

Returns:



93
94
95
# File 'lib/ridley-connectors/host_commander.rb', line 93

def bootstrap(host, options = {})
  execute(__method__, host, options)
end

#chef_client(host, options = {}) ⇒ HostConnector::Response

Perform a chef client run on a node

Parameters:

  • host (String)

    the host to perform the action on

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ssh (Hash)
    • :user (String) a shell user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of key(s) to authenticate the ssh user with instead of a password

    • :timeout (Float) timeout value for SSH bootstrap (5.0)

    • :sudo (Boolean) run as sudo

  • :winrm (Hash)
    • :user (String) a user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the user that will perform the bootstrap (required)

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :connector (String)

    a connector type to prefer

Returns:



116
117
118
# File 'lib/ridley-connectors/host_commander.rb', line 116

def chef_client(host, options = {})
  execute(__method__, host, options)
end

#connector_for(host, options = {}) ⇒ HostConnector::SSH, ...

Finds and returns the best HostConnector for a given host

Parameters:

  • host (String)

    the host to attempt to connect to

  • block (Proc)

    an optional block that is yielded the best HostConnector

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ssh (Hash)
    • :port (Fixnum) the ssh port to connect on the node the bootstrap will be performed on (22)

    • :timeout (Float) [5.0] timeout value for testing SSH connection

  • :winrm (Hash)
    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :connector (String)

    a connector type to prefer

Returns:



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ridley-connectors/host_commander.rb', line 212

def connector_for(host, options = {})
  options[:ssh]          ||= Hash.new
  options[:winrm]        ||= Hash.new
  options[:ssh][:port]   ||= HostConnector::SSH::DEFAULT_PORT
  options[:winrm][:port] ||= HostConnector::WinRM::DEFAULT_PORT
  options[:retries]      ||= RETRY_COUNT

  connector = options[:connector]

  if !VALID_CONNECTORS.include?(connector)
    log.warn { "Received connector '#{connector}' is not one of #{VALID_CONNECTORS}. Checking default connectors..." }
    connector = nil
  end

  if (connector == DEFAULT_WINDOWS_CONNECTOR || connector.nil?) && connector_port_open?(host, options[:winrm][:port], options[:winrm][:timeout], options[:retries])
    options.delete(:ssh)
    winrm
  elsif (connector == DEFAULT_LINUX_CONNECTOR || connector.nil?) && connector_port_open?(host, options[:ssh][:port], options[:ssh][:timeout], options[:retries])
    options.delete(:winrm)
    ssh
  else
    nil
  end
end

#put_secret(host, secret, options = {}) ⇒ HostConnector::Response

Write your encrypted data bag secret on a node

Parameters:

  • host (String)

    the host to perform the action on

  • secret (String)

    your organization’s encrypted data bag secret

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ssh (Hash)
    • :user (String) a shell user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of key(s) to authenticate the ssh user with instead of a password

    • :timeout (Float) timeout value for SSH bootstrap (5.0)

    • :sudo (Boolean) run as sudo

  • :winrm (Hash)
    • :user (String) a user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the user that will perform the bootstrap (required)

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :connector (String)

    a connector type to prefer

Returns:



141
142
143
# File 'lib/ridley-connectors/host_commander.rb', line 141

def put_secret(host, secret, options = {})
  execute(__method__, host, secret, options)
end

#ruby_script(host, command_lines, options = {}) ⇒ HostConnector::Response

Execute line(s) of Ruby code on a node using Chef’s embedded Ruby

Parameters:

  • host (String)

    the host to perform the action on

  • command_lines (Array<String>)

    An Array of lines of the command to be executed

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ssh (Hash)
    • :user (String) a shell user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of key(s) to authenticate the ssh user with instead of a password

    • :timeout (Float) timeout value for SSH bootstrap (5.0)

    • :sudo (Boolean) run as sudo

  • :winrm (Hash)
    • :user (String) a user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the user that will perform the bootstrap (required)

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :connector (String)

    a connector type to prefer

Returns:



166
167
168
# File 'lib/ridley-connectors/host_commander.rb', line 166

def ruby_script(host, command_lines, options = {})
  execute(__method__, host, command_lines, options)
end

#run(host, command, options = {}) ⇒ HostConnector::Response

Execute a shell command on a node

Parameters:

  • host (String)

    the host to perform the action on

  • command (String)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :ssh (Hash)
    • :user (String) a shell user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of key(s) to authenticate the ssh user with instead of a password

    • :timeout (Float) timeout value for SSH bootstrap (5.0)

    • :sudo (Boolean) run as sudo

  • :winrm (Hash)
    • :user (String) a user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the user that will perform the bootstrap (required)

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :connector (String)

    a connector type to prefer

Returns:



72
73
74
# File 'lib/ridley-connectors/host_commander.rb', line 72

def run(host, command, options = {})
  execute(__method__, host, command, options)
end

#uninstall_chef(host, options = {}) ⇒ HostConnector::Response

Uninstall Chef from a node

Parameters:

  • host (String)

    the host to perform the action on

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :skip_chef (Boolena) — default: false

    skip removal of the Chef package and the contents of the installation directory. Setting this to true will only remove any data and configurations generated by running Chef client.

  • :ssh (Hash)
    • :user (String) a shell user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the shell user that will perform the bootstrap

    • :keys (Array, String) an array of key(s) to authenticate the ssh user with instead of a password

    • :timeout (Float) timeout value for SSH bootstrap (5.0)

    • :sudo (Boolean) run as sudo (true)

  • :winrm (Hash)
    • :user (String) a user that will login to each node and perform the bootstrap command on

    • :password (String) the password for the user that will perform the bootstrap (required)

    • :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985)

  • :connector (String)

    a connector type to prefer

Returns:



193
194
195
# File 'lib/ridley-connectors/host_commander.rb', line 193

def uninstall_chef(host, options = {})
  execute(__method__, host, options)
end