vagrant-betterhosts 0.2.1 → 1.1.6

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: dc74b912ef82a47719c3907399df1352357b8ada449fa828efc8547ff2d47e01
4
- data.tar.gz: 87a4c486054efe57b80d8e86fb7a44f6df6e50266e32893b2763fd017ae70fe4
3
+ metadata.gz: 60bf7a7c9e9cc61760d6709cb16288e4365e86d609495ad885551e8e960a15b2
4
+ data.tar.gz: dadf8fa51c50cd6a52802717bb82407e1f2781354c844cf9935fb1b8ec9d9f7f
5
5
  SHA512:
6
- metadata.gz: 94a0f4e95673498d85e5169b0e6de34acabf88f886535507ef544fab7e854b31b55eef14afe58ec0ed52a5fece35e9eac4b17c895536e33ad3bd55348f1c706a
7
- data.tar.gz: '04197759c798ab04d10a435943be2e82834ea6490f5fc42778ccdef18179754d2254a3a269b78ef71df0f5118c8c04bbe63c4aebf0172d5184fd8b0a1c1ea87e'
6
+ metadata.gz: '099f622af619dcedf96c42f9496e45cb792cfda71c5128ccd18828ff5309aed13668268b8ef2b3cc015a159f5110b396a02b9210f5f15d98efbab0913e599bb3'
7
+ data.tar.gz: 037f1613518ec4f02f54f3898526e966edc6370d7ac4b05b20f0e40390dd740853c02f1874b8f205f772a80d6f70ac6ba4175ecc97e909bc2e6f5baa904a05ce
data/.rubocop.yml ADDED
@@ -0,0 +1,71 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - 'Gemfile'
5
+ - 'Rakefile'
6
+ - 'lib/vagrant-goodhosts/config.rb'
7
+
8
+ Naming/FileName:
9
+ Enabled: false
10
+ Naming/AccessorMethodName:
11
+ Enabled: false
12
+ Metrics/MethodLength:
13
+ Enabled: false
14
+ Metrics/PerceivedComplexity:
15
+ Enabled: false
16
+ Metrics/ModuleLength:
17
+ Enabled: false
18
+ Metrics/CyclomaticComplexity:
19
+ Enabled: false
20
+ Metrics/AbcSize:
21
+ Enabled: false
22
+ Style/AndOr:
23
+ Enabled: false
24
+ Style/FrozenStringLiteralComment:
25
+ Enabled: false
26
+ Style/RaiseArgs:
27
+ Enabled: false
28
+ Layout/LineLength:
29
+ Enabled: false
30
+ Style/StringLiterals:
31
+ Enabled: false
32
+ Style/ExpandPathArguments:
33
+ Enabled: false
34
+ Style/RedundantReturn:
35
+ Enabled: false
36
+ Style/SymbolArray:
37
+ Enabled: false
38
+ Style/ClassVars:
39
+ Enabled: false
40
+ Style/YodaCondition:
41
+ Enabled: false
42
+ Style/MethodCallWithoutArgsParentheses:
43
+ Enabled: false
44
+ Style/NumericPredicate:
45
+ Enabled: false
46
+ Style/ParallelAssignment:
47
+ Enabled: false
48
+ Style/Not:
49
+ Enabled: false
50
+ Style/EmptyLiteral:
51
+ Enabled: false
52
+ Style/IfUnlessModifier:
53
+ Enabled: false
54
+ Style/MutableConstant:
55
+ Enabled: false
56
+ Style/SymbolLiteral:
57
+ Enabled: false
58
+ Style/AccessorGrouping:
59
+ Enabled: false
60
+ Style/Next:
61
+ Enabled: false
62
+ Style/MultipleComparison:
63
+ Enabled: false
64
+ Style/FormatStringToken:
65
+ Enabled: false
66
+ Style/SoleNestedConditional:
67
+ Enabled: false
68
+ Style/GuardClause:
69
+ Enabled: false
70
+ Gemspec/RequiredRubyVersion:
71
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-3.0.0
1
+ ruby-3.2.2
data/Gemfile CHANGED
@@ -7,3 +7,5 @@ end
7
7
  group :plugins do
8
8
  gem "vagrant-betterhosts", path: "."
9
9
  end
10
+
11
+ gem 'os'
data/Gemfile.lock CHANGED
@@ -1,34 +1,40 @@
1
1
  GIT
2
2
  remote: https://github.com/hashicorp/vagrant.git
