module CORL::Mixin::Machine::SSH

Public Instance Methods

close_ssh_session() click to toggle source
    # File lib/core/mixin/machine/ssh.rb
128 def close_ssh_session
129   Util::SSH.close_session(node.public_ip, node.user)
130 end
init_ssh_session(reset = false, tries = 12, sleep_secs = 5) click to toggle source
   # File lib/core/mixin/machine/ssh.rb
 9 def init_ssh_session(reset = false, tries = 12, sleep_secs = 5)
10   ssh_wait_for_ready
11 
12   success     = true
13 
14   public_ip   = node.public_ip(true)
15   user        = node.user
16   ssh_port    = node.ssh_port
17   private_key = node.private_key
18 
19   ssh_config  = Config.new({
20     :keypair  => node.keypair,
21     :key_dir  => node.network.key_cache_directory,
22     :key_name => node.plugin_name,
23     :password => node.password
24   })
25 
26   begin
27     Util::SSH.session(public_ip, user, ssh_port, private_key, reset, ssh_config)
28     node.keypair = ssh_config[:keypair]
29 
30   rescue Net::SSH::HostKeyMismatch => error
31     error.remember_host!
32     sleep 0.2
33     reset = true
34     retry
35 
36   rescue Errno::ECONNREFUSED, Net::SSH::ConnectionTimeout, Net::SSH::Disconnect => error
37     if tries > 1
38       sleep(sleep_secs)
39 
40       tries -= 1
41       reset  = true
42       retry
43     else
44       success = false
45     end
46 
47   rescue => error
48     if error.is_a?(Net::SSH::AuthenticationFailed) && ssh_config[:keypair]
49       key_file_base = File.join(ssh_config[:key_dir], "#{ssh_config[:key_name]}_#{ssh_config[:keypair].type}")
50 
51       Util::Disk.delete(key_file_base)
52       Util::Disk.delete("#{key_file_base}.pub")
53 
54       node.keypair            = nil
55       ssh_config[:keypair]    = nil
56       ssh_config[:reset_conn] = true
57       retry
58     else
59       message = error.message
60       if message.include?("Neither PUB key nor PRIV key")
61         message = "Authentication failed for #{user}@#{public_ip} on port #{ssh_port} (most likely wrong password entered)"
62       end
63       warn(message, { :i18n => false })
64     end
65     success = false
66   end
67   success
68 end
ssh_download(remote_path, local_path, options = {}, &code) click to toggle source
   # File lib/core/mixin/machine/ssh.rb
72 def ssh_download(remote_path, local_path, options = {}, &code)
73   config  = Config.ensure(options)
74   success = false
75 
76   begin
77     if init_ssh_session
78       Util::SSH.download(node.public_ip, node.user, remote_path, local_path, config.export) do |name, received, total|
79         code.call(name, received, total) if code
80       end
81       success = true
82     end
83   rescue => error
84     error(error.message, { :i18n => false })
85   end
86 
87   success
88 end
ssh_exec(commands, options = {}, &code) click to toggle source
    # File lib/core/mixin/machine/ssh.rb
112 def ssh_exec(commands, options = {}, &code)
113   config  = Config.ensure(options)
114   results = nil
115 
116   if commands && commands = Util::Data.array(commands)
117     if init_ssh_session
118       results = Util::SSH.exec(node.public_ip, node.user, commands) do |type, command, data|
119         code.call(type, command, data) if code
120       end
121     end
122   end
123   results
124 end
ssh_terminal(user, options = {}) click to toggle source
    # File lib/core/mixin/machine/ssh.rb
134 def ssh_terminal(user, options = {})
135   Util::SSH.terminal(node.public_ip, user, Config.ensure(options).export)
136 end
ssh_upload(local_path, remote_path, options = {}, &code) click to toggle source
    # File lib/core/mixin/machine/ssh.rb
 92 def ssh_upload(local_path, remote_path, options = {}, &code)
 93   config  = Config.ensure(options)
 94   success = false
 95 
 96   begin
 97     if init_ssh_session
 98       Util::SSH.upload(node.public_ip, node.user, local_path, remote_path, config.export) do |name, sent, total|
 99         code.call(name, sent, total) if code
100       end
101       success = true
102     end
103   rescue => error
104     error(error.message, { :i18n => false })
105   end
106 
107   success
108 end
ssh_wait_for_ready() click to toggle source
    # File lib/core/mixin/machine/ssh.rb
141 def ssh_wait_for_ready
142   # Override in class if needed (see Fog Machine provider)
143 end