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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, definition = nil) ⇒ NetworkDefinition

Returns a new instance of NetworkDefinition.



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

def initialize(name, definition=nil)
  @name = name
  if definition
    doc = Nokogiri::XML(definition)
    @forward = doc.at_css("network forward")["mode"] if doc.at_css("network forward")
    @domain_name = doc.at_css("network domain")["name"] if doc.at_css("network domain")
    @base_ip = doc.at_css("network ip")["address"]
    @netmask = doc.at_css("network ip")["netmask"]
    @range = {
      :start => doc.at_css("network ip dhcp range")["start"],
      :end => doc.at_css("network ip dhcp range")["end"]
    }
    @hosts = []
    doc.css("network ip dhcp host").each do |host|
      @hosts << {
        :mac => host["mac"],
        :name => host["name"],
        :ip => host["ip"]
      }
    end
  else
    # create with defaults
    # XXX defaults should move to config
    @forward = "nat"
    @domain_name = "vagrant.local"
    @base_ip = "192.168.192.1"
    @netmask = "255.255.255.0"
    @range = {
      :start => "192.168.192.100",
      :end => "192.168.192.200"}
    @hosts = []
  end
end

Instance Attribute Details

#base_ipObject (readonly)

Returns the value of attribute base_ip.



11
12
13
# File 'lib/vagrant-kvm/util/network_definition.rb', line 11

def base_ip
  @base_ip
end

#domain_nameObject (readonly)

Returns the value of attribute domain_name.



10
11
12
# File 'lib/vagrant-kvm/util/network_definition.rb', line 10

def domain_name
  @domain_name
end

#nameObject (readonly)

Attributes of the Network



9
10
11
# File 'lib/vagrant-kvm/util/network_definition.rb', line 9

def name
  @name
end

Instance Method Details

#add_host(host) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant-kvm/util/network_definition.rb', line 81

def add_host(host)
  cur_host = @hosts.detect {|h| h[:mac] == host[:mac]}
  if cur_host
    cur_host[:ip] = host[:ip]
    cur_host[:name] = host[:name]
  else
    @hosts << {
      :mac => host[:mac],
      :name => host[:name],
      :ip => host[:ip]}
  end
end

#as_xmlObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vagrant-kvm/util/network_definition.rb', line 64

def as_xml
  xml = <<-EOXML
    <network>
      <name>#{@name}</name>
      <forward mode='#{@forward}'/>
      <domain name='#{@domain_name}'/>
      <ip address='#{@base_ip}' netmask='#{@netmask}'>
        <dhcp>
        <range start='#{@range[:start]}' end='#{@range[:end]}' />
        </dhcp>
      </ip>
    </network>
  EOXML
  xml = inject_hosts(xml) if @hosts.length > 0
  xml
end

#configure(config) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vagrant-kvm/util/network_definition.rb', line 47

def configure(config)
  config = {
    :forward => @forward,
    :domain_name => @domain_name,
    :base_ip => @base_ip,
    :netmask => @netmask,
    :range => @range,
    :hosts => @hosts}.merge(config)

    @forward = config[:forward]
    @domain_name = config[:domain_name]
    @base_ip = config[:base_ip]
    @netmask = config[:netmask]
    @range = config[:range]
    @hosts = config[:hosts]
end

#inject_hosts(xml) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/vagrant-kvm/util/network_definition.rb', line 94

def inject_hosts(xml)
  doc = Nokogiri::XML(xml)
  entry_point = doc.at_css("network ip dhcp range")
  @hosts.each do |host|
    entry_point.add_next_sibling "<host mac='#{host[:mac]}' name='#{host[:name]}' ip='#{host[:ip]}' />"
  end
  doc.to_xml
end