Sha256: 54e0f90bbd52635df311f94bbd9ddfeaa609d376af065e034bc0b63e36184b93

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

module Landrush
  module Cap
    module Darwin
      class ConfigureVisibilityOnHost
        class << self
          attr_writer :sudo, :config_dir

          def configure_visibility_on_host(env, _ip, tld)
            @env = env
            @tld = tld
            if contents_match?
              info 'Host DNS resolver config looks good.'
            else
              info 'Need to configure the host.'
              write_config!
            end
          end

          private

          def sudo
            @sudo ||= 'sudo'
          end

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

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

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

          def config_file
            config_dir.join(@tld)
          end

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

          def write_config!
            info 'Momentarily using sudo to put the host config in place...'
            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
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
landrush-1.2.0 lib/landrush/cap/host/darwin/configure_visibility_on_host.rb
landrush-1.1.2 lib/landrush/cap/host/darwin/configure_visibility_on_host.rb
landrush-1.1.1 lib/landrush/cap/host/darwin/configure_visibility_on_host.rb
landrush-1.1.0 lib/landrush/cap/host/darwin/configure_visibility_on_host.rb