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.


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

def initialize(app, _env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_libvirt::action::forward_ports')
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
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 13

def call(env)
  @env = env

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

  # Warn if we're port forwarding to any privileged ports
  env[:forwarded_ports].each do |fp|
    next unless fp[:host] <= 1024
    env[: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?
    env[:ui].info I18n.t('vagrant.actions.vm.forward_ports.forwarding')
    forward_ports
  end
end

#forward_portsObject


37
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
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 37

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

    @env[:ui].info(I18n.t(
                     'vagrant.actions.vm.forward_ports.forwarding_entry',
                     message_attributes
    ))

    if fp[:protocol] == 'udp'
      @env[:ui].warn I18n.t('vagrant_libvirt.warnings.forwarding_udp')
      next
    end

    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(fp[:host], ssh_pid)
  end
end