vagrant-betterhosts 0.2.0 → 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: eca3ca87fa47244989ff3f69adfa4eac5ac0793cc68d81b05ff1f77d053da052
4
- data.tar.gz: 26bdb0c3f05e282baa1760849a65e7dfeaeba2ab267ab95a6916a6f5dd0f7c02
3
+ metadata.gz: 60bf7a7c9e9cc61760d6709cb16288e4365e86d609495ad885551e8e960a15b2
4
+ data.tar.gz: dadf8fa51c50cd6a52802717bb82407e1f2781354c844cf9935fb1b8ec9d9f7f
5
5
  SHA512:
6
- metadata.gz: dc7655a442b2ba3aeee62349861c3ee0979e5f754ae8744a7925ff3b67ba184eb4dfc3c8784767332bbb6de080638b7a6b4bfd0b6e966c95305d3d2ee0923473
7
- data.tar.gz: e4c5bf8a7ae014223a0d688d56c8ee5221dcc2ef813ff086b63e164239f0f139a6824ad47d130a7445c01bd8eb0f7ddfaa1da52bfc4c630313752118b4cd8ffd
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-2.7.2
1
+ ruby-3.2.2
data/Gemfile CHANGED
@@ -1,9 +1,11 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  group :development do
4
- gem "vagrant", git: "https://github.com/hashicorp/vagrant.git"
4
+ gem "vagrant", git: "https://github.com/hashicorp/vagrant.git", branch: 'main'
5
5
  end
6
6
 
7
7
  group :plugins do
8
8
  gem "vagrant-betterhosts", path: "."
9
9
  end
10
+
11
+ gem 'os'
data/Gemfile.lock CHANGED
@@ -1,94 +1,114 @@
1
1
  GIT
2
2
  remote: https://github.com/hashicorp/vagrant.git
3
- revision: 22795b161bf67a4c0ebbe32c9ce8777cb888c4a7
3
+ revision: 8004b9e001db13179e47b4b1895c0c88a1f59335
4
+ branch: main
4
5
  specs:
5
- vagrant (2.2.11.dev)
6
- bcrypt_pbkdf (~> 1.0.0)
7
- childprocess (~> 4.0.0)
8
- ed25519 (~> 1.2.4)
6
+ vagrant (2.3.8.dev)
7
+ bcrypt_pbkdf (~> 1.1)
8
+ childprocess (~> 4.1.0)
9
+ ed25519 (~> 1.3.0)
9
10
  erubi
11
+ googleapis-common-protos-types (~> 1.3)
12
+ grpc
10
13
  hashicorp-checkpoint (~> 0.1.5)
11
- i18n (~> 1.8)
12
- listen (~> 3.1)
14
+ i18n (~> 1.12)
15
+ ipaddr (>= 1.2.4)
16
+ listen (~> 3.7)
13
17
  log4r (~> 1.1.9, < 1.1.11)
14
- mime (~> 0.4.4)
15
- net-scp (~> 1.2.0)
16
- net-sftp (~> 3.0)
17
- net-ssh (~> 6.0)
18
+ mime-types (~> 3.3)
19
+ net-ftp (~> 0.2)
20
+ net-scp (~> 4.0)
21
+ net-sftp (~> 4.0)
22
+ net-ssh (~> 7.0)
18
23
  rb-kqueue (~> 0.2.0)
19
- rest-client (>= 1.6.0, < 3.0)
20
- ruby_dep (<= 1.3.1)
21
- rubyzip (~> 2.0)
22
- vagrant_cloud (~> 2.0.3)
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)
24
+ rexml (~> 3.2)
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.0)
36
+ vagrant-betterhosts (1.1.6)
37
+ os (~> 0.9)
32
38
 
33
39
  GEM
34
40
  remote: https://rubygems.org/
35
41
  specs:
36
- bcrypt_pbkdf (1.0.1)
42
+ bcrypt_pbkdf (1.1.0)
37
43
  builder (3.2.4)
