vagrant-multihostmanager 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +118 -0
- data/Rakefile +12 -0
- data/lib/vagrant-multihostmanager.rb +14 -0
- data/lib/vagrant-multihostmanager/action.rb +31 -0
- data/lib/vagrant-multihostmanager/action/update_all.rb +46 -0
- data/lib/vagrant-multihostmanager/action/update_guest.rb +28 -0
- data/lib/vagrant-multihostmanager/action/update_host.rb +28 -0
- data/lib/vagrant-multihostmanager/command.rb +39 -0
- data/lib/vagrant-multihostmanager/config.rb +78 -0
- data/lib/vagrant-multihostmanager/errors.rb +6 -0
- data/lib/vagrant-multihostmanager/hosts_file.rb +157 -0
- data/lib/vagrant-multihostmanager/plugin.rb +38 -0
- data/lib/vagrant-multihostmanager/provisioner.rb +20 -0
- data/lib/vagrant-multihostmanager/version.rb +5 -0
- data/locales/en.yml +10 -0
- data/test/Vagrantfile +23 -0
- data/test/test.sh +14 -0
- data/vagrant-multihostmanager.gemspec +24 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9c7e0c45edc0e6b82800af16d0280d722e8e859
|
4
|
+
data.tar.gz: e38aa24aeccb1c95ad4841a45d3a223eaeabbe00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 387cddbaf83030a097dcbc99f2c8fd0004125d12f06c2a05144245015d750b72ad61ba6582dcb03bf0e2b27aa2be3393b5fb611e358cd52e40c526f40ea6fd67
|
7
|
+
data.tar.gz: 5601337f17b7d5603844eb672d5f39d5908d803d7be8f86f2741e08c47b0057ae4632607b1d5152b26feb089ca4a8732383fac982b3711768032870c062fc789
|
data/.gitignore
ADDED
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,118 @@
|
|
1
|
+
Vagrant Host Manager
|
2
|
+
====================
|
3
|
+
`vagrant-multihostmanager` is a Vagrant 1.1+ plugin that manages the `/etc/hosts`
|
4
|
+
file on guest machines (and optionally the host). Its goal is to enable
|
5
|
+
resolution of multi-machine environments deployed with a cloud provider
|
6
|
+
where IP addresses are not known in advance.
|
7
|
+
|
8
|
+
*NOTE:* Version 1.1 of the plugin prematurely introduced a feature to hook into
|
9
|
+
commands other than `vagrant up` and `vagrant destroy`. Version 1.1 broke support for some providers. Version 1.2 reverts this feature until a suitable implementation supporting all providers is available.
|
10
|
+
|
11
|
+
Installation
|
12
|
+
------------
|
13
|
+
Install the plugin following the typical Vagrant 1.1 procedure:
|
14
|
+
|
15
|
+
$ vagrant plugin install vagrant-multihostmanager
|
16
|
+
|
17
|
+
Usage
|
18
|
+
-----
|
19
|
+
To update the `/etc/hosts` file on each active machine, run the following
|
20
|
+
command:
|
21
|
+
|
22
|
+
$ vagrant hostmanager
|
23
|
+
|
24
|
+
The plugin hooks into the `vagrant up` and `vagrant destroy` commands
|
25
|
+
automatically.
|
26
|
+
When a machine enters or exits the running state , all active
|
27
|
+
machines with the same provider will have their `/etc/hosts` file updated
|
28
|
+
accordingly. Set the `hostmanager.enabled` attribute to `true` in the
|
29
|
+
Vagrantfile to activate this behavior.
|
30
|
+
|
31
|
+
To update the host's `/etc/hosts` file, set the `hostmanager.manage_host`
|
32
|
+
attribute to `true`.
|
33
|
+
|
34
|
+
A machine's IP address is defined by either the static IP for a private
|
35
|
+
network configuration or by the SSH host configuration. To disable
|
36
|
+
using the private network IP address, set `config.hostmanger.ignore_private_ip`
|
37
|
+
to true.
|
38
|
+
|
39
|
+
A machine's host name is defined by `config.vm.hostname`. If this is not
|
40
|
+
set, it falls back to the symbol defining the machine in the Vagrantfile.
|
41
|
+
|
42
|
+
If the `hostmanager.include_offline` attribute is set to `true`, boxes that are
|
43
|
+
up or have a private ip configured will be added to the hosts file.
|
44
|
+
|
45
|
+
In addition, the `hostmanager.aliases` configuration attribute can be used
|
46
|
+
to provide aliases for your host names.
|
47
|
+
|
48
|
+
Example configuration:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
Vagrant.configure("2") do |config|
|
52
|
+
config.hostmanager.enabled = true
|
53
|
+
config.hostmanager.manage_host = true
|
54
|
+
config.hostmanager.ignore_private_ip = false
|
55
|
+
config.hostmanager.include_offline = true
|
56
|
+
config.vm.define 'example-box' do |node|
|
57
|
+
node.vm.hostname = 'example-box-hostname'
|
58
|
+
node.vm.network :private_network, ip: '192.168.42.42'
|
59
|
+
node.hostmanager.aliases = %w(example-box.localdomain example-box-alias)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
As a last option, you can use hostmanager as a provisioner.
|
65
|
+
This allows you to use the provisioning order to ensure that hostmanager
|
66
|
+
runs before or after provisioning. The provisioner will collect hosts from
|
67
|
+
boxes with the same provider as the running box.
|
68
|
+
|
69
|
+
Use:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
config.vm.provision :hostmanager
|
73
|
+
```
|
74
|
+
|
75
|
+
Custom IP resolver
|
76
|
+
------------------
|
77
|
+
|
78
|
+
You can customize way, how host manager resolves IP address
|
79
|
+
for each machine. This might be handy in case of aws provider,
|
80
|
+
where host name is stored in ssh_info hash of each machine.
|
81
|
+
This causes generation of invalid /etc/hosts file.
|
82
|
+
|
83
|
+
Custom IP resolver gives you oportunity to calculate IP address
|
84
|
+
for each machine by yourself. For example:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
config.hostmanager.ip_resolver = proc do |vm|
|
88
|
+
if hostname = (vm.ssh_info && vm.ssh_info[:host])
|
89
|
+
`host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Windows support
|
95
|
+
---------------
|
96
|
+
|
97
|
+
Hostmanager will detect Windows guests and hosts and use the appropriate
|
98
|
+
path for the ```hosts``` file: ```%WINDIR%\System32\drivers\etc\hosts```
|
99
|
+
|
100
|
+
By default on a Windows host, the ```hosts``` file is not writable without
|
101
|
+
elevated privileges. If hostmanager detects that it cannot overwrite the file,
|
102
|
+
it will attempt to do so with elevated privileges, causing the
|
103
|
+
[UAC](http://en.wikipedia.org/wiki/User_Account_Control) prompt to appear.
|
104
|
+
|
105
|
+
### UAC limitations
|
106
|
+
|
107
|
+
Due to limitations caused by UAC, cancelling out of the UAC prompt will not cause any
|
108
|
+
visible errors, however the ```hosts``` file will not be updated.
|
109
|
+
|
110
|
+
Contribute
|
111
|
+
----------
|
112
|
+
Contributions are welcome.
|
113
|
+
|
114
|
+
1. Fork it
|
115
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
116
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
117
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
118
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'vagrant-multihostmanager/plugin'
|
2
|
+
require 'vagrant-multihostmanager/version'
|
3
|
+
require 'vagrant-multihostmanager/errors'
|
4
|
+
|
5
|
+
module VagrantPlugins
|
6
|
+
module MultiHostManager
|
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-multihostmanager/action/update_all'
|
2
|
+
require 'vagrant-multihostmanager/action/update_guest'
|
3
|
+
require 'vagrant-multihostmanager/action/update_host'
|
4
|
+
|
5
|
+
module VagrantPlugins
|
6
|
+
module MultiHostManager
|
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,46 @@
|
|
1
|
+
require 'vagrant-multihostmanager/hosts_file'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module MultiHostManager
|
5
|
+
module Action
|
6
|
+
class UpdateAll
|
7
|
+
include HostsFile
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@machine = env[:machine]
|
12
|
+
@global_env = @machine.env
|
13
|
+
@provider = @machine.provider_name
|
14
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_all')
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
# skip if machine is already active on up action
|
19
|
+
return @app.call(env) if @machine.id && env[:machine_action] == :up
|
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 @global_env.config_global.multihostmanager.enabled?
|
25
|
+
@logger.info 'Updating /etc/hosts file automatically'
|
26
|
+
|
27
|
+
@app.call(env)
|
28
|
+
|
29
|
+
# update /etc/hosts file on active machines
|
30
|
+
@global_env.active_machines.each do |name, p|
|
31
|
+
if p == @provider
|
32
|
+
machine = @global_env.machine(name, p)
|
33
|
+
update_guest(machine)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# update /etc/hosts files on host if enabled
|
38
|
+
if @machine.config.multihostmanager.manage_host?
|
39
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
|
40
|
+
update_host
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'vagrant-multihostmanager/hosts_file'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module MultiHostManager
|
5
|
+
module Action
|
6
|
+
class UpdateGuest
|
7
|
+
include HostsFile
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@machine = env[:machine]
|
12
|
+
@global_env = @machine.env
|
13
|
+
@provider = env[:provider]
|
14
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_guest', {
|
19
|
+
:name => @machine.name
|
20
|
+
})
|
21
|
+
update_guest(@machine)
|
22
|
+
|
23
|
+
@app.call(env)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'vagrant-multihostmanager/hosts_file'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module MultiHostManager
|
5
|
+
module Action
|
6
|
+
class UpdateHost
|
7
|
+
include HostsFile
|
8
|
+
|
9
|
+
def initialize(app, env)
|
10
|
+
@app = app
|
11
|
+
@global_env = env[:global_env]
|
12
|
+
@provider = env[:provider]
|
13
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_host')
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
if @global_env.config_global.multihostmanager.manage_host?
|
18
|
+
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
|
19
|
+
update_host
|
20
|
+
end
|
21
|
+
|
22
|
+
@app.call(env)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module MultiHostManager
|
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 multihostmanager [vm-name]'
|
10
|
+
o.separator ''
|
11
|
+
o.version = VagrantPlugins::MultiHostManager::VERSION
|
12
|
+
o.program_name = 'vagrant multihostmanager'
|
13
|
+
|
14
|
+
o.on('--provider provider', String,
|
15
|
+
'Update machines with the specific provider.') do |provider|
|
16
|
+
options[:provider] = provider.to_sym
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
argv = parse_options(opts)
|
21
|
+
options[:provider] ||= @env.default_provider
|
22
|
+
|
23
|
+
# update /etc/hosts file for specified guest machines
|
24
|
+
with_target_vms(argv, options) do |machine|
|
25
|
+
@env.action_runner.run(Action.update_guest, {
|
26
|
+
:machine => machine,
|
27
|
+
:provider => options[:provider]
|
28
|
+
})
|
29
|
+
end
|
30
|
+
|
31
|
+
# update /etc/hosts file for host
|
32
|
+
@env.action_runner.run(Action.update_host, {
|
33
|
+
:global_env => @env,
|
34
|
+
:provider => options[:provider]
|
35
|
+
})
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module MultiHostManager
|
3
|
+
class Config < Vagrant.plugin('2', :config)
|
4
|
+
attr_accessor :enabled
|
5
|
+
attr_accessor :manage_host
|
6
|
+
attr_accessor :ignore_private_ip
|
7
|
+
attr_accessor :aliases
|
8
|
+
attr_accessor :include_offline
|
9
|
+
attr_accessor :ip_resolver
|
10
|
+
|
11
|
+
alias_method :enabled?, :enabled
|
12
|
+
alias_method :include_offline?, :include_offline
|
13
|
+
alias_method :manage_host?, :manage_host
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@enabled = false
|
17
|
+
@manage_host = UNSET_VALUE
|
18
|
+
@ignore_private_ip = UNSET_VALUE
|
19
|
+
@include_offline = UNSET_VALUE
|
20
|
+
@aliases = []
|
21
|
+
@aliases = Array.new
|
22
|
+
@include_offline = false
|
23
|
+
@ip_resolver = nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def finalize!
|
27
|
+
@manage_host = false if @manage_host == UNSET_VALUE
|
28
|
+
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
|
29
|
+
@include_offline = false if @include_offline == UNSET_VALUE
|
30
|
+
@aliases = [ @aliases ].flatten
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate(machine)
|
34
|
+
errors = []
|
35
|
+
|
36
|
+
errors << validate_bool('multihostmanager.enabled', @enabled)
|
37
|
+
errors << validate_bool('multihostmanager.manage_host', @manage_host)
|
38
|
+
errors << validate_bool('multihostmanager.ignore_private_ip', @ignore_private_ip)
|
39
|
+
errors << validate_bool('multihostmanager.include_offline', @include_offline)
|
40
|
+
errors.compact!
|
41
|
+
|
42
|
+
# check if aliases option is an Array
|
43
|
+
if !machine.config.multihostmanager.aliases.kind_of?(Array) &&
|
44
|
+
!machine.config.multihostmanager.aliases.kind_of?(String)
|
45
|
+
errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
|
46
|
+
:config_key => 'multihostmanager.aliases',
|
47
|
+
:is_class => aliases.class.to_s,
|
48
|
+
})
|
49
|
+
end
|
50
|
+
|
51
|
+
if !machine.config.multihostmanager.ip_resolver.nil? &&
|
52
|
+
!machine.config.multihostmanager.ip_resolver.kind_of?(Proc)
|
53
|
+
errors << I18n.t('vagrant_hostmanager.config.not_a_proc', {
|
54
|
+
:config_key => 'multihostmanager.ip_resolver',
|
55
|
+
:is_class => ip_resolver.class.to_s,
|
56
|
+
})
|
57
|
+
end
|
58
|
+
|
59
|
+
errors.compact!
|
60
|
+
{ "MultiHostManager configuration" => errors }
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def validate_bool(key, value)
|
66
|
+
if ![TrueClass, FalseClass].include?(value.class) &&
|
67
|
+
value != UNSET_VALUE
|
68
|
+
I18n.t('vagrant_hostmanager.config.not_a_bool', {
|
69
|
+
:config_key => key,
|
70
|
+
:value => value.class.to_s
|
71
|
+
})
|
72
|
+
else
|
73
|
+
nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module MultiHostManager
|
5
|
+
module HostsFile
|
6
|
+
def update_guest(machine)
|
7
|
+
return unless machine.communicate.ready?
|
8
|
+
|
9
|
+
if (machine.communicate.test("uname -s | grep SunOS"))
|
10
|
+
realhostfile = '/etc/inet/hosts'
|
11
|
+
move_cmd = 'mv'
|
12
|
+
elsif (machine.communicate.test("test -d $Env:SystemRoot"))
|
13
|
+
realhostfile = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
|
14
|
+
move_cmd = 'mv -force'
|
15
|
+
else
|
16
|
+
realhostfile = '/etc/hosts'
|
17
|
+
move_cmd = 'mv'
|
18
|
+
end
|
19
|
+
# download and modify file with Vagrant-managed entries
|
20
|
+
file = @global_env.tmp_path.join("hosts.#{machine.name}")
|
21
|
+
machine.communicate.download(realhostfile, file)
|
22
|
+
update_file(file)
|
23
|
+
|
24
|
+
# upload modified file and remove temporary file
|
25
|
+
machine.communicate.upload(file, '/tmp/hosts')
|
26
|
+
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
|
27
|
+
# i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
|
28
|
+
begin
|
29
|
+
FileUtils.rm(file)
|
30
|
+
rescue Exception => e
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def update_host
|
35
|
+
# copy and modify hosts file on host with Vagrant-managed entries
|
36
|
+
file = @global_env.tmp_path.join('hosts.local')
|
37
|
+
|
38
|
+
if WindowsSupport.windows?
|
39
|
+
# lazily include windows Module
|
40
|
+
class << self
|
41
|
+
include WindowsSupport unless include? WindowsSupport
|
42
|
+
end
|
43
|
+
|
44
|
+
hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
|
45
|
+
copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
|
46
|
+
else
|
47
|
+
hosts_location = '/etc/hosts'
|
48
|
+
copy_proc = Proc.new { `sudo cp #{file} #{hosts_location}` }
|
49
|
+
end
|
50
|
+
|
51
|
+
FileUtils.cp(hosts_location, file)
|
52
|
+
update_file(file)
|
53
|
+
copy_proc.call
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def update_file(file)
|
59
|
+
# build array of host file entries from Vagrant configuration
|
60
|
+
entries = []
|
61
|
+
@global_env.config_global.multihostmanager.aliases.each do |a|
|
62
|
+
ip = a[:ip]
|
63
|
+
aliases = a[:hosts].join(' ').chomp
|
64
|
+
entries << "#{ip}\t#{aliases}\t# VAGRANT\n"
|
65
|
+
end
|
66
|
+
|
67
|
+
tmp_file = Tempfile.open('multihostmanager', @global_env.tmp_path, 'a')
|
68
|
+
begin
|
69
|
+
# copy each line not managed by Vagrant
|
70
|
+
File.open(file).each_line do |line|
|
71
|
+
next if line =~ /# VAGRANT/
|
72
|
+
tmp_file << line
|
73
|
+
end
|
74
|
+
|
75
|
+
# write a line for each Vagrant-managed entry
|
76
|
+
entries.each { |entry| tmp_file << entry }
|
77
|
+
ensure
|
78
|
+
tmp_file.close
|
79
|
+
FileUtils.cp(tmp_file, file)
|
80
|
+
tmp_file.unlink
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def get_ip_address(machine)
|
85
|
+
custom_ip_resolver = machine.config.multihostmanager.ip_resolver
|
86
|
+
if custom_ip_resolver
|
87
|
+
custom_ip_resolver.call(machine)
|
88
|
+
else
|
89
|
+
ip = nil
|
90
|
+
if machine.config.multihostmanager.ignore_private_ip != true
|
91
|
+
machine.config.vm.networks.each do |network|
|
92
|
+
key, options = network[0], network[1]
|
93
|
+
ip = options[:ip] if key == :private_network
|
94
|
+
next if ip
|
95
|
+
end
|
96
|
+
end
|
97
|
+
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def get_machines
|
102
|
+
if @global_env.config_global.multihostmanager.include_offline?
|
103
|
+
machines = []
|
104
|
+
@global_env.machine_names.each do |name|
|
105
|
+
begin
|
106
|
+
@global_env.machine(name, @provider)
|
107
|
+
machines << [name, @provider]
|
108
|
+
rescue Vagrant::Errors::MachineNotFound
|
109
|
+
end
|
110
|
+
end
|
111
|
+
machines
|
112
|
+
else
|
113
|
+
@global_env.active_machines
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
## Windows support for copying files, requesting elevated privileges if necessary
|
118
|
+
module WindowsSupport
|
119
|
+
require 'rbconfig'
|
120
|
+
|
121
|
+
def self.windows?
|
122
|
+
RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
|
123
|
+
end
|
124
|
+
|
125
|
+
require 'win32ole' if windows?
|
126
|
+
|
127
|
+
def windows_copy_file(source, dest)
|
128
|
+
begin
|
129
|
+
# First, try Ruby copy
|
130
|
+
FileUtils.cp(source, dest)
|
131
|
+
rescue Errno::EACCES
|
132
|
+
# Access denied, try with elevated privileges
|
133
|
+
windows_copy_file_elevated(source, dest)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def windows_copy_file_elevated(source, dest)
|
140
|
+
# copy command only supports backslashes as separators
|
141
|
+
source, dest = [source, dest].map { |s| s.to_s.gsub(/\//, '\\') }
|
142
|
+
|
143
|
+
# run 'cmd /C copy ...' with elevated privilege, minimized
|
144
|
+
copy_cmd = "copy \"#{source}\" \"#{dest}\""
|
145
|
+
WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{copy_cmd}", nil, 'runas', 7)
|
146
|
+
|
147
|
+
# Unfortunately, ShellExecute does not give us a status code,
|
148
|
+
# and it is non-blocking so we can't reliably compare the file contents
|
149
|
+
# to see if they were copied.
|
150
|
+
#
|
151
|
+
# If the user rejects the UAC prompt, vagrant will silently continue
|
152
|
+
# without updating the hostsfile.
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'vagrant-multihostmanager/action'
|
2
|
+
|
3
|
+
module VagrantPlugins
|
4
|
+
module MultiHostManager
|
5
|
+
class Plugin < Vagrant.plugin('2')
|
6
|
+
name 'MultiHostManager'
|
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 multihostmanager provisioner to update the hosts file.
|
12
|
+
DESC
|
13
|
+
|
14
|
+
config(:multihostmanager) do
|
15
|
+
require_relative 'config'
|
16
|
+
Config
|
17
|
+
end
|
18
|
+
|
19
|
+
action_hook(:multihostmanager, :machine_action_up) do |hook|
|
20
|
+
hook.prepend(Action.update_all)
|
21
|
+
end
|
22
|
+
|
23
|
+
action_hook(:multihostmanager, :machine_action_destroy) do |hook|
|
24
|
+
hook.prepend(Action.update_all)
|
25
|
+
end
|
26
|
+
|
27
|
+
provisioner(:multihostmanager) do
|
28
|
+
require_relative 'provisioner'
|
29
|
+
Provisioner
|
30
|
+
end
|
31
|
+
|
32
|
+
command(:multihostmanager) do
|
33
|
+
require_relative 'command'
|
34
|
+
Command
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module VagrantPlugins
|
2
|
+
module MultiHostManager
|
3
|
+
class Provisioner < Vagrant.plugin('2', :provisioner)
|
4
|
+
include HostsFile
|
5
|
+
|
6
|
+
def initialize(machine, config)
|
7
|
+
super(machine, config)
|
8
|
+
@global_env = machine.env
|
9
|
+
@provider = machine.provider_name
|
10
|
+
end
|
11
|
+
|
12
|
+
def provision
|
13
|
+
update_guest(@machine)
|
14
|
+
if @global_env.config_global.multihostmanager.manage_host?
|
15
|
+
update_host
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
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,23 @@
|
|
1
|
+
# -*- mode: ruby -*-
|
2
|
+
# vi: set ft=ruby :
|
3
|
+
|
4
|
+
Vagrant.require_plugin('vagrant-multihostmanager')
|
5
|
+
|
6
|
+
Vagrant.configure('2') do |config|
|
7
|
+
config.vm.box = 'precise64'
|
8
|
+
config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box'
|
9
|
+
|
10
|
+
config.hostmanager.enabled = true
|
11
|
+
config.hostmanager.manage_host = true
|
12
|
+
|
13
|
+
config.vm.define :server1 do |server|
|
14
|
+
server.vm.hostname = 'fry'
|
15
|
+
server.vm.network :private_network, :ip => '10.0.5.2'
|
16
|
+
server.hostmanager.aliases = %w(test-alias)
|
17
|
+
end
|
18
|
+
|
19
|
+
config.vm.define :server2 do |server|
|
20
|
+
server.vm.hostname = 'bender'
|
21
|
+
server.vm.network :private_network, :ip => '10.0.5.3'
|
22
|
+
end
|
23
|
+
end
|
data/test/test.sh
ADDED
@@ -0,0 +1,24 @@
|
|
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-multihostmanager/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = 'vagrant-multihostmanager'
|
9
|
+
gem.version = VagrantPlugins::MultiHostManager::VERSION
|
10
|
+
gem.authors = ['Shawn Dahlen']
|
11
|
+
gem.email = ['shawn@dahlen.me']
|
12
|
+
gem.description = %q{A Vagrant plugin that manages the /etc/hosts file within a multi-machine environment}
|
13
|
+
gem.summary = gem.description
|
14
|
+
gem.license = 'MIT'
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_development_dependency 'bundler', '~> 1.3'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
# gem.add_dependency 'pry'
|
23
|
+
# gem.add_dependency 'pry-nav'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vagrant-multihostmanager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shawn Dahlen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A Vagrant plugin that manages the /etc/hosts file within a multi-machine
|
42
|
+
environment
|
43
|
+
email:
|
44
|
+
- shawn@dahlen.me
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/vagrant-multihostmanager.rb
|
55
|
+
- lib/vagrant-multihostmanager/action.rb
|
56
|
+
- lib/vagrant-multihostmanager/action/update_all.rb
|
57
|
+
- lib/vagrant-multihostmanager/action/update_guest.rb
|
58
|
+
- lib/vagrant-multihostmanager/action/update_host.rb
|
59
|
+
- lib/vagrant-multihostmanager/command.rb
|
60
|
+
- lib/vagrant-multihostmanager/config.rb
|
61
|
+
- lib/vagrant-multihostmanager/errors.rb
|
62
|
+
- lib/vagrant-multihostmanager/hosts_file.rb
|
63
|
+
- lib/vagrant-multihostmanager/plugin.rb
|
64
|
+
- lib/vagrant-multihostmanager/provisioner.rb
|
65
|
+
- lib/vagrant-multihostmanager/version.rb
|
66
|
+
- locales/en.yml
|
67
|
+
- test/Vagrantfile
|
68
|
+
- test/test.sh
|
69
|
+
- vagrant-multihostmanager.gemspec
|
70
|
+
homepage:
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.0.14
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: A Vagrant plugin that manages the /etc/hosts file within a multi-machine
|
94
|
+
environment
|
95
|
+
test_files:
|
96
|
+
- test/Vagrantfile
|
97
|
+
- test/test.sh
|