Module: VagrantPlugins::BetterHosts::BetterHosts
- Included in:
- Action::BaseAction
- Defined in:
- lib/vagrant-betterhosts/BetterHosts.rb
Overview
Plugin module
Instance Method Summary collapse
- #add_betterhost_entries(ip_address, hostnames) ⇒ Object
- #add_host_entries ⇒ Object
- #check_hostnames_to_add(ip_address, hostnames) ⇒ Object
- #disable_clean(ip_address) ⇒ Object
- #generate_hostnames_by_ips ⇒ Object
- #get_clean_parameter_by_system(ip_address, is_win) ⇒ Object
- #get_cli ⇒ Object
-
#get_hostnames(ips) ⇒ Object
Get a hash of hostnames indexed by ip, e.g.
- #get_ips ⇒ Object
- #get_os_binary ⇒ Object
- #print_readme(error, error_text, command) ⇒ Object
- #remove_betterhost_entries(ip_address, hostnames) ⇒ Object
- #remove_host_entries ⇒ Object
Instance Method Details
#add_betterhost_entries(ip_address, hostnames) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 122 def add_betterhost_entries(ip_address, hostnames) cli = get_cli if cli.include? ".exe" clean = get_clean_parameter_by_system(ip_address, true) command = "Start-Process '#{cli}' -ArgumentList \"add\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs" stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", command) else clean = get_clean_parameter_by_system(ip_address, false) command = "sudo '#{cli}' add #{clean} #{ip_address} #{hostnames}" stdin, stdout, stderr, wait_thr = Open3.popen3(command) end return stdin, stdout, stderr, wait_thr, command end |
#add_host_entries ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 136 def add_host_entries error = false error_text = '' command = '' hostnames_by_ips = generate_hostnames_by_ips return if hostnames_by_ips.none? @ui.info "[vagrant-betterhosts] Checking for host entries" hostnames_by_ips.each do |ip_address, hostnames| if ip_address.nil? @ui.error "[vagrant-betterhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}" next end # filter out the hosts we've already added hosts_to_add = check_hostnames_to_add(ip_address, hostnames) next if hosts_to_add.empty? _stdin, _stdout, stderr, wait_thr, command = add_betterhost_entries(ip_address, hosts_to_add) unless wait_thr.value.success? error = true error_text = stderr.read.strip end end print_readme(error, error_text, command) end |
#check_hostnames_to_add(ip_address, hostnames) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 103 def check_hostnames_to_add(ip_address, hostnames) hostnames_to_add = Array.new hostnames = hostnames.split # check which hostnames actually need adding hostnames.each do |hostname| begin address = Resolv.getaddress(hostname) if address != ip_address hostnames_to_add.append(hostname) end rescue StandardError => _e hostnames_to_add.append(hostname) end rescue StandardError => _e hostnames_to_add.append(hostname) end return hostnames_to_add.join(' ') end |
#disable_clean(ip_address) ⇒ Object
96 97 98 99 100 101 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 96 def disable_clean(ip_address) unless ip_address.nil? return @machine.config.betterhosts.disable_clean end return true end |
#generate_hostnames_by_ips ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 234 def generate_hostnames_by_ips ips = get_ips return [] unless ips.any? hostnames_by_ips = {} hostnames = get_hostnames(ips) ips.each do |ip| hostnames_by_ips[ip] = hostnames[ip].join(' ') if hostnames[ip].any? end hostnames_by_ips end |
#get_clean_parameter_by_system(ip_address, is_win) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 204 def get_clean_parameter_by_system(ip_address, is_win) clean = "--clean" if is_win clean = "\"--clean\"," end if disable_clean(ip_address) clean = '' end return clean end |
#get_cli ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 64 def get_cli binary = get_os_binary path = format('%s%s', File.(File.dirname(File.dirname(__FILE__))), "/vagrant-betterhosts/bundle/") path = "#{path}#{binary}" return path end |
#get_hostnames(ips) ⇒ Object
Get a hash of hostnames indexed by ip, e.g. { ‘ip1’: [‘host1’], ‘ip2’: [‘host2’, ‘host3’] }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 73 def get_hostnames(ips) hostnames = Hash.new { |h, k| h[k] = [] } case @machine.config.betterhosts.aliases when Array hostnames[ips[0]] += @machine.config.betterhosts.aliases when Hash # complex definition of aliases for various ips @machine.config.betterhosts.aliases.each do |ip, hosts| hostnames[ip] += Array(hosts) end end # handle default hostname(s) if not already specified in the aliases Array(@machine.config.vm.hostname).each do |host| next unless hostnames.none? { |_, v| v.include?(host) } hostnames[ips[0]].unshift host end hostnames end |
#get_ips ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 11 def get_ips ips = [] if @machine.provider_name == :docker @ui.info '[vagrant-betterhosts] Docker detected, adding 127.0.0.1 and ::1 IP addresses' ip = "127.0.0.1" ips.push(ip) unless ip.nil? or ips.include? ip ip = "::1" ips.push(ip) unless ip.nil? or ips.include? ip return ips end if @machine.config.vm.networks.length == 0 @ui.error("[vagrant-betterhosts] No ip address found for this virtual machine") exit end @machine.config.vm.networks.each do |network| key, = network[0], network[1] if [:betterhosts] == "skip" @ui.info '[vagrant-betterhosts] Skipped adding host entries (config.vm.network betterhosts: "skip" is set)' end ip = [:ip] if (key == :private_network || key == :public_network) && [:betterhosts] != "skip" ips.push(ip) if ip end if @machine.provider_name == :hyperv ip = @machine.provider.driver.read_guest_ip["ip"] @ui.info "[vagrant-betterhosts] Read guest IP #{ip} from Hyper-V provider" ips.push(ip) unless ip.nil? or ips.include? ip end return ips end |
#get_os_binary ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 44 def get_os_binary if OS.windows? return 'cli.exe' elsif OS.mac? if Etc.uname[:version].include? 'ARM64' return 'cli_arm64_osx' else return 'cli_amd64_osx' end elsif OS.linux? if Etc.uname[:version].include? 'ARM64' return 'cli_arm64_linux' else return 'cli_amd64_linux' end else raise Error::WebDriverError, "unknown os: #{host_os.inspect}" end end |
#print_readme(error, error_text, command) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 216 def print_readme(error, error_text, command) unless error @ui.info "[vagrant-betterhosts] Finished processing" return false end cli = get_cli @ui.error "[vagrant-betterhosts] Issue executing goodhosts CLI: #{error_text}" @ui.error "[vagrant-betterhosts] Command: #{command}" @ui.error "[vagrant-betterhosts] Cli path: #{cli}" if cli.include? ".exe" @ui.error "[vagrant-betterhosts] Check the readme at https://github.com/betterhosts/vagrant#windows-uac-prompt" exit else @ui.error "[vagrant-betterhosts] Check the readme at https://github.com/betterhosts/vagrant#passwordless-sudo" end end |
#remove_betterhost_entries(ip_address, hostnames) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 165 def remove_betterhost_entries(ip_address, hostnames) cli = get_cli if cli.include? ".exe" clean = get_clean_parameter_by_system(ip_address, true) command = "Start-Process '#{cli}' -ArgumentList \"remove\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs" stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", command) else clean = get_clean_parameter_by_system(ip_address, false) command = "sudo '#{cli}' remove #{clean} #{ip_address} #{hostnames}" stdin, stdout, stderr, wait_thr = Open3.popen3(command) end return stdin, stdout, stderr, wait_thr, command end |
#remove_host_entries ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/vagrant-betterhosts/BetterHosts.rb', line 179 def remove_host_entries error = false error_text = '' command = '' hostnames_by_ips = generate_hostnames_by_ips return if hostnames_by_ips.none? @ui.info "[vagrant-betterhosts] Removing hosts" hostnames_by_ips.each do |ip_address, hostnames| if ip_address.nil? @ui.error "[vagrant-betterhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}" next end _stdin, _stdout, stderr, wait_thr, command = remove_betterhost_entries(ip_address, hostnames) unless wait_thr.value.success? error = true error_text = stderr.read.strip end end print_readme(error, error_text, command) end |