Method: Cnvrg::Ssh#initialize

Defined in:
lib/cnvrg/ssh.rb

#initialize(resp) ⇒ Ssh

Returns a new instance of Ssh.



11
12
13
14
15
16
17
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cnvrg/ssh.rb', line 11

def initialize(resp)
  begin
    @is_ssh = false
    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"])
    @container = URLcrypt.decrypt(resp["result"]["machine_c"])

    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?
          @is_ssh = true
          return
        else
          count+=1
          sleep(2)

        end
      rescue
        count+=1
        sleep(2)


      end
    end
    if tempssh
      tempssh.close
      tempssh.unlink
    end
    return false
  rescue => e

    puts e
  end
end