Skip to content

Commit

Permalink
Merge pull request #78 from cogitatio/develop
Browse files Browse the repository at this point in the history
New version to fix bugs and address other issues
  • Loading branch information
cgsmith committed Nov 8, 2015
2 parents bb1a495 + aa6b3fe commit 6793417
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 83 deletions.
55 changes: 7 additions & 48 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
$:.unshift File.expand_path("../lib", __FILE__)
source 'https://rubygems.org'

Gem::Specification.new do |s|
s.name = "vagrant-hostsupdater"
s.version = 0.2
s.platform = Gem::Platform::RUBY
s.authors = "Falk Kühnel"
s.email = "[email protected]"
s.homepage = "https://github.com/cogitatio/vagrant-hostsupdater"
s.summary = "Enables Vagrant to update hosts file on the host machine"
s.description = "Enables Vagrant to update hosts file on the host machine"


# s.add_development_dependency "rake"

# # The following block of code determines the files that should be included
# # in the gem. It does this by reading all the files in the directory where
# # this gemspec is, and parsing out the ignored files from the gitignore.
# # Note that the entire gitignore(5) syntax is not supported, specifically
# # the "!" syntax, but it should mostly work correctly.
# root_path = File.dirname(__FILE__)
# all_files = Dir.chdir(root_path) { Dir.glob("**/{*,.*}") }
# all_files.reject! { |file| [".", ".."].include?(File.basename(file)) }
# gitignore_path = File.join(root_path, ".gitignore")
# gitignore = File.readlines(gitignore_path)
# gitignore.map! { |line| line.chomp.strip }
# gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ }

# unignored_files = all_files.reject do |file|
# # Ignore any directories, the gemspec only cares about files
# next true if File.directory?(file)

# # Ignore any paths that match anything in the gitignore. We do
# # two tests here:
# #
# # - First, test to see if the entire path matches the gitignore.
# # - Second, match if the basename does, this makes it so that things
# # like '.DS_Store' will match sub-directories too (same behavior
# # as git).
# #
# gitignore.any? do |ignore|
# File.fnmatch(ignore, file, File::FNM_PATHNAME) ||
# File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME)
# end
# end

# s.files = unignored_files
# s.executables = unignored_files.map { |f| f[/^bin\/(.*)/, 1] }.compact
# s.require_path = 'lib'
group :development do
gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.6.2'
end

group :plugins do
gemspec
end
95 changes: 77 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
# Vagrant::Hostsupdater

