vagrant-hostsupdater 1.1.1.160 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 65e7b1e42f104505255bfa53fd108d470833d15bcbbb0d56df9ce5688889fa44
4
- data.tar.gz: b89c69cc787f8fedbc45d98ec143d526db1b9e29cb5140a9bd0b608cd66e3f34
3
+ metadata.gz: c931c60a9148df5bd393def93d08d89be804047f19249ccf253c4760d789f346
4
+ data.tar.gz: 89c817fbfa2232694ac757058fbeed8fc519765f07078372828ffc2a66b70167
5
5
  SHA512:
6
- metadata.gz: 5d39da2abf80d915f8d7f2c42ef95f2734c4addbcc6ef8110b02a8cd341fdd83efba813a89152acaf96512b34f67ec9689b2b32f8d793f3b89406ca6866a1997
7
- data.tar.gz: 12147031b83bd69fc6ac1dcf5ccb14fd3bc692e1d0f1598dd0a13302e964a9a006eeb595211d9c1aa2c12b0ad7126e23db1402dd6b0b38476d287bf570856559
6
+ metadata.gz: 68b582634e6a0c4fe5a10b5633a0e1bc18f7f2ce21cc78b2a0650d2413ca4c6e8ead758fc4e21455e1dba01284e3c213312bf3b2085a4559c8c06d506581a82e
7
+ data.tar.gz: 3376c1d9f2a7889aea6de4e8eda2c67661e4fe9995a5a0d6a3c4ecf15018142c20ff3f3540183997f3bdd644b2c788788bad5181c8f6719b2bb0d2cc893bb32c
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016 Falk Kühnel
1
+ Copyright 2018 Chris Smith
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  This plugin adds an entry to your /etc/hosts file on the host system.
12
12
 
13
- 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.
13
+ On **up**, **resume** and **reload** commands, it tries to add the information, if it does not already exist 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.
14
14
 
15
15
  On **halt**, **destroy**, and **suspend**, those entries will be removed again.
16
16
  By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** and **halt** will not remove them.
@@ -38,6 +38,27 @@ You currently only need the `hostname` and a `:private_network` network with a f
38
38
 
39
39
  This IP address and the hostname will be used for the entry in the `/etc/hosts` file.
40
40
 
41
+ ### Multiple private network adapters
42
+
43
+ If you have multiple network adapters i.e.:
44
+
45
+ config.vm.network :private_network, ip: "10.0.0.1"
46
+ config.vm.network :private_network, ip: "10.0.0.2"
47
+
48
+ you can specify which hostnames are bound to which IP by passing a hash mapping the IP of the network to an array of hostnames to create, e.g.:
49
+
50
+ config.hostsupdater.aliases = {
51
+ '10.0.0.1' => ['foo.com', 'bar.com'],
52
+ '10.0.0.2' => ['baz.com', 'bat.com']
53
+ }
54
+
55
+ This will produce `/etc/hosts` entries like so:
56
+
57
+ 10.0.0.1 foo.com
58
+ 10.0.0.1 bar.com
59
+ 10.0.0.2 baz.com
60
+ 10.0.0.2 bat.com
61
+
41
62
  ### Skipping hostupdater
42
63
 
43
64
  To skip adding some entries to the /etc/hosts file add `hostsupdater: "skip"` option to network configuration:
@@ -56,22 +77,77 @@ Example:
56
77
 
57
78
  To keep your /etc/hosts file unchanged simply add the line below to your `VagrantFile`:
58
79
 
59
- config.hostsupdater.resume_on_suspend = false
80
+ config.hostsupdater.remove_on_suspend = false
60
81
 
61
82
  This disables vagrant-hostsupdater from running on **suspend** and **halt**.
62
83
 
63
84
 
