From 7c06c812f6cd6fd56e97fcdd2bb04e248f7247c8 Mon Sep 17 00:00:00 2001 From: Damien Joldersma Date: Fri, 14 Nov 2014 12:42:54 -0800 Subject: [PATCH 01/21] Fix for issue #108, add a flag to manage guest hosts file. --- README.md | 4 ++++ lib/vagrant-hostmanager/action/update_all.rb | 12 +++++++----- lib/vagrant-hostmanager/action/update_guest.rb | 14 +++++++++----- lib/vagrant-hostmanager/command.rb | 1 + lib/vagrant-hostmanager/config.rb | 5 +++++ lib/vagrant-hostmanager/provisioner.rb | 4 +++- test/Vagrantfile | 1 + 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f851728..282b6b2 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ Vagrantfile to activate this behavior. To update the host's `/etc/hosts` file, set the `hostmanager.manage_host` attribute to `true`. +To update the guests' `/etc/hosts` file, set the `hostmanager.manage_guest` +attribute to `true`. + A machine's IP address is defined by either the static IP for a private network configuration or by the SSH host configuration. To disable using the private network IP address, set `config.hostmanager.ignore_private_ip` @@ -60,6 +63,7 @@ Example configuration: 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 'example-box' do |node| diff --git a/lib/vagrant-hostmanager/action/update_all.rb b/lib/vagrant-hostmanager/action/update_all.rb index f7b88ee..ee87734 100644 --- a/lib/vagrant-hostmanager/action/update_all.rb +++ b/lib/vagrant-hostmanager/action/update_all.rb @@ -27,11 +27,13 @@ def call(env) @app.call(env) # update /etc/hosts file on active machines - env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests') - @global_env.active_machines.each do |name, p| - if p == @provider - machine = @global_env.machine(name, p) - @updater.update_guest(machine) + if @machine.config.hostmanager.manage_guest? + env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests') + @global_env.active_machines.each do |name, p| + if p == @provider + machine = @global_env.machine(name, p) + @updater.update_guest(machine) + end end end diff --git a/lib/vagrant-hostmanager/action/update_guest.rb b/lib/vagrant-hostmanager/action/update_guest.rb index 3ca99d9..a957608 100644 --- a/lib/vagrant-hostmanager/action/update_guest.rb +++ b/lib/vagrant-hostmanager/action/update_guest.rb @@ -8,18 +8,22 @@ class UpdateGuest def initialize(app, env) @app = app + global_env = env[:global_env] + @config = Util.get_config(global_env) @machine = env[:machine] @updater = HostsFile::Updater.new(@machine.env, env[:provider]) @logger = Log4r::Logger.new('vagrant::hostmanager::update_guest') end def call(env) - env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', { - :name => @machine.name - }) - @updater.update_guest(@machine) + if @config.hostmanager.manage_guest? + env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', { + :name => @machine.name + }) + @updater.update_guest(@machine) - @app.call(env) + @app.call(env) + end end end end diff --git a/lib/vagrant-hostmanager/command.rb b/lib/vagrant-hostmanager/command.rb index 5d320c3..a52fe4e 100644 --- a/lib/vagrant-hostmanager/command.rb +++ b/lib/vagrant-hostmanager/command.rb @@ -27,6 +27,7 @@ def execute # update /etc/hosts file for specified guest machines with_target_vms(argv, options) do |machine| @env.action_runner.run(Action.update_guest, { + :global_env => @env, :machine => machine, :provider => options[:provider] }) diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 4e3d3dc..45a90c5 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -3,6 +3,7 @@ module HostManager class Config < Vagrant.plugin('2', :config) attr_accessor :enabled attr_accessor :manage_host + attr_accessor :manage_guest attr_accessor :ignore_private_ip attr_accessor :aliases attr_accessor :include_offline @@ -11,10 +12,12 @@ class Config < Vagrant.plugin('2', :config) alias_method :enabled?, :enabled alias_method :include_offline?, :include_offline alias_method :manage_host?, :manage_host + alias_method :manage_guest?, :manage_guest def initialize @enabled = UNSET_VALUE @manage_host = UNSET_VALUE + @manage_guest = UNSET_VALUE @ignore_private_ip = UNSET_VALUE @include_offline = UNSET_VALUE @aliases = UNSET_VALUE @@ -24,6 +27,7 @@ def initialize def finalize! @enabled = false if @enabled == UNSET_VALUE @manage_host = false if @manage_host == UNSET_VALUE + @manage_guest = false if @manage_guest == UNSET_VALUE @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE @include_offline = false if @include_offline == UNSET_VALUE @aliases = [] if @aliases == UNSET_VALUE @@ -37,6 +41,7 @@ def validate(machine) errors << validate_bool('hostmanager.enabled', @enabled) errors << validate_bool('hostmanager.manage_host', @manage_host) + errors << validate_bool('hostmanager.manage_guest', @manage_guest) errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip) errors << validate_bool('hostmanager.include_offline', @include_offline) errors.compact! diff --git a/lib/vagrant-hostmanager/provisioner.rb b/lib/vagrant-hostmanager/provisioner.rb index 1576c73..cbe6f7e 100644 --- a/lib/vagrant-hostmanager/provisioner.rb +++ b/lib/vagrant-hostmanager/provisioner.rb @@ -12,7 +12,9 @@ def initialize(machine, config) end def provision - @updater.update_guest(@machine) + if @config.hostmanager.manage_guest? + @updater.update_guest(@machine) + end if @config.hostmanager.manage_host? @updater.update_host end diff --git a/test/Vagrantfile b/test/Vagrantfile index 59ca656..33c5280 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -16,6 +16,7 @@ Vagrant.configure('2') do |config| config.hostmanager.enabled = true config.hostmanager.manage_host = true + config.hostmanager.manage_guest = true config.vm.define :server1 do |server| server.vm.hostname = 'fry' From ef2f01cbc3cb898e96f96f5be3c414d0ffe40785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Jane=C5=BE?= Date: Tue, 13 Oct 2015 10:53:00 +0200 Subject: [PATCH 02/21] Improve README.md for Passwordless sudo. Add information that the usual sudo group on Fedora/Red Hat systems is named 'wheel'. Generalize instructions to make them distribution-agnostic. Add step on adding yourself to the administrator group. --- README.md | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 81cd9dd..201e50e 100644 --- a/README.md +++ b/README.md @@ -120,13 +120,31 @@ end Passwordless sudo ----------------- -Add the following snippet to the sudoers file (for example, to -```/etc/sudoers.d/vagrant_hostmanager```) to make it stop asking -password when updating hosts file (replace ```/home/user``` with your -actual home directory): +To avoid being asked for the password every time the hosts file is updated, +enable passwordless sudo for the specific command that hostmanager uses to +update the hosts file. - Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp /home/user/.vagrant.d/tmp/hosts.local /etc/hosts - %sudo ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE + - Add the following snippet to the sudoers file (e.g. + `/etc/sudoers.d/vagrant_hostmanager`): + + ``` + Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp /.vagrant.d/tmp/hosts.local /etc/hosts + % ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE + ``` + + Replace `` with your actual home directory (e.g. + `/home/joe`) and `` with the group that is used by the system + for sudo access (usually `sudo` on Debian/Ubuntu systems and `wheel` + on Fedora/Red Hat systems). + + - If necessary, add yourself to the ``: + + ``` + usermod -aG + ``` + + Replace `` with the group that is used by the system for sudo + access (see above) and `` with you user name. Windows support --------------- From 270c15d903cf334e584c2700885070767e84dbee Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 6 Nov 2015 15:29:58 +0100 Subject: [PATCH 03/21] Prevent wiping of addresses of other providers Hostmanager can only deal with one provider at a time. If you `vagrant up` a virtualbox vm, then an aws, all information of your virtulabox vm will be removed from /etc/hosts on the host. By adding the provider type in the header/footer of the hostmanager section of /etc/hosts, we can have one section per provider and thus keep all machines, no matter the provider. --- lib/vagrant-hostmanager/hosts_file/updater.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 9514508..ef469bf 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -82,8 +82,8 @@ def update_file(file, resolving_machine = nil, include_id = true) def update_content(file_content, resolving_machine, include_id) id = include_id ? " id: #{read_or_create_id}" : "" - header = "## vagrant-hostmanager-start#{id}\n" - footer = "## vagrant-hostmanager-end\n" + header = "## vagrant-hostmanager-start-#{@provider}#{id}\n" + footer = "## vagrant-hostmanager-end-#{@provider}\n" body = get_machines .map { |machine| get_hosts_file_entry(machine, resolving_machine) } .join From 7715a76e3b285d1449dd7b64da5fea3cc3df8729 Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Wed, 30 Dec 2015 09:47:37 -0500 Subject: [PATCH 04/21] Updating Contribute section to be specific. --- README.md | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 81cd9dd..518eec1 100644 --- a/README.md +++ b/README.md @@ -161,12 +161,33 @@ rake gem:build vagrant plugin install pkg/vagrant-hostmanager-*.gem ``` + Contribute ---------- -Contributions are welcome. +To contribute, fork then clone the repository, and then the following: + +**Developing** + +1. Install [Bundler](http://bundler.io/) +2. Currently the Bundler version is locked to 1.6.9, please install this version. + * `sudo gem install bundler -v '1.6.9'` +3. Then install vagrant-hostmanager dependancies: + * `bundle _1.6.9_ install` + +**Releasing** + +To release a new version of vagrant-hostmanager you will need to do the following: + +*(only contributors of the GitHub repo and owners of the project at RubyGems will have rights to do this)* -1. Fork it -2. Create your feature branch (`git checkout -b my-new-feature`) -3. Commit your changes (`git commit -am 'Add some feature'`) -4. Push to the branch (`git push origin my-new-feature`) -5. Create new Pull Request +1. First, bump the version in ~/lib/vagrant-hostmanager/version.rb: + * Follow [Semantic Versioning](http://semver.org/). +2. Then, create a matching GitHub Release (this will also create a tag): + * Preface the version number with a `v`. + * https://github.com/smdahlen/vagrant-hostmanager/releases +3. You will then need to build and push the new gem to RubyGems: + * `rake gem:build` + * `gem push pkg/vagrant-hostmanager-1.6.1.gem` +4. Then, when John Doe runs the following, they will receive the updated vagrant-hostmanager plugin: + * `vagrant plugin update` + * `vagrant plugin update vagrant-hostmanager` From 117dc4bee7b8b65b0acbcbb7746d0cef2f273217 Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Wed, 30 Dec 2015 09:50:40 -0500 Subject: [PATCH 05/21] v1.7.0 release. --- lib/vagrant-hostmanager/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index 2b5cc41..83cb4e5 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '1.6.1' + VERSION = '1.7.0' end end From c92cff0617e1e0944ac3af718e56374d51884883 Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Fri, 15 Jan 2016 12:53:49 -0500 Subject: [PATCH 06/21] Update README.md --- README.md | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 518eec1..051011f 100644 --- a/README.md +++ b/README.md @@ -148,19 +148,6 @@ and give your user Modify permission. Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any visible errors, however the ```hosts``` file will not be updated. -Installing development version ------------------------------- - -If you want to install the bleeding version of vagrant-hostmanager (*at your own risk*), you can do the following -(requires ruby and git): - -``` -git clone https://github.com/smdahlen/vagrant-hostmanager.git -cd vagrant-hostmanager -rake gem:build -vagrant plugin install pkg/vagrant-hostmanager-*.gem -``` - Contribute ---------- @@ -174,6 +161,17 @@ To contribute, fork then clone the repository, and then the following: 3. Then install vagrant-hostmanager dependancies: * `bundle _1.6.9_ install` +**Testing** + +1. Build and package your newly developed code: + * `rake gem:build` +2. Then install the packaged plugin: + * `vagrant plugin install pkg/vagrant-hostmanager-*.gem` +3. Once you're done testing, roll-back to the latest released version: + * `vagrant plugin uninstall vagrant-hostmanager` + * `vagrant plugin install vagrant-hostmanager` +4. Once you're satisfied developing and testing your new code, please submit a pull request for review. + **Releasing** To release a new version of vagrant-hostmanager you will need to do the following: From 11677f246e1ea4d99a96ef3b549c56dc2fa55b76 Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Wed, 27 Jan 2016 14:26:19 -0500 Subject: [PATCH 07/21] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fe600d0..3470a30 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ Vagrant Host Manager ==================== +[![Gem](https://img.shields.io/gem/v/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager) [![Gem](https://img.shields.io/gem/dt/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager) [![Gem](https://img.shields.io/gem/dtv/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager) [![Twitter](https://img.shields.io/twitter/url/https/github.com/smdahlen/vagrant-hostmanager.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20Vagrant%20plugin%21&url=https%3A%2F%2Fgithub.com%2Fsmdahlen%2Fvagrant-hostmanager&hashtags=vagrant%hostmanager&original_referer=) From dd43278e83aaf3e383a4186f59f74eaeca68f493 Mon Sep 17 00:00:00 2001 From: Tony Lambiris Date: Fri, 29 Jan 2016 13:30:33 -0500 Subject: [PATCH 08/21] Ensure permissions on /etc/hosts stay intact --- lib/vagrant-hostmanager/hosts_file/updater.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 9514508..97d412c 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -17,17 +17,14 @@ def update_guest(machine) if (machine.communicate.test("uname -s | grep SunOS")) realhostfile = '/etc/inet/hosts' - move_cmd = 'mv' elsif (machine.communicate.test("test -d $Env:SystemRoot")) windir = "" machine.communicate.execute("echo %SYSTEMROOT%", {:shell => :cmd}) do |type, contents| windir << contents.gsub("\r\n", '') if type == :stdout end realhostfile = "#{windir}\\System32\\drivers\\etc\\hosts" - move_cmd = 'mv -force' else realhostfile = '/etc/hosts' - move_cmd = 'mv -f' end # download and modify file with Vagrant-managed entries file = @global_env.tmp_path.join("hosts.#{machine.name}") @@ -37,11 +34,9 @@ def update_guest(machine) # upload modified file and remove temporary file machine.communicate.upload(file, '/tmp/hosts') if windir - machine.communicate.sudo("#{move_cmd} /tmp/hosts/hosts.#{machine.name} #{realhostfile}") - elsif machine.communicate.test('test -f /.dockerinit') - machine.communicate.sudo("cat /tmp/hosts > #{realhostfile}") + machine.communicate.sudo("mv -force /tmp/hosts/hosts.#{machine.name} #{realhostfile}") else - machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}") + machine.communicate.sudo("cat /tmp/hosts > #{realhostfile}") end end From f1b0174c113fad554d0180ee29d8c89fec1d8c31 Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Fri, 29 Jan 2016 13:48:13 -0500 Subject: [PATCH 09/21] Releasing v1.7.1 --- lib/vagrant-hostmanager/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index 83cb4e5..cef4ad2 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '1.7.0' + VERSION = '1.7.1' end end From 6f4c0d7c6a5f2365e1353f7fec08950a5c874c3c Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Thu, 11 Feb 2016 10:54:33 -0500 Subject: [PATCH 10/21] Releasing v1.8.0 --- lib/vagrant-hostmanager/version.rb | 2 +- vagrant-hostmanager.gemspec | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index cef4ad2..3386fb9 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '1.7.1' + VERSION = '1.8.0' end end diff --git a/vagrant-hostmanager.gemspec b/vagrant-hostmanager.gemspec index 19f98e4..cb63b8f 100644 --- a/vagrant-hostmanager.gemspec +++ b/vagrant-hostmanager.gemspec @@ -7,8 +7,8 @@ require 'vagrant-hostmanager/version' Gem::Specification.new do |gem| gem.name = 'vagrant-hostmanager' gem.version = VagrantPlugins::HostManager::VERSION - gem.authors = ['Shawn Dahlen'] - gem.email = ['shawn@dahlen.me'] + gem.authors = ['Shawn Dahlen','Seth Reeser'] + gem.email = ['shawn@dahlen.me','info@devopsgroup.io'] gem.description = %q{A Vagrant plugin that manages the /etc/hosts file within a multi-machine environment} gem.summary = gem.description gem.license = 'MIT' From a179758eafebac4128b05a7b9197f3132fc59f4d Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Thu, 11 Feb 2016 14:13:12 -0500 Subject: [PATCH 11/21] Fixes #177 - Manage the guest by default. --- lib/vagrant-hostmanager/config.rb | 2 +- lib/vagrant-hostmanager/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 45a90c5..1ae26f4 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -27,7 +27,7 @@ def initialize def finalize! @enabled = false if @enabled == UNSET_VALUE @manage_host = false if @manage_host == UNSET_VALUE - @manage_guest = false if @manage_guest == UNSET_VALUE + @manage_guest = true if @manage_guest == UNSET_VALUE @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE @include_offline = false if @include_offline == UNSET_VALUE @aliases = [] if @aliases == UNSET_VALUE diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index 3386fb9..5a8d603 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '1.8.0' + VERSION = '1.8.1' end end From ab8b04184e84d6265a38987fafc9270885227399 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 6 Nov 2015 15:29:58 +0100 Subject: [PATCH 12/21] Prevent wiping of addresses of other providers Hostmanager can only deal with one provider at a time. If you `vagrant up` a virtualbox vm, then an aws, all information of your virtulabox vm will be removed from /etc/hosts on the host. By adding the provider type in the header/footer of the hostmanager section of /etc/hosts, we can have one section per provider and thus keep all machines, no matter the provider. --- lib/vagrant-hostmanager/hosts_file/updater.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 97d412c..7d85358 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -77,8 +77,8 @@ def update_file(file, resolving_machine = nil, include_id = true) def update_content(file_content, resolving_machine, include_id) id = include_id ? " id: #{read_or_create_id}" : "" - header = "## vagrant-hostmanager-start#{id}\n" - footer = "## vagrant-hostmanager-end\n" + header = "## vagrant-hostmanager-start-#{@provider}#{id}\n" + footer = "## vagrant-hostmanager-end-#{@provider}\n" body = get_machines .map { |machine| get_hosts_file_entry(machine, resolving_machine) } .join From 494a3e377c11b341225fdf1c63d36aebbccc0211 Mon Sep 17 00:00:00 2001 From: Seth Reeser Date: Wed, 22 Jul 2015 14:09:16 -0400 Subject: [PATCH 13/21] Merge pull request #117 from pbitty/separate_aliases_option make separating aliases into multiple lines optional Conflicts: test/Vagrantfile --- README.md | 5 +++++ lib/vagrant-hostmanager/config.rb | 3 +++ lib/vagrant-hostmanager/hosts_file/updater.rb | 13 ++++++++++++- test/Vagrantfile | 6 +++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b146a79..a688a85 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,11 @@ up or have a private ip configured will be added to the hosts file. In addition, the `hostmanager.aliases` configuration attribute can be used to provide aliases for your host names. +On some systems, long alias lines have been reported to cause issues +(see [#60](https://github.com/smdahlen/vagrant-hostmanager/issues/60)). +In such cases, you may render aliases on separate lines by setting +```hostmanager.aliases_on_separate_lines = true```. + Example configuration: ```ruby diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 1ae26f4..7dc2e3f 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -8,6 +8,7 @@ class Config < Vagrant.plugin('2', :config) attr_accessor :aliases attr_accessor :include_offline attr_accessor :ip_resolver + attr_accessor :aliases_on_separate_lines alias_method :enabled?, :enabled alias_method :include_offline?, :include_offline @@ -22,6 +23,7 @@ def initialize @include_offline = UNSET_VALUE @aliases = UNSET_VALUE @ip_resolver = UNSET_VALUE + @aliases_on_separate_lines = UNSET_VALUE end def finalize! @@ -32,6 +34,7 @@ def finalize! @include_offline = false if @include_offline == UNSET_VALUE @aliases = [] if @aliases == UNSET_VALUE @ip_resolver = nil if @ip_resolver == UNSET_VALUE + @aliases_on_separate_lines = false if @aliases_on_separate_lines == UNSET_VALUE @aliases = [ @aliases ].flatten end diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 7d85358..8676038 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -89,8 +89,19 @@ def get_hosts_file_entry(machine, resolving_machine) ip = get_ip_address(machine, resolving_machine) host = machine.config.vm.hostname || machine.name aliases = machine.config.hostmanager.aliases + if ip != nil - "#{ip}\t#{host}\n" + aliases.map{|a| "#{ip}\t#{a}"}.join("\n") + "\n" + # As per GH-60, we optionally render aliases on separate lines + current_machine_config = ((resolving_machine && resolving_machine.config) || @config) + if current_machine_config.hostmanager.aliases_on_separate_lines + rendered_aliases = aliases.map { |a| "#{ip}\t#{a}" }.join("\n") + separator = "\n" + else + rendered_aliases = aliases.join(" ") + separator = "\t" + end + + "#{ip}\t#{host}" + separator + rendered_aliases + "\n" end end diff --git a/test/Vagrantfile b/test/Vagrantfile index 33c5280..de89dbb 100644 --- a/test/Vagrantfile +++ b/test/Vagrantfile @@ -17,16 +17,20 @@ Vagrant.configure('2') do |config| config.hostmanager.enabled = true config.hostmanager.manage_host = true config.hostmanager.manage_guest = true + # config.hostmanager.aliases_on_separate_lines = true + config.vm.define :server1 do |server| server.vm.hostname = 'fry' server.vm.network :private_network, :ip => '10.0.5.2' - server.hostmanager.aliases = %w(test-alias) + server.hostmanager.aliases = %w(alias1 alias2) end config.vm.define :server2 do |server| server.vm.hostname = 'bender' server.vm.network :private_network, :ip => '10.0.5.3' + server.hostmanager.aliases = %w(alias3 alias4) + server.hostmanager.aliases_on_separate_lines = true end config.vm.define :server3 do |server| From 13d95bac8d63f20f00e47ba2d82a855f4621d59c Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 19 Feb 2016 12:00:22 +0100 Subject: [PATCH 14/21] Add option add_current_fqdn --- README.md | 18 +++++++---- lib/vagrant-hostmanager/config.rb | 4 ++- lib/vagrant-hostmanager/hosts_file/updater.rb | 32 +++++++++++++++---- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a688a85..81c22da 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ for some providers. Version 1.2 reverts this feature until a suitable implementa supporting all providers is available. ***Potentially breaking change in v1.5.0:*** the running order on `vagrant up` has changed -so that hostmanager runs before provisioning takes place. This ensures all hostnames are -available to the guest when it is being provisioned +so that hostmanager runs before provisioning takes place. This ensures all hostnames are +available to the guest when it is being provisioned (see [#73](https://github.com/smdahlen/vagrant-hostmanager/issues/73)). -Previously, hostmanager would run as the very last action. If you depend on the old behavior, +Previously, hostmanager would run as the very last action. If you depend on the old behavior, see the [provisioner](#provisioner) section. Installation @@ -68,6 +68,12 @@ On some systems, long alias lines have been reported to cause issues In such cases, you may render aliases on separate lines by setting ```hostmanager.aliases_on_separate_lines = true```. +If you have all your aliases on one line and you do not manage the fqdn fully +from vagrant (AWS for instance) you might want to add the fqdn as +well on this line to have only one canonical line. In such case, set +```hostmanager.add_current_fqdn = true```. + + Example configuration: ```ruby @@ -87,9 +93,9 @@ end ### Provisioner -Starting at version 1.5.0, `vagrant up` runs hostmanager before any provisioning occurs. -If you would like hostmanager to run after or during your provisioning stage, -you can use hostmanager as a provisioner. This allows you to use the provisioning +Starting at version 1.5.0, `vagrant up` runs hostmanager before any provisioning occurs. +If you would like hostmanager to run after or during your provisioning stage, +you can use hostmanager as a provisioner. This allows you to use the provisioning order to ensure that hostmanager runs when desired. The provisioner will collect hosts from boxes with the same provider as the running box. diff --git a/lib/vagrant-hostmanager/config.rb b/lib/vagrant-hostmanager/config.rb index 7dc2e3f..a2716a3 100644 --- a/lib/vagrant-hostmanager/config.rb +++ b/lib/vagrant-hostmanager/config.rb @@ -9,6 +9,7 @@ class Config < Vagrant.plugin('2', :config) attr_accessor :include_offline attr_accessor :ip_resolver attr_accessor :aliases_on_separate_lines + attr_accessor :add_current_fqdn alias_method :enabled?, :enabled alias_method :include_offline?, :include_offline @@ -24,6 +25,7 @@ def initialize @aliases = UNSET_VALUE @ip_resolver = UNSET_VALUE @aliases_on_separate_lines = UNSET_VALUE + @add_current_fqdn = UNSET_VALUE end def finalize! @@ -35,7 +37,7 @@ def finalize! @aliases = [] if @aliases == UNSET_VALUE @ip_resolver = nil if @ip_resolver == UNSET_VALUE @aliases_on_separate_lines = false if @aliases_on_separate_lines == UNSET_VALUE - + @add_current_fqdn = false if @add_current_fqdn == UNSET_VALUE @aliases = [ @aliases ].flatten end diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 8676038..7431589 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -89,19 +89,27 @@ def get_hosts_file_entry(machine, resolving_machine) ip = get_ip_address(machine, resolving_machine) host = machine.config.vm.hostname || machine.name aliases = machine.config.hostmanager.aliases + all_names = [host] + aliases if ip != nil # As per GH-60, we optionally render aliases on separate lines - current_machine_config = ((resolving_machine && resolving_machine.config) || @config) + current_machine_config = ((resolving_machine && resolving_machine.config) || @config) + + # Optionally prepend current fqdn as well. Useful when hostname set outside + # vagrant (eg. aws) + if current_machine_config.hostmanager.add_current_fqdn + fqdn = get_fqdn(resolving_machine) + unless fqdn.nil? + # put fqdn in front, removing it from the rest of the line if present + all_names = [fqdn] + all_names.select { |a| a != fqdn } + end + end + if current_machine_config.hostmanager.aliases_on_separate_lines - rendered_aliases = aliases.map { |a| "#{ip}\t#{a}" }.join("\n") - separator = "\n" + all_names.map { |a| "#{ip}\t#{a}" }.join("\n") + "\n" else - rendered_aliases = aliases.join(" ") - separator = "\t" + "#{ip}\t" + all_names.join(" ") + "\n" end - - "#{ip}\t#{host}" + separator + rendered_aliases + "\n" end end @@ -122,6 +130,16 @@ def get_ip_address(machine, resolving_machine) end end + def get_fqdn(machine) + fqdn = nil + unless machine.nil? + machine.communicate.execute('/bin/hostname -f') do |type, hostname| + fqdn = hostname.strip + end + end + fqdn + end + def get_machines if @config.hostmanager.include_offline? machines = @global_env.machine_names From 9b7f385263a4cdada6be6aa66a560295e537e96b Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 19 Feb 2016 14:26:30 +0100 Subject: [PATCH 15/21] refactor to have machine list in its own method --- lib/vagrant-hostmanager/hosts_file/updater.rb | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 7431589..45d091d 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -10,6 +10,7 @@ def initialize(global_env, provider) @global_env = global_env @config = Util.get_config(@global_env) @provider = provider + @current_machine_config = nil end def update_guest(machine) @@ -29,6 +30,7 @@ def update_guest(machine) # download and modify file with Vagrant-managed entries file = @global_env.tmp_path.join("hosts.#{machine.name}") machine.communicate.download(realhostfile, file) + @current_machine_config = ((machine && machine.config) || @config) if update_file(file, machine, false) # upload modified file and remove temporary file @@ -87,28 +89,13 @@ def update_content(file_content, resolving_machine, include_id) def get_hosts_file_entry(machine, resolving_machine) ip = get_ip_address(machine, resolving_machine) - host = machine.config.vm.hostname || machine.name - aliases = machine.config.hostmanager.aliases - all_names = [host] + aliases - if ip != nil - # As per GH-60, we optionally render aliases on separate lines - current_machine_config = ((resolving_machine && resolving_machine.config) || @config) - - # Optionally prepend current fqdn as well. Useful when hostname set outside - # vagrant (eg. aws) - if current_machine_config.hostmanager.add_current_fqdn - fqdn = get_fqdn(resolving_machine) - unless fqdn.nil? - # put fqdn in front, removing it from the rest of the line if present - all_names = [fqdn] + all_names.select { |a| a != fqdn } - end - end - - if current_machine_config.hostmanager.aliases_on_separate_lines - all_names.map { |a| "#{ip}\t#{a}" }.join("\n") + "\n" + unless ip.nil? + names = get_names(machine) + if @current_machine_config.hostmanager.aliases_on_separate_lines + names.map { |a| "#{ip}\t#{a}" }.join("\n") + "\n" else - "#{ip}\t" + all_names.join(" ") + "\n" + "#{ip}\t" + names.join(" ") + "\n" end end end @@ -130,6 +117,24 @@ def get_ip_address(machine, resolving_machine) end end + # get all names, in the right order (fqdn first if relevant) + def get_names(machine) + host = machine.config.vm.hostname || machine.name + aliases = machine.config.hostmanager.aliases + all_names = [host] + aliases + + # Optionally prepend current fqdn as well. Useful with hostname set outside + # vagrant (eg. aws) + if @current_machine_config.hostmanager.add_current_fqdn + fqdn = get_fqdn(machine) + unless fqdn.nil? + # put fqdn in front, removing it from the rest of the line if present + all_names = [fqdn] + all_names.select { |a| a != fqdn } + end + end + all_names + end + def get_fqdn(machine) fqdn = nil unless machine.nil? From abac3e841237ae42ca3411c15fc04cdfafd7fca3 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 19 Feb 2016 15:59:52 +0100 Subject: [PATCH 16/21] handle short name and removing from 127.* --- lib/vagrant-hostmanager/hosts_file/updater.rb | 64 +++++++++++++++---- 1 file changed, 50 insertions(+), 14 deletions(-) diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index 45d091d..fc4296a 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -91,7 +91,7 @@ def get_hosts_file_entry(machine, resolving_machine) ip = get_ip_address(machine, resolving_machine) unless ip.nil? - names = get_names(machine) + names = get_names(machine, resolving_machine) if @current_machine_config.hostmanager.aliases_on_separate_lines names.map { |a| "#{ip}\t#{a}" }.join("\n") + "\n" else @@ -117,8 +117,8 @@ def get_ip_address(machine, resolving_machine) end end - # get all names, in the right order (fqdn first if relevant) - def get_names(machine) + # get all names including aliases, in the right order (fqdn first if relevant) + def get_names(machine, resolving_machine) host = machine.config.vm.hostname || machine.name aliases = machine.config.hostmanager.aliases all_names = [host] + aliases @@ -126,23 +126,26 @@ def get_names(machine) # Optionally prepend current fqdn as well. Useful with hostname set outside # vagrant (eg. aws) if @current_machine_config.hostmanager.add_current_fqdn - fqdn = get_fqdn(machine) - unless fqdn.nil? - # put fqdn in front, removing it from the rest of the line if present - all_names = [fqdn] + all_names.select { |a| a != fqdn } - end + dns = get_dns(machine) + # put fqdn in front + all_names = dns + all_names end - all_names + + all_names.uniq # order is kept b uniq end - def get_fqdn(machine) - fqdn = nil + # return fqdn *and* short name + def get_dns(machine) + names = [] unless machine.nil? machine.communicate.execute('/bin/hostname -f') do |type, hostname| - fqdn = hostname.strip + names += [hostname.strip] + end + machine.communicate.execute('/bin/hostname') do |type, hostname| + names += [hostname.strip] end end - fqdn + names end def get_machines @@ -176,7 +179,40 @@ def get_new_content(header, footer, body, old_content) footer_pattern = Regexp.quote(footer) pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE) # Replace existing block or append - old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block + newcontent = old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block + + ### remove name duplication in 127.* + cleancontent = '' + + all_names = [] + # First, extract back names from the body. If a name appear there, it + # should not be anywhere else. + body.each_line do |line| + tokens = line.sub(/#.*$/, '').strip.split(/\s+/) + ip = tokens.shift + unless tokens.empty? + all_names += tokens + end + end + puts(all_names) + + # Then remove those names from any 127.* lines + newcontent.each_line do |line| + if line =~ /^\s*127\./ + # Here be dragons. + tokens = line.sub(/#.*$/, '').strip.split(/\s+/) + ip=tokens.shift + dedup = tokens - all_names + unless dedup.empty? + cleancontent += "#{ip}\t" + dedup.join(' ') + "\n" + end + else + cleancontent += line + end + end + + cleancontent + end def read_or_create_id From 43a88f83893e4226616badc3721059d05e830c0d Mon Sep 17 00:00:00 2001 From: Guillaume Date: Fri, 19 Feb 2016 16:14:33 +0100 Subject: [PATCH 17/21] remove old debug statement --- lib/vagrant-hostmanager/hosts_file/updater.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/vagrant-hostmanager/hosts_file/updater.rb b/lib/vagrant-hostmanager/hosts_file/updater.rb index fc4296a..39c1e59 100644 --- a/lib/vagrant-hostmanager/hosts_file/updater.rb +++ b/lib/vagrant-hostmanager/hosts_file/updater.rb @@ -194,7 +194,6 @@ def get_new_content(header, footer, body, old_content) all_names += tokens end end - puts(all_names) # Then remove those names from any 127.* lines newcontent.each_line do |line| From aa393516ecfab7983d2ef89223cca3fad875b374 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 22 Feb 2016 09:02:21 +0100 Subject: [PATCH 18/21] Prepare vagrant-hostoverseer release --- README.md | 23 ++++++++++++++++++++++- vagrant-hostmanager.gemspec | 6 +++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 81c22da..aa0928e 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,27 @@ Vagrant Host Manager [![Gem](https://img.shields.io/gem/dtv/vagrant-hostmanager.svg)](https://rubygems.org/gems/vagrant-hostmanager) [![Twitter](https://img.shields.io/twitter/url/https/github.com/smdahlen/vagrant-hostmanager.svg?style=social)](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20Vagrant%20plugin%21&url=https%3A%2F%2Fgithub.com%2Fsmdahlen%2Fvagrant-hostmanager&hashtags=vagrant%hostmanager&original_referer=) +`vagrant-hostoverseer` is a fork of `vagrant-hostmanager` adding a few extras: +- Manage hostnames of multiple providers, +- Add the option to manages aliases on one line only, +- Add the option of automagically add the fqdn to the alias line. + +The reasons of these patches are: +- In the upstream hostmanager, if you spawn a machine with one provider, the addresses +in all other providers are lost. +- The format of `/etc/hosts` according the man page is that one IP must appear on one line only. +- As there are a few ways to set up fqdn on a server and different tools will +use different ways to get the fqdn, only adding aliases in `/etc/hosts` created some issues, +for instance on AWS. + +I would like to have these patches merged upstream but first I am not sure upstream +considers these contributions worthwhile and second I know that there is at least an +annoying limitation where the fqdn option will work properly only for linux hosts. + +Beyond the command to install the plugin, all references and configurations are +still named hostmanager as my goal is not keep this fork, but have it eventually +properly merged. + `vagrant-hostmanager` is a Vagrant 1.1+ plugin that manages the `/etc/hosts` file on guest machines (and optionally the host). Its goal is to enable resolution of multi-machine environments deployed with a cloud provider @@ -27,7 +48,7 @@ Installation ------------ Install the plugin following the typical Vagrant 1.1 procedure: - $ vagrant plugin install vagrant-hostmanager + $ vagrant plugin install vagrant-oversser Usage ----- diff --git a/vagrant-hostmanager.gemspec b/vagrant-hostmanager.gemspec index cb63b8f..587b967 100644 --- a/vagrant-hostmanager.gemspec +++ b/vagrant-hostmanager.gemspec @@ -5,10 +5,10 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'vagrant-hostmanager/version' Gem::Specification.new do |gem| - gem.name = 'vagrant-hostmanager' + gem.name = 'vagrant-hostoverseer' gem.version = VagrantPlugins::HostManager::VERSION - gem.authors = ['Shawn Dahlen','Seth Reeser'] - gem.email = ['shawn@dahlen.me','info@devopsgroup.io'] + gem.authors = ['Shawn Dahlen','Seth Reeser','Guillaume'] + gem.email = ['shawn@dahlen.me','info@devopsgroup.io','guillaume@lomig.net'] gem.description = %q{A Vagrant plugin that manages the /etc/hosts file within a multi-machine environment} gem.summary = gem.description gem.license = 'MIT' From b30f83507f91c653fc1b67937f8af0a95f85404e Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 22 Feb 2016 09:05:31 +0100 Subject: [PATCH 19/21] prepare public release --- vagrant-hostmanager.gemspec => vagrant-hostoverseer.gemspec | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename vagrant-hostmanager.gemspec => vagrant-hostoverseer.gemspec (100%) diff --git a/vagrant-hostmanager.gemspec b/vagrant-hostoverseer.gemspec similarity index 100% rename from vagrant-hostmanager.gemspec rename to vagrant-hostoverseer.gemspec From 2a54f7828ab824bcb2315e1dc29e98a55590f97e Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 22 Feb 2016 09:10:28 +0100 Subject: [PATCH 20/21] Add git url in gemspec --- vagrant-hostoverseer.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/vagrant-hostoverseer.gemspec b/vagrant-hostoverseer.gemspec index 587b967..863651a 100644 --- a/vagrant-hostoverseer.gemspec +++ b/vagrant-hostoverseer.gemspec @@ -10,6 +10,7 @@ Gem::Specification.new do |gem| gem.authors = ['Shawn Dahlen','Seth Reeser','Guillaume'] gem.email = ['shawn@dahlen.me','info@devopsgroup.io','guillaume@lomig.net'] gem.description = %q{A Vagrant plugin that manages the /etc/hosts file within a multi-machine environment} + gem.homepage = 'https://github.com/lomignet/vagrant-hostoverseer' gem.summary = gem.description gem.license = 'MIT' From ae392406189eacb26060e2586963f09ccd749441 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 22 Feb 2016 09:13:07 +0100 Subject: [PATCH 21/21] Learning how to publish ruby gems... --- lib/vagrant-hostmanager/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-hostmanager/version.rb b/lib/vagrant-hostmanager/version.rb index 5a8d603..9e371fd 100644 --- a/lib/vagrant-hostmanager/version.rb +++ b/lib/vagrant-hostmanager/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module HostManager - VERSION = '1.8.1' + VERSION = '1.8.1.0' end end