Class: VagrantPlugins::ProviderLibvirt::Action::ForwardPorts

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/forward_ports.rb

Overview

Adds support for vagrant’s ‘forward_ports` configuration directive.

Constant Summary collapse

@@lock =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ForwardPorts

Returns a new instance of ForwardPorts.



10
11
12
13
14
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 10

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_libvirt::action::forward_ports')
  @ui     = env[:ui]
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 16

def call(env)
  # Get the ports we're forwarding
  env[:forwarded_ports] = compile_forwarded_ports(env[:machine])

  # Warn if we're port forwarding to any privileged ports
  env[:forwarded_ports].each do |fp|
    next unless fp[:host] <= 1024
    @ui.warn I18n.t(
      'vagrant.actions.vm.forward_ports.privileged_ports'
    )
    break
  end

  # Continue, we need the VM to be booted in order to grab its IP
  @app.call env

  if env[:forwarded_ports].any?
    @ui.info I18n.t('vagrant.actions.vm.forward_ports.forwarding')
    forward_ports(env)
  end
end

#forward_ports(env) ⇒ 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
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 38

def forward_ports(env)
  env[:forwarded_ports].each do |fp|
    message_attributes = {
      adapter: fp[:adapter] || 'eth0',
      guest_port: fp[:guest],
      host_port: fp[:host]
    }

    @ui.info(I18n.t(
                     'vagrant.actions.vm.forward_ports.forwarding_entry',
                     **message_attributes
    ))

    ssh_pid = redirect_port(
      env[:machine],
      fp[:host_ip] || '*',
      fp[:host],
      fp[:guest_ip] || env[:machine].provider.ssh_info[:host],
      fp[:guest],
      fp[:gateway_ports] || false
    )
    store_ssh_pid(env[:machine], fp[:host], ssh_pid)
  end
end