vagrant-hostmanager icon indicating copy to clipboard operation
vagrant-hostmanager copied to clipboard

no update on Windows host

Open kimpixel opened this issue 9 years ago • 6 comments

Hi

i try it on 3 different Windows hosts: Windows 7, Windows 8 and Windows 10

I have absolutely no clue what is wrong. plz help

Vagrant 1.7.4 or Vagrant 1.8.1


config.vm.hostname = "myhost.local" config.vm.network "private_network", ip: "192.168.33.63" config.hostmanager.enabled = true config.hostmanager.manage_host = true config.hostmanager.manage_guest = false config.hostmanager.ignore_private_ip = false


kimpixel avatar Feb 19 '16 22:02 kimpixel

Please provide your entire Vagrantfile, excluding any secrets.

seth-reeser avatar Mar 14 '16 22:03 seth-reeser

i have similiar problem

the hosts file on windows is not being updated

here my vagrant file:

Vagrant.configure(2) do |config|

  config.hostmanager.enabled = true
  config.hostmanager.manage_host = true
  config.hostmanager.manage_guest = true
  config.hostmanager.ignore_private_ip = false 
  config.hostmanager.include_offline = true
  config.vm.define 'api' do |node|
    node.vm.hostname = 'api.dev'
    node.vm.network :private_network, ip: '192.168.10.10'
    node.hostmanager.aliases = %w(www.api.dev)
  end

  config.vm.box = "lemp.dev"

  config.vm.synced_folder "www", "/var/www/html"

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
  end
end

The host file on windows only being updated when i run

vagrant hostmanager

Is it really necessary to run that command? I thought the configuration should handle that automatically..Please help, i have been pulling my hair for weeks now

nmsobri avatar May 04 '16 13:05 nmsobri

Mine don´t update even running vagrant hostmanager

killua99 avatar Jun 17 '16 20:06 killua99

Here's the vagrant hostmanager --debug output.

https://gist.github.com/killua99/2a5243e8dc7daf1d60c9ac2e6974371f

Hope it can help to solve this issue.

killua99 avatar Jun 17 '16 23:06 killua99

I have given up on the stock solution and am using a custom resolver, I hope this helps some people:

      config.hostmanager.ip_resolver = proc do |machine|
        result = ""
        machine.communicate.execute("gwmi Win32_NetworkAdapterConfiguration | Where { $_.IPAddress } | Select -Expand IPAddress | Select-Object -Skip 2 | Select-Object -First 1") do |type, data|
          result << data if type == :stdout
        end
        (ip = /(\d+\.\d+\.\d+\.\d+)/.match(result)) && ip[1]
      end

Don't shoot me for the shoddy scripting, after banging my head against the wall for a few hours, this is the best I could come up with and you can easily adapt it to suit your needs, you will not find any other working example for Windows with vagrant-hostmanager. You can find my full Vagrantfile in #200.

TomaszDom avatar Jun 20 '16 05:06 TomaszDom

I also have this problem.... I know this is closed but this might help people create a solution to this problem. I realised that the issue for me was with the read-only attribute remained when the ruby FileUtils.cp was used. A simple fix seems to be compounded by the fact that win32ole acts asynchronously and ruby just continues before it has completed - so you cannot safely alter a files attributes using Ruby before and back again after the win32ole action e.g. if you want to xcopy the hosts.local to hosts as read-only.

The patch (not fully tested) is below. The main difference is ensuring the hosts.local has the read-only attribute turned off, and that xcopy is used to copy the file back to the read-only hosts file (and then ensure it is read-only after this action for security reasons.)

diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb
index ba8f38b..499ded9 100644
--- a/lib/vagrant-hostmanager/hosts_file/updater.rb
+++ b/lib/vagrant-hostmanager/hosts_file/updater.rb
@@ -71,6 +71,11 @@ module VagrantPlugins
 
           FileUtils.cp(hosts_location, file)
           
+          # Update: In windows make sure the file attribute is NOT read-only
+          if Vagrant::Util::Platform.windows?
+            system "attrib -R #{file}"
+          end
+
           if update_file(file, nil, true, line_endings)
             copy_proc.call
           end
@@ -202,7 +207,7 @@ module VagrantPlugins
             source, dest = [source, dest].map { |s| s.to_s.gsub(/\//, '\\') }
 
             # run 'cmd /C copy ...' with elevated privilege, minimized
-            copy_cmd = "copy \"#{source}\" \"#{dest}\""
+            copy_cmd = "echo F | xcopy /r /k /y \"#{source}\" \"#{dest}\" && attrib +R \"#{dest}\""
             WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{copy_cmd}", nil, 'runas', 7)
 
             # Unfortunately, ShellExecute does not give us a status code,

NOTE: An alternative to testing if windows and setting the attribute might be to replace the FileUtils.cp function that copies the hosts to hosts.local with 2 system calls inside the 'if WindowsSupport.windows?' section e.g.

system "copy #{hosts_location} #{file}"
system "attrib -R #{file}"

PS

Just discovered an alternative to win32ole using system (which I believe is synchronous so might be useful when you need the copy to finish before continuing...). Try this as a .rb with the the source as your hosts file :

source='C:\Windows\System32\drivers\etc\hosts'
dest='PUTADESTINATIONFORTHEFILEHERE'
cmdout = "powershell Start-Process -FilePath 'powershell' -ArgumentList 'copy', 
\"#{source}\",\"#{dest}\" -Verb RunAs -PassThru"
print cmdout
system "#{cmdout}"
print "End"

xymox12 avatar Aug 05 '19 11:08 xymox12