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

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

Overview

Prepare all networks needed for domain connections.

Constant Summary collapse

@@lock =
Mutex.new

Instance Method Summary collapse

Methods included from Util::NetworkUtil

#configured_networks, #find_empty, #interface_network, #libvirt_networks, #network_interfaces

Methods included from Util::ErbTemplate

#to_xml

Constructor Details

#initialize(app, env) ⇒ CreateNetworks

Returns a new instance of CreateNetworks.



21
22
23
24
25
26
27
28
29
# File 'lib/vagrant-libvirt/action/create_networks.rb', line 21

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

  @available_networks = []
  @options = {}
  @libvirt_client = env[:machine].provider.driver.connection.client
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  if env[:machine].provider_config.qemu_use_session
    # Get a list of all (active and inactive) Libvirt networks. This
    # triggers a side effect to ensure networking is fully available
    # for VMs using sessions. It is likely that this should be done
    # to determine the correct virtual device for the management
    # network for sessions instead of assuming the default of virbr0.
    @available_networks = libvirt_networks(env[:machine].provider.driver)

    @app.call(env)
    return
  end

  # only one vm at a time should try to set up networks
  # otherwise they'll have inconsistent views of current state
  # and conduct redundant operations that cause errors
  @@lock.synchronize do
    # Iterate over networks If some network is not
    # available, create it if possible. Otherwise raise an error.
    configured_networks(env[:machine], @logger).each do |options|
      # Only need to create private networks
      next if options[:iface_type] != :private_network ||
              options.fetch(:tunnel_type, nil)
      @logger.debug "Searching for network with options #{options}"

      # should fix other methods so this doesn't have to be instance var
      @options = 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[:machine].provider.driver)

      current_network = @available_networks.detect { |network| network[:name] == @options[:network_name] }

      # 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,
        domain_name:      nil,
        ipv6_address:     options[:ipv6_address] || nil,
        ipv6_prefix:      options[:ipv6_prefix] || nil,
        created:          current_network.nil? ? false : true,
        active:           current_network.nil? ? false : current_network[:active],
        autostart:        options[:autostart] || false,
        guest_ipv6:       @options[:guest_ipv6] || 'yes',
        libvirt_network:  current_network.nil? ? nil : current_network[:libvirt_network]
      }

      if @options[:ip]
        handle_ip_option(env)
      elsif @options[:type].to_s == 'dhcp'
        handle_dhcp_private_network(env)
      elsif @options[:network_name]
        handle_network_name_option(env)
      else
        raise Errors::CreateNetworkError, error_message: @options
      end

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

  @app.call(env)
end