Module: CORL::Mixin::Machine::SSH

Included in:
CORL::Machine::FogBase, CORL::Machine::Vagrant
Defined in:
lib/core/mixin/machine/ssh.rb

Instance Method Summary collapse

Instance Method Details

#close_ssh_sessionObject




127
128
129
# File 'lib/core/mixin/machine/ssh.rb', line 127

def close_ssh_session
  Util::SSH.close_session(node.public_ip, node.user)
end

#init_ssh_session(reset = false, tries = 12, sleep_secs = 5) ⇒ Object


SSH Operations



9
10
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
# File 'lib/core/mixin/machine/ssh.rb', line 9

def init_ssh_session(reset = false, tries = 12, sleep_secs = 5)
  ssh_wait_for_ready

  success     = true

  public_ip   = node.public_ip(true)
  user        = node.user
  ssh_port    = node.ssh_port
  private_key = node.private_key

  ssh_config  = Config.new({
    :keypair  => node.keypair,
    :key_dir  => node.network.key_cache_directory,
    :key_name => node.plugin_name
  })

  begin
    Util::SSH.session(public_ip, user, ssh_port, private_key, reset, ssh_config)
    node.keypair = ssh_config[:keypair]

  rescue Net::SSH::HostKeyMismatch => error
    error.remember_host!
    sleep 0.2
    reset = true
    retry

  rescue Errno::ECONNREFUSED, Net::SSH::ConnectionTimeout, Net::SSH::Disconnect => error
    if tries > 1
      sleep(sleep_secs)

      tries -= 1
      reset  = true
      retry
    else
      success = false
    end

  rescue => error
    if error.is_a?(Net::SSH::AuthenticationFailed) && ssh_config[:keypair]
      key_file_base = File.join(ssh_config[:key_dir], "#{ssh_config[:key_name]}_#{ssh_config[:keypair].type}")

      Util::Disk.delete(key_file_base)
      Util::Disk.delete("#{key_file_base}.pub")

      node.keypair            = nil
      ssh_config[:keypair]    = nil
      ssh_config[:reset_conn] = true
      retry
    else
      message = error.message
      if message.include?("Neither PUB key nor PRIV key")
        message = "Authentication failed for #{user}@#{public_ip} on port #{ssh_port} (most likely wrong password entered)"
      end
      warn(message, { :i18n => false })
    end
    success = false
  end
  success
end

#ssh_download(remote_path, local_path, options = {}, &code) ⇒ Object




71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/core/mixin/machine/ssh.rb', line 71

def ssh_download(remote_path, local_path, options = {}, &code)
  config  = Config.ensure(options)
  success = false

  begin
    if init_ssh_session
      Util::SSH.download(node.public_ip, node.user, remote_path, local_path, config.export) do |name, received, total|
        code.call(name, received, total) if code
      end
      success = true
    end
  rescue => error
    error(error.message, { :i18n => false })
  end

  success
end

#ssh_exec(commands, options = {}, &code) ⇒ Object




111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/core/mixin/machine/ssh.rb', line 111

def ssh_exec(commands, options = {}, &code)
  config  = Config.ensure(options)
  results = nil

  if commands && commands = Util::Data.array(commands)
    if init_ssh_session
      results = Util::SSH.exec(node.public_ip, node.user, commands) do |type, command, data|
        code.call(type, command, data) if code
      end
    end
  end
  results
end

#ssh_terminal(user, options = {}) ⇒ Object




133
134
135
# File 'lib/core/mixin/machine/ssh.rb', line 133

def ssh_terminal(user, options = {})
  Util::SSH.terminal(node.public_ip, user, Config.ensure(options).export)
end

#ssh_upload(local_path, remote_path, options = {}, &code) ⇒ Object




91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/core/mixin/machine/ssh.rb', line 91

def ssh_upload(local_path, remote_path, options = {}, &code)
  config  = Config.ensure(options)
  success = false

  begin
    if init_ssh_session
      Util::SSH.upload(node.public_ip, node.user, local_path, remote_path, config.export) do |name, sent, total|
        code.call(name, sent, total) if code
      end
      success = true
    end
  rescue => error
    error(error.message, { :i18n => false })
  end

  success
end

#ssh_wait_for_readyObject


Utilities



140
141
142
# File 'lib/core/mixin/machine/ssh.rb', line 140

def ssh_wait_for_ready
  # Override in class if needed (see Fog Machine provider)
end