Module: Bcome::InstanceSsh

Included in:
Node::Instance
Defined in:
lib/helpers/instance_ssh.rb

Instance Method Summary collapse

Instance Method Details

#bastion_ip_addressObject



51
52
53
# File 'lib/helpers/instance_ssh.rb', line 51

def bastion_ip_address
  return environment.bastion_ip_address
end

#direct_ssh_commandObject



30
31
32
# File 'lib/helpers/instance_ssh.rb', line 30

def direct_ssh_command
  command = "ssh #{environment.ssh_mode_user}@#{public_ip_address}"
end

#do_ssh(command) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/helpers/instance_ssh.rb', line 11

def do_ssh(command)
  result = execute_task(:run_local, command)
  unless result
    if bootstrap_settings
      puts "\n BOOTSTRAP MODE".informational
      execute_task(:run_local, non_bootstrapped_ssh_command)
    end 
  end  
  return result
end

#execute_task(task_name, *args) ⇒ Object



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

def execute_task(task_name, *args)
  begin
    public_send(task_name, *args)
  rescue ::Seahorse::Client::NetworkingError
    puts "Could not execute '#{task_name}'. Cannot initiate connection - check your network connection and try again.".failure
  end
end

#is_direct_ssh?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/helpers/instance_ssh.rb', line 55

def is_direct_ssh?
  environment.ssh_mode_type == ::SSH_DIRECT_MODE_IDENTIFIER
end

#is_jump_ssh?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/helpers/instance_ssh.rb', line 59

def is_jump_ssh?
  environment.ssh_mode_type == ::SSH_JUMP_MODE_IDENTIFIER
end

#is_ssh_jump_host?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/helpers/instance_ssh.rb', line 80

def is_ssh_jump_host?
  @role == ::SSH_JUMP_HOST_ROLE_IDENTIFIER
end

#keysObject



47
48
49
# File 'lib/helpers/instance_ssh.rb', line 47

def keys
  [ssh_key_path]
end

#nat_userObject



34
35
36
# File 'lib/helpers/instance_ssh.rb', line 34

def nat_user
  return environment.ssh_nat_user ? environment.ssh_nat_user : ssh_user
end

#non_bootstrapped_ssh_commandObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/helpers/instance_ssh.rb', line 67

def non_bootstrapped_ssh_command
  command = is_jump_ssh? ? (
    is_ssh_jump_host? ? (
     "ssh -i #{bootstrap_settings[:key]} #{bootstrap_settings[:ssh_user]}@#{ip_address}"
    ) : (
     "ssh -i #{bootstrap_settings[:key]} -t #{bootstrap_settings[:ssh_user]}@#{bastion_ip_address} ssh -t #{ip_address}"
    )
  ) : (
     "ssh -i #{bootstrap_settings[:key]} #{bootstrap_settings[:ssh_user]}@#{ip_address}"
  )
  return command
end

#sshObject



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

def ssh
  is_jump_ssh? ? (is_ssh_jump_host? ? do_ssh(ssh_jump_command_to_bastion) : do_ssh(ssh_jump_command_to_internal_node)) : do_ssh(direct_ssh_command)
end

#ssh_jump_command_to_bastionObject



22
23
24
# File 'lib/helpers/instance_ssh.rb', line 22

def ssh_jump_command_to_bastion
  command = "ssh -o UserKnownHostsFile=/dev/null -i #{ssh_key_path} #{nat_user}@#{bastion_ip_address}"
end

#ssh_jump_command_to_internal_nodeObject



26
27
28
# File 'lib/helpers/instance_ssh.rb', line 26

def ssh_jump_command_to_internal_node
  command = "ssh -o UserKnownHostsFile=/dev/null -i #{ssh_key_path} -o \"ProxyCommand ssh -W %h:%p -i #{ssh_key_path} #{nat_user}@#{bastion_ip_address}\" #{ssh_user}@#{ip_address}"
end

#ssh_key_pathObject



43
44
45
# File 'lib/helpers/instance_ssh.rb', line 43

def ssh_key_path
  return environment.ssh_key_path ? environment.ssh_key_path : "~/.ssh/id_rsa" 
end

#ssh_userObject



38
39
40
41
# File 'lib/helpers/instance_ssh.rb', line 38

def ssh_user
  # defined by the environment or we fall back to the local user
  return environment.ssh_mode_user ? environment.ssh_mode_user : `whoami`.gsub("\n","")
end