Class: MysqlSlaver::Executor

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/mysql_slaver/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Constructor Details

#initialize(params = {}) ⇒ Executor

Returns a new instance of Executor.



7
8
9
10
# File 'lib/mysql_slaver/executor.rb', line 7

def initialize(params = {})
  @ssh_port = params[:ssh_port]
  @dry_run  = params.fetch(:dry_run, false)
end

Instance Attribute Details

#dry_runObject (readonly)

Returns the value of attribute dry_run.



3
4
5
# File 'lib/mysql_slaver/executor.rb', line 3

def dry_run
  @dry_run
end

#ssh_portObject (readonly)

Returns the value of attribute ssh_port.



3
4
5
# File 'lib/mysql_slaver/executor.rb', line 3

def ssh_port
  @ssh_port
end

Instance Method Details

#execute(cmd) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/mysql_slaver/executor.rb', line 20

def execute(cmd)
  string = cmd.is_a?(Array) ? cmd.join('; ') : cmd
  log "CMD: #{string}"
  if dry_run
    "[DUMMY RESULT]"
  else
    result = `#{string}`
    success? ? result : nil
  end
end

#ssh_command(cmd, host) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/mysql_slaver/executor.rb', line 12

def ssh_command(cmd, host)
  if ssh_port
    "ssh -p #{ssh_port} #{host} '#{cmd}'"
  else
    "ssh #{host} '#{cmd}'"
  end
end