Method: Cnvrg::Images.ssh_to_machine

Defined in:
lib/cnvrg/Images.rb

.ssh_to_machine(resp) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cnvrg/Images.rb', line 141

def self.ssh_to_machine(resp)

  sts_path = resp["result"]["sts_path"]

  uri = URI.parse(sts_path)

  http_object = Net::HTTP.new(uri.host, uri.port)
  http_object.use_ssl = true if uri.scheme == 'https'
  request = Net::HTTP::Get.new(sts_path)

  body = ""
  http_object.start do |http|
    response = http.request request
    body = response.read_body
  end

  URLcrypt::key = [body].pack('H*')

  ip = URLcrypt.decrypt(resp["result"]["machine_i"])

  user = URLcrypt.decrypt(resp["result"]["machine_u"])
  key = URLcrypt.decrypt(resp["result"]["machine_k"])
  tempssh = Tempfile.new "sshkey"
  tempssh.write open(key).read
  tempssh.rewind
  key_path = tempssh.path
  count = 0
  while count < 5

  begin
      ssh = Net::SSH.start(ip, user=user, :keys => key_path, :timeout => 10)
      if !ssh.nil?
        return ssh
      else
        count+=1
        sleep(2)

      end
    rescue
  count+=1
  sleep(2)


  end
  end
  if tempssh
    tempssh.close
    tempssh.unlink
  end
  return false
end