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
# File 'lib/script_executor/remote_command.rb', line 8

def initialize params
  @domain = params[:domain]
  @user = params[:user]
  @password = params[:password]
  @identity_file = params[:identity_file]
  @suppress_output = params[:suppress_output]
  @capture_output = params[:capture_output]
  @simulate = params[:simulate]
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

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#identity_fileObject (readonly)

Returns the value of attribute identity_file.



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

def identity_file
  @identity_file
end

#passwordObject (readonly)

Returns the value of attribute password.



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

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/script_executor/remote_command.rb', line 18

def execute commands, line_action
  print commands, $stdout

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

    password = HighLine.new.ask("Enter password for #{user}: ") { |q| q.echo = '*' } if @password.nil?

    Net::SSH.start(domain, user, :password => password) 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