64
- ## Passwordless sudo
85
+ ## Suppressing prompts for elevating privileges
86
+
87
+ These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
88
+ If you understand the risks that go with supressing them, here's how to do it.
89
+
90
+ ### Linux/OS X: Passwordless sudo
91
+
92
+ To allow vagrant to automatically update the hosts file without asking for a sudo password, add one of the following snippets to a new sudoers file include, i.e. `sudo visudo -f /etc/sudoers.d/vagrant_hostsupdater`.
93
+
94
+ For Ubuntu and most Linux environments:
95
+
96
+ # Allow passwordless startup of Vagrant with vagrant-hostsupdater.
97
+ Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
98
+ Cmnd_Alias VAGRANT_HOSTS_REMOVE = /bin/sed -i -e /*/ d /etc/hosts
99
+ %sudo ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
65
100
 
66
- Add the following snippet to the top of the sudoers file using `sudo visudo`. It will make vagrant
67
- stop asking password when updating hosts file:
101
+ For MacOS:
68
102
 
69
103
  # Allow passwordless startup of Vagrant with vagrant-hostsupdater.
70
104
  Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
71
105
  Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
72
106
  %admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
73
107
 
74
-
108
+ - 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.
109
+ - If vagrant still asks for a password on commands that trigger the `VAGRANT_HOSTS_REMOVE` alias above (like
110
+ **halt** or **suspend**), this might indicate that the location of **sed** in the `VAGRANT_HOSTS_REMOVE` alias is
111
+ pointing to the wrong location. The solution is to find the location of **sed** (ex. `which sed`) and
112
+ replace that location in the `VAGRANT_HOSTS_REMOVE` alias.
113
+
114
+ ### Windows: UAC Prompt
115
+
116
+ You can use `cacls` or `icacls` to grant your user account permanent write permission to the system's hosts file.
117
+ You have to open an elevated command prompt; hold `❖ Win` and press `X`, then choose "Command Prompt (Admin)"
118
+
119
+ cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W
120
+
121
+ ## Using AWS as a Provider
122
+
123
+ If you'd like AWS as a provider using [vagrant-aws](https://github.com/mitchellh/vagrant-aws) or other plugin,
124
+ this plugin will detect the instance public IP by the tag infomations.
125
+ For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures a tag infomations like the following.
126
+
127
+ config.vm.provider :aws do |aws, override|
128
+ aws.tags = {
129
+ "Name" => "vagrant",
130
+ ...
131
+ }
132
+ aws.elastic_ip = true
133
+ ...
134
+ end
135
+
136
+ * [AWS CLI](https://aws.amazon.com/cli/) is required
137
+ * The tag informations be unique for the instance
138
+ * Enable Elastic IP for the instance
139
+
140
+ ## Using Google as a provider
141
+
142
+ 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.
143
+ [vagrant-google](https://github.com/mitchellh/vagrant-google) provides a default name, but you can specify your own as follows:
144
+
145
+ config.vm.provider :google do |google, override|
146
+ google.name = "somename"
147
+ ...
148
+ end
149
+
150
+ * [Google Cloud SDK](https://cloud.google.com/sdk/) is required.
75
151
 
76
152
  ## Installing development version
77
153
 
@@ -96,6 +172,19 @@ vagrant plugin install vagrant-hostsupdater-*.gem
96
172
 
97
173
  ## Versions
98
174
 
175
+ ### 1.1.1
176
+ * Bugfix: AWS Feature broke part of the code [#155](/../../issues/155)
177
+
178
+ ### 1.1.0
179
+ * Feature: Added AWS support [#74](/../../pull/74)
180
+ * Feature: Added libvirt provider [#122](/../../pull/122)
181
+ * Feature: Add support for multiple private network adapters [#96](/../../pull/96)
182
+ * Feature: Add support for VMs without private/public networking [#23](/../../issues/23)
183
+ * Feature: Add Docker support [#149](/../../pull/149)
184
+ * Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
185
+ * Bugfix: Documentation update and type fix
186
+ * Misc: Added a note about suppressing UAC prompts
187
+
99
188
  ### 1.0.2
100
189
  * Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71)
101
190
  * Feature: Skip entries if they already exist [#69](/../../issues/69)
@@ -143,4 +232,4 @@ vagrant plugin install vagrant-hostsupdater-*.gem
143
232
  * fixed problem with removing hosts entries on destroy command (Thanks to Andy Bohne)
144
233
 
145
234
  ### 0.0.3
146
- * added aliases config option to define additional hostnames
235
+ * added aliases config option to define additional hostnames
@@ -1,39 +1,91 @@
1
+ require 'open3'
2
+
1
3
  module VagrantPlugins
2
4
  module HostsUpdater
3
5
  module HostsUpdater
4
- @@hosts_path = Vagrant::Util::Platform.windows? ? File.expand_path('system32/drivers/etc/hosts', ENV['windir']) : '/etc/hosts'
6
+ if ENV['VAGRANT_HOSTSUPDATER_PATH']
7
+ @@hosts_path = ENV['VAGRANT_HOSTSUPDATER_PATH']
8
+ else
9
+ @@hosts_path = Vagrant::Util::Platform.windows? ? File.expand_path('system32/drivers/etc/hosts', ENV['windir']) : '/etc/hosts'
10
+ end
11
+ @isWindowsHost = Vagrant::Util::Platform.windows?
12
+ @@ssh_known_hosts_path = '~/.ssh/known_hosts'
5
13
 
6
14
  def getIps
7
15
  ips = []
8
- @machine.config.vm.networks.each do |network|
9
- key, options = network[0], network[1]
10
- ip = options[:ip] if (key == :private_network || key == :public_network) && options[:hostsupdater] != "skip"
11
- ips.push(ip) if ip
12
- if options[:hostsupdater] == 'skip'
13
- @ui.info '[vagrant-hostsupdater] Skipping adding host entries (config.vm.network hostsupdater: "skip" is set)'
16
+
17
+ if ip = getAwsPublicIp
18
+ ips.push(ip)
19
+ elsif ip = getGooglePublicIp
20
+ ips.push(ip)
21
+ else
22
+ @machine.config.vm.networks.each do |network|
23
+ key, options = network[0], network[1]
24
+ ip = options[:ip] if (key == :private_network || key == :public_network) && options[:hostsupdater] != "skip"
25
+ ips.push(ip) if ip
26
+ if options[:hostsupdater] == 'skip'
27
+ @ui.info '[vagrant-hostsupdater] Skipping adding host entries (config.vm.network hostsupdater: "skip" is set)'
28
+ end
14
29
  end
15
30
  end
16
- return ips
17
- end
18
31
 
19
- def getHostnames
20
- hostnames = Array(@machine.config.vm.hostname)
21
- if @machine.config.hostsupdater.aliases
22
- hostnames.concat(@machine.config.hostsupdater.aliases)
32
+ if @machine.provider_name == :lxc
33
+ ip = @machine.provider.capability(:public_address)
34
+ ips.push(ip)
35
+ elsif @machine.provider_name == :docker
36
+ ip = @machine.provider.capability(:public_address)
37
+ ips.push(ip)
38
+ elsif @machine.provider_name == :libvirt
39
+ ssh_info = @machine.ssh_info
40
+ if ssh_info
41
+ ips.push(ssh_info[:host])
42
+ end
43
+ end
44
+ if not ips.any?
45
+ ips.push( '127.0.0.1' )
46
+ end
47
+ return ips.uniq
48
+ end
49
+
50
+ # Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
51
+ def getHostnames(ips)
52
+ hostnames = Hash.new { |h, k| h[k] = [] }
53
+
54
+ case @machine.config.hostsupdater.aliases
55
+ when Array
56
+ # simple list of aliases to link to all ips
57
+ ips.each do |ip|
58
+ hostnames[ip] += @machine.config.hostsupdater.aliases
59
+ end
60
+ when Hash
61
+ # complex definition of aliases for various ips
62
+ @machine.config.hostsupdater.aliases.each do |ip, hosts|
63
+ hostnames[ip] += Array(hosts)
64
+ end
23
65
  end
66
+
67
+ # handle default hostname(s) if not already specified in the aliases
68
+ Array(@machine.config.vm.hostname).each do |host|
69
+ if hostnames.none? { |k, v| v.include?(host) }
70
+ ips.each do |ip|
71
+ hostnames[ip].unshift host
72
+ end
73
+ end
74
+ end
75
+
24
76
  return hostnames
25
77
  end
26
78
 
27
- def addHostEntries()
79
+ def addHostEntries
28
80
  ips = getIps
29
- hostnames = getHostnames
81
+ hostnames = getHostnames(ips)
30
82
  file = File.open(@@hosts_path, "rb")
31
83
  hostsContents = file.read
32
84
  uuid = @machine.id
33
85
  name = @machine.name
34
86
  entries = []
35
87
  ips.each do |ip|
36
- hostnames.each do |hostname|
88
+ hostnames[ip].each do |hostname|
37
89
  entryPattern = hostEntryPattern(ip, hostname)
38
90
 
39
91
  if hostsContents.match(/#{entryPattern}/)
@@ -61,7 +113,8 @@ module VagrantPlugins
61
113
  uuid = @machine.id || @machine.config.hostsupdater.id
62
114
  hashedId = Digest::MD5.hexdigest(uuid)
63
115
  if hostsContents.match(/#{hashedId}/)
64
- removeFromHosts
116
+ removeFromHosts
117
+ removeFromSshKnownHosts
65
118
  end
66
119
  end
67
120
 
@@ -70,7 +123,7 @@ module VagrantPlugins
70
123
  end
71
124
 
72
125
  def createHostEntry(ip, hostname, name, uuid = self.uuid)
73
- %Q(#{ip} #{hostname} #{signature(name, uuid)})
126
+ %Q(#{ip} #{hostname} #{signature(name, uuid.to_s)})
74
127
  end
75
128
 
76
129
  # Create a regular expression that will match *any* entry describing the
@@ -86,15 +139,24 @@ module VagrantPlugins
86
139
 
87
140
  @ui.info "[vagrant-hostsupdater] Writing the following entries to (#@@hosts_path)"
88
141
  @ui.info "[vagrant-hostsupdater] " + entries.join("\n[vagrant-hostsupdater] ")
89
- @ui.info "[vagrant-hostsupdater] This operation requires administrative access. You may " +
90
- "skip it by manually adding equivalent entries to the hosts file."
91
142
  if !File.writable_real?(@@hosts_path)
143
+ @ui.info "[vagrant-hostsupdater] This operation requires administrative access. You may " +
144
+ "skip it by manually adding equivalent entries to the hosts file."
92
145
  if !sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
93
146
  @ui.error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo"
94
147
  adviseOnSudo
95
148
  end
149
+ elsif Vagrant::Util::Platform.windows?
150
+ require 'tmpdir'
151
+ uuid = @machine.id || @machine.config.hostsupdater.id
152
+ tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid + '.cmd')
153
+ File.open(tmpPath, "w") do |tmpFile|
154
+ entries.each { |line| tmpFile.puts(">>\"#{@@hosts_path}\" echo #{line}") }
155
+ end
156
+ sudo(tmpPath)
157
+ File.delete(tmpPath)
96
158
  else
97
- content = "\n" + content
159
+ content = "\n" + content + "\n"
98
160
  hostsFile = File.open(@@hosts_path, "a")
99
161
  hostsFile.write(content)
100
162
  hostsFile.close()
@@ -104,7 +166,7 @@ module VagrantPlugins
104
166
  def removeFromHosts(options = {})
105
167
  uuid = @machine.id || @machine.config.hostsupdater.id
106
168
  hashedId = Digest::MD5.hexdigest(uuid)
107
- if !File.writable_real?(@@hosts_path)
169
+ if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows?
108
170
  if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
109
171
  @ui.error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo"
110
172
  adviseOnSudo
@@ -114,13 +176,27 @@ module VagrantPlugins
114
176
  File.open(@@hosts_path).each do |line|
115
177
  hosts << line unless line.include?(hashedId)
116
178
  end
179
+ hosts.strip!
117
180
  hostsFile = File.open(@@hosts_path, "w")
118
181
  hostsFile.write(hosts)
119
182
  hostsFile.close()
120
183
  end
121
184
  end
122
185
 
123
-
186
+ def removeFromSshKnownHosts
187
+ if !@isWindowsHost
188
+ ips = getIps
189
+ hostnames = getHostnames(ips)
190
+ ips.each do |ip|
191
+ hostnames[ip].each do |hostname|
192
+ command = %Q(sed -i -e '/#{hostname}/ d' #@@ssh_known_hosts_path)
193
+ if system(command)
194
+ @ui.info "[vagrant-hostsupdater] Removed host: #{hostname} from ssh_known_hosts file: #@@ssh_known_hosts_path"
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
124
200
 
125
201
  def signature(name, uuid = self.uuid)
126
202
  hashedId = Digest::MD5.hexdigest(uuid)
@@ -130,7 +206,11 @@ module VagrantPlugins
130
206
  def sudo(command)
131
207
  return if !command
132
208
  if Vagrant::Util::Platform.windows?
133
- `#{command}`
209
+ require 'win32ole'
210
+ args = command.split(" ")
211
+ command = args.shift
212
+ sh = WIN32OLE.new('Shell.Application')
213
+ sh.ShellExecute(command, args.join(" "), '', 'runas', 0)
134
214
  else
135
215
  return system("sudo #{command}")
136
216
  end
@@ -138,7 +218,38 @@ module VagrantPlugins
138
218
 
139
219
  def adviseOnSudo
140
220
  @ui.error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:"
141
- @ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#passwordless-sudo"
221
+ @ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges"
222
+ end
223
+
224
+ def getAwsPublicIp
225
+ return nil if ! Vagrant.has_plugin?("vagrant-aws")
226
+ aws_conf = @machine.config.vm.get_provider_config(:aws)
227
+ return nil if ! aws_conf.is_a?(VagrantPlugins::AWS::Config)
228
+ filters = ( aws_conf.tags || [] ).map {|k,v| sprintf('"Name=tag:%s,Values=%s"', k, v) }.join(' ')
229
+ return nil if filters == ''
230
+ cmd = 'aws ec2 describe-instances --filter '+filters
231
+ stdout, stderr, stat = Open3.capture3(cmd)
232
+ @ui.error sprintf("Failed to execute '%s' : %s", cmd, stderr) if stderr != ''
233
+ return nil if stat.exitstatus != 0
234
+ begin
235
+ return JSON.parse(stdout)["Reservations"].first()["Instances"].first()["PublicIpAddress"]
236
+ rescue => e
237
+ @ui.error sprintf("Failed to get IP from the result of '%s' : %s", cmd, e.message)
238
+ return nil
239
+ end
240
+ end
241
+
242
+ def getGooglePublicIp
243
+ return nil if ! defined?(VagrantPlugins::Google)
244
+ google_conf = @machine.config.vm.get_provider_config(:google)
245
+ return nil if ! google_conf.is_a?(VagrantPlugins::Google::Config)
246
+ cmd = 'gcloud compute instances list --filter="name=%s" --format="value(networkInterfaces[0].accessConfigs[0].natIP)"'
247
+ cmd = sprintf(cmd, google_conf.name)
248
+ stdout, stderr, stat = Open3.capture3(cmd)
249
+ @ui.error "Failed to execute '#{cmd}' : #{stderr}" if stderr != ''
250
+ ip = stdout.strip
251
+ return nil if stat.exitstatus != 0 || ip == nil || ip == ''
252
+ return ip
142
253
  end
143
254
  end
144
255
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module HostsUpdater
3
- VERSION = '1.0.2'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ require 'vagrant-hostsupdater/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'vagrant-hostsupdater'
8
- spec.version = '1.1.1.160' #VagrantPlugins::HostsUpdater::VERSION
8
+ spec.version = VagrantPlugins::HostsUpdater::VERSION
9
9
  spec.authors = ['Falk Kühnel', 'Chris Smith']
10
10
  spec.email = ['fk@cogitatio.de', 'chris@cgsmith.net']
11
11
  spec.description = %q{Enables Vagrant to update hosts file on the host machine}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-hostsupdater
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1.160
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Falk Kühnel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-04-04 00:00:00.000000000 Z
12
+ date: 2020-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -81,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.7.6
84
+ rubygems_version: 3.1.2
86
85
  signing_key:
87
86
  specification_version: 4
88
87
  summary: Enables Vagrant to update hosts file on the host machine