class SSHake::BaseSession
Attributes
id[R]
An ID for this session
@return [String]
logger[RW]
A logger for this session
@return [Logger, nil]
raise_on_error[RW]
Specify the default behaviour for raising erors
@return [Boolean]
Public Class Methods
new(*_args)
click to toggle source
# File lib/sshake/base_session.rb, line 25 def initialize(*_args) @id = SecureRandom.hex(4) end
Public Instance Methods
connect()
click to toggle source
Connect to the SSH server
@return [void]
# File lib/sshake/base_session.rb, line 32 def connect raise 'Override #connect in sub-sessions' end
connected?()
click to toggle source
Is there an established SSH connection
@return [Boolean]
# File lib/sshake/base_session.rb, line 39 def connected? raise 'Override #connected? in sub-sessions' end
disconnect()
click to toggle source
Disconnect the underlying SSH connection
@return [void]
# File lib/sshake/base_session.rb, line 46 def disconnect raise 'Override #disconnect in sub-sessions' end
execute(_commands, _options = nil)
click to toggle source
Execute a command
# File lib/sshake/base_session.rb, line 57 def execute(_commands, _options = nil) raise 'Override #execute in sub-sessions' end
kill!()
click to toggle source
Kill the underlying connection
# File lib/sshake/base_session.rb, line 51 def kill! raise 'Override #kill! in sub-sessions' end
write_data(_path, _data, _options = nil)
click to toggle source
# File lib/sshake/base_session.rb, line 61 def write_data(_path, _data, _options = nil) raise 'Override #write_data in sub-sessions' end
Private Instance Methods
add_sudo_to_commands_array(commands, user, password = nil)
click to toggle source
# File lib/sshake/base_session.rb, line 67 def add_sudo_to_commands_array(commands, user, password = nil) sudo_prefix = "sudo -u #{user}" unless password.nil? sudo_prefix += " --stdin -p '[sshake-sudo-password]: ' " end commands.map do |command| "#{sudo_prefix} #{command}" end end
create_options(hash, block)
click to toggle source
# File lib/sshake/base_session.rb, line 77 def create_options(hash, block) if block && hash raise Error, 'You cannot provide a block and options' end if block ExecutionOptions.from_block(&block) elsif hash.is_a?(Hash) ExecutionOptions.from_hash(hash) else ExecutionOptions.new end end
handle_response(response, options)
click to toggle source
# File lib/sshake/base_session.rb, line 114 def handle_response(response, options) if !response.success? && ((options.raise_on_error.nil? && @raise_on_error) || options.raise_on_error?) raise ExecutionError, response end response end
log(type, text, _options = {})
click to toggle source
# File lib/sshake/base_session.rb, line 91 def log(type, text, _options = {}) logger = @logger || SSHake.logger return unless logger prefix = "[#{@id}] [#{@host}] " text.split(/\n/).each do |line| logger.send(type, prefix + line) end end
prepare_commands(commands, execution_options, **options)
click to toggle source
# File lib/sshake/base_session.rb, line 102 def prepare_commands(commands, execution_options, **options) commands = [commands] unless commands.is_a?(Array) # Map sudo onto command if execution_options.sudo_user && options[:add_sudo] != false commands = add_sudo_to_commands_array(commands, execution_options.sudo_user, execution_options.sudo_password) end # Construct a full command string to execute commands.join(' && ') end