Class: Kitchen::Transport::Base::Connection

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/kitchen/transport/base.rb

Overview

A Connection instance can be generated and re-generated, given new connection details such as connection port, hostname, credentials, etc. This object is responsible for carrying out the actions on the remote host such as executing commands, transferring files, etc.

Author:

Instance Method Summary collapse

Methods included from Logging

#banner, #debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(options = {}) {|self| ... } ⇒ Connection

Create a new Connection instance.

Parameters:

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

    connection options

Yields:

  • (self)

    yields itself for block-style invocation



96
97
98
99
100
# File 'lib/kitchen/transport/base.rb', line 96

def initialize(options = {})
  init_options(options)

  yield self if block_given?
end

Instance Method Details

#closeObject

Closes the session connection, if it is still active.



103
104
105
# File 'lib/kitchen/transport/base.rb', line 103

def close
  # this method may be left unimplemented if that is applicable
end

#download(remotes, local) ⇒ Object

Download remote files or directories to local host.

Parameters:

  • remotes (Array<String>)

    paths to remote files or directories

  • local (String)

    path to local destination. If ‘local` is an existing directory, `remote` will be downloaded into the directory using its original name

Raises:

  • (TransportFailed)

    if the files could not all be downloaded successfully, which may vary by implementation



175
176
177
# File 'lib/kitchen/transport/base.rb', line 175

def download(remotes, local) # rubocop:disable Lint/UnusedMethodArgument
  raise ClientError, "#{self.class}#download must be implemented"
end

#execute(command) ⇒ Object

Execute a command on the remote host.

Parameters:

  • command (String)

    command string to execute

Raises:

  • (TransportFailed)

    if the command does not exit successfully, which may vary by implementation



112
113
114
# File 'lib/kitchen/transport/base.rb', line 112

def execute(command)
  raise ClientError, "#{self.class}#execute must be implemented"
end

#execute_with_retry(command, retryable_exit_codes = [], max_retries = 1, wait_time = 30) ⇒ Object

Execute a command on the remote host and retry

Parameters:

  • command (String)

    command string to execute

  • retryable_exit_codes (Array) (defaults to: [])

    Array of exit codes to retry against

  • max_retries (Fixnum) (defaults to: 1)

    maximum number of retry attempts

  • wait_time (Fixnum) (defaults to: 30)

    number of seconds to wait before retrying command

Raises:

  • (TransportFailed)

    if the command does not exit successfully, which may vary by implementation



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/kitchen/transport/base.rb', line 124

def execute_with_retry(command, retryable_exit_codes = [], max_retries = 1, wait_time = 30)
  tries = 0
  begin
    tries += 1
    debug("Attempting to execute command - try #{tries} of #{max_retries}.")
    execute(command)
  rescue Kitchen::Transport::TransportFailed => e
    if retry?(tries, max_retries, retryable_exit_codes, e.exit_code)
      close
      sleep wait_time
      retry
    else
      raise e
    end
  end
end

#login_commandLoginCommand

Builds a LoginCommand which can be used to open an interactive session on the remote host.

Returns:

  • (LoginCommand)

    an object containing the array of command line tokens and exec options to be used in a fork/exec

Raises:



153
154
155
# File 'lib/kitchen/transport/base.rb', line 153

def 
  raise ActionFailed, "Remote login not supported in #{self.class}."
end

#retry?(current_try, max_retries, retryable_exit_codes, exit_code) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
145
# File 'lib/kitchen/transport/base.rb', line 141

def retry?(current_try, max_retries, retryable_exit_codes, exit_code)
  current_try <= max_retries &&
    !retryable_exit_codes.nil? &&
    retryable_exit_codes.flatten.include?(exit_code)
end

#upload(locals, remote) ⇒ Object

Uploads local files or directories to remote host.

Parameters:

  • locals (Array<String>)

    paths to local files or directories

  • remote (String)

    path to remote destination

Raises:

  • (TransportFailed)

    if the files could not all be uploaded successfully, which may vary by implementation



163
164
165
# File 'lib/kitchen/transport/base.rb', line 163

def upload(locals, remote) # rubocop:disable Lint/UnusedMethodArgument
  raise ClientError, "#{self.class}#upload must be implemented"
end

#wait_until_readyObject

Block and return only when the remote host is prepared and ready to execute command and upload files. The semantics and details will vary by implementation, but a round trip through the hosted service is preferred to simply waiting on a socket to become available.



184
185
186
# File 'lib/kitchen/transport/base.rb', line 184

def wait_until_ready
  # this method may be left unimplemented if that is applicable
end