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

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

Instance Method Summary collapse

Instance Method Details

#close_ssh_sessionObject




111
112
113
# File 'lib/core/mixin/machine/ssh.rb', line 111

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
# 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
  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
    warn(error, { :i18n => false })
    success = false
  end
  success
end

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




55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/core/mixin/machine/ssh.rb', line 55

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




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

def ssh_exec(commands, options = {}, &code)
  config  = Config.ensure(options)
  results = nil
  
  if commands && commands = commands.clone
    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




117
118
119
# File 'lib/core/mixin/machine/ssh.rb', line 117

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




75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/core/mixin/machine/ssh.rb', line 75

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



124
125
126
# File 'lib/core/mixin/machine/ssh.rb', line 124

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