Class: VagrantPlugins::Brightbox::Action::MapCloudIp

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-brightbox/action/map_cloud_ip.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ MapCloudIp

Returns a new instance of MapCloudIp.



8
9
10
11
# File 'lib/vagrant-brightbox/action/map_cloud_ip.rb', line 8

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

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-brightbox/action/map_cloud_ip.rb', line 13

def call(env)

  @app.call(env)

	  machine = env[:machine]

	  if no_cloud_ips_required?(env[:machine].config.vm.networks)
	    @logger.info("No public ipv4 network required")
	    return
	  end

	  if machine.id.nil?
	    @logger.info("No server found - can't map public ips")
	    return
	  end

	  server = env[:brightbox_compute].servers.get(machine.id)
	  if server.nil?
	    @logger.info("Machine cannot be found; assuming it got destroyed.")
	    machine.id = nil
	    return
	  end

	  unless server.cloud_ips.empty?
	    @logger.info("Server already has public ip address.")
	    return
	  end

	  target_ip = unallocated_cloud_ip(env[:brightbox_compute].cloud_ips)

	  if target_ip.nil?
	    @logger.info("Couldn't allocate a cloud ip")
	    env[:ui].info I18n.t("vagrant_brightbox.errors.no_free_cloud_ip")
	    return
	  end

	  target_ip.map(server)
	  env[:ui].info I18n.t("vagrant_brightbox.mapped_cloud_ip", :server => server.id, :cloud_ip => target_ip.public_ip)

end

#no_cloud_ips_required?(networks) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/vagrant-brightbox/action/map_cloud_ip.rb', line 58

def no_cloud_ips_required?(networks)
  networks.any? do |x|
    x.first == :private_network ||
      (x.first == :public_network && element[1][:ipv6])
  end
end

#unallocated_cloud_ip(ip_list) ⇒ Object



54
55
56
# File 'lib/vagrant-brightbox/action/map_cloud_ip.rb', line 54

def unallocated_cloud_ip(ip_list)
  ip_list.all.detect(ip_list.method(:allocate)) {|ip| !ip.mapped? }
end