Sha256: 2b21438bb228c32091fb30e9ad59074d0820c71a9cb0f1c5c4dd95358f1e1101

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

class DHCPD
  attr_reader :config

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

    parse
  end

  def save
    tmp_path = '.tmp.dhcpd.conf'
    File.open(tmp_path, 'w') do |f|
      f.write @preamble
      f.puts BEGIN_BUM

      @config.each do |host, host_config|
        f.puts "host #{host} {"
        host_config.each do |key, value|
          f.puts "\t#{key} #{value};"
        end
        f.puts "}"
      end

      f.puts END_BUM
      f.write @postamble
    end

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

  def restart
    system 'sudo "/Library/Application Support/VMware Fusion/boot.sh" --restart >>vmware.log 2>&1'
  end


  private

  BEGIN_BUM = "#### BEGIN BUM ####"
  END_BUM = "#### END BUM ####"

  def parse
    state = :preamble
    host = nil

    File.open(@conf_path).each_line do |rawline|
      line = rawline.strip

      case state
      when :preamble
        if line == BEGIN_BUM
          state = :outside_host
        else
          @preamble << rawline
        end

      when :outside_host
        next unless line =~ /^host .* \{$/
        words = line.split(' ').map { |x| x.strip }
        host = words[1]
        @config[host] = {}
        state = :inside_host

      when :inside_host
        next if line.size == 0 || line.strip =~ /^#/
        if line == '}'
          state = :outside_host
          host = nil
        elsif line == END_BUM
          state = :postamble
        else
          parts = line.chomp(';').split(' ')
          @config[host][parts[0..-2].join(' ')] = parts[-1]
        end

      when :postamble
        @postamble << rawline
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
pauper-0.0.4 lib/dhcpd.rb
bum-0.0.4 lib/dhcpd.rb
bum-0.0.3 lib/dhcpd.rb
bum-0.0.2 lib/dhcpd.rb
bum-0.0.1 lib/dhcpd.rb