3
- revision: 22bcb125696647295efce5efd63f809e190a7933
3
+ revision: 8004b9e001db13179e47b4b1895c0c88a1f59335
4
4
  branch: main
5
5
  specs:
6
- vagrant (2.2.18.dev)
6
+ vagrant (2.3.8.dev)
7
7
  bcrypt_pbkdf (~> 1.1)
8
8
  childprocess (~> 4.1.0)
9
- ed25519 (~> 1.2.4)
9
+ ed25519 (~> 1.3.0)
10
10
  erubi
11
+ googleapis-common-protos-types (~> 1.3)
12
+ grpc
11
13
  hashicorp-checkpoint (~> 0.1.5)
12
- i18n (~> 1.8)
13
- listen (~> 3.5)
14
+ i18n (~> 1.12)
15
+ ipaddr (>= 1.2.4)
16
+ listen (~> 3.7)
14
17
  log4r (~> 1.1.9, < 1.1.11)
15
18
  mime-types (~> 3.3)
16
- net-scp (~> 3.0.0)
17
- net-sftp (~> 3.0)
18
- net-ssh (>= 6.1.0, < 6.2)
19
+ net-ftp (~> 0.2)
20
+ net-scp (~> 4.0)
21
+ net-sftp (~> 4.0)
22
+ net-ssh (~> 7.0)
19
23
  rb-kqueue (~> 0.2.0)
20
24
  rexml (~> 3.2)
21
- rubyzip (~> 2.0)
22
- vagrant_cloud (~> 3.0.4)
23
- wdm (~> 0.1.0)
24
- winrm (>= 2.3.4, < 3.0)
25
- winrm-elevated (>= 1.2.1, < 2.0)
26
- winrm-fs (>= 1.3.4, < 2.0)
25
+ rgl (~> 0.5.10)
26
+ rubyzip (~> 2.3.2)
27
+ vagrant_cloud (~> 3.0.5)
28
+ wdm (~> 0.1.1)
29
+ winrm (>= 2.3.6, < 3.0)
30
+ winrm-elevated (>= 1.2.3, < 2.0)
31
+ winrm-fs (>= 1.3.5, < 2.0)
27
32
 
28
33
  PATH
29
34
  remote: .
30
35
  specs:
31
- vagrant-betterhosts (0.2.1)
36
+ vagrant-betterhosts (1.1.6)
37
+ os (~> 0.9)
32
38
 
33
39
  GEM
34
40
  remote: https://rubygems.org/
@@ -36,48 +42,73 @@ GEM
36
42
  bcrypt_pbkdf (1.1.0)
37
43
  builder (3.2.4)
38
44
  childprocess (4.1.0)
39
- concurrent-ruby (1.1.9)
40
- ed25519 (1.2.4)
41
- erubi (1.10.0)
42
- excon (0.84.0)
43
- ffi (1.15.3)
45
+ concurrent-ruby (1.2.2)
46
+ date (3.3.3)
47
+ ed25519 (1.3.0)
48
+ erubi (1.12.0)
49
+ excon (0.100.0)
50
+ ffi (1.15.5)
51
+ google-protobuf (3.23.4-x86_64-linux)
52
+ googleapis-common-protos-types (1.7.0)
53
+ google-protobuf (~> 3.14)
54
+ grpc (1.56.2-x86_64-linux)
55
+ google-protobuf (~> 3.23)
56
+ googleapis-common-protos-types (~> 1.0)
44
57
  gssapi (1.3.1)
45
58
  ffi (>= 1.0.1)
46
- gyoku (1.3.1)
59
+ gyoku (1.4.0)
47
60
  builder (>= 2.1.2)
61
+ rexml (~> 3.0)
48
62
  hashicorp-checkpoint (0.1.5)
49
63
  httpclient (2.8.3)
50
- i18n (1.8.10)
64
+ i18n (1.14.1)
51
65
  concurrent-ruby (~> 1.0)
52
- listen (3.5.1)
66
+ ipaddr (1.2.5)
67
+ listen (3.8.0)
53
68
  rb-fsevent (~> 0.10, >= 0.10.3)
54
69
  rb-inotify (~> 0.9, >= 0.9.10)
55
70
  little-plugger (1.1.4)
56
71
  log4r (1.1.10)
57
- logging (2.3.0)
72
+ logging (2.3.1)
58
73
  little-plugger (~> 1.1)
59
74
  multi_json (~> 1.14)
