Class: Cnvrg::Ssh

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resp) ⇒ Ssh



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
69
# File 'lib/cnvrg/ssh.rb', line 12

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

Instance Attribute Details

#is_sshObject (readonly)

Returns the value of attribute is_ssh.



9
10
11
# File 'lib/cnvrg/ssh.rb', line 9

def is_ssh
  @is_ssh
end

Instance Method Details

#close_sshObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cnvrg/ssh.rb', line 79

def close_ssh()


  begin

    @ssh.close
  rescue => e

  end


end

#exec_command(command) ⇒ Object



73
74
75
76
# File 'lib/cnvrg/ssh.rb', line 73

def exec_command(command)
  exec_command = "sudo -i -u #{@user} cnvrg exec_container #{@container} \"#{command}\" "
  return @ssh.exec!(exec_command)
end