devopsgroup-io / vagrant-hostmanager

:pencil: A Vagrant plugin that manages hosts files within a multi-machine environment.
Mozilla Public License 2.0
1.46k stars 146 forks source link

Fix incorrectly named file for Windows support #229

Closed pearcec closed 7 years ago

pearcec commented 7 years ago

https://github.com/devopsgroup-io/vagrant-hostmanager/issues/228

228

BadgerOps commented 7 years ago

Just ran into this issue as well:

==> windows: Updating /etc/hosts file on active guest machines...
The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mv -force /tmp/hosts/hosts.windows C:\Windows\System32\drivers\etc\hosts

Stdout from the command:

Stderr from the command:

Cannot find path 'C:\tmp\hosts\hosts.windows' because it does not exist.
At line:1 char:1
+ mv -force /tmp/hosts/hosts.windows C:\Windows\System32\drivers\etc\hosts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\tmp\hosts\hosts.windows:String) [Move-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand

Double checked c:\tmp and the file is indeed 'hosts' (Screenshot attached) screen shot 2017-04-23 at 10 49 30 pm

austinlparker commented 7 years ago

This is impacting us as well. @seth-reeser can this be merged and released?

seth-reeser commented 7 years ago

Can everyone please provide a snippet from their Vagrant files as to how you're defining your machines? Providing that you use the syntax below this is a non-issue:

Vagrant.configure("2") do |config|
  config.vm.define "windows_server-2012r2-standard-amd64-nocm" do |config|
    ...
  end
end
BadgerOps commented 7 years ago

Here's ours @seth-reeser edit: fixed indent

Vagrant.configure('2') do |config|
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end
  if Vagrant.has_plugin?("vagrant-hostmanager")
  config.hostmanager.enabled = true
  config.hostmanager.manage_host = false
  config.hostmanager.manage_guest = true
  config.hostmanager.ignore_private_ip = false
  config.hostmanager.include_offline = true
  end
  config.vm.define 'windows', autostart: false do |windows|
      windows.vm.provider "virtualbox" do |v|
        v.linked_clone = true
      end
      windows.vm.box = WINDOWS_BOX
      windows.vm.hostname = 'windows'
      windows.vm.communicator = 'winrm'
      windows.winrm.username = 'Administrator'
      windows.winrm.password = 'vagrant'
      windows.vm.network 'private_network', ip: '192.168.50.6'
      windows.vm.network 'forwarded_port', host: 33389, guest: 3389
      windows.vm.provision :salt do |salt|
        salt.minion_config = 'config/minion'
        salt.minion_key = 'config/key/windows.pem'
        salt.minion_pub = 'config/key/windows.pub'
        salt.masterless = false
        salt.run_highstate = true
        salt.version = SALT_VERSION
      end
    end
  end
seth-reeser commented 7 years ago

@BadgerOps, what version of Vagrant and vagrant-hostmanager are you running?

BadgerOps commented 7 years ago
Variks:~ badger$ vagrant --version
Vagrant 1.9.3
Variks:~ badger$ vagrant plugin list
vagrant-hostmanager (1.8.5)
  - Version Constraint: > 0

@seth-reeser I'm happy to help test with anything too - just let me know what I can do to help :)

seth-reeser commented 7 years ago

@BadgerOps what should be happening is a hosts file be created in a c:\tmp\hosts directory with a file name of hosts.#{machine.name}. Try deleting that hosts file in c:\tmp and try again. In order for the true c:\drivers\etc\hosts file to get updated, you'll need to remove any vagrant-hostmanager blocks, as a file comparison is performed.

austinlparker commented 7 years ago

Our Vagrantfile looks like this, roughly -


  config.hostmanager.enabled = true
  config.hostmanager.manage_host = true
  config.hostmanager.manage_guest = true
  config.hostmanager.ignore_private_ip = false
  config.hostmanager.include_offline = false
  ...
  config.vm.define "boxcar-dc" do |dc|
    ...
  end
end

The error we're seeing is as follows:

mv -force /tmp/hosts/hosts.boxcar-dc C:\Windows\System32\drivers\etc\hosts

Stdout from the command:

Stderr from the command:

Cannot find path 'C:\tmp\hosts\hosts.boxcar-dc' because it does not exist.
At line:1 char:1
+ mv -force /tmp/hosts/hosts.boxcar-dc C:\Windows\System32\drivers\etc\hosts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\tmp\hosts\hosts.boxcar-dc:String) [Move-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand
seth-reeser commented 7 years ago

What are the operating systems of your workstations and guest virtual machines in question?

BadgerOps commented 7 years ago

Hi seth - I'm re-testing now, Host: OS X Sierra VM1: RHEL7.3 VM2: opentable/win-2012r2-standard-amd64-nocm

austinlparker commented 7 years ago

Host is either Windows 10 or MacOS Sierra, guest is opentable/win-2012r2-standard-amd64-nocm

BadgerOps commented 7 years ago

ok @seth-reeser just re-tested with a fresh VM and the above Vagrant config:

The following WinRM command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mv -force /tmp/hosts/hosts.windows C:\Windows\System32\drivers\etc\hosts

Stdout from the command:

Stderr from the command:

Cannot find path 'C:\tmp\hosts\hosts.windows' because it does not exist.
At line:1 char:1
+ mv -force /tmp/hosts/hosts.windows C:\Windows\System32\drivers\etc\hosts
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\tmp\hosts\hosts.windows:String) [Move-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand

and c:\tmp\hosts exists - but not hosts.default or hosts.#{machine.name}

edit: re-ran 'vagrant up' with debug logging turned on, and I see the following:

DEBUG updater: file is: /Users/badger/.vagrant.d/tmp/hosts.windows
DEBUG updater: class of file is: Pathname
 INFO environment: Getting machine: saltmaster (virtualbox)
 INFO environment: Returning cached machine: saltmaster (virtualbox)
 INFO environment: Getting machine: linux-1 (virtualbox)
 INFO environment: Returning cached machine: linux-1 (virtualbox)
 INFO environment: Getting machine: windows (virtualbox)
 INFO environment: Returning cached machine: windows (virtualbox)
 INFO winrm: Uploading: /Users/badger/.vagrant.d/tmp/hosts.windows to /tmp/hosts <<< this is where we're breaking
DEBUG winrmshell: creating hash for file /tmp/hosts
DEBUG winrmshell: Running check_files.ps1
BadgerOps commented 7 years ago

Ah, looks like maybe this line ?I'm not that familiar with ruby, but could it simply be missing a trailing / ?

BadgerOps commented 7 years ago

Aha! got it solved @seth-reeser,

DEBUG winrmshell: Finished uploading /Users/badger/.vagrant.d/tmp/hosts.windows to /tmp/hosts/hosts.windows (1.264 KB over 1 chunks) in (0m0.77s)
DEBUG winrmshell: No remote files to extract, skipping
DEBUG winrmshell: Uploaded 1 items dirty_check: (0m1.46s) stream_files: (0m0.77s) extract: (0m0.00s)
DEBUG winrm: powershell executing:
mv -force /tmp/hosts/hosts.windows C:\Windows\System32\drivers\etc\hosts
DEBUG winrmshell: [WinRM] opening remote shell on http://127.0.0.1:55985/wsman

the solution was to modify the following lines:

 -            machine.communicate.upload(file.to_s, '/tmp/hosts')
 +            machine.communicate.upload(file.to_s, "/tmp/hosts.#{machine.name}")
              if windir
 -              machine.communicate.sudo("mv -force /tmp/hosts/hosts.#{machine.name} #{realhostfile}")
 +              machine.communicate.sudo("mv -force /tmp/hosts.#{machine.name} #{realhostfile}")
              else
 -              machine.communicate.sudo("cat /tmp/hosts > #{realhostfile}")
 +              machine.communicate.sudo("cat /tmp/hosts.#{machine.name} > #{realhostfile} && rm /tmp/hosts.#{machine.name}")       

I've submitted a PR with this change here unless @pearcec wants to update this one.

Thanks!