Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cgsmith authored Nov 24, 2020
2 parents 9b2e71f + a8d43ed commit 8960f2e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Falk Kühnel
Copyright 2018 Chris Smith

MIT License

Expand Down
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ To allow vagrant to automatically update the hosts file without asking for a sud
For Ubuntu and most Linux environments:

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

Expand All @@ -105,7 +105,7 @@ For MacOS:
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
%admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE

- If vagrant still asks for a password on commands that trigger the `VAGRANT_HOSTS_ADD` alias above (like **up**), you might need to wrap the echo statement in quotes, i.e. `Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c 'echo "*" >> /etc/hosts'`. This seems to be a problem with older versions of Linux and MacOS.
- If vagrant still asks for a password on commands that trigger the `VAGRANT_HOSTS_REMOVE` alias above (like
**halt** or **suspend**), this might indicate that the location of **sed** in the `VAGRANT_HOSTS_REMOVE` alias is
pointing to the wrong location. The solution is to find the location of **sed** (ex. `which sed`) and
Expand Down Expand Up @@ -137,6 +137,18 @@ For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures
* The tag informations be unique for the instance
* Enable Elastic IP for the instance

## Using Google as a provider

If you'd like a Google provider using [vagrant-google](https://github.com/mitchellh/vagrant-google), this plugin will detect the public IP from the name of the instance.
[vagrant-google](https://github.com/mitchellh/vagrant-google) provides a default name, but you can specify your own as follows:

config.vm.provider :google do |google, override|
google.name = "somename"
...
end

* [Google Cloud SDK](https://cloud.google.com/sdk/) is required.

## Installing development version

If you would like to install vagrant-hostsupdater on the development version perform the following:
Expand All @@ -160,9 +172,17 @@ vagrant plugin install vagrant-hostsupdater-*.gem

## Versions

### 1.1.1
* Bugfix: AWS Feature broke part of the code [#155](/../../issues/155)

### 1.1.0
* Feature: Added AWS support [#74](/../../pull/74)
* Feature: Added libvirt provider [#122](/../../pull/122)
* Feature: Add support for multiple private network adapters [#96](/../../pull/96)
* Feature: Add support for VMs without private/public networking [#23](/../../issues/23)
* Feature: Add Docker support [#149](/../../pull/149)
* Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
* Bugfix: Documentation update and type fix
* Misc: Added a note about suppressing UAC prompts

### 1.0.2
Expand Down
54 changes: 36 additions & 18 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ def getIps

if ip = getAwsPublicIp
ips.push(ip)
elsif ip = getGooglePublicIp
ips.push(ip)
else
@machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if (key == :private_network || key == :public_network) && options[:hostsupdater] != "skip"
ips.push(ip) if ip
if options[:hostsupdater] == 'skip'
@ui.info '[vagrant-hostsupdater] Skipping adding host entries (config.vm.network hostsupdater: "skip" is set)'
@machine.config.vm.networks.each do |network|
key, options = network[0], network[1]
ip = options[:ip] if (key == :private_network || key == :public_network) && options[:hostsupdater] != "skip"
ips.push(ip) if ip
if options[:hostsupdater] == 'skip'
@ui.info '[vagrant-hostsupdater] Skipping adding host entries (config.vm.network hostsupdater: "skip" is set)'
end
end
end
Expand All @@ -39,9 +41,11 @@ def getIps
ips.push(ssh_info[:host])
end
end

return ips
end
if not ips.any?
ips.push( '127.0.0.1' )
end
return ips.uniq
end

# Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
def getHostnames(ips)
Expand Down Expand Up @@ -119,7 +123,7 @@ def host_entry(ip, hostnames, name, uuid = self.uuid)
end

def createHostEntry(ip, hostname, name, uuid = self.uuid)
%Q(#{ip} #{hostname} #{signature(name, uuid)})
%Q(#{ip} #{hostname} #{signature(name, uuid.to_s)})
end

# Create a regular expression that will match *any* entry describing the
Expand Down Expand Up @@ -181,11 +185,14 @@ def removeFromHosts(options = {})

def removeFromSshKnownHosts
if !@isWindowsHost
hostnames = getHostnames
hostnames.each do |hostname|
command = %Q(sed -i -e '/#{hostname}/ d' #@@ssh_known_hosts_path)
if system(command)
@ui.info "[vagrant-hostsupdater] Removed host: #{hostname} from ssh_known_hosts file: #@@ssh_known_hosts_path"
ips = getIps
hostnames = getHostnames(ips)
ips.each do |ip|
hostnames[ip].each do |hostname|
command = %Q(sed -i -e '/#{hostname}/ d' #@@ssh_known_hosts_path)
if system(command)
@ui.info "[vagrant-hostsupdater] Removed host: #{hostname} from ssh_known_hosts file: #@@ssh_known_hosts_path"
end
end
end
end
Expand Down Expand Up @@ -214,10 +221,8 @@ def adviseOnSudo
@ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges"
end

private

def getAwsPublicIp
return nil if ! defined?(VagrantPlugins::AWS)
return nil if ! Vagrant.has_plugin?("vagrant-aws")
aws_conf = @machine.config.vm.get_provider_config(:aws)
return nil if ! aws_conf.is_a?(VagrantPlugins::AWS::Config)
filters = ( aws_conf.tags || [] ).map {|k,v| sprintf('"Name=tag:%s,Values=%s"', k, v) }.join(' ')
Expand All @@ -233,6 +238,19 @@ def getAwsPublicIp
return nil
end
end

def getGooglePublicIp
return nil if ! defined?(VagrantPlugins::Google)
google_conf = @machine.config.vm.get_provider_config(:google)
return nil if ! google_conf.is_a?(VagrantPlugins::Google::Config)
cmd = 'gcloud compute instances list --filter="name=%s" --format="value(networkInterfaces[0].accessConfigs[0].natIP)"'
cmd = sprintf(cmd, google_conf.name)
stdout, stderr, stat = Open3.capture3(cmd)
@ui.error "Failed to execute '#{cmd}' : #{stderr}" if stderr != ''
ip = stdout.strip
return nil if stat.exitstatus != 0 || ip == nil || ip == ''
return ip
end
end
end
end
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 = '1.0.2'
VERSION = '1.1.1'
end
end

0 comments on commit 8960f2e

Please sign in to comment.