38
- childprocess (4.0.0)
39
- concurrent-ruby (1.1.9)
40
- domain_name (0.5.20190701)
41
- unf (>= 0.0.5, < 1.0.0)
42
- ed25519 (1.2.4)
43
- erubi (1.10.0)
44
- ffi (1.15.1)
44
+ childprocess (4.1.0)
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)
45
57
  gssapi (1.3.1)
46
58
  ffi (>= 1.0.1)
47
- gyoku (1.3.1)
59
+ gyoku (1.4.0)
48
60
  builder (>= 2.1.2)
61
+ rexml (~> 3.0)
49
62
  hashicorp-checkpoint (0.1.5)
50
- http-cookie (1.0.3)
51
- domain_name (~> 0.5)
52
63
  httpclient (2.8.3)
53
- i18n (1.8.10)
64
+ i18n (1.14.1)
54
65
  concurrent-ruby (~> 1.0)
55
- listen (3.5.1)
66
+ ipaddr (1.2.5)
67
+ listen (3.8.0)
56
68
  rb-fsevent (~> 0.10, >= 0.10.3)
57
69
  rb-inotify (~> 0.9, >= 0.9.10)
58
70
  little-plugger (1.1.4)
59
71
  log4r (1.1.10)
60
- logging (2.3.0)
72
+ logging (2.3.1)
61
73
  little-plugger (~> 1.1)
62
74
  multi_json (~> 1.14)
63
- mime (0.4.4)
64
- mime-types (3.3.1)
75
+ mime-types (3.4.1)
65
76
  mime-types-data (~> 3.2015)
66
- mime-types-data (3.2021.0225)
77
+ mime-types-data (3.2023.0218.1)
67
78
  multi_json (1.15.0)
68
- net-scp (1.2.1)
69
- net-ssh (>= 2.6.5)
70
- net-sftp (3.0.0)
71
- net-ssh (>= 5.0.0, < 7.0.0)
72
- net-ssh (6.1.0)
73
- netrc (0.11.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)
74
89
  nori (2.6.0)
75
- rb-fsevent (0.11.0)
90
+ os (0.9.6)
91
+ pairing_heap (3.0.1)
92
+ rb-fsevent (0.11.2)
76
93
  rb-inotify (0.10.1)
77
94
  ffi (~> 1.0)
78
- rb-kqueue (0.2.6)
95
+ rb-kqueue (0.2.8)
79
96
  ffi (>= 0.5.0)
80
- rest-client (2.0.2)
81
- http-cookie (>= 1.0.2, < 2.0)
82
- mime-types (>= 1.16, < 4.0)
83
- netrc (~> 0.8)
84
- ruby_dep (1.3.1)
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)
85
102
  rubyntlm (0.6.3)
86
- rubyzip (2.3.0)
87
- unf (0.1.4)
88
- unf_ext
89
- unf_ext (0.0.7.7)
90
- vagrant_cloud (2.0.3)
91
- rest-client (~> 2.0.2)
103
+ rubyzip (2.3.2)
104
+ stream (0.5.5)
105
+ time (0.2.2)
106
+ date
107
+ timeout (0.4.0)
108
+ vagrant_cloud (3.0.5)
109
+ excon (~> 0.73)
110
+ log4r (~> 1.1.10)
111
+ rexml (~> 3.2.5)
92
112
  wdm (0.1.1)
93
113
  winrm (2.3.6)
94
114
  builder (>= 2.1.2)
@@ -110,11 +130,12 @@ GEM
110
130
  winrm (~> 2.0)
111
131
 
112
132
  PLATFORMS
113
- ruby
133
+ x86_64-linux
114
134
 
115
135
  DEPENDENCIES
136
+ os
116
137
  vagrant!
117
138
  vagrant-betterhosts!
118
139
 
119
140
  BUNDLED WITH
120
- 2.1.4
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.0'
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", "< 2.8"
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.0
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-06-19 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
@@ -82,19 +99,16 @@ require_paths:
82
99
  - lib
83
100
  required_ruby_version: !ruby/object:Gem::Requirement
84
101
  requirements:
85
- - - "~>"
102
+ - - ">="
86
103
  - !ruby/object:Gem::Version
87
104
  version: '2.5'
88
- - - "<"
89
- - !ruby/object:Gem::Version
90
- version: '2.8'
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.1.4
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