Sha256: 1aceea2f224f88bbf3f683534d11cc8c5ef5fc28515b85a11437e00578efe220

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

require 'tempfile'

module Landrush
  class ResolverConfig
    def self.desired_contents; <<-EOS.gsub(/^      /, '')
      # Generated by landrush, a vagrant plugin
      nameserver 127.0.0.1
      port 10053
      EOS
    end

    def self.osx?
      `uname`.chomp == 'Darwin'
    end

    def self.config_dir
      @config_dir ||= Pathname('/etc/resolver')
    end

    def self.config_file
      config_dir.join('vagrant.dev')
    end

    def self.contents_match?
      config_file.exist? && File.read(config_file) == desired_contents
    end

    def self.write_config(opts)
      puts "Mometarily using sudo to put the host config in place..."
      sudo = opts.fetch(:sudo, 'sudo')
      system "#{sudo} mkdir #{config_dir}" unless config_dir.directory?
      Tempfile.open('vagrant_landrush_host_config') do |f|
        f.write(desired_contents)
        f.close
        system "#{sudo} cp #{f.path} #{config_file}"
        system "#{sudo} chmod 644 #{config_file}"
      end
    end

    def self.ensure_config_exists(opts={})
      unless osx?
        puts "Not an OSX machine, so skipping host DNS resolver config."
        return
      end

      if contents_match?
        puts "Host DNS resolver config looks good."
      else
        puts "Need to configure the host."
        write_config(opts)
      end
    end

    def self.puts(str)
      Kernel.puts("[landrush] #{str}")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
landrush-0.8.0 lib/landrush/resolver_config.rb
landrush-0.7.1 lib/landrush/resolver_config.rb