This plugin adds an entry to your /etc/hosts file on the host system.
[![Gem Version](https://badge.fury.io/rb/vagrant-hostsupdater.svg)](https://badge.fury.io/rb/vagrant-hostsupdater)
[![Gem](https://img.shields.io/gem/dt/vagrant-hostsupdater.svg)](https://rubygems.org/gems/vagrant-hostsupdater)
[![Gem](https://img.shields.io/gem/dtv/vagrant-hostsupdater.svg)](https://rubygems.org/gems/vagrant-hostsupdater)

On **up**, **resume** and **reload** commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/cogitatio/vagrant-hostsupdater?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![Twitter](https://img.shields.io/twitter/url/https/github.com/cogitatio/vagrant-hostsupdater.svg?style=social)](https://twitter.com/intent/tweet?text=Checkout%20this%20awesome%20Vagrant%20plugin!&url=https%3A%2F%2Fgithub.com%2Fcogiatio%2Fvagrant-hostsupdater&hashtags=hostsupdater,vagrant)

On **halt** and **destroy**, those entries will be removed again.
By setting the remove\_on\_suspend option, you can have them removed on **suspend**, too:

config.hostsupdater.remove_on_suspend = true
This plugin adds an entry to your /etc/hosts file on the host system.

On **up**, **resume** and **reload** commands, it tries to add the information, if its not already existant in your hosts file. If it needs to be added, you will be asked for an administrator password, since it uses sudo to edit the file.

On **halt**, **destroy**, and **suspend**, those entries will be removed again.
By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** will not remove them.


## Installation
Expand All @@ -19,17 +24,80 @@ Uninstall it with:

$ vagrant plugin uninstall vagrant-hostsupdater

Update the plugin with:

$ vagrant plugin update vagrant-hostsupdater

## Usage

At the moment, the only things you need, are the hostname and a :private_network network with a fixed ip.
You currently only need the `hostname` and a `:private_network` network with a fixed IP address.

config.vm.network :private_network, ip: "192.168.3.10"
config.vm.hostname = "www.testing.de"
config.hostsupdater.aliases = ["alias.testing.de", "alias2.somedomain.com"]

This ip and the hostname will be used for the entry in the /etc/hosts file.
This IP address and the hostname will be used for the entry in the `/etc/hosts` file.

### Skipping hostupdater

To skip adding some entries to the /etc/hosts file add `hostsupdater: "skip"` option to network configuration:

## Versions
config.vm.network "private_network", ip: "172.21.9.9", hostsupdater: "skip"

Example:

config.vm.network :private_network, ip: "192.168.50.4"
config.vm.network :private_network,
ip: "172.21.9.9",
netmask: "255.255.240.0",
hostsupdater: "skip"

## Passwordless sudo

Add the following snippet to the sudoers file (for example, to `/etc/sudoers.d/vagrant_hostupdater`) to make it
stop asking password when updating hosts file:

# Allow passwordless startup of Vagrant with vagrant-hostsupdater.
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
%sudo ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE

## Installing development version

If you would like to install vagrant-hostsupdater on the development version perform the following:

```
git clone https://github.com/cogitatio/vagrant-hostsupdater
cd vagrant-hostsupdater
git checkout develop
gem build vagrant-hostsupdater.gemspec
vagrant plugin install vagrant-hostsupdater-*.gem
```

## Contributing

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 on the `develop` branch


## Versions

### 1.0.0
* Stable release
* Feature: Added `skip` flag [#69](/../../issues/69)
* Feature: Hosts update on provision action [#65](/../../issues/65)
* Bugfix: `remove_on_suspend` should be true [#19](/../../issues/19)
* Bugfix: Line break not inserted before first host [#37](/../../issues/37)
* Bugfix: Old changes not removed in linux [#67](/../../issues/67)
* Bugfix: Writable issue on OSX [#47](/../../issues/47)
* Bugfix: Update hosts before provisioning [#31](/../../issues/31)
* Misc: Using Semantic Versioning for version number
* Misc: Added note regarding sudoers file

### 0.0.11
* bugfix: Fix additional new lines being added to hosts file (Thanks to vincentmac)
Expand Down Expand Up @@ -57,13 +125,4 @@ This ip and the hostname will be used for the entry in the /etc/hosts file.
* fixed problem with removing hosts entries on destroy command (Thanks to Andy Bohne)

### 0.0.3
* added aliases config option to define additional hostnames


## Contributing

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
* added aliases config option to define additional hostnames
2 changes: 1 addition & 1 deletion lib/vagrant-hostsupdater/Action/RemoveHosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(app, env)
def call(env)
machine_action = env[:machine_action]
if machine_action != :destroy || !@machine.id
if machine_action != :suspend || @machine.config.hostsupdater.remove_on_suspend
if machine_action != :suspend || false != @machine.config.hostsupdater.remove_on_suspend
@ui.info "Removing hosts"
removeHostEntries
else
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-hostsupdater/Action/UpdateHosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def initialize(app, env)

def call(env)
@ui.info "Checking for host entries"
@app.call(env)
addHostEntries()
@app.call(env)
end

end
Expand Down
11 changes: 7 additions & 4 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ def getIps
ips = []
@machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if key == :private_network
ip = options[:ip] if (key == :private_network || key == :public_network) && options[:hostsupdater] != "skip"
ips.push(ip) if ip
if options[:hostsupdater] == 'skip'
@ui.info 'Skipping adding host entries (config.vm.network hostsupdater: "skip" is set)'
end
end
return ips
end
Expand Down Expand Up @@ -74,8 +77,8 @@ def getHostEntries(ip, hostnames, name, uuid = self.uuid)

def addToHosts(entries)
return if entries.length == 0
content = entries.join("\n").strip
if !File.writable?(@@hosts_path)
content = entries.join("\n").strip.concat("\n")
if !File.writable_real?(@@hosts_path)
sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
else
content = "\n" + content
Expand All @@ -88,7 +91,7 @@ def addToHosts(entries)
def removeFromHosts(options = {})
uuid = @machine.id || @machine.config.hostsupdater.id
hashedId = Digest::MD5.hexdigest(uuid)
if !File.writable?(@@hosts_path)
if !File.writable_real?(@@hosts_path)
sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
else
hosts = ""
Expand Down
9 changes: 8 additions & 1 deletion lib/vagrant-hostsupdater/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class Plugin < Vagrant.plugin('2')
end

action_hook(:hostsupdater, :machine_action_up) do |hook|
hook.append(Action::UpdateHosts)
hook.prepend(Action::RemoveHosts)
hook.after(Action::RemoveHosts, Action::UpdateHosts)
end

action_hook(:hostsupdater, :machine_action_provision) do |hook|
hook.before(Vagrant::Action::Builtin::Provision, Action::UpdateHosts)
end

action_hook(:hostsupdater, :machine_action_halt) do |hook|
Expand All @@ -37,10 +42,12 @@ class Plugin < Vagrant.plugin('2')
end

action_hook(:hostsupdater, :machine_action_reload) do |hook|
hook.prepend(Action::RemoveHosts)
hook.append(Action::UpdateHosts)
end

action_hook(:hostsupdater, :machine_action_resume) do |hook|
hook.prepend(Action::RemoveHosts)
hook.append(Action::UpdateHosts)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-hostsupdater/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module VagrantPlugins
module HostsUpdater
VERSION = "0.0.11"
VERSION = "1.0.0"
end
end
18 changes: 9 additions & 9 deletions vagrant-hostsupdater.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'vagrant-hostsupdater/version'

Gem::Specification.new do |spec|
spec.name = "vagrant-hostsupdater"
spec.name = 'vagrant-hostsupdater'
spec.version = VagrantPlugins::HostsUpdater::VERSION
spec.authors = ["Falk Kühnel"]
spec.email = ["[email protected]"]
spec.authors = ['Falk Kühnel', 'Chris Smith']
spec.email = ['[email protected]', '[email protected]']
spec.description = %q{Enables Vagrant to update hosts file on the host machine}
spec.summary = %q{Enables Vagrant to update hosts file on the host machine}
spec.homepage = "https://github.com/cogitatio/vagrant-hostsupdater"
spec.license = "MIT"
spec.summary = spec.description
spec.homepage = 'https://github.com/cogitatio/vagrant-hostsupdater'
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake'
end

0 comments on commit 6793417

Please sign in to comment.