Class: DHCPD

Inherits:
Object
  • Object
show all
Defined in:
lib/dhcpd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf_path) ⇒ DHCPD

Returns a new instance of DHCPD.



4
5
6
7
8
9
10
11
# File 'lib/dhcpd.rb', line 4

def initialize(conf_path)
  @conf_path = conf_path
  @preamble = ''
  @postamble = ''
  @config = {}

  parse
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



2
3
4
# File 'lib/dhcpd.rb', line 2

def config
  @config
end

Instance Method Details

#saveObject



35
36
37
38
39
40
41
42
# File 'lib/dhcpd.rb', line 35

def save
  tmp_path = '.tmp.dhcpd.conf'
  File.open(tmp_path, 'w') do |f|
    f.write serialize
  end

  system 'sudo', 'mv', tmp_path, @conf_path
end

#serializeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dhcpd.rb', line 17

def serialize
  out = @preamble
  out += BEGIN_PAUPER + "\n"

  @config.each do |host, host_config|
    out += "host #{host} {\n"
    host_config.each do |key, value|
      out += "\t#{key} #{value};\n"
    end
    out += "}\n"
  end

  out += END_PAUPER + "\n"
  out += @postamble

  out
end

#subnetObject



13
14
15
# File 'lib/dhcpd.rb', line 13

def subnet
  @preamble.match(/subnet (\d+\.\d+\.\d+)\.0 netmask/)[1]
end