Class: VagrantPlugins::ProviderLibvirt::Action::CreateNetworks

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Util::NetworkIP, Vagrant::Util::ScopedHashOverride, Util::ErbTemplate, Util::LibvirtUtil
Defined in:
lib/vagrant-libvirt/action/create_networks.rb

Overview

Prepare all networks needed for domain connections.

Instance Method Summary collapse

Methods included from Util::LibvirtUtil

#libvirt_networks

Methods included from Util::ErbTemplate

#to_xml

Constructor Details

#initialize(app, env) ⇒ CreateNetworks

Returns a new instance of CreateNetworks.



16
17
18
19
20
21
22
23
24
# File 'lib/vagrant-libvirt/action/create_networks.rb', line 16

def initialize(app, env)
  mess = 'vagrant_libvirt::action::create_networks'
  @logger = Log4r::Logger.new(mess)
  @app = app

  @available_networks = []
  @options = {}
  @libvirt_client = env[:libvirt_compute].client
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)

  # Iterate over networks requested from config. If some network is not
  # available, create it if possible. Otherwise raise an error.
  env[:machine].config.vm.networks.each do |type, options|

    # Get a list of all (active and inactive) libvirt networks. This
    # list is used throughout this class and should be easier to
    # process than libvirt API calls.
    @available_networks = libvirt_networks(env[:libvirt_compute].client)

    # Now, we support private networks only. There are two other types
    # public network and port forwarding, but there are problems with
    # creating them via libvirt API, so this provider doesn't implement
    # them.
    next if type != :private_network

    # Get options for this interface network. Options can be specified
    # in Vagrantfile in short format (:ip => ...), or provider format
    # (:libvirt__network_name => ...).
    @options = scoped_hash_override(options, :libvirt)
    @options = {
      netmask:      '255.255.255.0',
      dhcp_enabled: true,
      forward_mode: 'nat',
    }.merge(@options)

    # Prepare a hash describing network for this specific interface.
    @interface_network = {
      name:             nil,
      ip_address:       nil,
      netmask:          @options[:netmask],
      network_address:  nil,
      bridge_name:      nil,
      created:          false,
      active:           false,
      autostart:        false,
      libvirt_network:  nil,
    }

    if @options[:ip]
      handle_ip_option(env)
    elsif @options[:network_name]
      handle_network_name_option
    else
      # TODO: Should be smarter than just using fixed 'default' string.
      @interface_network = lookup_network_by_name('default')
      if !@interface_network
        raise Errors::NetworkNotAvailableError,
              network_name: 'default'
      end
    end

    autostart_network if !@interface_network[:autostart]
    activate_network if !@interface_network[:active]
  end

  @app.call(env)
end