Skip to content

Commit ae33856

Browse files
committed
Replace DNS resolution with hosts file checking
Changes: - Removed dependency on 'resolv' library - Added hosts_entry_exists?() method using CLI check command - Updated check_hostnames_to_add() to use hosts file checking Benefits: - No sudo required for checking if hosts exist - Only requests sudo when hosts are actually missing - Checks hosts file directly instead of DNS resolution - Prevents unnecessary privilege escalation How it works: - Uses bundled CLI binary's check command (read-only, no sudo) - CLI check command reads /etc/hosts directly - Returns success if IP/hostname mapping exists - Only adds hosts that are actually missing This resolves VVV pain point: - No more unnecessary sudo prompts on `vagrant up` - Vagrant only requests sudo when hosts need to be added - Existing hosts in /etc/hosts are properly detected Note: Requires CLI with batch check support Will work with individual checks for backward compatibility
1 parent 0349fdd commit ae33856

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/vagrant-goodhosts/GoodHosts.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# The core of the plugin
22
require "rbconfig"
33
require "open3"
4-
require "resolv"
54
require "os"
65

76
module VagrantPlugins
@@ -98,22 +97,35 @@ def disable_clean(ip_address)
9897
return true
9998
end
10099

100+
# Check if a specific IP/hostname mapping exists in the hosts file
101+
# Uses the CLI check command instead of DNS resolution
102+
def hosts_entry_exists?(ip_address, hostname)
103+
cli = get_cli
104+
# Use check command - no sudo needed for read-only operation
105+
if cli.include? ".exe"
106+
# Windows: direct command execution
107+
stdout, stderr, status = Open3.capture3("\"#{cli}\" check \"#{ip_address}\" \"#{hostname}\"")
108+
else
109+
# Unix/macOS: no sudo needed for check
110+
stdout, stderr, status = Open3.capture3("'#{cli}' check '#{ip_address}' '#{hostname}'")
111+
end
112+
return status.success?
113+
rescue StandardError => e
114+
@ui.warn "[vagrant-goodhosts] Error checking host entry: #{e.message}"
115+
return false
116+
end
117+
101118
def check_hostnames_to_add(ip_address, hostnames)
102119
hostnames_to_add = Array.new
103-
hostnames = hostnames.split
104-
# check which hostnames actually need adding
105-
hostnames.each do |hostname|
106-
begin
107-
address = Resolv.getaddress(hostname)
108-
if address != ip_address
109-
hostnames_to_add.append(hostname)
110-
end
111-
rescue StandardError => _e
120+
hostnames_arr = hostnames.split
121+
122+
# Check each hostname using the CLI check command
123+
hostnames_arr.each do |hostname|
124+
unless hosts_entry_exists?(ip_address, hostname)
112125
hostnames_to_add.append(hostname)
113126
end
114-
rescue StandardError => _e
115-
hostnames_to_add.append(hostname)
116127
end
128+
117129
return hostnames_to_add.join(' ')
118130
end
119131

0 commit comments

Comments
 (0)