Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:cogitatio/vagrant-hostsupdater i…
Browse files Browse the repository at this point in the history
…nto develop
  • Loading branch information
cgsmith committed Aug 11, 2016
2 parents aa21ffc + baf1779 commit 1ae2737
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ To keep your /etc/hosts file unchanged simply add the line below to your `Vagran
This disables vagrant-hostsupdater from running on **suspend** and **halt**.


## Passwordless sudo
## Suppressing prompts for elevating privileges

These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
If you understand the risks that go with supressing them, here's how to do it.

### Linux/OS X: Passwordless sudo

Add the following snippet to the top of the sudoers file using `sudo visudo`. It will make vagrant
stop asking password when updating hosts file:
Expand All @@ -92,7 +97,14 @@ stop asking password when updating hosts file:
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
%admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE

## For AWS as a Provider
### Windows: UAC Prompt

You can use `cacls` or `icacls` to grant your user account permanent write permission to the system's hosts file.
You have to open an elevated command prompt; hold `❖ Win` and press `X`, then choose "Command Prompt (Admin)"

cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W

## Using AWS as a Provider

If you'd like AWS as a provider using [vagrant-aws](https://github.com/mitchellh/vagrant-aws) or other plugin,
this plugin will detect the instance public IP by the tag infomations.
Expand All @@ -108,10 +120,9 @@ For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures
end

* [AWS CLI](https://aws.amazon.com/cli/) is required
* Make the tag infomations be unique for the instance
* The tag informations be unique for the instance
* Enable Elastic IP for the instance


## Installing development version

If you would like to install vagrant-hostsupdater on the development version perform the following:
Expand All @@ -135,6 +146,11 @@ vagrant plugin install vagrant-hostsupdater-*.gem

## Versions

### next version
* Feature: Added AWS support [#74](/../../pull/74)
* Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
* Misc: Added a note about suppressing UAC prompts

### 1.0.2
* Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71)
* Feature: Skip entries if they already exist [#69](/../../issues/69)
Expand Down
19 changes: 16 additions & 3 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ def addToHosts(entries)
@ui.error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo"
adviseOnSudo
end
elsif Vagrant::Util::Platform.windows?
require 'tmpdir'
uuid = @machine.id || @machine.config.hostsupdater.id
tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid + '.cmd')
File.open(tmpPath, "w") do |tmpFile|
entries.each { |line| tmpFile.puts(">>\"#{@@hosts_path}\" echo #{line}") }
end
sudo(tmpPath)
File.delete(tmpPath)
else
content = "\n" + content
hostsFile = File.open(@@hosts_path, "a")
Expand All @@ -131,7 +140,7 @@ def addToHosts(entries)
def removeFromHosts(options = {})
uuid = @machine.id || @machine.config.hostsupdater.id
hashedId = Digest::MD5.hexdigest(uuid)
if !File.writable_real?(@@hosts_path)
if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows?
if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
@ui.error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo"
adviseOnSudo
Expand All @@ -157,15 +166,19 @@ def signature(name, uuid = self.uuid)
def sudo(command)
return if !command
if Vagrant::Util::Platform.windows?
`#{command}`
require 'win32ole'
args = command.split(" ")
command = args.shift
sh = WIN32OLE.new('Shell.Application')
sh.ShellExecute(command, args.join(" "), '', 'runas', 0)
else
return system("sudo #{command}")
end
end

def adviseOnSudo
@ui.error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:"
@ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#passwordless-sudo"
@ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges"

private

Expand Down

0 comments on commit 1ae2737

Please sign in to comment.