vagrant-hostsupdater 1.1.1.160 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +115 -7
- data/lib/vagrant-hostsupdater/Action/BaseAction.rb +44 -0
- data/lib/vagrant-hostsupdater/Action/CacheHosts.rb +2 -9
- data/lib/vagrant-hostsupdater/Action/RemoveHosts.rb +11 -18
- data/lib/vagrant-hostsupdater/Action/UpdateHosts.rb +3 -13
- data/lib/vagrant-hostsupdater/HostsUpdater.rb +135 -25
- data/lib/vagrant-hostsupdater/plugin.rb +5 -6
- data/lib/vagrant-hostsupdater/version.rb +1 -1
- data/vagrant-hostsupdater.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8fa0669f6bc99a98b0182d0daeb88f22f35987827d604cdac71402a67ebd3fbe
|
4
|
+
data.tar.gz: 0bc5cefa703b1fef67d9f07f06b5fb9bc64c5e7ce90267f8ee25881967d3cbbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74abfa02f179167a588d63892c6e9c47e067c5c19f8a39d2f22ce652bd8cf35f17f17d9585f764c53e4c2c37cae9f4ae52d58f65a9f6d0a24ce9ec01f2819597
|
7
|
+
data.tar.gz: 46d6b9bc921ffb0005aa88eae4a6299d9322ad079372e1a1335548324e23d9799b79e2784f7929632e31fcf66ea953517496821b51ba5f9d363f7b30ebcf2c87
|
data/LICENSE.txt
CHANGED
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,7 +26,7 @@
|
|
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
|
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
32
|
By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** and **halt** will not remove them.
|
@@ -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:
|
@@ -56,22 +93,77 @@ Example:
|
|
56
93
|
|
57
94
|
To keep your /etc/hosts file unchanged simply add the line below to your `VagrantFile`:
|
58
95
|
|
59
|
-
config.hostsupdater.
|
96
|
+
config.hostsupdater.remove_on_suspend = false
|
60
97
|
|
61
98
|
This disables vagrant-hostsupdater from running on **suspend** and **halt**.
|
62
99
|
|
63
100
|
|
64
|
-
##
|
101
|
+
## Suppressing prompts for elevating privileges
|
65
102
|
|
66
|
-
|
67
|
-
|
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.
|
105
|
+
|
106
|
+
### Linux/OS X: Passwordless sudo
|
107
|
+
|
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:
|
68
118
|
|
69
119
|
# Allow passwordless startup of Vagrant with vagrant-hostsupdater.
|
70
120
|
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
|
71
121
|
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
|
72
122
|
%admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
|
73
123
|
|
74
|
-
|
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.
|
75
167
|
|
76
168
|
## Installing development version
|
77
169
|
|
@@ -96,6 +188,22 @@ vagrant plugin install vagrant-hostsupdater-*.gem
|
|
96
188
|
|
97
189
|
## Versions
|
98
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
|
+
|
99
207
|
### 1.0.2
|
100
208
|
* Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71)
|
101
209
|
* Feature: Skip entries if they already exist [#69](/../../issues/69)
|
@@ -143,4 +251,4 @@ vagrant plugin install vagrant-hostsupdater-*.gem
|
|
143
251
|
* fixed problem with removing hosts entries on destroy command (Thanks to Andy Bohne)
|
144
252
|
|
145
253
|
### 0.0.3
|
146
|
-
* added aliases config option to define additional hostnames
|
254
|
+
* added aliases config option to define additional hostnames
|
@@ -0,0 +1,44 @@
|
|
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
|
+
# Check whether the plugin has been executed for a particular
|
25
|
+
# VM as it may happen that a single Vagrantfile defines multiple
|
26
|
+
# machines and having a static flag will result in a plugin being
|
27
|
+
# executed just once.
|
28
|
+
# https://github.com/agiledivider/vagrant-hostsupdater/issues/198
|
29
|
+
if @machine.id and not @@completed.key?("#{self.class.name}-#{@machine.name}")
|
30
|
+
run(env)
|
31
|
+
@@completed["#{self.class.name}-#{@machine.name}"] = true
|
32
|
+
end
|
33
|
+
|
34
|
+
@app.call(env)
|
35
|
+
end
|
36
|
+
|
37
|
+
def run(env)
|
38
|
+
raise NotImplementedError.new("Must be implemented!")
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
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
|
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,28 +1,21 @@
|
|
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
|
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
|
-
if
|
16
|
-
if
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
@ui.info "[vagrant-hostsupdater] Removing hosts on suspend disabled"
|
22
|
-
end
|
8
|
+
if [:suspend, :halt].include? machine_action
|
9
|
+
if @machine.config.hostsupdater.remove_on_suspend == false
|
10
|
+
@ui.info "[vagrant-hostsupdater] Not removing hosts (remove_on_suspend false)"
|
11
|
+
else
|
12
|
+
@ui.info "[vagrant-hostsupdater] Removing hosts on suspend"
|
13
|
+
removeHostEntries
|
23
14
|
end
|
15
|
+
else
|
16
|
+
@ui.info "[vagrant-hostsupdater] Removing hosts"
|
17
|
+
removeHostEntries
|
24
18
|
end
|
25
|
-
@app.call(env)
|
26
19
|
end
|
27
20
|
|
28
21
|
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)
|
6
|
+
def run(env)
|
16
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,38 +1,89 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
1
3
|
module VagrantPlugins
|
2
4
|
module HostsUpdater
|
3
5
|
module HostsUpdater
|
4
|
-
|
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
|
-
|
9
|
-
|
10
|
-
ip
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
32
|
+
if @machine.provider_name == :lxc
|
33
|
+
ip = @machine.provider.capability(:public_address)
|
34
|
+
ips.push(ip)
|
35
|
+
elsif @machine.provider_name == :docker
|
36
|
+
ip = @machine.provider.capability(:public_address)
|
37
|
+
ips.push(ip)
|
38
|
+
elsif @machine.provider_name == :libvirt
|
39
|
+
ssh_info = @machine.ssh_info
|
40
|
+
if ssh_info
|
41
|
+
ips.push(ssh_info[:host])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
if not ips.any?
|
45
|
+
ips.push( '127.0.0.1' )
|
46
|
+
end
|
47
|
+
return ips.uniq
|
48
|
+
end
|
49
|
+
|
50
|
+
# Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
|
51
|
+
def getHostnames(ips)
|
52
|
+
hostnames = Hash.new { |h, k| h[k] = [] }
|
53
|
+
|
54
|
+
case @machine.config.hostsupdater.aliases
|
55
|
+
when Array
|
56
|
+
# simple list of aliases to link to all ips
|
57
|
+
ips.each do |ip|
|
58
|
+
hostnames[ip] += @machine.config.hostsupdater.aliases
|
59
|
+
end
|
60
|
+
when Hash
|
61
|
+
# complex definition of aliases for various ips
|
62
|
+
@machine.config.hostsupdater.aliases.each do |ip, hosts|
|
63
|
+
hostnames[ip] += Array(hosts)
|
64
|
+
end
|
23
65
|
end
|
66
|
+
|
67
|
+
# handle default hostname(s) if not already specified in the aliases
|
68
|
+
Array(@machine.config.vm.hostname).each do |host|
|
69
|
+
if hostnames.none? { |k, v| v.include?(host) }
|
70
|
+
ips.each do |ip|
|
71
|
+
hostnames[ip].unshift host
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
24
76
|
return hostnames
|
25
77
|
end
|
26
78
|
|
27
|
-
def addHostEntries
|
79
|
+
def addHostEntries
|
28
80
|
ips = getIps
|
29
|
-
hostnames = getHostnames
|
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|
|
86
|
+
getHostnames(ips).each do |ip, hostnames|
|
36
87
|
hostnames.each do |hostname|
|
37
88
|
entryPattern = hostEntryPattern(ip, hostname)
|
38
89
|
|
@@ -61,7 +112,8 @@ module VagrantPlugins
|
|
61
112
|
uuid = @machine.id || @machine.config.hostsupdater.id
|
62
113
|
hashedId = Digest::MD5.hexdigest(uuid)
|
63
114
|
if hostsContents.match(/#{hashedId}/)
|
64
|
-
|
115
|
+
removeFromHosts
|
116
|
+
removeFromSshKnownHosts
|
65
117
|
end
|
66
118
|
end
|
67
119
|
|
@@ -70,7 +122,7 @@ module VagrantPlugins
|
|
70
122
|
end
|
71
123
|
|
72
124
|
def createHostEntry(ip, hostname, name, uuid = self.uuid)
|
73
|
-
%Q(#{ip} #{hostname} #{signature(name, uuid)})
|
125
|
+
%Q(#{ip} #{hostname} #{signature(name, uuid.to_s)})
|
74
126
|
end
|
75
127
|
|
76
128
|
# Create a regular expression that will match *any* entry describing the
|
@@ -86,15 +138,24 @@ module VagrantPlugins
|
|
86
138
|
|
87
139
|
@ui.info "[vagrant-hostsupdater] Writing the following entries to (#@@hosts_path)"
|
88
140
|
@ui.info "[vagrant-hostsupdater] " + entries.join("\n[vagrant-hostsupdater] ")
|
89
|
-
@ui.info "[vagrant-hostsupdater] This operation requires administrative access. You may " +
|
90
|
-
"skip it by manually adding equivalent entries to the hosts file."
|
91
141
|
if !File.writable_real?(@@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."
|
92
144
|
if !sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
|
93
145
|
@ui.error "[vagrant-hostsupdater] Failed to add hosts, could not use sudo"
|
94
146
|
adviseOnSudo
|
95
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)
|
96
157
|
else
|
97
|
-
content = "\n" + content
|
158
|
+
content = "\n" + content + "\n"
|
98
159
|
hostsFile = File.open(@@hosts_path, "a")
|
99
160
|
hostsFile.write(content)
|
100
161
|
hostsFile.close()
|
@@ -104,7 +165,7 @@ module VagrantPlugins
|
|
104
165
|
def removeFromHosts(options = {})
|
105
166
|
uuid = @machine.id || @machine.config.hostsupdater.id
|
106
167
|
hashedId = Digest::MD5.hexdigest(uuid)
|
107
|
-
if !File.writable_real?(@@hosts_path)
|
168
|
+
if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows?
|
108
169
|
if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
|
109
170
|
@ui.error "[vagrant-hostsupdater] Failed to remove hosts, could not use sudo"
|
110
171
|
adviseOnSudo
|
@@ -114,13 +175,27 @@ module VagrantPlugins
|
|
114
175
|
File.open(@@hosts_path).each do |line|
|
115
176
|
hosts << line unless line.include?(hashedId)
|
116
177
|
end
|
178
|
+
hosts.strip!
|
117
179
|
hostsFile = File.open(@@hosts_path, "w")
|
118
180
|
hostsFile.write(hosts)
|
119
181
|
hostsFile.close()
|
120
182
|
end
|
121
183
|
end
|
122
184
|
|
123
|
-
|
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
|
124
199
|
|
125
200
|
def signature(name, uuid = self.uuid)
|
126
201
|
hashedId = Digest::MD5.hexdigest(uuid)
|
@@ -130,7 +205,11 @@ module VagrantPlugins
|
|
130
205
|
def sudo(command)
|
131
206
|
return if !command
|
132
207
|
if Vagrant::Util::Platform.windows?
|
133
|
-
|
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)
|
134
213
|
else
|
135
214
|
return system("sudo #{command}")
|
136
215
|
end
|
@@ -138,7 +217,38 @@ module VagrantPlugins
|
|
138
217
|
|
139
218
|
def adviseOnSudo
|
140
219
|
@ui.error "[vagrant-hostsupdater] Consider adding the following to your sudoers file:"
|
141
|
-
@ui.error "[vagrant-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#
|
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
|
238
|
+
end
|
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
|
142
252
|
end
|
143
253
|
end
|
144
254
|
end
|
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
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
|
@@ -34,9 +36,6 @@ module VagrantPlugins
|
|
34
36
|
|
35
37
|
action_hook(:hostsupdater, :machine_action_destroy) do |hook|
|
36
38
|
hook.prepend(Action::CacheHosts)
|
37
|
-
end
|
38
|
-
|
39
|
-
action_hook(:hostsupdater, :machine_action_destroy) do |hook|
|
40
39
|
hook.append(Action::RemoveHosts)
|
41
40
|
end
|
42
41
|
|
@@ -5,7 +5,7 @@ require 'vagrant-hostsupdater/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'vagrant-hostsupdater'
|
8
|
-
spec.version =
|
8
|
+
spec.version = VagrantPlugins::HostsUpdater::VERSION
|
9
9
|
spec.authors = ['Falk Kühnel', 'Chris Smith']
|
10
10
|
spec.email = ['fk@cogitatio.de', 'chris@cgsmith.net']
|
11
11
|
spec.description = %q{Enables Vagrant to update hosts file on the host machine}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-hostsupdater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Falk Kühnel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-04-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -53,6 +53,7 @@ files:
|
|
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
|
@@ -81,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
82
|
- !ruby/object:Gem::Version
|
82
83
|
version: '0'
|
83
84
|
requirements: []
|
84
|
-
|
85
|
-
rubygems_version: 2.7.6
|
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
|