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
21
# 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)
  @output_stream = params[:output_stream]
  @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

#output_streamObject (readonly)

Returns the value of attribute output_stream.



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

def output_stream
  @output_stream
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



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
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/script_executor/remote_command.rb', line 23

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

  print commands, $stdout

  unless simulate
    storage = capture_output ? OutputBuffer.new : nil
    output = if output_stream
               output_stream
             else
               suppress_output ? nil : $stdout
             end

    Net::SSH.start(host, user, options) do |session|
      session.exec(commands) do |channel, _, line|
        if ask_password?(line)
          channel.request_pty
          channel.send_data options[:password] + "\n"
        else
          if output
            output.write(line)
            output.flush
          end

          if storage
            storage.save(line.chomp)
          end

          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