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.



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

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



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

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