Sha256: 2560445e55c24d4e97a09a391b4abbfa80c765b97e7fd75e5da93b89f3ee09f2

Contents?: true

Size: 1004 Bytes

Versions: 6

Compression:

Stored size: 1004 Bytes

Contents

require 'fileutils'
require 'tempfile'

RESOLV = '/etc/resolv.conf'
LOCAL = "nameserver 127.0.0.1\n"

class Module
  def redefine_const(name, value)
    __send__(:remove_const, name) if const_defined?(name)
    const_set(name, value)
  end
end

def atomic_write(path, content)
  temp_path = Tempfile.new(path)
  File.open(temp_path, 'w+') do |f|
    f.write(content)
  end
  FileUtils.mv(temp_path, path)
end

module VagrantDns
  class ResolvConf
    def append()
	input =  IO.readlines(RESOLV)
	ns_pos = input.find_index do |line|
	  line.include?('nameserver')
	end

	if(!local_defined?(input,ns_pos))
	  head = (input.slice(0,ns_pos) << LOCAL)
	  tail = input.slice(ns_pos,input.size)
	  newconf = head.concat(tail).join('')
	  atomic_write(RESOLV,newconf)
	end
    end

    def clear
	input = IO.readlines(RESOLV)
	cleaned = input.select{|line| !line.eql?(LOCAL)}.join('')
      atomic_write(RESOLV,cleaned)
    end

    def local_defined?(input,pos)
	input[pos].eql?(LOCAL.chomp)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vagrant-dns-server-0.4.3 lib/vagrant_dns/resolver.rb
vagrant-dns-server-0.4.2 lib/vagrant_dns/resolver.rb
vagrant-dns-server-0.4.1 lib/vagrant_dns/resolver.rb
vagrant-dns-server-0.4 lib/vagrant_dns/resolver.rb
vagrant-dns-server-0.3 lib/vagrant_dns/resolver.rb
vagrant-dns-server-0.2 lib/vagrant_dns/resolver.rb