class VagrantPlugins::ProviderLibvirt::Action::ForwardPorts

Adds support for vagrant's `forward_ports` configuration directive.

Public Class Methods

new(app, env) click to toggle source
# File lib/vagrant-libvirt/action/forward_ports.rb, line 6
def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new('vagrant_libvirt::action::forward_ports')
end

Public Instance Methods

call(env) click to toggle source
# File lib/vagrant-libvirt/action/forward_ports.rb, line 11
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|
    if fp[:host] <= 1024
      env[:ui].warn I18n.t(
        'vagrant.actions.vm.forward_ports.privileged_ports'
      )
      break
    end
  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_ports() click to toggle source
# File lib/vagrant-libvirt/action/forward_ports.rb, line 36
def forward_ports
  @env[:forwarded_ports].each do |fp|
    message_attributes = {
      adapter: 'eth0',
      guest_port: fp[:guest],
      host_port: fp[:host]
    }

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

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

Private Instance Methods

compile_forwarded_ports(config) click to toggle source
# File lib/vagrant-libvirt/action/forward_ports.rb, line 62
def compile_forwarded_ports(config)
  mappings = {}

  config.vm.networks.each do |type, options|
    next if options[:disabled]

    if type == :forwarded_port && options[:id] != 'ssh'
      if options.fetch(:host_ip, '').to_s.strip.empty?
        options.delete(:host_ip)
      end
      mappings[options[:host]] = options
    end
  end

  mappings.values
end
redirect_port(machine, host_ip, host_port, guest_ip, guest_port) click to toggle source
# File lib/vagrant-libvirt/action/forward_ports.rb, line 79
def redirect_port(machine, host_ip, host_port, guest_ip, guest_port)
  ssh_info = machine.ssh_info
  params = %W(
    "-L #{host_ip}:#{host_port}:#{guest_ip}:#{guest_port}"
    -N
    #{ssh_info[:host]}
  ).join(' ')

  options = (%W(
    User=#{ssh_info[:username]}
    Port=#{ssh_info[:port]}
    UserKnownHostsFile=/dev/null
    StrictHostKeyChecking=no
    PasswordAuthentication=no
    ForwardX11=#{ssh_info[:forward_x11] ? 'yes' : 'no'}
  ) + ssh_info[:private_key_path].map do |pk|
      "IdentityFile=#{pk}"
    end).map { |s| s.prepend('-o ') }.join(' ')
    
  ssh_cmd = "ssh #{options} #{params}"

  @logger.debug "Forwarding port with `#{ssh_cmd}`"
  spawn(ssh_cmd,  [:out, :err] => '/dev/null')
end
store_ssh_pid(host_port, ssh_pid) click to toggle source
# File lib/vagrant-libvirt/action/forward_ports.rb, line 104
def store_ssh_pid(host_port, ssh_pid)
  data_dir = @env[:machine].data_dir.join('pids')
  data_dir.mkdir unless data_dir.directory?

  data_dir.join("ssh_#{host_port}.pid").open('w') do |pid_file|
    pid_file.write(ssh_pid)
  end
end