Sha256: a55bec5a96b132ed1e4f75d06375986a8b19b8ca73dd1ac60371c66fc9fe7661

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

require 'tempfile'

module Landrush
  class ResolverConfig
    class << self;
      attr_writer :sudo, :config_dir
    end

    def self.sudo
      @sudo ||= 'sudo'
    end

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

    def config_dir
      self.class.config_dir
    end

    def initialize(env={})
      @env = env
    end

    def info(msg)
      @env[:ui].info("[landrush] #{msg}")
    end

    def desired_contents; <<-EOS.gsub(/^      /, '')
      # Generated by landrush, a vagrant plugin
      nameserver 127.0.0.1
      port #{Server.port}
      EOS
    end

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

    def config_dir
      self.class.config_dir
    end

    def config_file
      config_dir.join(@env[:machine].config.landrush.tld)
    end

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

    def write_config!
      info "Mometarily using sudo to put the host config in place..."
      system "#{self.class.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 ensure_config_exists!
      if contents_match?
        info "Host DNS resolver config looks good."
      else
        info "Need to configure the host."
        write_config!
      end
    end

    def sudo
      self.class.sudo
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
landrush-0.15.3 lib/landrush/resolver_config.rb
landrush-0.15.2 lib/landrush/resolver_config.rb
landrush-0.15.1 lib/landrush/resolver_config.rb
landrush-0.15.0 lib/landrush/resolver_config.rb
landrush-0.14.1 lib/landrush/resolver_config.rb
landrush-0.14.0 lib/landrush/resolver_config.rb