vagrant-hostoverseer 1.8.1.0
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.
- data/.gitignore +4 -0
- data/CHANGELOG.md +67 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +246 -0
- data/Rakefile +12 -0
- data/lib/vagrant-hostmanager.rb +14 -0
- data/lib/vagrant-hostmanager/action.rb +31 -0
- data/lib/vagrant-hostmanager/action/update_all.rb +49 -0
- data/lib/vagrant-hostmanager/action/update_guest.rb +31 -0
- data/lib/vagrant-hostmanager/action/update_host.rb +30 -0
- data/lib/vagrant-hostmanager/command.rb +44 -0
- data/lib/vagrant-hostmanager/config.rb +90 -0
- data/lib/vagrant-hostmanager/errors.rb +6 -0
- data/lib/vagrant-hostmanager/hosts_file/updater.rb +270 -0
- data/lib/vagrant-hostmanager/plugin.rb +44 -0
- data/lib/vagrant-hostmanager/provisioner.rb +24 -0
- data/lib/vagrant-hostmanager/util.rb +14 -0
- data/lib/vagrant-hostmanager/version.rb +5 -0
- data/locales/en.yml +10 -0
- data/test/Vagrantfile +46 -0
- data/test/test.sh +14 -0
- data/vagrant-hostoverseer.gemspec +23 -0
- metadata +109 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## 1.6.1
|
4
|
+
### Bug fixes
|
5
|
+
* Retain tmp hosts file and fix a bug where powershell mv does not move folder contents, in that context it's moving a file. [[#157](https://github.com/smdahlen/vagrant-hostmanager/issues/157)]
|
6
|
+
|
7
|
+
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.6.0...v1.6.1)
|
8
|
+
|
9
|
+
|
10
|
+
## 1.6.0
|
11
|
+
### Features
|
12
|
+
* splits hostnames across many lines [[#67](https://github.com/smdahlen/vagrant-hostmanager/pull/103)]
|
13
|
+
|
14
|
+
### Bug fixes
|
15
|
+
* show description for hostmanager when vagrant list-commands is triggered [[#105](https://github.com/smdahlen/vagrant-hostmanager/pull/105)]
|
16
|
+
|
17
|
+
### Miscelaneous
|
18
|
+
* extract old vagrant version compatibility code into util method [[#97](https://github.com/smdahlen/vagrant-hostmanager/pull/97)]
|
19
|
+
* migrate HostsFile code into its own class [[#98](https://github.com/smdahlen/vagrant-hostmanager/pull/97)]
|
20
|
+
|
21
|
+
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.5.0...v1.6.0)
|
22
|
+
|
23
|
+
|
24
|
+
## 1.5.0
|
25
|
+
### Features
|
26
|
+
* hostmanager now runs *before* provisioning takes place, on `up` action [[#73](https://github.com/smdahlen/vagrant-hostmanager/issues/73)]
|
27
|
+
|
28
|
+
### Bug fixes
|
29
|
+
* properly detect hosts file location on Windows guests [[#67](https://github.com/smdahlen/vagrant-hostmanager/pull/67)]
|
30
|
+
* do not add host if IP cannot be determined [[#85](https://github.com/smdahlen/vagrant-hostmanager/pull/85)]
|
31
|
+
* force moving of hosts file on Linux guests [[#93](https://github.com/smdahlen/vagrant-hostmanager/pull/93)]
|
32
|
+
* allow top-level config options (eg. `ip_resolver`) to propagate to machine configs [[#91](https://github.com/smdahlen/vagrant-hostmanager/issues/91)]
|
33
|
+
|
34
|
+
### Miscelaneous
|
35
|
+
* add passwordless sudo instructions to README [[#95](https://github.com/smdahlen/vagrant-hostmanager/pull/95)]
|
36
|
+
|
37
|
+
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.4.0...v1.5.0)
|
38
|
+
|
39
|
+
|
40
|
+
## 1.4.0
|
41
|
+
### Features
|
42
|
+
* supports vagrant 1.5 [[#80](https://github.com/smdahlen/vagrant-hostmanager/issues/80), [#81](https://github.com/smdahlen/vagrant-hostmanager/pull/81)]
|
43
|
+
* only updates hosts file if contents have changed [[#78](https://github.com/smdahlen/vagrant-hostmanager/pull/78)]
|
44
|
+
* custom ip resolver now has access to the machine whose hosts file is being updated [[#62](https://github.com/smdahlen/vagrant-hostmanager/pull/62)]
|
45
|
+
|
46
|
+
### Bug fixes
|
47
|
+
* custom IP resolver result no longer ignored [[#57](https://github.com/smdahlen/vagrant-hostmanager/pull/57)]
|
48
|
+
* when multiple private_networks are configured, the first one is used [[#64](https://github.com/smdahlen/vagrant-hostmanager/pull/64)]
|
49
|
+
* destroyed machines are now removed from hosts file [[#52](https://github.com/smdahlen/vagrant-hostmanager/pull/52)]
|
50
|
+
|
51
|
+
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.3.0...v1.4.0)
|
52
|
+
|
53
|
+
|
54
|
+
## 1.3.0
|
55
|
+
### Features
|
56
|
+
* allow defining a custom IP resolver block [[#15](https://github.com/smdahlen/vagrant-hostmanager/pull/15)]
|
57
|
+
* handle removing destroyed machines from hosts file (currently only works with `include_offline = true`) [[#45](https://github.com/smdahlen/vagrant-hostmanager/pull/45)]
|
58
|
+
* attempt to elevate privileges when needed in Windows hosts [[#48](https://github.com/smdahlen/vagrant-hostmanager/pull/48)]
|
59
|
+
|
60
|
+
### Bug fixes
|
61
|
+
* `--provider` command-line option now finds machines as expected [[#46](https://github.com/smdahlen/vagrant-hostmanager/pull/46)]
|
62
|
+
* uses proper `hosts` file location in Windows under cygwin [[#49](https://github.com/smdahlen/vagrant-hostmanager/pull/49)]
|
63
|
+
|
64
|
+
### Miscelaneous
|
65
|
+
* MIT license added to gemspec
|
66
|
+
|
67
|
+
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.2.3...v1.3.0)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Shawn Dahlen
|
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,246 @@
|
|
1
|
+
Vagrant Host Manager
|
2
|
+
====================
|
3
|
+
|
4
|
+
[](https://rubygems.org/gems/vagrant-hostmanager)
|
5
|
+
[](https://rubygems.org/gems/vagrant-hostmanager)
|
6
|
+
[](https://rubygems.org/gems/vagrant-hostmanager)
|
7
|
+
[](https://twitter.com/intent/tweet?text=Check%20out%20this%20awesome%20Vagrant%20plugin%21&url=https%3A%2F%2Fgithub.com%2Fsmdahlen%2Fvagrant-hostmanager&hashtags=vagrant%hostmanager&original_referer=)
|
8
|
+
|
9
|
+
`vagrant-hostoverseer` is a fork of `vagrant-hostmanager` adding a few extras:
|
10
|
+
- Manage hostnames of multiple providers,
|
11
|
+
- Add the option to manages aliases on one line only,
|
12
|
+
- Add the option of automagically add the fqdn to the alias line.
|
13
|
+
|
14
|
+
The reasons of these patches are:
|
15
|
+
- In the upstream hostmanager, if you spawn a machine with one provider, the addresses
|
16
|
+
in all other providers are lost.
|
17
|
+
- The format of `/etc/hosts` according the man page is that one IP must appear on one line only.
|
18
|
+
- As there are a few ways to set up fqdn on a server and different tools will
|
19
|
+
use different ways to get the fqdn, only adding aliases in `/etc/hosts` created some issues,
|
20
|
+
for instance on AWS.
|
21
|
+
|
22
|
+
I would like to have these patches merged upstream but first I am not sure upstream
|
23
|
+
considers these contributions worthwhile and second I know that there is at least an
|
24
|
+
annoying limitation where the fqdn option will work properly only for linux hosts.
|
25
|
+
|
26
|
+
Beyond the command to install the plugin, all references and configurations are
|
27
|
+
still named hostmanager as my goal is not keep this fork, but have it eventually
|
28
|
+
properly merged.
|
29
|
+
|
30
|
+
`vagrant-hostmanager` is a Vagrant 1.1+ plugin that manages the `/etc/hosts`
|
31
|
+
file on guest machines (and optionally the host). Its goal is to enable
|
32
|
+
resolution of multi-machine environments deployed with a cloud provider
|
33
|
+
where IP addresses are not known in advance.
|
34
|
+
|
35
|
+
*NOTE:* Version 1.1 of the plugin prematurely introduced a feature to hook into
|
36
|
+
commands other than `vagrant up` and `vagrant destroy`. Version 1.1 broke support
|
37
|
+
for some providers. Version 1.2 reverts this feature until a suitable implementation
|
38
|
+
supporting all providers is available.
|
39
|
+
|
40
|
+
***Potentially breaking change in v1.5.0:*** the running order on `vagrant up` has changed
|
41
|
+
so that hostmanager runs before provisioning takes place. This ensures all hostnames are
|
42
|
+
available to the guest when it is being provisioned
|
43
|
+
(see [#73](https://github.com/smdahlen/vagrant-hostmanager/issues/73)).
|
44
|
+
Previously, hostmanager would run as the very last action. If you depend on the old behavior,
|
45
|
+
see the [provisioner](#provisioner) section.
|
46
|
+
|
47
|
+
Installation
|
48
|
+
------------
|
49
|
+
Install the plugin following the typical Vagrant 1.1 procedure:
|
50
|
+
|
51
|
+
$ vagrant plugin install vagrant-oversser
|
52
|
+
|
53
|
+
Usage
|
54
|
+
-----
|
55
|
+
To update the `/etc/hosts` file on each active machine, run the following
|
56
|
+
command:
|
57
|
+
|
58
|
+
$ vagrant hostmanager
|
59
|
+
|
60
|
+
The plugin hooks into the `vagrant up` and `vagrant destroy` commands
|
61
|
+
automatically.
|
62
|
+
When a machine enters or exits the running state , all active
|
63
|
+
machines with the same provider will have their `/etc/hosts` file updated
|
64
|
+
accordingly. Set the `hostmanager.enabled` attribute to `true` in the
|
65
|
+
Vagrantfile to activate this behavior.
|
66
|
+
|
67
|
+
To update the host's `/etc/hosts` file, set the `hostmanager.manage_host`
|
68
|
+
attribute to `true`.
|
69
|
+
|
70
|
+
To update the guests' `/etc/hosts` file, set the `hostmanager.manage_guest`
|
71
|
+
attribute to `true`.
|
72
|
+
|
73
|
+
A machine's IP address is defined by either the static IP for a private
|
74
|
+
network configuration or by the SSH host configuration. To disable
|
75
|
+
using the private network IP address, set `config.hostmanager.ignore_private_ip`
|
76
|
+
to true.
|
77
|
+
|
78
|
+
A machine's host name is defined by `config.vm.hostname`. If this is not
|
79
|
+
set, it falls back to the symbol defining the machine in the Vagrantfile.
|
80
|
+
|
81
|
+
If the `hostmanager.include_offline` attribute is set to `true`, boxes that are
|
82
|
+
up or have a private ip configured will be added to the hosts file.
|
83
|
+
|
84
|
+
In addition, the `hostmanager.aliases` configuration attribute can be used
|
85
|
+
to provide aliases for your host names.
|
86
|
+
|
87
|
+
On some systems, long alias lines have been reported to cause issues
|
88
|
+
(see [#60](https://github.com/smdahlen/vagrant-hostmanager/issues/60)).
|
89
|
+
In such cases, you may render aliases on separate lines by setting
|
90
|
+
```hostmanager.aliases_on_separate_lines = true```.
|
91
|
+
|
92
|
+
If you have all your aliases on one line and you do not manage the fqdn fully
|
93
|
+
from vagrant (AWS for instance) you might want to add the fqdn as
|
94
|
+
well on this line to have only one canonical line. In such case, set
|
95
|
+
```hostmanager.add_current_fqdn = true```.
|
96
|
+
|
97
|
+
|
98
|
+
Example configuration:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
Vagrant.configure("2") do |config|
|
102
|
+
config.hostmanager.enabled = true
|
103
|
+
config.hostmanager.manage_host = true
|
104
|
+
config.hostmanager.manage_guest = true
|
105
|
+
config.hostmanager.ignore_private_ip = false
|
106
|
+
config.hostmanager.include_offline = true
|
107
|
+
config.vm.define 'example-box' do |node|
|
108
|
+
node.vm.hostname = 'example-box-hostname'
|
109
|
+
node.vm.network :private_network, ip: '192.168.42.42'
|
110
|
+
node.hostmanager.aliases = %w(example-box.localdomain example-box-alias)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
### Provisioner
|
116
|
+
|
117
|
+
Starting at version 1.5.0, `vagrant up` runs hostmanager before any provisioning occurs.
|
118
|
+
If you would like hostmanager to run after or during your provisioning stage,
|
119
|
+
you can use hostmanager as a provisioner. This allows you to use the provisioning
|
120
|
+
order to ensure that hostmanager runs when desired. The provisioner will collect
|
121
|
+
hosts from boxes with the same provider as the running box.
|
122
|
+
|
123
|
+
Example:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
# Disable the default hostmanager behavior
|
127
|
+
config.hostmanager.enabled = false
|
128
|
+
|
129
|
+
# ... possible provisioner config before hostmanager ...
|
130
|
+
|
131
|
+
# hostmanager provisioner
|
132
|
+
config.vm.provision :hostmanager
|
133
|
+
|
134
|
+
# ... possible provisioning config after hostmanager ...
|
135
|
+
```
|
136
|
+
|
137
|
+
Custom IP resolver
|
138
|
+
------------------
|
139
|
+
|
140
|
+
You can customize way, how host manager resolves IP address
|
141
|
+
for each machine. This might be handy in case of aws provider,
|
142
|
+
where host name is stored in ssh_info hash of each machine.
|
143
|
+
This causes generation of invalid /etc/hosts file.
|
144
|
+
|
145
|
+
Custom IP resolver gives you oportunity to calculate IP address
|
146
|
+
for each machine by yourself, giving You also access to the machine that is
|
147
|
+
updating /etc/hosts. For example:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
|
151
|
+
if hostname = (vm.ssh_info && vm.ssh_info[:host])
|
152
|
+
`host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
Passwordless sudo
|
158
|
+
-----------------
|
159
|
+
|
160
|
+
To avoid being asked for the password every time the hosts file is updated,
|
161
|
+
enable passwordless sudo for the specific command that hostmanager uses to
|
162
|
+
update the hosts file.
|
163
|
+
|
164
|
+
- Add the following snippet to the sudoers file (e.g.
|
165
|
+
`/etc/sudoers.d/vagrant_hostmanager`):
|
166
|
+
|
167
|
+
```
|
168
|
+
Cmnd_Alias VAGRANT_HOSTMANAGER_UPDATE = /bin/cp <home-directory>/.vagrant.d/tmp/hosts.local /etc/hosts
|
169
|
+
%<admin-group> ALL=(root) NOPASSWD: VAGRANT_HOSTMANAGER_UPDATE
|
170
|
+
```
|
171
|
+
|
172
|
+
Replace `<home-directory>` with your actual home directory (e.g.
|
173
|
+
`/home/joe`) and `<admin-group>` with the group that is used by the system
|
174
|
+
for sudo access (usually `sudo` on Debian/Ubuntu systems and `wheel`
|
175
|
+
on Fedora/Red Hat systems).
|
176
|
+
|
177
|
+
- If necessary, add yourself to the `<admin-group>`:
|
178
|
+
|
179
|
+
```
|
180
|
+
usermod -aG <admin-group> <user-name>
|
181
|
+
```
|
182
|
+
|
183
|
+
Replace `<admin-group>` with the group that is used by the system for sudo
|
184
|
+
access (see above) and `<user-name>` with you user name.
|
185
|
+
|
186
|
+
Windows support
|
187
|
+
---------------
|
188
|
+
|
189
|
+
Hostmanager will detect Windows guests and hosts and use the appropriate
|
190
|
+
path for the ```hosts``` file: ```%WINDIR%\System32\drivers\etc\hosts```
|
191
|
+
|
192
|
+
By default on a Windows host, the ```hosts``` file is not writable without
|
193
|
+
elevated privileges. If hostmanager detects that it cannot overwrite the file,
|
194
|
+
it will attempt to do so with elevated privileges, causing the
|
195
|
+
[UAC](http://en.wikipedia.org/wiki/User_Account_Control) prompt to appear.
|
196
|
+
|
197
|
+
To avoid the UAC prompt, open ```%WINDIR%\System32\drivers\etc\``` in
|
198
|
+
Explorer, right-click the hosts file, go to Properties > Security > Edit
|
199
|
+
and give your user Modify permission.
|
200
|
+
|
201
|
+
### UAC limitations
|
202
|
+
|
203
|
+
Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any
|
204
|
+
visible errors, however the ```hosts``` file will not be updated.
|
205
|
+
|
206
|
+
|
207
|
+
Contribute
|
208
|
+
----------
|
209
|
+
To contribute, fork then clone the repository, and then the following:
|
210
|
+
|
211
|
+
**Developing**
|
212
|
+
|
213
|
+
1. Install [Bundler](http://bundler.io/)
|
214
|
+
2. Currently the Bundler version is locked to 1.6.9, please install this version.
|
215
|
+
* `sudo gem install bundler -v '1.6.9'`
|
216
|
+
3. Then install vagrant-hostmanager dependancies:
|
217
|
+
* `bundle _1.6.9_ install`
|
218
|
+
|
219
|
+
**Testing**
|
220
|
+
|
221
|
+
1. Build and package your newly developed code:
|
222
|
+
* `rake gem:build`
|
223
|
+
2. Then install the packaged plugin:
|
224
|
+
* `vagrant plugin install pkg/vagrant-hostmanager-*.gem`
|
225
|
+
3. Once you're done testing, roll-back to the latest released version:
|
226
|
+
* `vagrant plugin uninstall vagrant-hostmanager`
|
227
|
+
* `vagrant plugin install vagrant-hostmanager`
|
228
|
+
4. Once you're satisfied developing and testing your new code, please submit a pull request for review.
|
229
|
+
|
230
|
+
**Releasing**
|
231
|
+
|
232
|
+
To release a new version of vagrant-hostmanager you will need to do the following:
|
233
|
+
|
234
|
+
*(only contributors of the GitHub repo and owners of the project at RubyGems will have rights to do this)*
|
235
|
+
|
236
|
+
1. First, bump the version in ~/lib/vagrant-hostmanager/version.rb:
|
237
|
+
* Follow [Semantic Versioning](http://semver.org/).
|
238
|
+
2. Then, create a matching GitHub Release (this will also create a tag):
|
239
|
+
* Preface the version number with a `v`.
|
240
|
+
* https://github.com/smdahlen/vagrant-hostmanager/releases
|
241
|
+
3. You will then need to build and push the new gem to RubyGems:
|
242
|
+
* `rake gem:build`
|
243
|
+
* `gem push pkg/vagrant-hostmanager-1.6.1.gem`
|
244
|
+
4. Then, when John Doe runs the following, they will receive the updated vagrant-hostmanager plugin:
|
245
|
+
* `vagrant plugin update`
|
246
|
+
* `vagrant plugin update vagrant-hostmanager`
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'vagrant-hostmanager/plugin'
|
2
|
+
require 'vagrant-hostmanager/version'
|
3
|
+
require 'vagrant-hostmanager/errors'
|
4
|
+
|
5
|
+
module VagrantPlugins
|
6
|
+
module HostManager
|
7
|
+
def self.source_root
|
8
|
+
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
|
9
|
+
end
|
10
|
+
|
11
|
+
I18n.load_path << File.expand_path('locales/en.yml', source_root)
|
12
|
+
I18n.reload!
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'vagrant-hostmanager/action/update_all'
|
2
|
+
require 'vagrant-hostmanager/action/update_guest'
|
3
|
+
require 'vagrant-hostmanager/action/update_host'
|
4
|
+
|
5
|
+
module VagrantPlugins
|
6
|
+
module HostManager
|
7
|
+
module Action
|
8
|
+
include Vagrant::Action::Builtin
|
9
|
+
|
10
|
+
def self.update_all
|
11
|
+
Vagrant::Action::Builder.new.tap do |builder|
|
12
|
+
builder.use ConfigValidate
|
13
|
+
builder.use UpdateAll
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.update_guest
|
18
|
+
Vagrant::Action::Builder.new.tap do |builder|
|
19
|
+
builder.use ConfigValidate
|
20
|
+
builder.use UpdateGuest
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.update_host
|
25
|
+
Vagrant::Action::Builder.new.tap do |builder|
|
26
|
+
builder.use UpdateHost
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'vagrant-hostmanager/hosts_file/updater'
|
2
|
+
require 'vagrant-hostmanager/util'
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module HostManager
|
6
|
+
module Action
|
7
|
+
class UpdateAll
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@machine = env[:machine]
|
12
|
+
@global_env = @machine.env
|
13
|
+
@provider = @machine.provider_name
|
14
|
+
@config = Util.get_config(@global_env)
|
15
|
+
@updater = HostsFile::Updater.new(@global_env, @provider)
|
16
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_all')
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
# skip if machine is not active on destroy action
|
21
|
+
return @app.call(env) if !@machine.id && env[:machine_action] == :destroy
|
22
|
+
|
23
|
+
# check config to see if the hosts file should be update automatically
|
24
|
+
return @app.call(env) unless @config.hostmanager.enabled?
|
25
|
+
@logger.info 'Updating /etc/hosts file automatically'
|
26
|
+
|
27
|
+
@app.call(env)
|
28
|
+
|
29
|
+
# update /etc/hosts file on active machines
|
30
|
+
if @machine.config.hostmanager.manage_guest?
|
31
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guests')
|
32
|
+
@global_env.active_machines.each do |name, p|
|
33
|
+
if p == @provider
|
34
|
+
machine = @global_env.machine(name, p)
|
35
|
+
@updater.update_guest(machine)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# update /etc/hosts files on host if enabled
|
41
|
+
if @machine.config.hostmanager.manage_host?
|
42
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
|
43
|
+
@updater.update_host
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'vagrant-hostmanager/hosts_file/updater'
|
2
|
+
require 'vagrant-hostmanager/util'
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module HostManager
|
6
|
+
module Action
|
7
|
+
class UpdateGuest
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
global_env = env[:global_env]
|
12
|
+
@config = Util.get_config(global_env)
|
13
|
+
@machine = env[:machine]
|
14
|
+
@updater = HostsFile::Updater.new(@machine.env, env[:provider])
|
15
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
if @config.hostmanager.manage_guest?
|
20
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', {
|
21
|
+
:name => @machine.name
|
22
|
+
})
|
23
|
+
@updater.update_guest(@machine)
|
24
|
+
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'vagrant-hostmanager/hosts_file/updater'
|
2
|
+
require 'vagrant-hostmanager/util'
|
3
|
+
|
4
|
+
module VagrantPlugins
|
5
|
+
module HostManager
|
6
|
+
module Action
|
7
|
+
class UpdateHost
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
|
12
|
+
global_env = env[:global_env]
|
13
|
+
@config = Util.get_config(global_env)
|
14
|
+
@updater = HostsFile::Updater.new(global_env, env[:provider])
|
15
|
+
|
16
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_host')
|
17
|
+
end
|
18
|
+
|
19
|
+
def call(env)
|
20
|
+
if @config.hostmanager.manage_host?
|
21
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
|
22
|
+
@updater.update_host
|
23
|
+
end
|
24
|
+
|
25
|
+
@app.call(env)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module HostManager
|
3
|
+
class Command < Vagrant.plugin('2', :command)
|
4
|
+
|
5
|
+
# Show description when `vagrant list-commands` is triggered
|
6
|
+
def self.synopsis
|
7
|
+
"plugin: vagrant-hostmanager: manages the /etc/hosts file within a multi-machine environment"
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute
|
11
|
+
options = {}
|
12
|
+
opts = OptionParser.new do |o|
|
13
|
+
o.banner = 'Usage: vagrant hostmanager [vm-name]'
|
14
|
+
o.separator ''
|
15
|
+
o.version = VagrantPlugins::HostManager::VERSION
|
16
|
+
o.program_name = 'vagrant hostmanager'
|
17
|
+
|
18
|
+
o.on('--provider provider', String,
|
19
|
+
'Update machines with the specific provider.') do |provider|
|
20
|
+
options[:provider] = provider.to_sym
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
argv = parse_options(opts)
|
25
|
+
options[:provider] ||= @env.default_provider
|
26
|
+
|
27
|
+
# update /etc/hosts file for specified guest machines
|
28
|
+
with_target_vms(argv, options) do |machine|
|
29
|
+
@env.action_runner.run(Action.update_guest, {
|
30
|
+
:global_env => @env,
|
31
|
+
:machine => machine,
|
32
|
+
:provider => options[:provider]
|
33
|
+
})
|
34
|
+
end
|
35
|
+
|
36
|
+
# update /etc/hosts file for host
|
37
|
+
@env.action_runner.run(Action.update_host, {
|
38
|
+
:global_env => @env,
|
39
|
+
:provider => options[:provider]
|
40
|
+
})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module HostManager
|
3
|
+
class Config < Vagrant.plugin('2', :config)
|
4
|
+
attr_accessor :enabled
|
5
|
+
attr_accessor :manage_host
|
6
|
+
attr_accessor :manage_guest
|
7
|
+
attr_accessor :ignore_private_ip
|
8
|
+
attr_accessor :aliases
|
9
|
+
attr_accessor :include_offline
|
10
|
+
attr_accessor :ip_resolver
|
11
|
+
attr_accessor :aliases_on_separate_lines
|
12
|
+
attr_accessor :add_current_fqdn
|
13
|
+
|
14
|
+
alias_method :enabled?, :enabled
|
15
|
+
alias_method :include_offline?, :include_offline
|
16
|
+
alias_method :manage_host?, :manage_host
|
17
|
+
alias_method :manage_guest?, :manage_guest
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@enabled = UNSET_VALUE
|
21
|
+
@manage_host = UNSET_VALUE
|
22
|
+
@manage_guest = UNSET_VALUE
|
23
|
+
@ignore_private_ip = UNSET_VALUE
|
24
|
+
@include_offline = UNSET_VALUE
|
25
|
+
@aliases = UNSET_VALUE
|
26
|
+
@ip_resolver = UNSET_VALUE
|
27
|
+
@aliases_on_separate_lines = UNSET_VALUE
|
28
|
+
@add_current_fqdn = UNSET_VALUE
|
29
|
+
end
|
30
|
+
|
31
|
+
def finalize!
|
32
|
+
@enabled = false if @enabled == UNSET_VALUE
|
33
|
+
@manage_host = false if @manage_host == UNSET_VALUE
|
34
|
+
@manage_guest = true if @manage_guest == UNSET_VALUE
|
35
|
+
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
|
36
|
+
@include_offline = false if @include_offline == UNSET_VALUE
|
37
|
+
@aliases = [] if @aliases == UNSET_VALUE
|
38
|
+
@ip_resolver = nil if @ip_resolver == UNSET_VALUE
|
39
|
+
@aliases_on_separate_lines = false if @aliases_on_separate_lines == UNSET_VALUE
|
40
|
+
@add_current_fqdn = false if @add_current_fqdn == UNSET_VALUE
|
41
|
+
@aliases = [ @aliases ].flatten
|
42
|
+
end
|
43
|
+
|
44
|
+
def validate(machine)
|
45
|
+
errors = []
|
46
|
+
|
47
|
+
errors << validate_bool('hostmanager.enabled', @enabled)
|
48
|
+
errors << validate_bool('hostmanager.manage_host', @manage_host)
|
49
|
+
errors << validate_bool('hostmanager.manage_guest', @manage_guest)
|
50
|
+
errors << validate_bool('hostmanager.ignore_private_ip', @ignore_private_ip)
|
51
|
+
errors << validate_bool('hostmanager.include_offline', @include_offline)
|
52
|
+
errors.compact!
|
53
|
+
|
54
|
+
# check if aliases option is an Array
|
55
|
+
if !machine.config.hostmanager.aliases.kind_of?(Array) &&
|
56
|
+
!machine.config.hostmanager.aliases.kind_of?(String)
|
57
|
+
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
|
58
|
+
:config_key => 'hostmanager.aliases',
|
59
|
+
:is_class => aliases.class.to_s,
|
60
|
+
})
|
61
|
+
end
|
62
|
+
|
63
|
+
if !machine.config.hostmanager.ip_resolver.nil? &&
|
64
|
+
!machine.config.hostmanager.ip_resolver.kind_of?(Proc)
|
65
|
+
errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
|
66
|
+
:config_key => 'hostmanager.ip_resolver',
|
67
|
+
:is_class => ip_resolver.class.to_s,
|
68
|
+
})
|
69
|
+
end
|
70
|
+
|
71
|
+
errors.compact!
|
72
|
+
{ "HostManager configuration" => errors }
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def validate_bool(key, value)
|
78
|
+
if ![TrueClass, FalseClass].include?(value.class) &&
|
79
|
+
value != UNSET_VALUE
|
80
|
+
I18n.t('vagrant_hostmanager.config.not_a_bool', {
|
81
|
+
:config_key => key,
|
82
|
+
:value => value.class.to_s
|
83
|
+
})
|
84
|
+
else
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,270 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module HostManager
|
5
|
+
module HostsFile
|
6
|
+
|
7
|
+
class Updater
|
8
|
+
|
9
|
+
def initialize(global_env, provider)
|
10
|
+
@global_env = global_env
|
11
|
+
@config = Util.get_config(@global_env)
|
12
|
+
@provider = provider
|
13
|
+
@current_machine_config = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def update_guest(machine)
|
17
|
+
return unless machine.communicate.ready?
|
18
|
+
|
19
|
+
if (machine.communicate.test("uname -s | grep SunOS"))
|
20
|
+
realhostfile = '/etc/inet/hosts'
|
21
|
+
elsif (machine.communicate.test("test -d $Env:SystemRoot"))
|
22
|
+
windir = ""
|
23
|
+
machine.communicate.execute("echo %SYSTEMROOT%", {:shell => :cmd}) do |type, contents|
|
24
|
+
windir << contents.gsub("\r\n", '') if type == :stdout
|
25
|
+
end
|
26
|
+
realhostfile = "#{windir}\\System32\\drivers\\etc\\hosts"
|
27
|
+
else
|
28
|
+
realhostfile = '/etc/hosts'
|
29
|
+
end
|
30
|
+
# download and modify file with Vagrant-managed entries
|
31
|
+
file = @global_env.tmp_path.join("hosts.#{machine.name}")
|
32
|
+
machine.communicate.download(realhostfile, file)
|
33
|
+
@current_machine_config = ((machine && machine.config) || @config)
|
34
|
+
if update_file(file, machine, false)
|
35
|
+
|
36
|
+
# upload modified file and remove temporary file
|
37
|
+
machine.communicate.upload(file, '/tmp/hosts')
|
38
|
+
if windir
|
39
|
+
machine.communicate.sudo("mv -force /tmp/hosts/hosts.#{machine.name} #{realhostfile}")
|
40
|
+
else
|
41
|
+
machine.communicate.sudo("cat /tmp/hosts > #{realhostfile}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
def update_host
|
48
|
+
# copy and modify hosts file on host with Vagrant-managed entries
|
49
|
+
file = @global_env.tmp_path.join('hosts.local')
|
50
|
+
|
51
|
+
if WindowsSupport.windows?
|
52
|
+
# lazily include windows Module
|
53
|
+
class << self
|
54
|
+
include WindowsSupport unless include? WindowsSupport
|
55
|
+
end
|
56
|
+
|
57
|
+
hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
|
58
|
+
copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
|
59
|
+
else
|
60
|
+
hosts_location = '/etc/hosts'
|
61
|
+
copy_proc = Proc.new { `sudo cp #{file} #{hosts_location}` }
|
62
|
+
end
|
63
|
+
|
64
|
+
FileUtils.cp(hosts_location, file)
|
65
|
+
if update_file(file)
|
66
|
+
copy_proc.call
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def update_file(file, resolving_machine = nil, include_id = true)
|
73
|
+
file = Pathname.new(file)
|
74
|
+
old_file_content = file.read
|
75
|
+
new_file_content = update_content(old_file_content, resolving_machine, include_id)
|
76
|
+
file.open('wb') { |io| io.write(new_file_content) }
|
77
|
+
old_file_content != new_file_content
|
78
|
+
end
|
79
|
+
|
80
|
+
def update_content(file_content, resolving_machine, include_id)
|
81
|
+
id = include_id ? " id: #{read_or_create_id}" : ""
|
82
|
+
header = "## vagrant-hostmanager-start-#{@provider}#{id}\n"
|
83
|
+
footer = "## vagrant-hostmanager-end-#{@provider}\n"
|
84
|
+
body = get_machines
|
85
|
+
.map { |machine| get_hosts_file_entry(machine, resolving_machine) }
|
86
|
+
.join
|
87
|
+
get_new_content(header, footer, body, file_content)
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_hosts_file_entry(machine, resolving_machine)
|
91
|
+
ip = get_ip_address(machine, resolving_machine)
|
92
|
+
|
93
|
+
unless ip.nil?
|
94
|
+
names = get_names(machine, resolving_machine)
|
95
|
+
if @current_machine_config.hostmanager.aliases_on_separate_lines
|
96
|
+
names.map { |a| "#{ip}\t#{a}" }.join("\n") + "\n"
|
97
|
+
else
|
98
|
+
"#{ip}\t" + names.join(" ") + "\n"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def get_ip_address(machine, resolving_machine)
|
104
|
+
custom_ip_resolver = machine.config.hostmanager.ip_resolver
|
105
|
+
if custom_ip_resolver
|
106
|
+
custom_ip_resolver.call(machine, resolving_machine)
|
107
|
+
else
|
108
|
+
ip = nil
|
109
|
+
if machine.config.hostmanager.ignore_private_ip != true
|
110
|
+
machine.config.vm.networks.each do |network|
|
111
|
+
key, options = network[0], network[1]
|
112
|
+
ip = options[:ip] if key == :private_network
|
113
|
+
break if ip
|
114
|
+
end
|
115
|
+
end
|
116
|
+
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# get all names including aliases, in the right order (fqdn first if relevant)
|
121
|
+
def get_names(machine, resolving_machine)
|
122
|
+
host = machine.config.vm.hostname || machine.name
|
123
|
+
aliases = machine.config.hostmanager.aliases
|
124
|
+
all_names = [host] + aliases
|
125
|
+
|
126
|
+
# Optionally prepend current fqdn as well. Useful with hostname set outside
|
127
|
+
# vagrant (eg. aws)
|
128
|
+
if @current_machine_config.hostmanager.add_current_fqdn
|
129
|
+
dns = get_dns(machine)
|
130
|
+
# put fqdn in front
|
131
|
+
all_names = dns + all_names
|
132
|
+
end
|
133
|
+
|
134
|
+
all_names.uniq # order is kept b uniq
|
135
|
+
end
|
136
|
+
|
137
|
+
# return fqdn *and* short name
|
138
|
+
def get_dns(machine)
|
139
|
+
names = []
|
140
|
+
unless machine.nil?
|
141
|
+
machine.communicate.execute('/bin/hostname -f') do |type, hostname|
|
142
|
+
names += [hostname.strip]
|
143
|
+
end
|
144
|
+
machine.communicate.execute('/bin/hostname') do |type, hostname|
|
145
|
+
names += [hostname.strip]
|
146
|
+
end
|
147
|
+
end
|
148
|
+
names
|
149
|
+
end
|
150
|
+
|
151
|
+
def get_machines
|
152
|
+
if @config.hostmanager.include_offline?
|
153
|
+
machines = @global_env.machine_names
|
154
|
+
else
|
155
|
+
machines = @global_env.active_machines
|
156
|
+
.select { |name, provider| provider == @provider }
|
157
|
+
.collect { |name, provider| name }
|
158
|
+
end
|
159
|
+
# Collect only machines that exist for the current provider
|
160
|
+
machines.collect do |name|
|
161
|
+
begin
|
162
|
+
machine = @global_env.machine(name, @provider)
|
163
|
+
rescue Vagrant::Errors::MachineNotFound
|
164
|
+
# ignore
|
165
|
+
end
|
166
|
+
machine
|
167
|
+
end
|
168
|
+
.reject(&:nil?)
|
169
|
+
end
|
170
|
+
|
171
|
+
def get_new_content(header, footer, body, old_content)
|
172
|
+
if body.empty?
|
173
|
+
block = "\n"
|
174
|
+
else
|
175
|
+
block = "\n\n" + header + body + footer + "\n"
|
176
|
+
end
|
177
|
+
# Pattern for finding existing block
|
178
|
+
header_pattern = Regexp.quote(header)
|
179
|
+
footer_pattern = Regexp.quote(footer)
|
180
|
+
pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
|
181
|
+
# Replace existing block or append
|
182
|
+
newcontent = old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
|
183
|
+
|
184
|
+
### remove name duplication in 127.*
|
185
|
+
cleancontent = ''
|
186
|
+
|
187
|
+
all_names = []
|
188
|
+
# First, extract back names from the body. If a name appear there, it
|
189
|
+
# should not be anywhere else.
|
190
|
+
body.each_line do |line|
|
191
|
+
tokens = line.sub(/#.*$/, '').strip.split(/\s+/)
|
192
|
+
ip = tokens.shift
|
193
|
+
unless tokens.empty?
|
194
|
+
all_names += tokens
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Then remove those names from any 127.* lines
|
199
|
+
newcontent.each_line do |line|
|
200
|
+
if line =~ /^\s*127\./
|
201
|
+
# Here be dragons.
|
202
|
+
tokens = line.sub(/#.*$/, '').strip.split(/\s+/)
|
203
|
+
ip=tokens.shift
|
204
|
+
dedup = tokens - all_names
|
205
|
+
unless dedup.empty?
|
206
|
+
cleancontent += "#{ip}\t" + dedup.join(' ') + "\n"
|
207
|
+
end
|
208
|
+
else
|
209
|
+
cleancontent += line
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
cleancontent
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
def read_or_create_id
|
218
|
+
file = Pathname.new("#{@global_env.local_data_path}/hostmanager/id")
|
219
|
+
if (file.file?)
|
220
|
+
id = file.read.strip
|
221
|
+
else
|
222
|
+
id = SecureRandom.uuid
|
223
|
+
file.dirname.mkpath
|
224
|
+
file.open('w') { |io| io.write(id) }
|
225
|
+
end
|
226
|
+
id
|
227
|
+
end
|
228
|
+
|
229
|
+
## Windows support for copying files, requesting elevated privileges if necessary
|
230
|
+
module WindowsSupport
|
231
|
+
require 'rbconfig'
|
232
|
+
|
233
|
+
def self.windows?
|
234
|
+
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
|
235
|
+
end
|
236
|
+
|
237
|
+
require 'win32ole' if windows?
|
238
|
+
|
239
|
+
def windows_copy_file(source, dest)
|
240
|
+
begin
|
241
|
+
# First, try Ruby copy
|
242
|
+
FileUtils.cp(source, dest)
|
243
|
+
rescue Errno::EACCES
|
244
|
+
# Access denied, try with elevated privileges
|
245
|
+
windows_copy_file_elevated(source, dest)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
private
|
250
|
+
|
251
|
+
def windows_copy_file_elevated(source, dest)
|
252
|
+
# copy command only supports backslashes as separators
|
253
|
+
source, dest = [source, dest].map { |s| s.to_s.gsub(/\//, '\\') }
|
254
|
+
|
255
|
+
# run 'cmd /C copy ...' with elevated privilege, minimized
|
256
|
+
copy_cmd = "copy \"#{source}\" \"#{dest}\""
|
257
|
+
WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{copy_cmd}", nil, 'runas', 7)
|
258
|
+
|
259
|
+
# Unfortunately, ShellExecute does not give us a status code,
|
260
|
+
# and it is non-blocking so we can't reliably compare the file contents
|
261
|
+
# to see if they were copied.
|
262
|
+
#
|
263
|
+
# If the user rejects the UAC prompt, vagrant will silently continue
|
264
|
+
# without updating the hostsfile.
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'vagrant-hostmanager/action'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module HostManager
|
5
|
+
class Plugin < Vagrant.plugin('2')
|
6
|
+
name 'HostManager'
|
7
|
+
description <<-DESC
|
8
|
+
This plugin manages the /etc/hosts file for the host and guest machines.
|
9
|
+
An entry is created for each running machine using the hostname attribute.
|
10
|
+
|
11
|
+
You can also use the hostmanager provisioner to update the hosts file.
|
12
|
+
DESC
|
13
|
+
|
14
|
+
config(:hostmanager) do
|
15
|
+
require_relative 'config'
|
16
|
+
Config
|
17
|
+
end
|
18
|
+
|
19
|
+
action_hook(:hostmanager, :machine_action_up) do |hook|
|
20
|
+
hook.after(Vagrant::Action::Builtin::Provision, Action.update_all)
|
21
|
+
end
|
22
|
+
|
23
|
+
action_hook(:hostmanager, :machine_action_destroy) do |hook|
|
24
|
+
hook.prepend(Action.update_all)
|
25
|
+
end
|
26
|
+
|
27
|
+
provisioner(:hostmanager) do
|
28
|
+
require_relative 'provisioner'
|
29
|
+
Provisioner
|
30
|
+
end
|
31
|
+
|
32
|
+
# Work-around for vagrant >= 1.5
|
33
|
+
# It breaks without a provisioner config, so we provide a dummy one
|
34
|
+
config(:hostmanager, :provisioner) do
|
35
|
+
::Vagrant::Config::V2::DummyConfig.new
|
36
|
+
end
|
37
|
+
|
38
|
+
command(:hostmanager) do
|
39
|
+
require_relative 'command'
|
40
|
+
Command
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'vagrant-hostmanager/hosts_file/updater'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module HostManager
|
5
|
+
class Provisioner < Vagrant.plugin('2', :provisioner)
|
6
|
+
|
7
|
+
def initialize(machine, config)
|
8
|
+
super(machine, config)
|
9
|
+
global_env = machine.env
|
10
|
+
@config = Util.get_config(global_env)
|
11
|
+
@updater = HostsFile::Updater.new(global_env, machine.provider_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def provision
|
15
|
+
if @config.hostmanager.manage_guest?
|
16
|
+
@updater.update_guest(@machine)
|
17
|
+
end
|
18
|
+
if @config.hostmanager.manage_host?
|
19
|
+
@updater.update_host
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module HostManager
|
3
|
+
module Util
|
4
|
+
def self.get_config(env)
|
5
|
+
# config_global has been removed from v1.5
|
6
|
+
if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
|
7
|
+
env.vagrantfile.config
|
8
|
+
else
|
9
|
+
env.config_global
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/locales/en.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
en:
|
2
|
+
vagrant_hostmanager:
|
3
|
+
action:
|
4
|
+
update_guests: "Updating /etc/hosts file on active guest machines..."
|
5
|
+
update_guest: "[%{name}] Updating /etc/hosts file..."
|
6
|
+
update_host: "Updating /etc/hosts file on host machine (password may be required)..."
|
7
|
+
config:
|
8
|
+
not_a_bool: "A value for %{config_key} can only be true or false, not type '%{value}'"
|
9
|
+
not_an_array_or_string: "A value for %{config_key} must be an Array or String, not type '%{is_class}'"
|
10
|
+
not_a_proc: "A value for %{config_key} must be a Proc, not type '%{is_class}'"
|
data/test/Vagrantfile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
# vi: set ft=ruby :
|
3
|
+
|
4
|
+
if Gem::Version.new(::Vagrant::VERSION) < Gem::Version.new('1.5')
|
5
|
+
Vagrant.require_plugin('vagrant-hostmanager')
|
6
|
+
end
|
7
|
+
|
8
|
+
Vagrant.configure('2') do |config|
|
9
|
+
|
10
|
+
if ENV.key? 'VAGRANT_BOX'
|
11
|
+
config.vm.box = ENV['VAGRANT_BOX']
|
12
|
+
else
|
13
|
+
config.vm.box = 'precise64'
|
14
|
+
config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box'
|
15
|
+
end
|
16
|
+
|
17
|
+
config.hostmanager.enabled = true
|
18
|
+
config.hostmanager.manage_host = true
|
19
|
+
config.hostmanager.manage_guest = true
|
20
|
+
# config.hostmanager.aliases_on_separate_lines = true
|
21
|
+
|
22
|
+
|
23
|
+
config.vm.define :server1 do |server|
|
24
|
+
server.vm.hostname = 'fry'
|
25
|
+
server.vm.network :private_network, :ip => '10.0.5.2'
|
26
|
+
server.hostmanager.aliases = %w(alias1 alias2)
|
27
|
+
end
|
28
|
+
|
29
|
+
config.vm.define :server2 do |server|
|
30
|
+
server.vm.hostname = 'bender'
|
31
|
+
server.vm.network :private_network, :ip => '10.0.5.3'
|
32
|
+
server.hostmanager.aliases = %w(alias3 alias4)
|
33
|
+
server.hostmanager.aliases_on_separate_lines = true
|
34
|
+
end
|
35
|
+
|
36
|
+
config.vm.define :server3 do |server|
|
37
|
+
server.vm.hostname = 'leena'
|
38
|
+
server.vm.network :private_network, :ip => '10.0.5.4'
|
39
|
+
server.vm.provision :hostmanager
|
40
|
+
end
|
41
|
+
|
42
|
+
config.vm.define :server4 do |server|
|
43
|
+
server.vm.hostname = 'scruffy'
|
44
|
+
server.vm.provision :hostmanager
|
45
|
+
end
|
46
|
+
end
|
data/test/test.sh
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'vagrant-hostmanager/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = 'vagrant-hostoverseer'
|
9
|
+
gem.version = VagrantPlugins::HostManager::VERSION
|
10
|
+
gem.authors = ['Shawn Dahlen','Seth Reeser','Guillaume']
|
11
|
+
gem.email = ['shawn@dahlen.me','info@devopsgroup.io','guillaume@lomig.net']
|
12
|
+
gem.description = %q{A Vagrant plugin that manages the /etc/hosts file within a multi-machine environment}
|
13
|
+
gem.homepage = 'https://github.com/lomignet/vagrant-hostoverseer'
|
14
|
+
gem.summary = gem.description
|
15
|
+
gem.license = 'MIT'
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vagrant-hostoverseer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.8.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Shawn Dahlen
|
9
|
+
- Seth Reeser
|
10
|
+
- Guillaume
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2016-02-22 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: bundler
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '1.3'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ~>
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '1.3'
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rake
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
description: A Vagrant plugin that manages the /etc/hosts file within a multi-machine
|
49
|
+
environment
|
50
|
+
email:
|
51
|
+
- shawn@dahlen.me
|
52
|
+
- info@devopsgroup.io
|
53
|
+
- guillaume@lomig.net
|
54
|
+
executables: []
|
55
|
+
extensions: []
|
56
|
+
extra_rdoc_files: []
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- CHANGELOG.md
|
60
|
+
- Gemfile
|
61
|
+
- LICENSE.txt
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- lib/vagrant-hostmanager.rb
|
65
|
+
- lib/vagrant-hostmanager/action.rb
|
66
|
+
- lib/vagrant-hostmanager/action/update_all.rb
|
67
|
+
- lib/vagrant-hostmanager/action/update_guest.rb
|
68
|
+
- lib/vagrant-hostmanager/action/update_host.rb
|
69
|
+
- lib/vagrant-hostmanager/command.rb
|
70
|
+
- lib/vagrant-hostmanager/config.rb
|
71
|
+
- lib/vagrant-hostmanager/errors.rb
|
72
|
+
- lib/vagrant-hostmanager/hosts_file/updater.rb
|
73
|
+
- lib/vagrant-hostmanager/plugin.rb
|
74
|
+
- lib/vagrant-hostmanager/provisioner.rb
|
75
|
+
- lib/vagrant-hostmanager/util.rb
|
76
|
+
- lib/vagrant-hostmanager/version.rb
|
77
|
+
- locales/en.yml
|
78
|
+
- test/Vagrantfile
|
79
|
+
- test/test.sh
|
80
|
+
- vagrant-hostoverseer.gemspec
|
81
|
+
homepage: https://github.com/lomignet/vagrant-hostoverseer
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.23
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: A Vagrant plugin that manages the /etc/hosts file within a multi-machine
|
106
|
+
environment
|
107
|
+
test_files:
|
108
|
+
- test/Vagrantfile
|
109
|
+
- test/test.sh
|