vagrant-goodhosts 1.0.15 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00dc15e9dc6876fb9f1610777d190e21c6bf6397531950a1cae0f35496e1e989
4
- data.tar.gz: 30f87a2f1855fde5e646148f02f1861d400de9054109920302e04ab29b95b353
3
+ metadata.gz: 9df62f1e1e6994b0e268f5a4c70e05992d2aa8717359bc843468f7fe209d735c
4
+ data.tar.gz: 5de2dbc4f9eefe5dddfed6a74c8db799b9cf5fd8003d111ea5d47e6bb07d68d3
5
5
  SHA512:
6
- metadata.gz: b9fd306e3cbff7ad32f524ba17f3248b1b5fdd113e59e341f34deb153f52c39212c1da1b97382d78567101e6edf74b1b1559180a3bdfbcadf3cabb7786c8edad
7
- data.tar.gz: 908da7239733a24b072256659e453d3864c429306b42f3128a6856e7013b635e0c922d3436c5c4ef902c43c9935f1a91f247fa7a0a0b68c1daab8531d931b4d1
6
+ metadata.gz: a80232c8572f03701db820c2ab9be2e37288ab1fa258381c35843ebfc68c121f844c1765b18c6c743c3f9b0da18014e5bc32d7dde6d3b91d03ed6c2de24cff44
7
+ data.tar.gz: 69f5149b8e73aaedabc0c4011bade5b0a12f1cda452603b230fef861e774f2fed5eec14ec311ec6157fa4ec4cdb925d292faaf1bc448e6c9fb6e766bee5aaf7b
data/README.md CHANGED
@@ -30,7 +30,7 @@ You currently only need the `hostname` and a `:private_network` network with a f
30
30
 
31
31
  ```ruby
32
32
  config.vm.network :private_network, ip: "192.168.3.10"
33
- config.vm.hostname = "www.testing.de"
33
+ config.vm.hostname = "www.testing.de" # This is not used by the plugin
34
34
  config.goodhosts.aliases = ["alias.testing.de", "alias2.somedomain.com"]
35
35
  ```
36
36
 
@@ -96,15 +96,15 @@ If you understand the risks that go with supressing them, here's how to do it.
96
96
  ### Linux/OS X: Passwordless sudo
97
97
 
