Class: VagrantPlugins::OVirtProvider::Action::CreateNetworkInterfaces

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::ScopedHashOverride
Defined in:
lib/vagrant-ovirt/action/create_network_interfaces.rb

Overview

Create network interfaces for machine, before VM is running.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ CreateNetworkInterfaces

Returns a new instance of CreateNetworkInterfaces.



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

def initialize(app, env)
  @logger = Log4r::Logger.new("vagrant_ovirt::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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vagrant-ovirt/action/create_network_interfaces.rb', line 16

def call(env)
  # Get machine first.
  begin
    machine = OVirtProvider::Util::Collection.find_matching(
      env[:ovirt_compute].servers.all, env[:machine].id.to_s)
  rescue => e
    raise Errors::NoVMError,
      :vm_name => env[:machine].id.to_s
  end

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

  # First interface is for provisioning, so this slot is not usable.
  # This interface should be available already from template.
  adapters[0] = {
    :network_name => 'rhevm'
  }

  env[:machine].config.vm.networks.each do |type, options|
    # We support private and public networks only. They mean both the
    # same right now.
    next if type != :private_network and type != :public_network

    # Get options for this interface. Options can be specified in
    # Vagrantfile in short format (:ip => ...), or provider format
    # (:ovirt__network_name => ...).
    options = scoped_hash_override(options, :ovirt)
    options = {
      :netmask      => '255.255.255.0',
      :network_name => 'rhevm'
    }.merge(options)

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

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

    adapters[free_slot] = options
  end

  # Create each interface as new domain device
  adapters.each_with_index do |opts, slot_number|
    next if slot_number == 0
    iface_number = slot_number + 1

    #require 'pp'
    #pp env[:ovirt_client].networks(:cluster => env[:ovirt_cluster].id)

    # Get network id
    network = OVirtProvider::Util::Collection.find_matching(
      env[:ovirt_client].networks(:cluster_id => env[:ovirt_cluster].id),
      opts[:network_name])
    if network == nil
      raise Errors::NoNetworkError,
        :network_name => opts[:network_name]
    end

    @logger.info("Creating network interface nic#{iface_number}")
    begin
      machine.add_interface(
        :name    => "nic#{iface_number}",
        :network => network.id,

        # TODO This should be configurable in Vagrantfile.
        :interface => 'virtio',
      )
    rescue => e
      raise Errors::AddInterfaceError,
        :error_message => e.message
    end
  end

  # Continue the middleware chain.
  @app.call(env)

  # Configure interfaces that user requested. Machine should be up and
  # running now.
  networks_to_configure = []

  adapters.each_with_index do |opts, slot_number|
    # Skip configuring first interface. It's used for provisioning and
    # it has to be available during provisioning - ifdown command is
    # not acceptable here.
    next if slot_number == 0

    network = {
      :interface => slot_number,
      #:mac => ...,
    }

    if opts[:ip]
      network = {
        :type    => :static,
        :ip      => opts[:ip],
        :netmask => opts[:netmask],
      }.merge(network)
    else
      network[:type] = :dhcp
    end

    networks_to_configure << network
  end

  env[:ui].info I18n.t("vagrant.actions.vm.network.configuring")
  env[:machine].guest.capability(
    :configure_networks, networks_to_configure)
end