Class: Deltacloud::Runner::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(network, credentials, key = nil) ⇒ SSH

Returns a new instance of SSH.



64
65
66
67
# File 'lib/deltacloud/runner.rb', line 64

def initialize(network, credentials, key=nil)
  @network, @credentials, @key = network, credentials, key
  @result = ""
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



62
63
64
# File 'lib/deltacloud/runner.rb', line 62

def command
  @command
end

#credentialsObject

Returns the value of attribute credentials.



61
62
63
# File 'lib/deltacloud/runner.rb', line 61

def credentials
  @credentials
end

#keyObject

Returns the value of attribute key.



61
62
63
# File 'lib/deltacloud/runner.rb', line 61

def key
  @key
end

#networkObject (readonly)

Returns the value of attribute network.



60
61
62
# File 'lib/deltacloud/runner.rb', line 60

def network
  @network
end

Instance Method Details

#execute(command) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/deltacloud/runner.rb', line 69

def execute(command)
  @command = command
  config = ssh_config(@network, @credentials, @key)
  begin
    session = nil
    # Default timeout for connecting to an instance.
    # 20 seconds should be OK for most of connections, if you are
    # experiencing some Exceptions with Timeouts increase this value.
    # Please keep in mind that the HTTP request timeout is set to 60
    # seconds, so you need to fit into this time
    Timeout::timeout(20) do
      session = Net::SSH.start(@network.ip, 'root', config)
    end
    session.open_channel do |channel|
      channel.on_data do |ch, data|
        @result += data
      end
      channel.exec(command)
      session.loop
    end
    session.close
  rescue Exception => e
    raise InstanceSSHError.new("#{e.class.name}: #{e.message}")
  ensure
    # FileUtils.rm(config[:keys].first) rescue nil
  end
  Deltacloud::Runner::Response.new(self, @result)
end