Class: VagrantPlugins::Joyent::Action::ReadSSHInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-joyent/action/read_ssh_info.rb

Overview

This action reads the SSH info for the machine and puts it into the ‘:machine_ssh_info` key in the environment.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ReadSSHInfo

Returns a new instance of ReadSSHInfo.



10
11
12
13
# File 'lib/vagrant-joyent/action/read_ssh_info.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_joyent::action::read_ssh_info")
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
# File 'lib/vagrant-joyent/action/read_ssh_info.rb', line 15

def call(env)
  env[:machine_ssh_info] = read_ssh_info(env[:joyent_compute], env[:machine])

  @app.call(env)
end

#is_linklocal(ip) ⇒ Object



21
22
23
24
# File 'lib/vagrant-joyent/action/read_ssh_info.rb', line 21

def is_linklocal(ip)
  linklocal = IPAddr.new "169.254.0.0/16"
  return linklocal.include?(ip)
end

#is_loopback(ip) ⇒ Object



26
27
28
29
# File 'lib/vagrant-joyent/action/read_ssh_info.rb', line 26

def is_loopback(ip)
  loopback = IPAddr.new "127.0.0.0/8"
  return loopback.include?(ip)
end

#is_private(ip) ⇒ Object



31
32
33
34
35
36
# File 'lib/vagrant-joyent/action/read_ssh_info.rb', line 31

def is_private(ip)
  block_a = IPAddr.new "10.0.0.0/8"
  block_b = IPAddr.new "172.16.0.0/12"
  block_c = IPAddr.new "192.168.0.0/16"
  return (block_a.include?(ip) or block_b.include?(ip) or block_c.include?(ip))
end

#read_ssh_info(joyent, machine) ⇒ Object



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/vagrant-joyent/action/read_ssh_info.rb', line 38

def read_ssh_info(joyent, machine)
  return nil if machine.id.nil?
  
  # Find the machine
  server = joyent.servers.get(machine.id)
  if server.nil?
    # The machine can't be found
    @logger.info("Machine couldn't be found, assuming it got destroyed.")
    machine.id = nil
    return nil
  end
  
  # IP address to bootstrap
  bootstrap_ip_addresses = server.ips.select{ |ip| ip and not(is_loopback(ip) or is_linklocal(ip)) }
  if bootstrap_ip_addresses.count == 1
    bootstrap_ip_address = bootstrap_ip_addresses.first
  else
    bootstrap_ip_address = bootstrap_ip_addresses.find{|ip| not is_private(ip)}            
  end
            
  config = machine.provider_config
  
  # Read the DNS info
  return {
    :host => bootstrap_ip_address,
    :port => 22,
    :private_key_path => config.keyfile,
    :username => config.ssh_username
  }
end