vagrant-hostsupdater 1.0.1 → 1.2.2

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
- SHA1:
3
- metadata.gz: a2a17cded96ebcabea7b7c83fe53a211fec0f67d
4
- data.tar.gz: b78d9ce4c6ad59cf4b84e21b20cf757bd20babab
2
+ SHA256:
3
+ metadata.gz: 28da9dcc8ae93032fce5f1005a560e515d04c52dd7fbd8b59a4fe02a5dc28d9f
4
+ data.tar.gz: 9b52816b56418ef87eeb9d4570832af2b21f4a54e1ff3fff8410f58cf79cb42a
5
5
  SHA512:
6
- metadata.gz: e050e308db233943db5d919bdc29e955ac2107dac000e55d2c5cb126347dad34b48b20e7d6a020c606f38d77e06866e0790db0e203841b917bfb723a87a78db7
7
- data.tar.gz: 0e9ab9617ce0683000bda509941020f2f8540c49a0664a9db48c78ad330ecff32a9f901dfe2ab2bc78a9c563663f7ab933bdabdb99ef03bdfb8eaf129bd2bad3
6
+ metadata.gz: 017dd22e81cec47f73a0af31ecfa58489fd8bc89c57b722d97ef4c381a5fd8d04365138268f5466bda9a486f7fad075071eb1b0ecc484dea7842a26e74dccbe3
7
+ data.tar.gz: 0f6b2d84f9c791d35e930ac6b75472a0fc38272b88bccfaa9c8b60df2476dcd1c3dd7d9a16b9bed0a9108b960dc077798216fb63c35e0dbaab233a4aa6df6554
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.gem
2
+ .idea
2
3
  *.rbc
3
4
  .bundle
4
5
  .config
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Falk Kühnel
1
+ Copyright 2018 Chris Smith
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,3 +1,19 @@
1
+ # NOT MAINTAINED
2
+ # USE AN ALTERNATIVE
3
+
4
+ March 3, 2021
5
+
6
+ Hi Everyone. @cgsmith here. I took maintenance over from @agiledivider (Falk Kühnel) in 2015. Myself and the company I worked for relied on Vagrant for our development machines and I relied on vagrant-hostsupdater for changing hosts files. When I found out he was no longer maintaining the project I reached out to him on Twitter and he was willing to let me maintain it and apply some much needed pull requests.
7
+
8
+ Maintaining a large project is extremely rewarding. I enjoyed doing so but have stopped using Vagrant for development. There are other plugins to help you manage your hosts file or you can feel free to fork this one into your own.
9
+
10
+ Don't worry if you don't know Ruby... I didn't. I'm a PHP developer. I figured it out though and asked the right questions to get the problem solved.
11
+
12
+ So long... may we meet elsewhere on this vast planet.
13
+
14
+ - Chris Smith
15
+
16
+
1
17
  # Vagrant::Hostsupdater
2
18
 
