Class: Dkdeploy::InteractionHandler::MySql

Inherits:
Object
  • Object
show all
Defined in:
lib/dkdeploy/interaction_handler/mysql.rb

Overview

Interaction handler for mysql

Instance Method Summary collapse

Constructor Details

#initialize(password) ⇒ MySql

Interaction handler for sending password to MySQL client This InteractionHandler provides output of the error code if MySQL answers with an error to the command.



10
11
12
13
14
15
# File 'lib/dkdeploy/interaction_handler/mysql.rb', line 10

def initialize(password)
  @password = password
  # these two are declared as instance variables because the on_data method is called multiple times
  @return_message = ''
  @mysql_error_seen = false
end

Instance Method Details

#on_data(_command, _stream_name, data, channel) ⇒ Object

Method to send password to terminal

Parameters:

  • _command (SSHKit::Command)
  • _stream_name (Symbol)
  • data (String)
  • channel (Net::SSH::Connection::Channel)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dkdeploy/interaction_handler/mysql.rb', line 23

def on_data(_command, _stream_name, data, channel)
  if data =~ /.*password.*/i
    channel.send_data("#{@password}\n")
  else
    @mysql_error_seen = true if data =~ /.*ERROR.*/i
    return raise 'Unexpected data from stream. Can not send password to undefined stream' unless @mysql_error_seen
    # combine the multiple lines from error message. The fact that the error message will be shown multiple times is simply ignored
    @return_message << data
    message = 'Error on executing MySQL command! Response (error code) is: '
    SSHKit.config.output.send(:error, "#{message}\n         #{@return_message}")
    raise 'InteractionHandler caught a MySQL error'
  end
end