Class: RemoteCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/script_executor/remote_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ RemoteCommand

Returns a new instance of RemoteCommand.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/script_executor/remote_command.rb', line 8

def initialize params
  params = sanitize_parameters params

  @host = params.delete(:domain)
  @host = params.delete(:host) if host.nil? and params[:host]
  @user = params.delete(:user)

  @suppress_output = params.delete(:suppress_output)
  @capture_output = params.delete(:capture_output)
  @simulate = params.delete(:simulate)

  @options = params
end

Instance Attribute Details

#capture_outputObject (readonly)

Returns the value of attribute capture_output.



6
7
8
# File 'lib/script_executor/remote_command.rb', line 6

def capture_output
  @capture_output
end

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/script_executor/remote_command.rb', line 6

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/script_executor/remote_command.rb', line 6

def options
  @options
end

#simulateObject (readonly)

Returns the value of attribute simulate.



6
7
8
# File 'lib/script_executor/remote_command.rb', line 6

def simulate
  @simulate
end

#suppress_outputObject (readonly)

Returns the value of attribute suppress_output.



6
7
8
# File 'lib/script_executor/remote_command.rb', line 6

def suppress_output
  @suppress_output
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/script_executor/remote_command.rb', line 6

def user
  @user
end

Instance Method Details

#execute(commands, line_action) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/script_executor/remote_command.rb', line 22

def execute commands, line_action
  options[:password] = HighLine.new.ask("Enter password for #{options[:user]}: ") { |q| q.echo = '*' } if options[:password].nil?
  options[:user] = user

  print commands, $stdout

  unless simulate
    storage = capture_output ? OutputBuffer.new : nil
    output = suppress_output ? nil : $stdout

    Net::SSH.start(host, user, options) do |session|
      session.exec(commands) do |channel, _, line|
        if ask_password?(line)
          channel.request_pty
          channel.send_data password + "\n"
        else
          output.print(line) if output
          storage.save(line.chomp) if storage

          line_action.call(line) if line_action
        end
      end

      session.loop # run the aggregated event loop
    end

    storage.buffer.join("\n") if storage
  end
end