Class: VagrantPlugins::Libvirt::Action::CreateNetworkInterfaces

Inherits:
Object
  • Object
show all
Includes:
Util::ErbTemplate
Defined in:
lib/vagrant-libvirt/action/create_network_interfaces.rb

Overview

Create network interfaces for domain, before domain is running.

Instance Method Summary collapse

Methods included from Util::ErbTemplate

#to_xml

Constructor Details

#initialize(app, env) ⇒ CreateNetworkInterfaces

Returns a new instance of CreateNetworkInterfaces.



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

def initialize(app, env)
  @logger = Log4r::Logger.new("vagrant_libvirt::action::create_network_interfaces")
  @app = app
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
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
66
67
68
69
70
# File 'lib/vagrant-libvirt/action/create_network_interfaces.rb', line 16

def call(env)
  # Get domain first.
  begin
    domain = env[:libvirt_compute].client.lookup_domain_by_uuid(
      env[:machine].id.to_s)
  rescue => e
    raise Errors::NoDomainError,
      :error_message => e.message
  end

  # Setup list of interfaces before creating them
  adapters = []

  # Assign main interface for provisioning to first slot.
  # Use network 'default' as network for ssh connecting and
  # machine provisioning. This should be maybe configurable in
  # Vagrantfile in future.
  adapters[0] = 'default'

  env[:machine].config.vm.networks.each do |type, options|
    # Other types than bridged are not supported for now.
    next if type != :bridged

    network_name = 'default'
    network_name = options[:bridge] if options[:bridge]

    if options[:adapter]
      if adapters[options[:adapter]]
        raise Errors::InterfaceSlotNotAvailable
      end

      adapters[options[:adapter].to_i] = network_name
    else
      empty_slot = find_empty(adapters, start=1)
      raise Errors::InterfaceSlotNotAvailable if empty_slot == nil

      adapters[empty_slot] = network_name
    end           
  end

  # Create each interface as new domain device
  adapters.each_with_index do |network_name, slot_number|
    @iface_number = slot_number
    @network_name = network_name
    @logger.info("Creating network interface eth#{@iface_number}")
    begin
      domain.attach_device(to_xml('interface'))
    rescue => e
      raise Errors::AttachDeviceError,
        :error_message => e.message
    end
  end

  @app.call(env)
end