3
19
  [![Gem Version](https://badge.fury.io/rb/vagrant-hostsupdater.svg)](https://badge.fury.io/rb/vagrant-hostsupdater)
@@ -10,10 +26,10 @@
10
26
 
11
27
  This plugin adds an entry to your /etc/hosts file on the host system.
12
28
 
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.
29
+ 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
30
 
15
31
  On **halt**, **destroy**, and **suspend**, those entries will be removed again.
16
- By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** will not remove them.
32
+ By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** and **halt** will not remove them.
17
33
 
18
34
 
19
35
  ## Installation
@@ -38,6 +54,27 @@ You currently only need the `hostname` and a `:private_network` network with a f
38
54
 
39
55
  This IP address and the hostname will be used for the entry in the `/etc/hosts` file.
40
56
 
57
+ ### Multiple private network adapters
58
+
59
+ If you have multiple network adapters i.e.:
60
+
61
+ config.vm.network :private_network, ip: "10.0.0.1"
62
+ config.vm.network :private_network, ip: "10.0.0.2"
63
+
64
+ 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.:
65
+
66
+ config.hostsupdater.aliases = {
67
+ '10.0.0.1' => ['foo.com', 'bar.com'],
68
+ '10.0.0.2' => ['baz.com', 'bat.com']
69
+ }
70
+
71
+ This will produce `/etc/hosts` entries like so:
72
+
73
+ 10.0.0.1 foo.com
74
+ 10.0.0.1 bar.com
75
+ 10.0.0.2 baz.com
76
+ 10.0.0.2 bat.com
77
+
41
78
  ### Skipping hostupdater
42
79
 
43
80
  To skip adding some entries to the /etc/hosts file add `hostsupdater: "skip"` option to network configuration:
@@ -52,18 +89,81 @@ Example:
52
89
  netmask: "255.255.240.0",
53
90
  hostsupdater: "skip"
54
91
 
92
+ ### Keeping Host Entries After Suspend/Halt
93
+
94
+ To keep your /etc/hosts file unchanged simply add the line below to your `VagrantFile`:
95
+
96
+ config.hostsupdater.remove_on_suspend = false
97
+
98
+ This disables vagrant-hostsupdater from running on **suspend** and **halt**.
99
+
100
+
101
+ ## Suppressing prompts for elevating privileges
102
+
103
+ These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
104
+ If you understand the risks that go with supressing them, here's how to do it.
55
105
 
56
- ## Passwordless sudo
106
+ ### Linux/OS X: Passwordless sudo
57
107
 
58
- Add the following snippet to the top of the sudoers file using `sudo visudo`. It will make vagrant
59
- stop asking password when updating hosts file:
108
+ 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`.
109
+
110
+ For Ubuntu and most Linux environments:
111
+
112
+ # Allow passwordless startup of Vagrant with vagrant-hostsupdater.
113
+ Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
114
+ Cmnd_Alias VAGRANT_HOSTS_REMOVE = /bin/sed -i -e /*/ d /etc/hosts
115
+ %sudo ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
116
+
117
+ For MacOS:
60
118
 
61
119
  # Allow passwordless startup of Vagrant with vagrant-hostsupdater.
62
120
  Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
63
121
  Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
64
122
  %admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
65
123
 
66
-
124
+ - 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.
125
+ - If vagrant still asks for a password on commands that trigger the `VAGRANT_HOSTS_REMOVE` alias above (like
126
+ **halt** or **suspend**), this might indicate that the location of **sed** in the `VAGRANT_HOSTS_REMOVE` alias is
127
+ pointing to the wrong location. The solution is to find the location of **sed** (ex. `which sed`) and
128
+ replace that location in the `VAGRANT_HOSTS_REMOVE` alias.
129
+
130
+ ### Windows: UAC Prompt
131
+
132
+ You can use `cacls` or `icacls` to grant your user account permanent write permission to the system's hosts file.
133
+ You have to open an elevated command prompt; hold `❖ Win` and press `X`, then choose "Command Prompt (Admin)"
134
+
135
+ cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W
136
+
137
+ ## Using AWS as a Provider
138
+
139
+ If you'd like AWS as a provider using [vagrant-aws](https://github.com/mitchellh/vagrant-aws) or other plugin,
140
+ this plugin will detect the instance public IP by the tag infomations.
141
+ For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures a tag infomations like the following.
142
+
143
+ config.vm.provider :aws do |aws, override|
144
+ aws.tags = {
145
+ "Name" => "vagrant",
146
+ ...
147
+ }
148
+ aws.elastic_ip = true
149
+ ...
150
+ end
151
+
152
+ * [AWS CLI](https://aws.amazon.com/cli/) is required
153
+ * The tag informations be unique for the instance
154
+ * Enable Elastic IP for the instance
155
+
156
+ ## Using Google as a provider
157
+
158
+ 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.
159
+ [vagrant-google](https://github.com/mitchellh/vagrant-google) provides a default name, but you can specify your own as follows:
160
+
161
+ config.vm.provider :google do |google, override|
162
+ google.name = "somename"
163
+ ...
164
+ end
165
+
166
+ * [Google Cloud SDK](https://cloud.google.com/sdk/) is required.
67
167
 
68
168
  ## Installing development version
69
169
 
@@ -88,6 +188,28 @@ vagrant plugin install vagrant-hostsupdater-*.gem
88
188
 
89
189
  ## Versions
90
190
 
191
+ ### 1.2.0
192
+ * Bugfix: Fixing null string [#188](/../../issues/188)
193
+
194
+ ### 1.1.1
195
+ * Bugfix: AWS Feature broke part of the code [#155](/../../issues/155)
196
+
197
+ ### 1.1.0
198
+ * Feature: Added AWS support [#74](/../../pull/74)
199
+ * Feature: Added libvirt provider [#122](/../../pull/122)
200
+ * Feature: Add support for multiple private network adapters [#96](/../../pull/96)
201
+ * Feature: Add support for VMs without private/public networking [#23](/../../issues/23)
202
+ * Feature: Add Docker support [#149](/../../pull/149)
203
+ * Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
204
+ * Bugfix: Documentation update and type fix
205
+ * Misc: Added a note about suppressing UAC prompts
206
+
207
+ ### 1.0.2
208
+ * Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71)
209
+ * Feature: Skip entries if they already exist [#69](/../../issues/69)
210
+ * Bugfix: Fixing extra lines in /etc/hosts file [#87](/../../pull/87)
211
+ * Misc: Fix yellow text on UI [#39](/../../issues/39)
212
+
91
213
  ### 1.0.1
92
214
  * Bugfix: Fixing `up` issue on initialize [#28](/../../issues/28)
93
215
 
@@ -129,4 +251,4 @@ vagrant plugin install vagrant-hostsupdater-*.gem
129
251
  * fixed problem with removing hosts entries on destroy command (Thanks to Andy Bohne)
130
252
 
131
253
  ### 0.0.3
132
- * added aliases config option to define additional hostnames
254
+ * added aliases config option to define additional hostnames
@@ -0,0 +1,39 @@
1
+ module VagrantPlugins
2
+ module HostsUpdater
3
+ module Action
4
+ class BaseAction
5
+ include HostsUpdater
6
+
7
+ # Vagrant 2.2.14 has changed the hooks execution policy so they
8
+ # started to be triggered more than once (a lot actually) which
9
+ # is non-performant and floody. With this static property, we
10
+ # control the executions and allowing just one.
11
+ #
12
+ # - https://github.com/hashicorp/vagrant/issues/12070#issuecomment-732271918
13
+ # - https://github.com/hashicorp/vagrant/compare/v2.2.13..v2.2.14#diff-4d1af7c67af870f20d303c3c43634084bab8acc101055b2e53ddc0d07f6f64dcL176-L180
14
+ # - https://github.com/agiledivider/vagrant-hostsupdater/issues/187
15
+ @@completed = {}
16
+
17
+ def initialize(app, env)
18
+ @app = app
19
+ @machine = env[:machine]
20
+ @ui = env[:ui]
21
+ end
22
+
23
+ def call(env)
24
+ if not @@completed.key?(self.class.name)
25
+ run(env)
26
+ @@completed[self.class.name] = true
27
+ end
28
+
29
+ @app.call(env)
30
+ end
31
+
32
+ def run(env)
33
+ raise NotImplementedError.new("Must be implemented!")
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,17 +1,10 @@
1
1
  module VagrantPlugins
2
2
  module HostsUpdater
3
3
  module Action
4
- class CacheHosts
5
- include HostsUpdater
4
+ class CacheHosts < BaseAction
6
5
 
7
- def initialize(app, env)
8
- @app = app
9
- @machine = env[:machine]
10
- end
11
-
12
- def call(env)
6
+ def run(env)
13
7
  cacheHostEntries
14
- @app.call(env)
15
8
  end
16
9
 
17
10
  end
@@ -1,26 +1,20 @@
1
1
  module VagrantPlugins
2
2
  module HostsUpdater
3
3
  module Action
4
- class RemoveHosts
5
- include HostsUpdater
4
+ class RemoveHosts < BaseAction
6
5
 
7
- def initialize(app, env)
8
- @app = app
9
- @machine = env[:machine]
10
- @ui = env[:ui]
11
- end
12
-
13
- def call(env)
6
+ def run(env)
14
7
  machine_action = env[:machine_action]
15
8
  if machine_action != :destroy || !@machine.id
16
9
  if machine_action != :suspend || false != @machine.config.hostsupdater.remove_on_suspend
17
- @ui.info "Removing hosts"
18
- removeHostEntries
19
- else
20
- @ui.info "Removing hosts on suspend disabled"
10
+ if machine_action != :halt || false != @machine.config.hostsupdater.remove_on_suspend
11
+ @ui.info "[vagrant-hostsupdater] Removing hosts"
12
+ removeHostEntries
13
+ else
14
+ @ui.info "[vagrant-hostsupdater] Removing hosts on suspend disabled"
15
+ end
21
16
  end
22
17
  end
23
- @app.call(env)
24
18
  end
25
19
 
26
20
  end
@@ -1,24 +1,14 @@
1
- require_relative "../HostsUpdater"
2
1
  module VagrantPlugins
3
2
  module HostsUpdater
4
3
  module Action
5
- class UpdateHosts
6
- include HostsUpdater
4
+ class UpdateHosts < BaseAction
7
5
 
8
-
9
- def initialize(app, env)
10
- @app = app
11
- @machine = env[:machine]
12
- @ui = env[:ui]
13
- end
14
-
15
- def call(env)
16
- @ui.info "Checking for host entries"
6
+ def run(env)
7
+ @ui.info "[vagrant-hostsupdater] Checking for host entries"
17
8
  addHostEntries()
18
- @app.call(env)
19
9
  end
20
10
 
21
11
  end
22
12
  end
23
13
  end
24
- end
14
+ end
@@ -1,43 +1,96 @@
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 '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
23
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
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
30
81
  file = File.open(@@hosts_path, "rb")
31
82
  hostsContents = file.read
32
83
  uuid = @machine.id
33
84
  name = @machine.name
34
85
  entries = []
35
- ips.each do |ip|
36
- hostEntries = getHostEntries(ip, hostnames, name, uuid)
37
- hostEntries.each do |hostEntry|
38
- escapedEntry = Regexp.quote(hostEntry)
39
- if !hostsContents.match(/#{escapedEntry}/)
40
- @ui.info "adding to (#@@hosts_path) : #{hostEntry}"
86
+ getHostnames(ips).each do |ip, hostnames|
87
+ hostnames.each do |hostname|
88
+ entryPattern = hostEntryPattern(ip, hostname)
89
+
90
+ if hostsContents.match(/#{entryPattern}/)
91
+ @ui.info "[vagrant-hostsupdater] found entry for: #{ip} #{hostname}"
92
+ else
93
+ hostEntry = createHostEntry(ip, hostname, name, uuid)
41
94
  entries.push(hostEntry)
42
95
  end
43
96
  end
@@ -51,7 +104,7 @@ module VagrantPlugins
51
104
 
52
105
  def removeHostEntries
53
106
  if !@machine.id and !@machine.config.hostsupdater.id
54
- @ui.warn "No machine id, nothing removed from #@@hosts_path"
107
+ @ui.info "[vagrant-hostsupdater] No machine id, nothing removed from #@@hosts_path"
55
108
  return
56
109
  end
57
110
  file = File.open(@@hosts_path, "rb")
@@ -59,7 +112,8 @@ module VagrantPlugins
59
112
  uuid = @machine.id || @machine.config.hostsupdater.id
60
113
  hashedId = Digest::MD5.hexdigest(uuid)
61
114
  if hostsContents.match(/#{hashedId}/)
62
- removeFromHosts
115
+ removeFromHosts
116
+ removeFromSshKnownHosts
63
117
  end
64
118
  end
65
119
 
@@ -67,21 +121,41 @@ module VagrantPlugins
67
121
  %Q(#{ip} #{hostnames.join(' ')} #{signature(name, uuid)})
68
122
  end
69
123
 
70
- def getHostEntries(ip, hostnames, name, uuid = self.uuid)
71
- entries = []
72
- hostnames.each do |hostname|
73
- entries.push(%Q(#{ip} #{hostname} #{signature(name, uuid)}))
74
- end
75
- return entries
124
+ def createHostEntry(ip, hostname, name, uuid = self.uuid)
125
+ %Q(#{ip} #{hostname} #{signature(name, uuid.to_s)})
126
+ end
127
+
128
+ # Create a regular expression that will match *any* entry describing the
129
+ # given IP/hostname pair. This is intentionally generic in order to
130
+ # recognize entries created by the end user.
131
+ def hostEntryPattern(ip, hostname)
132
+ Regexp.new('^\s*' + ip + '\s+' + hostname + '\s*(#.*)?$')
76
133
  end
77
134
 
78
135
  def addToHosts(entries)
79
136
  return if entries.length == 0
80
- content = entries.join("\n").strip.concat("\n")
137
+ content = entries.join("\n").strip
138
+
139
+ @ui.info "[vagrant-hostsupdater] Writing the following entries to (#@@hosts_path)"
140
+ @ui.info "[vagrant-hostsupdater] " + entries.join("\n[vagrant-hostsupdater] ")
81
141
  if !File.writable_real?(@@hosts_path)
82
- sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
142
+ @ui.info "[vagrant-hostsupdater] This operation requires administrative access. You may " +
143
+ "skip it by manually adding equivalent entries to the hosts file."
144
+ if !sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
145
+ @ui.error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo"
146
+ adviseOnSudo
147
+ end
148
+ elsif Vagrant::Util::Platform.windows?
149
+ require 'tmpdir'
150
+ uuid = @machine.id || @machine.config.hostsupdater.id
151
+ tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid.to_s + '.cmd')
152
+ File.open(tmpPath, "w") do |tmpFile|
153
+ entries.each { |line| tmpFile.puts(">>\"#{@@hosts_path}\" echo #{line}") }
154
+ end
155
+ sudo(tmpPath)
156
+ File.delete(tmpPath)
83
157
  else
84
- content = "\n" + content
158
+ content = "\n" + content + "\n"
85
159
  hostsFile = File.open(@@hosts_path, "a")
86
160
  hostsFile.write(content)
87
161
  hostsFile.close()
@@ -91,20 +165,37 @@ module VagrantPlugins
91
165
  def removeFromHosts(options = {})
92
166
  uuid = @machine.id || @machine.config.hostsupdater.id
93
167
  hashedId = Digest::MD5.hexdigest(uuid)
94
- if !File.writable_real?(@@hosts_path)
95
- sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
168
+ if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows?
169
+ if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
170
+ @ui.error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo"
171
+ adviseOnSudo
172
+ end
96
173
  else
97
174
  hosts = ""
98
175
  File.open(@@hosts_path).each do |line|
99
176
  hosts << line unless line.include?(hashedId)
100
177
  end
178
+ hosts.strip!
101
179
  hostsFile = File.open(@@hosts_path, "w")
102
180
  hostsFile.write(hosts)
103
181
  hostsFile.close()
104
182
  end
105
183
  end
106
184
 
107
-
185
+ def removeFromSshKnownHosts
186
+ if !@isWindowsHost
187
+ ips = getIps
188
+ hostnames = getHostnames(ips)
189
+ ips.each do |ip|
190
+ hostnames[ip].each do |hostname|
191
+ command = %Q(sed -i -e '/#{hostname}/ d' #@@ssh_known_hosts_path)
192
+ if system(command)
193
+ @ui.info "[vagrant-hostsupdater] Removed host: #{hostname} from ssh_known_hosts file: #@@ssh_known_hosts_path"
194
+ end
195
+ end
196
+ end
197
+ end
198
+ end
108
199
 
109
200
  def signature(name, uuid = self.uuid)
110
201
  hashedId = Digest::MD5.hexdigest(uuid)
@@ -114,11 +205,51 @@ module VagrantPlugins
114
205
  def sudo(command)
115
206
  return if !command
116
207
  if Vagrant::Util::Platform.windows?
117
- `#{command}`
208
+ require 'win32ole'
209
+ args = command.split(" ")
210
+ command = args.shift
211
+ sh = WIN32OLE.new('Shell.Application')
212
+ sh.ShellExecute(command, args.join(" "), '', 'runas', 0)
118
213
  else
119
- `sudo #{command}`
214
+ return system("sudo #{command}")
215
+ end
216
+ end
217
+
218
+ def adviseOnSudo
219
+ @ui.error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:"
220
+ @ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges"
221
+ end
222
+
223
+ def getAwsPublicIp
224
+ return nil if ! Vagrant.has_plugin?("vagrant-aws")
225
+ aws_conf = @machine.config.vm.get_provider_config(:aws)
226
+ return nil if ! aws_conf.is_a?(VagrantPlugins::AWS::Config)
227
+ filters = ( aws_conf.tags || [] ).map {|k,v| sprintf('"Name=tag:%s,Values=%s"', k, v) }.join(' ')
228
+ return nil if filters == ''
229
+ cmd = 'aws ec2 describe-instances --filter '+filters
230
+ stdout, stderr, stat = Open3.capture3(cmd)
231
+ @ui.error sprintf("Failed to execute '%s' : %s", cmd, stderr) if stderr != ''
232
+ return nil if stat.exitstatus != 0
233
+ begin
234
+ return JSON.parse(stdout)["Reservations"].first()["Instances"].first()["PublicIpAddress"]
235
+ rescue => e
236
+ @ui.error sprintf("Failed to get IP from the result of '%s' : %s", cmd, e.message)
237
+ return nil
120
238
  end
121
239
  end
240
+
241
+ def getGooglePublicIp
242
+ return nil if ! defined?(VagrantPlugins::Google)
243
+ google_conf = @machine.config.vm.get_provider_config(:google)
244
+ return nil if ! google_conf.is_a?(VagrantPlugins::Google::Config)
245
+ cmd = 'gcloud compute instances list --filter="name=%s" --format="value(networkInterfaces[0].accessConfigs[0].natIP)"'
246
+ cmd = sprintf(cmd, google_conf.name)
247
+ stdout, stderr, stat = Open3.capture3(cmd)
248
+ @ui.error "Failed to execute '#{cmd}' : #{stderr}" if stderr != ''
249
+ ip = stdout.strip
250
+ return nil if stat.exitstatus != 0 || ip == nil || ip == ''
251
+ return ip
252
+ end
122
253
  end
123
254
  end
124
255
  end
@@ -1,6 +1,8 @@
1
- require "vagrant-hostsupdater/Action/UpdateHosts"
2
- require "vagrant-hostsupdater/Action/CacheHosts"
3
- require "vagrant-hostsupdater/Action/RemoveHosts"
1
+ require_relative "HostsUpdater"
2
+ require_relative "Action/BaseAction"
3
+ require_relative "Action/UpdateHosts"
4
+ require_relative "Action/CacheHosts"
5
+ require_relative "Action/RemoveHosts"
4
6
 
5
7
  module VagrantPlugins
6
8
  module HostsUpdater
@@ -17,8 +19,7 @@ module VagrantPlugins
17
19
  end
18
20
 
19
21
  action_hook(:hostsupdater, :machine_action_up) do |hook|
20
- hook.prepend(Action::RemoveHosts)
21
- hook.after(Vagrant::Action::Builtin::SetHostname, Action::UpdateHosts)
22
+ hook.append(Action::UpdateHosts)
22
23
  end
23
24
 
24
25
  action_hook(:hostsupdater, :machine_action_provision) do |hook|
@@ -35,9 +36,6 @@ module VagrantPlugins
35
36
 
36
37
  action_hook(:hostsupdater, :machine_action_destroy) do |hook|
37
38
  hook.prepend(Action::CacheHosts)
38
- end
39
-
40
- action_hook(:hostsupdater, :machine_action_destroy) do |hook|
41
39
  hook.append(Action::RemoveHosts)
42
40
  end
43
41
 
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module HostsUpdater
3
- VERSION = '1.0.1'
3
+ VERSION = '1.2.2'
4
4
  end
5
5
  end
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.0.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Falk Kühnel
@@ -9,34 +9,34 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-11-09 00:00:00.000000000 Z
12
+ date: 2021-04-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ~>
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '1.3'
21
21
  type: :development
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ~>
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: '1.3'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - '>='
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  description: Enables Vagrant to update hosts file on the host machine
@@ -47,12 +47,13 @@ executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
- - .gitignore
50
+ - ".gitignore"
51
51
  - Gemfile
52
52
  - LICENSE.txt
53
53
  - README.md
54
54
  - Rakefile
55
55
  - lib/vagrant-hostsupdater.rb
56
+ - lib/vagrant-hostsupdater/Action/BaseAction.rb
56
57
  - lib/vagrant-hostsupdater/Action/CacheHosts.rb
57
58
  - lib/vagrant-hostsupdater/Action/RemoveHosts.rb
58
59
  - lib/vagrant-hostsupdater/Action/UpdateHosts.rb
@@ -72,17 +73,16 @@ require_paths:
72
73
  - lib
73
74
  required_ruby_version: !ruby/object:Gem::Requirement
74
75
  requirements:
75
- - - '>='
76
+ - - ">="
76
77
  - !ruby/object:Gem::Version
77
78
  version: '0'
78
79
  required_rubygems_version: !ruby/object:Gem::Requirement
79
80
  requirements:
80
- - - '>='
81
+ - - ">="
81
82
  - !ruby/object:Gem::Version
82
83
  version: '0'
83
84
  requirements: []
84
- rubyforge_project:
85
- rubygems_version: 2.0.14
85
+ rubygems_version: 3.1.2
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Enables Vagrant to update hosts file on the host machine