Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent wiping of addresses of other providers #169

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7c06c81
Fix for issue #108, add a flag to manage guest hosts file.
Nov 14, 2014
ef2f01c
Improve README.md for Passwordless sudo.
tjanez Oct 13, 2015
270c15d
Prevent wiping of addresses of other providers
Nov 6, 2015
7715a76
Updating Contribute section to be specific.
seth-reeser Dec 30, 2015
117dc4b
v1.7.0 release.
seth-reeser Dec 30, 2015
c92cff0
Update README.md
seth-reeser Jan 15, 2016
dcd0351
Merge pull request #167 from tjanez/readme-passwordless-sudo
seth-reeser Jan 20, 2016
11677f2
Update README.md
seth-reeser Jan 27, 2016
dd43278
Ensure permissions on /etc/hosts stay intact
Jan 29, 2016
d1ba474
Merge pull request #176 from criticalstack/fix-selinux-permissions-on…
seth-reeser Jan 29, 2016
f1b0174
Releasing v1.7.1
seth-reeser Jan 29, 2016
3b840be
Merge pull request #125 from damienjoldersma/feature/manage-guest-flag
seth-reeser Feb 11, 2016
6f4c0d7
Releasing v1.8.0
seth-reeser Feb 11, 2016
a179758
Fixes #177 - Manage the guest by default.
seth-reeser Feb 11, 2016
ab8b041
Prevent wiping of addresses of other providers
Nov 6, 2015
2b5e0ae
Merge branch 'master' of github.com:lomignet/vagrant-hostmanager
Feb 19, 2016
494a3e3
Merge pull request #117 from pbitty/separate_aliases_option
seth-reeser Jul 22, 2015
13d95ba
Add option add_current_fqdn
Feb 19, 2016
9b7f385
refactor to have machine list in its own method
Feb 19, 2016
abac3e8
handle short name and removing from 127.*
Feb 19, 2016
43a88f8
remove old debug statement
Feb 19, 2016
aa39351
Prepare vagrant-hostoverseer release
Feb 22, 2016
b30f835
prepare public release
Feb 22, 2016
2a54f78
Add git url in gemspec
Feb 22, 2016
ae39240
Learning how to publish ruby gems...
Feb 22, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 106 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
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=)

`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
Expand All @@ -16,17 +38,17 @@ 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
------------
Install the plugin following the typical Vagrant 1.1 procedure:

$ vagrant plugin install vagrant-hostmanager
$ vagrant plugin install vagrant-oversser

Usage
-----
Expand All @@ -45,6 +67,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`
Expand All @@ -59,12 +84,24 @@ 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```.

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
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|
Expand All @@ -77,9 +114,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.

Expand Down Expand Up @@ -120,13 +157,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.

- Add the following snippet to the sudoers file (e.g.
`/etc/sudoers.d/vagrant_hostmanager`):

Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp /home/user/.vagrant.d/tmp/hosts.local /etc/hosts
%sudo ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE
```
Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp <home-directory>/.vagrant.d/tmp/hosts.local /etc/hosts
%<admin-group> ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE
```

Replace `<home-directory>` with your actual home directory (e.g.
`/home/joe`) and `<admin-group>` 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 `<admin-group>`:

```
usermod -aG <admin-group> <user-name>
```

Replace `<admin-group>` with the group that is used by the system for sudo
access (see above) and `<user-name>` with you user name.

Windows support
---------------
Expand All @@ -148,25 +203,44 @@ 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
----------
Contributions are welcome.

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
To contribute, fork then clone the repository, and then the following:

**Developing**

1. Install [Bundler](http:https://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`

**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:

*(only contributors of the GitHub repo and owners of the project at RubyGems will have rights to do this)*

1. First, bump the version in ~/lib/vagrant-hostmanager/version.rb:
* Follow [Semantic Versioning](http:https://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`
12 changes: 7 additions & 5 deletions lib/vagrant-hostmanager/action/update_all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 9 additions & 5 deletions lib/vagrant-hostmanager/action/update_guest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant-hostmanager/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down
12 changes: 11 additions & 1 deletion lib/vagrant-hostmanager/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,41 @@ 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
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
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
@ip_resolver = UNSET_VALUE
@aliases_on_separate_lines = UNSET_VALUE
@add_current_fqdn = UNSET_VALUE
end

def finalize!
@enabled = false if @enabled == UNSET_VALUE
@manage_host = false if @manage_host == 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
@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

Expand All @@ -37,6 +46,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!
Expand Down
Loading