60
- mime-types (3.3.1)
75
+ mime-types (3.4.1)
61
76
  mime-types-data (~> 3.2015)
62
- mime-types-data (3.2021.0704)
77
+ mime-types-data (3.2023.0218.1)
63
78
  multi_json (1.15.0)
64
- net-scp (3.0.0)
65
- net-ssh (>= 2.6.5, < 7.0.0)
66
- net-sftp (3.0.0)
67
- net-ssh (>= 5.0.0, < 7.0.0)
68
- net-ssh (6.1.0)
79
+ net-ftp (0.2.0)
80
+ net-protocol
81
+ time
82
+ net-protocol (0.2.1)
83
+ timeout
84
+ net-scp (4.0.0)
85
+ net-ssh (>= 2.6.5, < 8.0.0)
86
+ net-sftp (4.0.0)
87
+ net-ssh (>= 5.0.0, < 8.0.0)
88
+ net-ssh (7.1.0)
69
89
  nori (2.6.0)
70
- rb-fsevent (0.11.0)
90
+ os (0.9.6)
91
+ pairing_heap (3.0.1)
92
+ rb-fsevent (0.11.2)
71
93
  rb-inotify (0.10.1)
72
94
  ffi (~> 1.0)
73
- rb-kqueue (0.2.6)
95
+ rb-kqueue (0.2.8)
74
96
  ffi (>= 0.5.0)
75
97
  rexml (3.2.5)
98
+ rgl (0.5.10)
99
+ pairing_heap (>= 0.3.0)
100
+ rexml (~> 3.2, >= 3.2.4)
101
+ stream (~> 0.5.3)
76
102
  rubyntlm (0.6.3)
77
103
  rubyzip (2.3.2)
78
- vagrant_cloud (3.0.4)
104
+ stream (0.5.5)
105
+ time (0.2.2)
106
+ date
107
+ timeout (0.4.0)
108
+ vagrant_cloud (3.0.5)
79
109
  excon (~> 0.73)
80
110
  log4r (~> 1.1.10)
111
+ rexml (~> 3.2.5)
81
112
  wdm (0.1.1)
82
113
  winrm (2.3.6)
83
114
  builder (>= 2.1.2)
@@ -102,8 +133,9 @@ PLATFORMS
102
133
  x86_64-linux
103
134
 
104
135
  DEPENDENCIES
136
+ os
105
137
  vagrant!
106
138
  vagrant-betterhosts!
107
139
 
108
140
  BUNDLED WITH
109
- 2.2.3
141
+ 2.4.10
data/README.md CHANGED
@@ -91,20 +91,25 @@ This enable `vagrant-betterhosts` from running the clean command in every call.
91
91
  ## Suppressing prompts for elevating privileges
92
92
 
93
93
  These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
94
+ The command path is printed in red when there are errors with Vagrant eg. if the sudo password is entered incorrectly for 3 times.
94
95
  If you understand the risks that go with supressing them, here's how to do it.
95
96
 
96
97
  ### Linux/OS X: Passwordless sudo
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_betterhosts`.
99
- The command path is printed when there are errors with sudo.
99
+ 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`.
100
+ The command path is printed when there are errors with Vagrant, check the output marked in red.
100
101
 
101
102
  For Ubuntu and most Linux environments:
102
103
 
103
- %sudo ALL=(root) NOPASSWD: [the-path]
104
+ %sudo ALL=(root) NOPASSWD: [the-command-path]
105
+
106
+ An example complete:
107
+
108
+ %sudo ALL=(root) NOPASSWD: /home/user/sites/vvv/.vagrant/plugins/gems/3.1.2/gems/vagrant-goodhosts-1.1.6/lib/vagrant-goodhosts/bundle/cli_amd64_linux
104
109
 
105
110
  For MacOS:
106
111
 
107
- %admin ALL=(root) NOPASSWD: [the-path]
112
+ %admin ALL=(root) NOPASSWD: [the-command-path]
108
113
 
109
114
  Replace in both %sudo/%admin with the username it if it is not working for you.
110
115
 
@@ -115,7 +120,7 @@ You have to open an elevated command prompt; hold `❖ Win` and press `X`, then
115
120
 
116
121
  cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W
117
122
 
118
- ## Installing The Development Version
123
+ ## Generate The Development Version
119
124
 
120
125
  If you would like to install `vagrant-betterhosts` to make contributions or changes, run the following commands::
121
126
 
@@ -125,3 +130,7 @@ cd vagrant-betterhosts
125
130
  ./package.sh
