Module: Bcome::EnvironmentSSH

Included in:
Node::Environment, Stack::Environment
Defined in:
lib/helpers/environment_ssh.rb

Instance Method Summary collapse

Instance Method Details

#bastion_ip_addressObject



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/helpers/environment_ssh.rb', line 93

def bastion_ip_address
  unless bastion_server
    raise "Unable to find the jump host. No instance found with identifier '#{@ssh_mode[:jump_host_identifier]}'.  Check your network.yml config for this stack, and ensure that you've got the correct value for key 'jump_host_identifier'"
  end

  if dynamic_network_lookup? 
    return bastion_server.public_ip_address
  else
    bastion_ip_address = @ssh_mode[:jump_host_ip_address]
    raise "No jump_host_ip_address specified in your configuration.".failure unless bastion_ip_address
    return bastion_ip_address
  end
end

#bastion_serverObject



63
64
65
# File 'lib/helpers/environment_ssh.rb', line 63

def bastion_server
  machines.select{|instance| instance.identifier == @ssh_mode[:jump_host_identifier] }.first
end

#bootstrap_proxy_commandObject



17
18
19
# File 'lib/helpers/environment_ssh.rb', line 17

def bootstrap_proxy_command
  return "ssh -i #{bootstrap_settings[:key]} -o StrictHostKeyChecking=no -W %h:%p #{bootstrap_settings[:ssh_user]}@#{bastion_ip_address}"
end

#dynamic_network_lookup?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/helpers/environment_ssh.rb', line 118

def dynamic_network_lookup?
  return network_lookup_type == "dynamic"
end

#execute_cmd(raw_commands, instance, proxy, bootstrap = false) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/helpers/environment_ssh.rb', line 31

def execute_cmd(raw_commands, instance, proxy, bootstrap = false)
  commands = raw_commands.collect{|raw_command|
    raw_command.is_a?(::Bcome::Command) ? raw_command : ::Bcome::Command.new(:command => raw_command, :instance => instance, :bootstrap => bootstrap)
  }

  ssh = ::Bcome::Ssh.new(commands)
  ssh.execute!
  return commands
end

#execute_command(commands, instance, bootstrap = false) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/helpers/environment_ssh.rb', line 21

def execute_command(commands, instance, bootstrap = false)
  begin
    return execute_cmd(commands, instance, proxy(bootstrap), bootstrap)
  rescue Net::SSH::AuthenticationFailed
    raise "Could not authenticate connection to #{instance.identifier}".failure
  rescue Net::SSH::Disconnect
    raise "SSH connection to #{instance.identifier} was disconnected".failure
  end
end

#execute_scp_download(remote_path, local_path, instance) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/helpers/environment_ssh.rb', line 52

def execute_scp_download(remote_path, local_path, instance)
  begin
    scp = ::Bcome::Scp.new(proxy, instance)
    scp.download!(remote_path, local_path)
  rescue Net::SSH::AuthenticationFailed
    raise "Could not authenticate connection to #{instance.identifier}".failure
  rescue Net::SSH::Disconnect
    raise "SSH connection to #{instance.identifier} was disconnected".failure
  end
end

#execute_scp_upload(files, remote_path, instance) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/helpers/environment_ssh.rb', line 41

def execute_scp_upload(files, remote_path, instance)
  begin
    scp = ::Bcome::Scp.new(proxy, instance)
    scp.upload!(files, remote_path)
  rescue Net::SSH::AuthenticationFailed
    raise "Could not authenticate connection to #{instance.identifier}".failure
  rescue Net::SSH::Disconnect 
    raise "SSH connection to #{instance.identifier} was disconnected".failure
  end
end

#network_lookupObject



107
108
109
110
# File 'lib/helpers/environment_ssh.rb', line 107

def network_lookup
  raise "Missing network lookup in networks_environment configuration".failure unless @network_lookup
  return @network_lookup
end

#network_lookup_typeObject



112
113
114
115
116
# File 'lib/helpers/environment_ssh.rb', line 112

def network_lookup_type
  type = network_lookup[:type]
  raise "Unknown network lookup type '#{type}" unless ["dynamic", "static"].include?(type)
  return type
end

#proxy(bootstrap = false) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/helpers/environment_ssh.rb', line 3

def proxy(bootstrap = false)
  return nil unless ssh_mode_type == ::SSH_JUMP_MODE_IDENTIFIER

  unless bootstrap
    return Net::SSH::Proxy::Command.new(proxy_command)
  else
    return Net::SSH::Proxy::Command.new(bootstrap_proxy_command)
  end
end

#proxy_commandObject



13
14
15
# File 'lib/helpers/environment_ssh.rb', line 13

def proxy_command
  return "ssh -W %h:%p #{bastion_ip_address}"
end

#ssh_key_pathObject



85
86
87
# File 'lib/helpers/environment_ssh.rb', line 85

def ssh_key_path
  return @ssh_mode[:ssh_key_path]
end

#ssh_mode_typeObject



67
68
69
70
71
# File 'lib/helpers/environment_ssh.rb', line 67

def ssh_mode_type
  ssh_mode_type = @ssh_mode[:type]
  raise "Invalid ssh mode type #{ssh_mode_type}. Should be one of #{valid_ssh_modes.join(", ")}".failure unless valid_ssh_modes.include?(ssh_mode_type)
  return @ssh_mode[:type]
end

#ssh_mode_userObject



73
74
75
# File 'lib/helpers/environment_ssh.rb', line 73

def ssh_mode_user
  return @ssh_mode[:ssh_user]
end

#ssh_nat_userObject



81
82
83
# File 'lib/helpers/environment_ssh.rb', line 81

def ssh_nat_user
  return @ssh_mode[:nat_user]
end

#ssh_userObject



77
78
79
# File 'lib/helpers/environment_ssh.rb', line 77

def ssh_user
  (@ssh_mode && ssh_mode_user) ? ssh_mode_user : `whoami`.gsub("\n","")
end

#valid_ssh_modesObject



89
90
91
# File 'lib/helpers/environment_ssh.rb', line 89

def valid_ssh_modes
  [::SSH_JUMP_MODE_IDENTIFIER, ::SSH_DIRECT_MODE_IDENTIFIER]
end