Class: VagrantPlugins::ProviderKvm::Util::NetworkDefinition

Inherits:
Object
  • Object
show all
Includes:
DefinitionAttributes
Defined in:
lib/vagrant-kvm/util/network_definition.rb

Instance Method Summary collapse

Methods included from DefinitionAttributes

included

Constructor Details

#initialize(name, definition = nil) ⇒ NetworkDefinition



10
11
12
13
14
15
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
# File 'lib/vagrant-kvm/util/network_definition.rb', line 10

def initialize(name, definition=nil)
  # create with defaults
  # XXX defaults should move to config
  self.attributes = {
    :forward => "nat",
    :domain_name => "vagrant.local",
    :base_ip => "192.168.123.1",
    :netmask => "255.255.255.0",
    :range => {
      :start => "192.168.123.100",
      :end => "192.168.123.200",
    },
    :hosts => [],
    name: name,
  }

  if definition
    doc = REXML::Document.new definition
    if doc.elements["/network/forward"]
      set(:forward, doc.elements["/network/forward"].attributes["mode"]) 
    end

    if doc.elements["/network/domain"]
      set(:domain_name, doc.elements["/network/domain"].attributes["name"]) 
    end
    set(:base_ip, doc.elements["/network/ip"].attributes["address"])
    set(:netmask, doc.elements["/network/ip"].attributes["netmask"])
    set(:range, {
      :start => doc.elements["/network/ip/dhcp/range"].attributes["start"],
      :end => doc.elements["/network/ip/dhcp/range"].attributes["end"]
    })
    hosts = []
    doc.elements.each("/network/ip/dhcp/host") do |host|
      hosts << {
        :mac => host.attributes["mac"],
        :name => host.attributes["name"],
        :ip => host.attributes["ip"]
      }
    end
    set(:hosts, hosts)
  end
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
# File 'lib/vagrant-kvm/util/network_definition.rb', line 53

def ==(other)
  # Don't compare the hosts
  [:forward, :domain_name, :base_ip, :netmask, :range,].all? do |key|
    get(key) == other.get(key)
  end
end

#already_exist_host?(config) ⇒ Boolean



77
78
79
80
81
82
# File 'lib/vagrant-kvm/util/network_definition.rb', line 77

def already_exist_host?(config)
  hosts.each do |host|
    return true if host[:mac]==config[:mac]
  end
  false
end

#already_exist_ip?(config) ⇒ Boolean



84
85
86
87
88
89
# File 'lib/vagrant-kvm/util/network_definition.rb', line 84

def already_exist_ip?(config)
  hosts.each do |host|
    return true if host[:ip]==config[:ip]
  end
  false
end

#as_host_xmlObject

Provide host xml block to update definiton through libvirt(>= 0.5.0)



65
66
67
68
69
70
71
# File 'lib/vagrant-kvm/util/network_definition.rb', line 65

def as_host_xml
  xml = ""
  hosts.each do |host|
    xml = xml + "<host mac='#{host[:mac]}' name='#{host[:name]}' ip='#{host[:ip]}' />"
  end
  xml
end

#as_xmlObject



60
61
62
# File 'lib/vagrant-kvm/util/network_definition.rb', line 60

def as_xml
  KvmTemplateRenderer.render("libvirt_network", attributes.dup)
end

#hostsObject



73
74
75
# File 'lib/vagrant-kvm/util/network_definition.rb', line 73

def hosts
  get(:hosts)
end