98
98
  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_goodhosts`.
99
- The command path is printed when there are errors with sudo.
99
+ The command path is printed when there are errors with Vagrant, check the output marked in red.
100
100
 
101
101
  For Ubuntu and most Linux environments:
102
102
 
103
- %sudo ALL=(root) NOPASSWD: [the-path]
103
+ %sudo ALL=(root) NOPASSWD: [the-command-path]
104
104
 
105
105
  For MacOS:
106
106
 
107
- %admin ALL=(root) NOPASSWD: [the-path]
107
+ %admin ALL=(root) NOPASSWD: [the-command-path]
108
108
 
109
109
  Replace in both %sudo/%admin with the username it if it is not working for you.
110
110
 
@@ -26,9 +26,10 @@ module VagrantPlugins
26
26
  # machines and having a static flag will result in a plugin being
27
27
  # executed just once.
28
28
  # https://github.com/goodhosts/vagrant/issues/30
29
- if not @@completed.key?(@machine.name)
29
+ @@completed[@machine.name] = [] unless @@completed.key?(@machine.name)
30
+ unless @@completed[@machine.name].include? self.class.name
30
31
  run(env)
31
- @@completed[@machine.name] = true
32
+ @@completed[@machine.name] << self.class.name
32
33
  end
33
34
 
34
35
  @app.call(env)
@@ -5,15 +5,14 @@ module VagrantPlugins
5
5
 
6
6
  def run(env)
7
7
  machine_action = env[:machine_action]
8
- if machine_action != :destroy || !@machine.id
9
- if machine_action != :suspend || false != @machine.config.goodhosts.remove_on_suspend
10
- if machine_action != :halt || false != @machine.config.goodhosts.remove_on_suspend
11
- @ui.info "[vagrant-goodhosts] Removing hosts"
12
- removeHostEntries
13
- else
14
- @ui.info "[vagrant-goodhosts] Removing hosts on suspend disabled"
15
- end
16
- end
8
+
9
+ return unless @machine.id
10
+ return unless [:destroy, :halt, :suspend].include? machine_action
11
+
12
+ if ([:halt, :suspend].include? machine_action) && (false == @machine.config.goodhosts.remove_on_suspend)
13
+ @ui.info "[vagrant-goodhosts] Removing hosts on suspend disabled"
14
+ else
15
+ removeHostEntries
17
16
  end
18
17
  end
19
18
 
@@ -4,7 +4,6 @@ module VagrantPlugins
4
4
  class UpdateHosts < BaseAction
5
5
 
6
6
  def run(env)
7
- @ui.info "[vagrant-goodhosts] Checking for host entries"
8
7
  addHostEntries()
9
8
  end
10
9
 
@@ -1,5 +1,6 @@
1
1
  require "rbconfig"
2
2
  require "open3"
3
+ require "resolv"
3
4
 
4
5
  module VagrantPlugins
5
6
  module GoodHosts
@@ -8,17 +9,17 @@ module VagrantPlugins
8
9
  ips = []
9
10
 
10
11
  if @machine.config.vm.networks.length == 0
11
- @ui.error("[vagrant-goodhosts] No ip address found for this virtual machine")
12
- exit
12
+ @ui.error("[vagrant-goodhosts] No networks are available yet for this virtual machine to add IP/hosts for")
13
+ return ips
13
14
  end
14
-
15
+
15
16
  @machine.config.vm.networks.each do |network|
16
17
  key, options = network[0], network[1]
17
- ip = options[:ip] if (key == :private_network || key == :public_network) && options[:goodhosts] != "skip"
18
- ips.push(ip) if ip
19
18
  if options[:goodhosts] == "skip"
20
19
  @ui.info '[vagrant-goodhosts] Skipped adding host entries (config.vm.network goodhosts: "skip" is set)'
21
20
  end
21
+ ip = options[:ip] if (key == :private_network || key == :public_network) && options[:goodhosts] != "skip"
22
+ ips.push(ip) if ip
22
23
 
23
24
  @machine.config.vm.provider :hyperv do |v|
24
25
  timeout = @machine.provider_config.ip_address_timeout
@@ -37,7 +38,6 @@ module VagrantPlugins
37
38
  end
38
39
  end
39
40
 
40
-
41
41
  end
42
42
  return ips
43
43
  end
@@ -45,16 +45,16 @@ module VagrantPlugins
45
45
  # https://stackoverflow.com/a/13586108/1902215
46
46
  def get_os_binary
47
47
  return os ||= (host_os = RbConfig::CONFIG["host_os"]
48
- case host_os
49
- when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
50
- :'cli.exe'
51
- when /darwin|mac os/
52
- :'cli_osx'
53
- when /linux/
54
- :'cli'
55
- else
56
- raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
57
- end)
48
+ case host_os
49
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
50
+ :'cli.exe'
51
+ when /darwin|mac os/
52
+ :'cli_osx'
53
+ when /linux/
54
+ :'cli'
55
+ else
56
+ raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
57
+ end)
58
58
  end
59
59
 
60
60
  def get_cli
@@ -84,7 +84,7 @@ module VagrantPlugins
84
84
 
85
85
  return hostnames
86
86
  end
87
-
87
+
88
88
  def disableClean(ip_address)
89
89
  unless ip_address.nil?
90
90
  return @machine.config.goodhosts.disable_clean
@@ -92,32 +92,61 @@ module VagrantPlugins
92
92
  return true
93
93
  end
94
94
 
95
+ def checkHostnamesToAdd(ip_address, hostnames)
96
+ hostnames_to_add = Array.new
97
+ hostnames = hostnames.split
98
+ # check which hostnames actually need adding
99
+ hostnames.each do |hostname|
100
+ begin
101
+ address = Resolv.getaddress(hostname)
102
+ if address != ip_address
103
+ hostnames_to_add.append( hostname )
104
+ end
105
+ rescue => exception
106
+ hostnames_to_add.append(hostname)
107
+ end
108
+ end
109
+ return hostnames_to_add
110
+ end
111
+
112
+ def addGoodhostEntries(ip_address, hostnames)
113
+ if cli.include? ".exe"
114
+ clean = "\"--clean\","
115
+ if disableClean(ip_address)
116
+ clean = ''
117
+ end
118
+ stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", "Start-Process '#{cli}' -ArgumentList \"add\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs")
119
+ else
120
+ clean = "--clean"
121
+ if disableClean(ip_address)
122
+ clean = ''
123
+ end
124
+ stdin, stdout, stderr, wait_thr = Open3.popen3("sudo '#{cli}' add #{clean} #{ip_address} #{hostnames}")
125
+ end
126
+ return stdin, stdout, stderr, wait_thr
127
+ end
128
+
95
129
  def addHostEntries
96
130
  error = false
97
131
  errorText = ""
98
132
  cli = get_cli
99
133
  hostnames_by_ips = generateHostnamesByIps
100
-
134
+
101
135
  return if not hostnames_by_ips.any?
102
136
 
137
+ @ui.info "[vagrant-goodhosts] Checking for host entries"
138
+
103
139
  hostnames_by_ips.each do |ip_address, hostnames|
104
140
  if ip_address.nil?
105
141
  @ui.error "[vagrant-goodhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}"
106
142
  next
107
143
  end
108
- if cli.include? ".exe"
109
- clean = "\"--clean\","
110
- if disableClean(ip_address)
111
- clean = ''
112
- end
113
- stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", "Start-Process '#{cli}' -ArgumentList \"add\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs")
114
- else
115
- clean = "--clean"
116
- if disableClean(ip_address)
117
- clean = ''
118
- end
119
- stdin, stdout, stderr, wait_thr = Open3.popen3("sudo '#{cli}' add #{clean} #{ip_address} #{hostnames}")
120
- end
144
+
145
+ # filter out the hosts we've already added
146
+ hosts_to_add = checkHostnamesToAdd(ip_address, hostnames)
147
+ next if not hosts_to_add.any?
148
+
149
+ stdin, stdout, stderr, wait_thr = addGoodhostEntries(ip_address, hosts_to_add)
121
150
  if !wait_thr.value.success?
122
151
  error = true
123
152
  errorText = stderr.read.strip
@@ -126,6 +155,23 @@ module VagrantPlugins
126
155
  printReadme(error, errorText)
127
156
  end
128
157
 
158
+ def removeGoodhostEntries(ip_address, hostnames)
159
+ if cli.include? ".exe"
160
+ clean = "\"--clean\","
161
+ if disableClean(ip_address)
162
+ clean = ''
163
+ end
164
+ stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", "Start-Process '#{cli}' -ArgumentList \"remove\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs")
165
+ else
166
+ clean = "\"--clean\","
167
+ if disableClean(ip_address)
168
+ clean = ''
169
+ end
170
+ stdin, stdout, stderr, wait_thr = Open3.popen3("sudo '#{cli}' remove #{clean} #{ip_address} #{hostnames}")
171
+ end
172
+ return stdin, stdout, stderr, wait_thr
173
+ end
174
+
129
175
  def removeHostEntries
130
176
  error = false
131
177
  errorText = ""
@@ -134,24 +180,15 @@ module VagrantPlugins
134
180
 
135
181
  return if not hostnames_by_ips.any?
136
182
 
183
+ @ui.info "[vagrant-goodhosts] Removing hosts"
184
+
137
185
  hostnames_by_ips.each do |ip_address, hostnames|
138
186
  if ip_address.nil?
139
187
  @ui.error "[vagrant-goodhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}"
140
188
  next
141
189
  end
142
- if cli.include? ".exe"
143
- clean = "\"--clean\","
144
- if disableClean(ip_address)
145
- clean = ''
146
- end
147
- stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", "Start-Process '#{cli}' -ArgumentList \"remove\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs")
148
- else
149
- clean = "\"--clean\","
150
- if disableClean(ip_address)
151
- clean = ''
152
- end
153
- stdin, stdout, stderr, wait_thr = Open3.popen3("sudo '#{cli}' remove #{clean} #{ip_address} #{hostnames}")
154
- end
190
+
191
+ stdin, stdout, stderr, wait_thr = removeGoodhostEntries(ip_address, hostnames)
155
192
  if !wait_thr.value.success?
156
193
  error = true
157
194
  errorText = stderr.read.strip
@@ -178,13 +215,14 @@ module VagrantPlugins
178
215
  hostnames_by_ips = []
179
216
  ips = getIps
180
217
  if ips.count() < 1
218
+ @ui.error("[vagrant-goodhosts] No ip address found for this virtual machine")
181
219
  return hostnames_by_ips
182
220
  end
183
221
  hostnames = getHostnames(ips)
184
222
  if ips.count() > 1
185
223
  ips.each do |ip|
186
224
  ip_address = ip
187
- if hostnames[ip].count() > 1
225
+ if hostnames[ip].count() > 0
188
226
  hostnames[ip].each do |hostname|
189
227
  if !ip_address.nil?
190
228
  @ui.info "[vagrant-goodhosts] - found entry for: #{ip_address} #{hostname}"
@@ -195,7 +233,7 @@ module VagrantPlugins
195
233
  end
196
234
  else
197
235
  ip_address = ips[0]
198
- if hostnames[ip_address].count() > 1
236
+ if hostnames[ip_address].count() > 0
199
237
  hostnames[ip_address].each do |hostname|
200
238
  if !ip_address.nil?
201
239
  @ui.info "[vagrant-goodhosts] - found entry for: #{ip_address} #{hostname}"
@@ -9,6 +9,7 @@ module VagrantPlugins
9
9
  attr_accessor :disable_clean
10
10
 
11
11
  def initialize
12
+ @remove_on_suspend = true
12
13
  @disable_clean = true
13
14
  end
14
15
  end
@@ -21,6 +21,10 @@ module VagrantPlugins
21
21
  hook.append(Action::UpdateHosts)
22
22
  end
23
23
 
24
+ action_hook(:goodhosts, :machine_action_boot) do |hook|
25
+ hook.append(Action::UpdateHosts)
26
+ end
27
+
24
28
  action_hook(:goodhosts, :machine_action_provision) do |hook|
25
29
  hook.before(Vagrant::Action::Builtin::Provision, Action::UpdateHosts)
26
30
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module GoodHosts
3
- VERSION = '1.0.15'
3
+ VERSION = '1.0.19'
4
4
  end
5
5
  end
data/package.sh CHANGED
@@ -13,3 +13,4 @@ rm -f ./*.txt
13
13
  # Generate
14
14
  cd ../../../
15
15
  gem build vagrant-goodhosts.gemspec
16
+ # gem push
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-goodhosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.15
4
+ version: 1.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniele Scasciafratte
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-19 00:00:00.000000000 Z
11
+ date: 2021-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,7 @@ homepage: https://github.com/goodhosts/vagrant
67
67
  licenses:
68
68
  - MIT
69
69
  metadata: {}
70
- post_install_message:
70
+ post_install_message:
71
71
  rdoc_options: []
72
72
  require_paths:
73
73
  - lib
@@ -82,8 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.2.5
86
- signing_key:
85
+ rubygems_version: 3.2.27
86
+ signing_key:
87
87
  specification_version: 4
88
88
  summary: Enables Vagrant to update hosts file on the host machine with goodhosts
89
89
  test_files: []