126
131
  vagrant plugin install vagrant-betterhosts-*.gem
127
132
  ```
133
+
134
+ ## Test the plugin
135
+
136
+ You need to run a Vagrant machine with the minimum settings specified in the [Usage](https://github.com/goodhosts/vagrant#usage) section, it is enough a turn on and off and in the meantime if the hosts file in your machine is written in the right way.
@@ -1,6 +1,8 @@
1
+ # Action to extend for the plugin needs, detects if it was already executed etc
1
2
  module VagrantPlugins
2
3
  module BetterHosts
3
4
  module Action
5
+ # Extend it!
4
6
  class BaseAction
5
7
  include BetterHosts
6
8
 
@@ -35,10 +37,9 @@ module VagrantPlugins
35
37
  @app.call(env)
36
38
  end
37
39
 
38
- def run(env)
40
+ def run(_env)
39
41
  raise NotImplementedError.new("Must be implemented!")
40
42
  end
41
-
42
43
  end
43
44
  end
44
45
  end
@@ -1,8 +1,9 @@
1
+ # Run when is removing the hosts
1
2
  module VagrantPlugins
2
3
  module BetterHosts
3
4
  module Action
5
+ # Remove hosts
4
6
  class RemoveHosts < BaseAction
5
-
6
7
  def run(env)
7
8
  machine_action = env[:machine_action]
8
9
 
@@ -12,11 +13,9 @@ module VagrantPlugins
12
13
  if (%i[halt suspend].include? machine_action) && (false == @machine.config.goodhosts.remove_on_suspend)
13
14
  @ui.info '[vagrant-betterhosts] Removing hosts on suspend disabled'
14
15
  else
15
- @ui.info '[vagrant-betterhosts] Removing hosts'
16
- removeHostEntries
16
+ remove_host_entries
17
17
  end
18
18
  end
19
-
20
19
  end
21
20
  end
22
21
  end
@@ -1,13 +1,12 @@
1
+ # Run when is adding hosts
1
2
  module VagrantPlugins
2
3
  module BetterHosts
3
4
  module Action
5
+ # Update hosts
4
6
  class UpdateHosts < BaseAction
5
-
6
- def run(env)
7
- @ui.info "[vagrant-betterhosts] Checking for host entries"
8
- addHostEntries()
7
+ def run(_env)
8
+ add_host_entries()
9
9
  end
10
-
11
10
  end
12
11
  end
13
12
  end
@@ -1,12 +1,25 @@
1
+ # The core of the plugin
1
2
  require "rbconfig"
2
3
  require "open3"
4
+ require "resolv"
5
+ require "os"
3
6
 
4
7
  module VagrantPlugins
5
8
  module BetterHosts
9
+ # Plugin module
6
10
  module BetterHosts
7
- def getIps
11
+ def get_ips
8
12
  ips = []
9
13
 
14
+ if @machine.provider_name == :docker
15
+ @ui.info '[vagrant-betterhosts] Docker detected, adding 127.0.0.1 and ::1 IP addresses'
16
+ ip = "127.0.0.1"
17
+ ips.push(ip) unless ip.nil? or ips.include? ip
18
+ ip = "::1"
19
+ ips.push(ip) unless ip.nil? or ips.include? ip
20
+ return ips
21
+ end
22
+
10
23
  if @machine.config.vm.networks.length == 0
11
24
  @ui.error("[vagrant-betterhosts] No ip address found for this virtual machine")
12
25
  exit
@@ -14,59 +27,50 @@ module VagrantPlugins
14
27
 
15
28
  @machine.config.vm.networks.each do |network|
16
29
  key, options = network[0], network[1]
17
- ip = options[:ip] if (key == :private_network || key == :public_network) && options[:betterhosts] != "skip"
18
- ips.push(ip) if ip
19
30
  if options[:betterhosts] == "skip"
20
31
  @ui.info '[vagrant-betterhosts] Skipped adding host entries (config.vm.network betterhosts: "skip" is set)'
21
32
  end
22
-
23
- @machine.config.vm.provider :hyperv do |v|
24
- timeout = @machine.provider_config.ip_address_timeout
25
- @ui.output("[vagrant-betterhosts] Waiting for the guest machine to report its IP address ( this might take some time, have patience )...")
26
- @ui.detail("Timeout: #{timeout} seconds")
27
-
28
- options = {
29
- vmm_server_address: @machine.provider_config.vmm_server_address,
30
- proxy_server_address: @machine.provider_config.proxy_server_address,
31
- timeout: timeout,
32
- machine: @machine,
33
- }
34
- network = @machine.provider.driver.read_guest_ip(options)
35
- if network["ip"]
36
- ips.push(network["ip"]) unless ips.include? network["ip"]
37
- end
38
- end
39
-
40
-
33
+ ip = options[:ip] if (key == :private_network || key == :public_network) && options[:betterhosts] != "skip"
34
+ ips.push(ip) if ip
35
+ end
36
+ if @machine.provider_name == :hyperv
37
+ ip = @machine.provider.driver.read_guest_ip["ip"]
38
+ @ui.info "[vagrant-betterhosts] Read guest IP #{ip} from Hyper-V provider"
39
+ ips.push(ip) unless ip.nil? or ips.include? ip
41
40
  end
42
41
  return ips
43
42
  end
44
43
 
45
- # https://stackoverflow.com/a/13586108/1902215
46
44
  def get_os_binary
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)
45
+ if OS.windows?
46
+ return 'cli.exe'
47
+ elsif OS.mac?
48
+ if Etc.uname[:version].include? 'ARM64'
49
+ return 'cli_arm64_osx'
50
+ else
51
+ return 'cli_amd64_osx'
52
+ end
53
+ elsif OS.linux?
54
+ if Etc.uname[:version].include? 'ARM64'
55
+ return 'cli_arm64_linux'
56
+ else
57
+ return 'cli_amd64_linux'
58
+ end
59
+ else
60
+ raise Error::WebDriverError, "unknown os: #{host_os.inspect}"
61
+ end
58
62
  end
59
63
 
60
64
  def get_cli
61
65
  binary = get_os_binary
62
- path = File.expand_path(File.dirname(File.dirname(__FILE__))) + "/vagrant-betterhosts/bundle/"
66
+ path = format('%s%s', File.expand_path(File.dirname(File.dirname(__FILE__))), "/vagrant-betterhosts/bundle/")
63
67
  path = "#{path}#{binary}"
64
68
 
65
69
  return path
66
70
  end
67
71
 
68
72
  # Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
69
- def getHostnames(ips)
73
+ def get_hostnames(ips)
70
74
  hostnames = Hash.new { |h, k| h[k] = [] }
71
75
 
72
76
  case @machine.config.betterhosts.aliases
@@ -89,102 +93,150 @@ module VagrantPlugins
89
93
  hostnames
90
94
  end
91
95
 
92
- def disableClean(ip_address)
96
+ def disable_clean(ip_address)
93
97
  unless ip_address.nil?
94
98
  return @machine.config.betterhosts.disable_clean
95
99
  end
96
100
  return true
97
101
  end
98
102
 
99
- def addHostEntries
100
- error = false
101
- errorText = ""
103
+ def check_hostnames_to_add(ip_address, hostnames)
104
+ hostnames_to_add = Array.new
105
+ hostnames = hostnames.split
106
+ # check which hostnames actually need adding
107
+ hostnames.each do |hostname|
108
+ begin
109
+ address = Resolv.getaddress(hostname)
110
+ if address != ip_address
111
+ hostnames_to_add.append(hostname)
112
+ end
113
+ rescue StandardError => _e
114
+ hostnames_to_add.append(hostname)
115
+ end
116
+ rescue StandardError => _e
117
+ hostnames_to_add.append(hostname)
118
+ end
119
+ return hostnames_to_add.join(' ')
120
+ end
121
+
122
+ def add_betterhost_entries(ip_address, hostnames)
102
123
  cli = get_cli
103
- hostnames_by_ips = generateHostnamesByIps
124
+ if cli.include? ".exe"
125
+ clean = get_clean_parameter_by_system(ip_address, true)
126
+ command = "Start-Process '#{cli}' -ArgumentList \"add\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs"
127
+ stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", command)
128
+ else
129
+ clean = get_clean_parameter_by_system(ip_address, false)
130
+ command = "sudo '#{cli}' add #{clean} #{ip_address} #{hostnames}"
131
+ stdin, stdout, stderr, wait_thr = Open3.popen3(command)
132
+ end
133
+ return stdin, stdout, stderr, wait_thr, command
134
+ end
135
+
136
+ def add_host_entries
137
+ error = false
138
+ error_text = ''
139
+ command = ''
140
+ hostnames_by_ips = generate_hostnames_by_ips
104
141
 
105
- return if not hostnames_by_ips.any?
142
+ return if hostnames_by_ips.none?
143
+
144
+ @ui.info "[vagrant-betterhosts] Checking for host entries"
106
145
 
107
146
  hostnames_by_ips.each do |ip_address, hostnames|
108
147
  if ip_address.nil?
109
148
  @ui.error "[vagrant-betterhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}"
110
149
  next
111
150
  end
112
- @ui.info "[vagrant-betterhosts] Adding #{hostnames} for address #{ip_address}"
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
- if !wait_thr.value.success?
151
+
152
+ # filter out the hosts we've already added
153
+ hosts_to_add = check_hostnames_to_add(ip_address, hostnames)
154
+ next if hosts_to_add.empty?
155
+
156
+ _stdin, _stdout, stderr, wait_thr, command = add_betterhost_entries(ip_address, hosts_to_add)
157
+ unless wait_thr.value.success?
127
158
  error = true
128
- errorText = stderr.read.strip
159
+ error_text = stderr.read.strip
129
160
  end
130
161
  end
131
- printReadme(error, errorText)
162
+ print_readme(error, error_text, command)
132
163
  end
133
164
 
134
- def removeHostEntries
135
- error = false
136
- errorText = ""
165
+ def remove_betterhost_entries(ip_address, hostnames)
137
166
  cli = get_cli
138
- hostnames_by_ips = generateHostnamesByIps
167
+ if cli.include? ".exe"
168
+ clean = get_clean_parameter_by_system(ip_address, true)
169
+ command = "Start-Process '#{cli}' -ArgumentList \"remove\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs"
170
+ stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", command)
171
+ else
172
+ clean = get_clean_parameter_by_system(ip_address, false)
173
+ command = "sudo '#{cli}' remove #{clean} #{ip_address} #{hostnames}"
174
+ stdin, stdout, stderr, wait_thr = Open3.popen3(command)
175
+ end
176
+ return stdin, stdout, stderr, wait_thr, command
177
+ end
178
+
179
+ def remove_host_entries
180
+ error = false
181
+ error_text = ''
182
+ command = ''
183
+ hostnames_by_ips = generate_hostnames_by_ips
139
184
 
140
- return if not hostnames_by_ips.any?
185
+ return if hostnames_by_ips.none?
186
+
187
+ @ui.info "[vagrant-betterhosts] Removing hosts"
141
188
 
142
189
  hostnames_by_ips.each do |ip_address, hostnames|
143
190
  if ip_address.nil?
144
191
  @ui.error "[vagrant-betterhosts] Error adding some hosts, no IP was provided for the following hostnames: #{hostnames}"
145
192
  next
146
193
  end
147
- if cli.include? ".exe"
148
- clean = "\"--clean\","
149
- if disableClean(ip_address)
150
- clean = ''
151
- end
152
- stdin, stdout, stderr, wait_thr = Open3.popen3("powershell", "-Command", "Start-Process '#{cli}' -ArgumentList \"remove\",#{clean}\"#{ip_address}\",\"#{hostnames}\" -Verb RunAs")
153
- else
154
- clean = "\"--clean\","
155
- if disableClean(ip_address)
156
- clean = ''
157
- end
158
- stdin, stdout, stderr, wait_thr = Open3.popen3("sudo '#{cli}' remove #{clean} #{ip_address} #{hostnames}")
159
- end
160
- if !wait_thr.value.success?
194
+
195
+ _stdin, _stdout, stderr, wait_thr, command = remove_betterhost_entries(ip_address, hostnames)
196
+ unless wait_thr.value.success?
161
197
  error = true
162
- errorText = stderr.read.strip
198
+ error_text = stderr.read.strip
163
199
  end
164
200
  end
165
- printReadme(error, errorText)
201
+ print_readme(error, error_text, command)
166
202
  end
167
203
 
168
- def printReadme(error, errorText)
169
- if error
170
- cli = get_cli
171
- @ui.error "[vagrant-betterhosts] Issue executing goodhosts CLI: #{errorText}"
172
- @ui.error "[vagrant-betterhosts] Cli path: #{cli}"
173
- if cli.include? ".exe"
174
- @ui.error "[vagrant-betterhosts] Check the readme at https://github.com/ajxb/vagrant-betterhosts#windows-uac-prompt"
175
- exit
176
- else
177
- @ui.error "[vagrant-betterhosts] Check the readme at https://github.com/ajxb/vagrant-betterhosts#passwordless-sudo"
178
- end
204
+ def get_clean_parameter_by_system(ip_address, is_win)
205
+ clean = "--clean"
206
+ if is_win
207
+ clean = "\"--clean\","
208
+ end
209
+
210
+ if disable_clean(ip_address)
211
+ clean = ''
212
+ end
213
+ return clean
214
+ end
215
+
216
+ def print_readme(error, error_text, command)
217
+ unless error
218
+ @ui.info "[vagrant-betterhosts] Finished processing"
219
+ return false
220
+ end
221
+
222
+ cli = get_cli
223
+ @ui.error "[vagrant-betterhosts] Issue executing goodhosts CLI: #{error_text}"
224
+ @ui.error "[vagrant-betterhosts] Command: #{command}"
225
+ @ui.error "[vagrant-betterhosts] Cli path: #{cli}"
226
+ if cli.include? ".exe"
227
+ @ui.error "[vagrant-betterhosts] Check the readme at https://github.com/betterhosts/vagrant#windows-uac-prompt"
228
+ exit
229
+ else
230
+ @ui.error "[vagrant-betterhosts] Check the readme at https://github.com/betterhosts/vagrant#passwordless-sudo"
179
231
  end
180
232
  end
181
233
 
182
- def generateHostnamesByIps()
183
- ips = getIps
234
+ def generate_hostnames_by_ips
235
+ ips = get_ips
184
236
  return [] unless ips.any?
185
237
 
186
238
  hostnames_by_ips = {}
187
- hostnames = getHostnames(ips)
239
+ hostnames = get_hostnames(ips)
188
240
  ips.each do |ip|
189
241
  hostnames_by_ips[ip] = hostnames[ip].join(' ') if hostnames[ip].any?
190
242
  end
Binary file
@@ -2,6 +2,7 @@ require "vagrant"
2
2
 
3
3
  module VagrantPlugins
4
4
  module BetterHosts
5
+ # Vagrant plugin definition
5
6
  class Config < Vagrant.plugin("2", :config)
6
7
  attr_accessor :aliases
7
8
  attr_accessor :id
@@ -5,6 +5,7 @@ require_relative "Action/RemoveHosts"
5
5
 
6
6
  module VagrantPlugins
7
7
  module BetterHosts
8
+ # Various Vagrant hooks
8
9
  class Plugin < Vagrant.plugin('2')
9
10
  name 'BetterHosts'
10
11
  description <<-DESC
@@ -21,20 +22,24 @@ module VagrantPlugins
21
22
  hook.append(Action::UpdateHosts)
22
23
  end
23
24
 
25
+ action_hook(:betterhosts, :machine_action_boot) do |hook|
26
+ hook.append(Action::UpdateHosts)
27
+ end
28
+
24
29
  action_hook(:betterhosts, :machine_action_provision) do |hook|
25
30
  hook.before(Vagrant::Action::Builtin::Provision, Action::UpdateHosts)
26
31
  end
27
32
 
28
33
  action_hook(:betterhosts, :machine_action_halt) do |hook|
29
- hook.append(Action::RemoveHosts)
34
+ hook.prepend(Action::RemoveHosts)
30
35
  end
31
36
 
32
37
  action_hook(:betterhosts, :machine_action_suspend) do |hook|
33
- hook.append(Action::RemoveHosts)
38
+ hook.prepend(Action::RemoveHosts)
34
39
  end
35
40
 
36
41
  action_hook(:betterhosts, :machine_action_destroy) do |hook|
37
- hook.append(Action::RemoveHosts)
42
+ hook.prepend(Action::RemoveHosts)
38
43
  end
39
44
 
40
45
  action_hook(:betterhosts, :machine_action_reload) do |hook|
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module BetterHosts
3
- VERSION = '0.2.1'
3
+ VERSION = '1.1.6'
4
4
  end
5
5
  end
@@ -1,11 +1,13 @@
1
+ # Root file of the plugin
1
2
  require "vagrant-betterhosts/version"
2
3
  require "vagrant-betterhosts/plugin"
3
4
 
5
+ # Extend Vagrant Plugins
4
6
  module VagrantPlugins
7
+ # Load our plugin
5
8
  module BetterHosts
6
9
  def self.source_root
7
10
  @source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
8
11
  end
9
12
  end
10
13
  end
11
-
data/package.sh CHANGED
@@ -5,9 +5,11 @@ cd ./lib/vagrant-betterhosts/bundle
5
5
  # Download
6
6
  curl -s https://api.github.com/repos/goodhosts/cli/releases/latest | jq --raw-output '.assets[] | .browser_download_url' | xargs wget -i
7
7
  # Extract
8
- tar -zxvf goodhosts_darwin_amd64.tar.gz goodhosts && mv goodhosts cli_osx
9
- tar -zxvf goodhosts_linux_amd64.tar.gz goodhosts && mv goodhosts cli
10
- tar -zxvf goodhosts_windows_amd64.tar.gz goodhosts.exe && mv goodhosts.exe cli.exe
8
+ tar -zxvf goodhosts-1.1.0-darwin-amd64.tar.gz goodhosts && mv goodhosts cli_amd64_osx
9
+ tar -zxvf goodhosts-1.1.0-darwin-arm64.tar.gz goodhosts && mv goodhosts cli_arm64_osx
10
+ tar -zxvf goodhosts-1.1.0-linux-amd64.tar.gz goodhosts && mv goodhosts cli_amd64_linux
11
+ tar -zxvf goodhosts-1.1.0-linux-arm64.tar.gz goodhosts && mv goodhosts cli_arm64_linux
12
+ tar -zxvf goodhosts-1.1.0-windows-amd64.tar.gz goodhosts.exe && mv goodhosts.exe cli.exe
11
13
  rm -f ./*.tar.gz
12
14
  rm -f ./*.txt
13
15
  # Generate
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.homepage = 'https://github.com/ajxb/vagrant-betterhosts'
15
15
  s.license = 'MIT'
16
16
 
17
- s.required_ruby_version = ">= 2.5", "< 3.1"
17
+ s.required_ruby_version = ">= 2.5"
18
18
  s.files = `git ls-files`.split($/)
19
19
  s.files += Dir.glob("lib/vagrant-betterhosts/bundle/*")
20
20
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -23,4 +23,6 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.add_development_dependency 'bundler', '~> 1.3'
25
25
  s.add_development_dependency 'rake', '~> 13.0'
26
+
27
+ s.add_runtime_dependency 'os', '~> 0.9'
26
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-betterhosts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Butler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-12 00:00:00.000000000 Z
11
+ date: 2023-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: os
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
41
55
  description: Enables Vagrant to update hosts file on the host machine using the goodhosts
42
56
  cli tool
43
57
  email:
@@ -52,6 +66,7 @@ files:
52
66
  - ".idea/modules.xml"
53
67
  - ".idea/vagrant-betterhosts.iml"
54
68
  - ".idea/vcs.xml"
69
+ - ".rubocop.yml"
55
70
  - ".ruby-gemset"
56
71
  - ".ruby-version"
57
72
  - Gemfile
@@ -64,9 +79,11 @@ files:
64
79
  - lib/vagrant-betterhosts/Action/RemoveHosts.rb
65
80
  - lib/vagrant-betterhosts/Action/UpdateHosts.rb
66
81
  - lib/vagrant-betterhosts/BetterHosts.rb
67
- - lib/vagrant-betterhosts/bundle/cli
68
82
  - lib/vagrant-betterhosts/bundle/cli.exe
69
- - lib/vagrant-betterhosts/bundle/cli_osx
83
+ - lib/vagrant-betterhosts/bundle/cli_amd64_linux
84
+ - lib/vagrant-betterhosts/bundle/cli_amd64_osx
85
+ - lib/vagrant-betterhosts/bundle/cli_arm64_linux
86
+ - lib/vagrant-betterhosts/bundle/cli_arm64_osx
70
87
  - lib/vagrant-betterhosts/config.rb
71
88
  - lib/vagrant-betterhosts/plugin.rb
72
89
  - lib/vagrant-betterhosts/version.rb
@@ -85,16 +102,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
85
102
  - - ">="
86
103
  - !ruby/object:Gem::Version
87
104
  version: '2.5'
88
- - - "<"
89
- - !ruby/object:Gem::Version
90
- version: '3.1'
91
105
  required_rubygems_version: !ruby/object:Gem::Requirement
92
106
  requirements:
93
107
  - - ">="
94
108
  - !ruby/object:Gem::Version
95
109
  version: '0'
96
110
  requirements: []
97
- rubygems_version: 3.2.3
111
+ rubygems_version: 3.4.10
98
112
  signing_key:
99
113
  specification_version: 4
100
114
  summary: Vagrant plugin to manage the hosts file on the host machine
Binary file
Binary file