vagrant-virtual-hostsupdater 1.1.2
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 +7 -0
- data/.gitignore +18 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +226 -0
- data/Rakefile +3 -0
- data/lib/vagrant-virtual-hostsupdater.rb +11 -0
- data/lib/vagrant-virtual-hostsupdater/Action/CacheHosts.rb +20 -0
- data/lib/vagrant-virtual-hostsupdater/Action/RemoveHosts.rb +31 -0
- data/lib/vagrant-virtual-hostsupdater/Action/UpdateHosts.rb +24 -0
- data/lib/vagrant-virtual-hostsupdater/VirtualHostsUpdater.rb +178 -0
- data/lib/vagrant-virtual-hostsupdater/command.rb +30 -0
- data/lib/vagrant-virtual-hostsupdater/config.rb +11 -0
- data/lib/vagrant-virtual-hostsupdater/plugin.rb +59 -0
- data/lib/vagrant-virtual-hostsupdater/version.rb +5 -0
- data/vagrant-virtual-hostsupdater.gemspec +23 -0
- metadata +90 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6aca82124501a1c94a59ccee3849e3c43bfa5049
|
4
|
+
data.tar.gz: 8bf2e43df77ad8a3951e95ce848267aaae4f1993
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6224d8d17cbdcc211aee1848357e8b12506946202ebdc92998bde5ec0ae5ee86923ebdc8de175727796255ad88e6de8973a2f8b36443d9aa7f9d54e8aba113cf
|
7
|
+
data.tar.gz: 16796cfb0cdd4870028ff230ef783a144ebc06fd398ca0b27a873c7a6146251fa0bbc6195d2c06ebd186c7bf2ccafd3592aae4e03aa5e1844c8ef1a1b94c47af
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright 2018 Chris Smith
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
# Vagrant::Hostsupdater
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/vagrant-hostsupdater)
|
4
|
+
[](https://rubygems.org/gems/vagrant-hostsupdater)
|
5
|
+
[](https://rubygems.org/gems/vagrant-hostsupdater)
|
6
|
+
|
7
|
+
[](https://gitter.im/cogitatio/vagrant-hostsupdater?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
8
|
+
[](https://twitter.com/intent/tweet?text=Checkout%20this%20awesome%20Vagrant%20plugin!&url=https%3A%2F%2Fgithub.com%2Fcogiatio%2Fvagrant-hostsupdater&hashtags=hostsupdater,vagrant)
|
9
|
+
|
10
|
+
|
11
|
+
This plugin adds an entry to your /etc/hosts file on the host system.
|
12
|
+
|
13
|
+
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
|
+
|
15
|
+
On **halt**, **destroy**, and **suspend**, those entries will be removed again.
|
16
|
+
By setting the `config.hostsupdater.remove_on_suspend = false`, **suspend** and **halt** will not remove them.
|
17
|
+
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
$ vagrant plugin install vagrant-hostsupdater
|
22
|
+
|
23
|
+
Uninstall it with:
|
24
|
+
|
25
|
+
$ vagrant plugin uninstall vagrant-hostsupdater
|
26
|
+
|
27
|
+
Update the plugin with:
|
28
|
+
|
29
|
+
$ vagrant plugin update vagrant-hostsupdater
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
You currently only need the `hostname` and a `:private_network` network with a fixed IP address.
|
34
|
+
|
35
|
+
config.vm.network :private_network, ip: "192.168.3.10"
|
36
|
+
config.vm.hostname = "www.testing.de"
|
37
|
+
config.hostsupdater.aliases = ["alias.testing.de", "alias2.somedomain.com"]
|
38
|
+
|
39
|
+
This IP address and the hostname will be used for the entry in the `/etc/hosts` file.
|
40
|
+
|
41
|
+
### Multiple private network adapters
|
42
|
+
|
43
|
+
If you have multiple network adapters i.e.:
|
44
|
+
|
45
|
+
config.vm.network :private_network, ip: "10.0.0.1"
|
46
|
+
config.vm.network :private_network, ip: "10.0.0.2"
|
47
|
+
|
48
|
+
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.:
|
49
|
+
|
50
|
+
config.hostsupdater.aliases = {
|
51
|
+
'10.0.0.1' => ['foo.com', 'bar.com'],
|
52
|
+
'10.0.0.2' => ['baz.com', 'bat.com']
|
53
|
+
}
|
54
|
+
|
55
|
+
This will produce `/etc/hosts` entries like so:
|
56
|
+
|
57
|
+
10.0.0.1 foo.com
|
58
|
+
10.0.0.1 bar.com
|
59
|
+
10.0.0.2 baz.com
|
60
|
+
10.0.0.2 bat.com
|
61
|
+
|
62
|
+
### Skipping hostupdater
|
63
|
+
|
64
|
+
To skip adding some entries to the /etc/hosts file add `hostsupdater: "skip"` option to network configuration:
|
65
|
+
|
66
|
+
config.vm.network "private_network", ip: "172.21.9.9", hostsupdater: "skip"
|
67
|
+
|
68
|
+
Example:
|
69
|
+
|
70
|
+
config.vm.network :private_network, ip: "192.168.50.4"
|
71
|
+
config.vm.network :private_network,
|
72
|
+
ip: "172.21.9.9",
|
73
|
+
netmask: "255.255.240.0",
|
74
|
+
hostsupdater: "skip"
|
75
|
+
|
76
|
+
### Keeping Host Entries After Suspend/Halt
|
77
|
+
|
78
|
+
To keep your /etc/hosts file unchanged simply add the line below to your `VagrantFile`:
|
79
|
+
|
80
|
+
config.hostsupdater.remove_on_suspend = false
|
81
|
+
|
82
|
+
This disables vagrant-hostsupdater from running on **suspend** and **halt**.
|
83
|
+
|
84
|
+
|
85
|
+
## Suppressing prompts for elevating privileges
|
86
|
+
|
87
|
+
These prompts exist to prevent anything that is being run by the user from inadvertently updating the hosts file.
|
88
|
+
If you understand the risks that go with supressing them, here's how to do it.
|
89
|
+
|
90
|
+
### Linux/OS X: Passwordless sudo
|
91
|
+
|
92
|
+
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`.
|
93
|
+
|
94
|
+
For Ubuntu and most Linux environments:
|
95
|
+
|
96
|
+
# Allow passwordless startup of Vagrant with vagrant-hostsupdater.
|
97
|
+
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
|
98
|
+
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /bin/sed -i -e /*/ d /etc/hosts
|
99
|
+
%sudo ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
|
100
|
+
|
101
|
+
For MacOS:
|
102
|
+
|
103
|
+
# Allow passwordless startup of Vagrant with vagrant-hostsupdater.
|
104
|
+
Cmnd_Alias VAGRANT_HOSTS_ADD = /bin/sh -c echo "*" >> /etc/hosts
|
105
|
+
Cmnd_Alias VAGRANT_HOSTS_REMOVE = /usr/bin/sed -i -e /*/ d /etc/hosts
|
106
|
+
%admin ALL=(root) NOPASSWD: VAGRANT_HOSTS_ADD, VAGRANT_HOSTS_REMOVE
|
107
|
+
|
108
|
+
- 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.
|
109
|
+
- If vagrant still asks for a password on commands that trigger the `VAGRANT_HOSTS_REMOVE` alias above (like
|
110
|
+
**halt** or **suspend**), this might indicate that the location of **sed** in the `VAGRANT_HOSTS_REMOVE` alias is
|
111
|
+
pointing to the wrong location. The solution is to find the location of **sed** (ex. `which sed`) and
|
112
|
+
replace that location in the `VAGRANT_HOSTS_REMOVE` alias.
|
113
|
+
|
114
|
+
### Windows: UAC Prompt
|
115
|
+
|
116
|
+
You can use `cacls` or `icacls` to grant your user account permanent write permission to the system's hosts file.
|
117
|
+
You have to open an elevated command prompt; hold `❖ Win` and press `X`, then choose "Command Prompt (Admin)"
|
118
|
+
|
119
|
+
cacls %SYSTEMROOT%\system32\drivers\etc\hosts /E /G %USERNAME%:W
|
120
|
+
|
121
|
+
## Using AWS as a Provider
|
122
|
+
|
123
|
+
If you'd like AWS as a provider using [vagrant-aws](https://github.com/mitchellh/vagrant-aws) or other plugin,
|
124
|
+
this plugin will detect the instance public IP by the tag infomations.
|
125
|
+
For example, [vagrant-aws](https://github.com/mitchellh/vagrant-aws) configures a tag infomations like the following.
|
126
|
+
|
127
|
+
config.vm.provider :aws do |aws, override|
|
128
|
+
aws.tags = {
|
129
|
+
"Name" => "vagrant",
|
130
|
+
...
|
131
|
+
}
|
132
|
+
aws.elastic_ip = true
|
133
|
+
...
|
134
|
+
end
|
135
|
+
|
136
|
+
* [AWS CLI](https://aws.amazon.com/cli/) is required
|
137
|
+
* The tag informations be unique for the instance
|
138
|
+
* Enable Elastic IP for the instance
|
139
|
+
|
140
|
+
## Installing development version
|
141
|
+
|
142
|
+
If you would like to install vagrant-hostsupdater on the development version perform the following:
|
143
|
+
|
144
|
+
```
|
145
|
+
git clone https://github.com/cogitatio/vagrant-hostsupdater
|
146
|
+
cd vagrant-hostsupdater
|
147
|
+
git checkout develop
|
148
|
+
gem build vagrant-hostsupdater.gemspec
|
149
|
+
vagrant plugin install vagrant-hostsupdater-*.gem
|
150
|
+
```
|
151
|
+
|
152
|
+
## Contributing
|
153
|
+
|
154
|
+
1. Fork it
|
155
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
156
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
157
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
158
|
+
5. Create new Pull Request on the `develop` branch
|
159
|
+
|
160
|
+
|
161
|
+
## Versions
|
162
|
+
|
163
|
+
### 1.1.2
|
164
|
+
TODO: gseng
|
165
|
+
|
166
|
+
### 1.1.1
|
167
|
+
* Bugfix: AWS Feature broke part of the code [#155](/../../issues/155)
|
168
|
+
|
169
|
+
### 1.1.0
|
170
|
+
* Feature: Added AWS support [#74](/../../pull/74)
|
171
|
+
* Feature: Added libvirt provider [#122](/../../pull/122)
|
172
|
+
* Feature: Add support for multiple private network adapters [#96](/../../pull/96)
|
173
|
+
* Feature: Add support for VMs without private/public networking [#23](/../../issues/23)
|
174
|
+
* Feature: Add Docker support [#149](/../../pull/149)
|
175
|
+
* Bugfix: Windows users get UAC prompt [#40](/../../issues/40)
|
176
|
+
* Bugfix: Documentation update and type fix
|
177
|
+
* Misc: Added a note about suppressing UAC prompts
|
178
|
+
|
179
|
+
### 1.0.2
|
180
|
+
* Feature: Added `remove_on_suspend` for `vagrant_halt` [#71](/../../issues/71)
|
181
|
+
* Feature: Skip entries if they already exist [#69](/../../issues/69)
|
182
|
+
* Bugfix: Fixing extra lines in /etc/hosts file [#87](/../../pull/87)
|
183
|
+
* Misc: Fix yellow text on UI [#39](/../../issues/39)
|
184
|
+
|
185
|
+
### 1.0.1
|
186
|
+
* Bugfix: Fixing `up` issue on initialize [#28](/../../issues/28)
|
187
|
+
|
188
|
+
### 1.0.0
|
189
|
+
* Stable release
|
190
|
+
* Feature: Added `skip` flag [#69](/../../issues/69)
|
191
|
+
* Feature: Hosts update on provision action [#65](/../../issues/65)
|
192
|
+
* Bugfix: `remove_on_suspend` should be true [#19](/../../issues/19)
|
193
|
+
* Bugfix: Line break not inserted before first host [#37](/../../issues/37)
|
194
|
+
* Bugfix: Old changes not removed in linux [#67](/../../issues/67)
|
195
|
+
* Bugfix: Writable issue on OSX [#47](/../../issues/47)
|
196
|
+
* Bugfix: Update hosts before provisioning [#31](/../../issues/31)
|
197
|
+
* Misc: Using Semantic Versioning for version number
|
198
|
+
* Misc: Added note regarding sudoers file
|
199
|
+
|
200
|
+
### 0.0.11
|
201
|
+
* bugfix: Fix additional new lines being added to hosts file (Thanks to vincentmac)
|
202
|
+
|
203
|
+
### 0.0.10
|
204
|
+
* bugfix: wrong path on Windows systems (Thanks to Im0rtality)
|
205
|
+
|
206
|
+
### 0.0.9
|
207
|
+
* bugfix: now not trying to remove anything if no machine id is given
|
208
|
+
|
209
|
+
### 0.0.8
|
210
|
+
* trying to use proper windows hosts file
|
211
|
+
|
212
|
+
### 0.0.7
|
213
|
+
* using hashed uids now to identify hosts entries (you might need to remove previous hostentries manually)
|
214
|
+
* fixed removing of host entries
|
215
|
+
|
216
|
+
### 0.0.6
|
217
|
+
* no sudo, if /etc/hosts is writeable
|
218
|
+
|
219
|
+
### 0.0.5
|
220
|
+
* option added to not remove hosts on suspend, adding hosts on resume (Thanks to Svelix)
|
221
|
+
|
222
|
+
### 0.0.4
|
223
|
+
* fixed problem with removing hosts entries on destroy command (Thanks to Andy Bohne)
|
224
|
+
|
225
|
+
### 0.0.3
|
226
|
+
* added aliases config option to define additional hostnames
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require "vagrant-virtual-hostsupdater/version"
|
2
|
+
require "vagrant-virtual-hostsupdater/plugin"
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module VirtualHostsUpdater
|
6
|
+
def self.source_root
|
7
|
+
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module VirtualHostsUpdater
|
3
|
+
module Action
|
4
|
+
class CacheHosts
|
5
|
+
include VirtualHostsUpdater
|
6
|
+
|
7
|
+
def initialize(app, env)
|
8
|
+
@app = app
|
9
|
+
@machine = env[:machine]
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
cacheHostEntries
|
14
|
+
@app.call(env)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module VirtualHostsUpdater
|
3
|
+
module Action
|
4
|
+
class RemoveHosts
|
5
|
+
include VirtualHostsUpdater
|
6
|
+
|
7
|
+
def initialize(app, env)
|
8
|
+
@app = app
|
9
|
+
@machine = env[:machine]
|
10
|
+
@ui = env[:ui]
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
machine_action = env[:machine_action]
|
15
|
+
if machine_action != :destroy || !@machine.id
|
16
|
+
if machine_action != :suspend || false != @machine.config.virtualhostsupdater.remove_on_suspend
|
17
|
+
if machine_action != :halt || false != @machine.config.virtualhostsupdater.remove_on_suspend
|
18
|
+
@ui.info "[vagrant-virtual-hostsupdater] Removing hosts"
|
19
|
+
removeHostEntries
|
20
|
+
else
|
21
|
+
@ui.info "[vagrant-virtual-hostsupdater] Removing hosts on suspend disabled"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative "../VirtualHostsUpdater"
|
2
|
+
module VagrantPlugins
|
3
|
+
module VirtualHostsUpdater
|
4
|
+
module Action
|
5
|
+
class UpdateHosts
|
6
|
+
include VirtualHostsUpdater
|
7
|
+
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@machine = env[:machine]
|
12
|
+
@ui = env[:ui]
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
@ui.info "[vagrant-virtual-hostsupdater] Checking for host entries"
|
17
|
+
addHostEntries()
|
18
|
+
@app.call(env)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,178 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module VirtualHostsUpdater
|
5
|
+
module VirtualHostsUpdater
|
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
|
+
|
13
|
+
# Get a hash of hostnames indexed by ip, e.g. { 'ip1': ['host1'], 'ip2': ['host2', 'host3'] }
|
14
|
+
def getHostnames()
|
15
|
+
@ui.info '[vagrant-virtual-hostsupdater] retrieving the hostnames'
|
16
|
+
hostnames = Hash.new { |h, k| h[k] = [] }
|
17
|
+
|
18
|
+
case @machine.config.virtualhostsupdater.aliases
|
19
|
+
when Hash
|
20
|
+
# complex definition of aliases for various ips
|
21
|
+
@machine.config.virtualhostsupdater.aliases.each do |ip, hosts|
|
22
|
+
hostnames[ip] += Array(hosts)
|
23
|
+
# else
|
24
|
+
# @ui.error "[vagrant-virtual-hostsupdater] this version only supports the hash format!"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
return hostnames
|
29
|
+
end
|
30
|
+
|
31
|
+
def addHostEntries
|
32
|
+
hostnames = getHostnames()
|
33
|
+
file = File.open(@@hosts_path, "rb")
|
34
|
+
hostsContents = file.read
|
35
|
+
uuid = @machine.id
|
36
|
+
name = @machine.name
|
37
|
+
entries = []
|
38
|
+
hostnames.each do |ip, hosts|
|
39
|
+
hosts.each do |hostname|
|
40
|
+
entryPattern = hostEntryPattern(ip, hostname)
|
41
|
+
|
42
|
+
if hostsContents.match(/#{entryPattern}/)
|
43
|
+
@ui.info "[vagrant-virtual-hostsupdater] found entry for: #{ip} #{hostname}"
|
44
|
+
else
|
45
|
+
hostEntry = createHostEntry(ip, hostname, name, uuid)
|
46
|
+
entries.push(hostEntry)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
addToHosts(entries)
|
51
|
+
end
|
52
|
+
|
53
|
+
def cacheHostEntries
|
54
|
+
@machine.config.virtualhostsupdater.id = @machine.id
|
55
|
+
end
|
56
|
+
|
57
|
+
def removeHostEntries
|
58
|
+
if !@machine.id and !@machine.config.virtualhostsupdater.id
|
59
|
+
@ui.info "[vagrant-virtual-hostsupdater] No machine id, nothing removed from #@@hosts_path"
|
60
|
+
return
|
61
|
+
end
|
62
|
+
file = File.open(@@hosts_path, "rb")
|
63
|
+
hostsContents = file.read
|
64
|
+
uuid = @machine.id || @machine.config.virtualhostsupdater.id
|
65
|
+
hashedId = Digest::MD5.hexdigest(uuid)
|
66
|
+
if hostsContents.match(/#{hashedId}/)
|
67
|
+
removeFromHosts
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def host_entry(ip, hostnames, name, uuid = self.uuid)
|
72
|
+
%Q(#{ip} #{hostnames.join(' ')} #{signature(name, uuid)})
|
73
|
+
end
|
74
|
+
|
75
|
+
def createHostEntry(ip, hostname, name, uuid = self.uuid)
|
76
|
+
%Q(#{ip} #{hostname} #{signature(name, uuid)})
|
77
|
+
end
|
78
|
+
|
79
|
+
# Create a regular expression that will match *any* entry describing the
|
80
|
+
# given IP/hostname pair. This is intentionally generic in order to
|
81
|
+
# recognize entries created by the end user.
|
82
|
+
def hostEntryPattern(ip, hostname)
|
83
|
+
Regexp.new('^\s*' + ip + '\s+' + hostname + '\s*(#.*)?$')
|
84
|
+
end
|
85
|
+
|
86
|
+
def addToHosts(entries)
|
87
|
+
return if entries.length == 0
|
88
|
+
content = entries.join("\n").strip
|
89
|
+
|
90
|
+
@ui.info "[vagrant-virtual-hostsupdater] Writing the following entries to (#@@hosts_path)"
|
91
|
+
@ui.info "[vagrant-virtual-hostsupdater] " + entries.join("\n[vagrant-virtual-hostsupdater] ")
|
92
|
+
if !File.writable_real?(@@hosts_path)
|
93
|
+
@ui.info "[vagrant-virtual-hostsupdater] This operation requires administrative access. You may " +
|
94
|
+
"skip it by manually adding equivalent entries to the hosts file."
|
95
|
+
if !sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
|
96
|
+
@ui.error "[vagrant-virtual-hostsupdater] Failed to add hosts, could not use sudo"
|
97
|
+
adviseOnSudo
|
98
|
+
end
|
99
|
+
elsif Vagrant::Util::Platform.windows?
|
100
|
+
require 'tmpdir'
|
101
|
+
uuid = @machine.id || @machine.config.virtualhostsupdater.id
|
102
|
+
tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid + '.cmd')
|
103
|
+
File.open(tmpPath, "w") do |tmpFile|
|
104
|
+
entries.each { |line| tmpFile.puts(">>\"#{@@hosts_path}\" echo #{line}") }
|
105
|
+
end
|
106
|
+
sudo(tmpPath)
|
107
|
+
File.delete(tmpPath)
|
108
|
+
else
|
109
|
+
content = "\n" + content + "\n"
|
110
|
+
hostsFile = File.open(@@hosts_path, "a")
|
111
|
+
hostsFile.write(content)
|
112
|
+
hostsFile.close()
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def removeFromHosts(options = {})
|
117
|
+
uuid = @machine.id || @machine.config.virtualhostsupdater.id
|
118
|
+
hashedId = Digest::MD5.hexdigest(uuid)
|
119
|
+
if !File.writable_real?(@@hosts_path) || Vagrant::Util::Platform.windows?
|
120
|
+
if !sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
|
121
|
+
@ui.error "[vagrant-virtual-hostsupdater] Failed to remove hosts, could not use sudo"
|
122
|
+
adviseOnSudo
|
123
|
+
end
|
124
|
+
else
|
125
|
+
hosts = ""
|
126
|
+
File.open(@@hosts_path).each do |line|
|
127
|
+
hosts << line unless line.include?(hashedId)
|
128
|
+
end
|
129
|
+
hosts.strip!
|
130
|
+
hostsFile = File.open(@@hosts_path, "w")
|
131
|
+
hostsFile.write(hosts)
|
132
|
+
hostsFile.close()
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def signature(name, uuid = self.uuid)
|
137
|
+
hashedId = Digest::MD5.hexdigest(uuid)
|
138
|
+
%Q(# VAGRANT: #{hashedId} (#{name}) / #{uuid})
|
139
|
+
end
|
140
|
+
|
141
|
+
def sudo(command)
|
142
|
+
return if !command
|
143
|
+
if Vagrant::Util::Platform.windows?
|
144
|
+
require 'win32ole'
|
145
|
+
args = command.split(" ")
|
146
|
+
command = args.shift
|
147
|
+
sh = WIN32OLE.new('Shell.Application')
|
148
|
+
sh.ShellExecute(command, args.join(" "), '', 'runas', 0)
|
149
|
+
else
|
150
|
+
return system("sudo #{command}")
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def adviseOnSudo
|
155
|
+
@ui.error "[vagrant-virtual-hostsupdater] Consider adding the following to your sudoers file:"
|
156
|
+
@ui.error "[vagrant-virtual-hostsupdater] https://github.com/cogitatio/vagrant-hostsupdater#suppressing-prompts-for-elevating-privileges"
|
157
|
+
end
|
158
|
+
|
159
|
+
def getAwsPublicIp
|
160
|
+
return nil if ! Vagrant.has_plugin?("vagrant-aws")
|
161
|
+
aws_conf = @machine.config.vm.get_provider_config(:aws)
|
162
|
+
return nil if ! aws_conf.is_a?(VagrantPlugins::AWS::Config)
|
163
|
+
filters = ( aws_conf.tags || [] ).map {|k,v| sprintf('"Name=tag:%s,Values=%s"', k, v) }.join(' ')
|
164
|
+
return nil if filters == ''
|
165
|
+
cmd = 'aws ec2 describe-instances --filter '+filters
|
166
|
+
stdout, stderr, stat = Open3.capture3(cmd)
|
167
|
+
@ui.error sprintf("Failed to execute '%s' : %s", cmd, stderr) if stderr != ''
|
168
|
+
return nil if stat.exitstatus != 0
|
169
|
+
begin
|
170
|
+
return JSON.parse(stdout)["Reservations"].first()["Instances"].first()["PublicIpAddress"]
|
171
|
+
rescue => e
|
172
|
+
@ui.error sprintf("Failed to get IP from the result of '%s' : %s", cmd, e.message)
|
173
|
+
return nil
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module VirtualHostsUpdater
|
3
|
+
class Command < Vagrant.plugin('2', :command)
|
4
|
+
#include HostsFile
|
5
|
+
|
6
|
+
def execute
|
7
|
+
# options = {}
|
8
|
+
# opts = OptionParser.new do |o|
|
9
|
+
# o.banner = 'Usage: vagrant hostmanager [vm-name]'
|
10
|
+
# o.separator ''
|
11
|
+
|
12
|
+
# o.on('--provider provider', String,
|
13
|
+
# 'Update machines with the specific provider.') do |provider|
|
14
|
+
# options[:provider] = provider
|
15
|
+
# end
|
16
|
+
# end
|
17
|
+
|
18
|
+
# argv = parse_options(opts)
|
19
|
+
# options[:provider] ||= @env.default_provider
|
20
|
+
|
21
|
+
# generate(@env, options[:provider].to_sym)
|
22
|
+
|
23
|
+
# with_target_vms(argv, options) do |machine|
|
24
|
+
# update(machine)
|
25
|
+
# end
|
26
|
+
puts "ran command"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "vagrant-virtual-hostsupdater/Action/UpdateHosts"
|
2
|
+
require "vagrant-virtual-hostsupdater/Action/CacheHosts"
|
3
|
+
require "vagrant-virtual-hostsupdater/Action/RemoveHosts"
|
4
|
+
|
5
|
+
module VagrantPlugins
|
6
|
+
module VirtualHostsUpdater
|
7
|
+
class Plugin < Vagrant.plugin('2')
|
8
|
+
name 'VirtualHostsUpdater'
|
9
|
+
description <<-DESC
|
10
|
+
This plugin manages the /etc/hosts file for the host machine. An entry is
|
11
|
+
created for the hostname attribute in the vm.config.
|
12
|
+
DESC
|
13
|
+
|
14
|
+
config(:virtualhostsupdater) do
|
15
|
+
require_relative 'config'
|
16
|
+
Config
|
17
|
+
end
|
18
|
+
|
19
|
+
action_hook(:virtualhostsupdater, :machine_action_up) do |hook|
|
20
|
+
hook.append(Action::UpdateHosts)
|
21
|
+
end
|
22
|
+
|
23
|
+
action_hook(:virtualhostsupdater, :machine_action_provision) do |hook|
|
24
|
+
hook.before(Vagrant::Action::Builtin::Provision, Action::UpdateHosts)
|
25
|
+
end
|
26
|
+
|
27
|
+
action_hook(:virtualhostsupdater, :machine_action_halt) do |hook|
|
28
|
+
hook.append(Action::RemoveHosts)
|
29
|
+
end
|
30
|
+
|
31
|
+
action_hook(:virtualhostsupdater, :machine_action_suspend) do |hook|
|
32
|
+
hook.append(Action::RemoveHosts)
|
33
|
+
end
|
34
|
+
|
35
|
+
action_hook(:virtualhostsupdater, :machine_action_destroy) do |hook|
|
36
|
+
hook.prepend(Action::CacheHosts)
|
37
|
+
end
|
38
|
+
|
39
|
+
action_hook(:virtualhostsupdater, :machine_action_destroy) do |hook|
|
40
|
+
hook.append(Action::RemoveHosts)
|
41
|
+
end
|
42
|
+
|
43
|
+
action_hook(:virtualhostsupdater, :machine_action_reload) do |hook|
|
44
|
+
hook.prepend(Action::RemoveHosts)
|
45
|
+
hook.append(Action::UpdateHosts)
|
46
|
+
end
|
47
|
+
|
48
|
+
action_hook(:virtualhostsupdater, :machine_action_resume) do |hook|
|
49
|
+
hook.prepend(Action::RemoveHosts)
|
50
|
+
hook.append(Action::UpdateHosts)
|
51
|
+
end
|
52
|
+
|
53
|
+
command(:virtualhostsupdater) do
|
54
|
+
require_relative 'command'
|
55
|
+
Command
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vagrant-virtual-hostsupdater/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'vagrant-virtual-hostsupdater'
|
8
|
+
spec.version = VagrantPlugins::VirtualHostsUpdater::VERSION
|
9
|
+
spec.authors = ['Falk Kühnel', 'Chris Smith', 'Gökhan Şengün']
|
10
|
+
spec.email = ['fk@cogitatio.de', 'chris@cgsmith.net']
|
11
|
+
spec.description = %q{Enables Vagrant to update hosts file on the host machine}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = 'https://github.com/gokhansengun/vagrant-hostsupdater'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vagrant-virtual-hostsupdater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Falk Kühnel
|
8
|
+
- Chris Smith
|
9
|
+
- Gökhan Şengün
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2018-07-11 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.3'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
description: Enables Vagrant to update hosts file on the host machine
|
44
|
+
email:
|
45
|
+
- fk@cogitatio.de
|
46
|
+
- chris@cgsmith.net
|
47
|
+
executables: []
|
48
|
+
extensions: []
|
49
|
+
extra_rdoc_files: []
|
50
|
+
files:
|
51
|
+
- ".gitignore"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/vagrant-virtual-hostsupdater.rb
|
57
|
+
- lib/vagrant-virtual-hostsupdater/Action/CacheHosts.rb
|
58
|
+
- lib/vagrant-virtual-hostsupdater/Action/RemoveHosts.rb
|
59
|
+
- lib/vagrant-virtual-hostsupdater/Action/UpdateHosts.rb
|
60
|
+
- lib/vagrant-virtual-hostsupdater/VirtualHostsUpdater.rb
|
61
|
+
- lib/vagrant-virtual-hostsupdater/command.rb
|
62
|
+
- lib/vagrant-virtual-hostsupdater/config.rb
|
63
|
+
- lib/vagrant-virtual-hostsupdater/plugin.rb
|
64
|
+
- lib/vagrant-virtual-hostsupdater/version.rb
|
65
|
+
- vagrant-virtual-hostsupdater.gemspec
|
66
|
+
homepage: https://github.com/gokhansengun/vagrant-hostsupdater
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.5.2
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: Enables Vagrant to update hosts file on the host machine
|
90
|
+
test_files: []
|