vagrant-hostmanager 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -14
- data/lib/vagrant-hostmanager.rb +3 -1
- data/lib/vagrant-hostmanager/action/update_hosts_file.rb +11 -10
- data/lib/vagrant-hostmanager/command.rb +2 -2
- data/lib/vagrant-hostmanager/config.rb +6 -8
- data/lib/vagrant-hostmanager/hosts_file.rb +9 -9
- data/lib/vagrant-hostmanager/plugin.rb +8 -48
- data/lib/vagrant-hostmanager/version.rb +1 -1
- data/locales/en.yml +1 -1
- data/test/Vagrantfile +2 -0
- data/test/test.sh +0 -4
- metadata +2 -3
- data/lib/vagrant-hostmanager/helpers/translator.rb +0 -20
data/README.md
CHANGED
@@ -21,24 +21,21 @@ Install the plugin following the typical Vagrant 1.1 procedure:
|
|
21
21
|
|
22
22
|
Usage
|
23
23
|
-----
|
24
|
-
|
25
|
-
|
24
|
+
The plugin hooks into the `vagrant up` and `vagrant destroy` commands
|
25
|
+
automatically. When a machine is created or destroyed, all active
|
26
|
+
machines with the same provider will have their `/etc/hosts` file updated
|
27
|
+
accordingly. Auto update may be disabled by setting the
|
28
|
+
`config.hostmanager.auto_update` attribute to false in the Vagrantfile.
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
The plugin may hook into the `vagrant up` and `vagrant destroy` commands
|
30
|
-
automatically to update the `/etc/hosts` file on each active machine that
|
31
|
-
is using the same provider. To enable this, add the following configuration
|
32
|
-
to your Vagrant file:
|
30
|
+
To update the `/etc/hosts` file on each active machine manually, run the
|
31
|
+
following command:
|
33
32
|
|
34
|
-
|
35
|
-
Vagrant.configure('2') do |config|
|
36
|
-
config.hostmanager.auto_update = true
|
37
|
-
end
|
38
|
-
```
|
33
|
+
$ vagrant hostmanager
|
39
34
|
|
40
35
|
A machine's IP address is defined by either the static IP for a private
|
41
|
-
network configuration or by the SSH host configuration.
|
36
|
+
network configuration or by the SSH host configuration. To disable
|
37
|
+
using the private network IP address, set `config.hostmanger.ignore_private_ip`
|
38
|
+
to true.
|
42
39
|
|
43
40
|
A machine's host name is defined by `config.vm.hostname`. If this is not
|
44
41
|
set, it falls back to the symbol defining the machine in the Vagrantfile.
|
data/lib/vagrant-hostmanager.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'vagrant'
|
2
1
|
require 'vagrant-hostmanager/plugin'
|
3
2
|
require 'vagrant-hostmanager/version'
|
4
3
|
require 'vagrant-hostmanager/errors'
|
@@ -8,5 +7,8 @@ module VagrantPlugins
|
|
8
7
|
def self.source_root
|
9
8
|
@source_root ||= Pathname.new(File.expand_path('../../', __FILE__))
|
10
9
|
end
|
10
|
+
|
11
|
+
I18n.load_path << File.expand_path('locales/en.yml', source_root)
|
12
|
+
I18n.reload!
|
11
13
|
end
|
12
14
|
end
|
@@ -9,22 +9,23 @@ module VagrantPlugins
|
|
9
9
|
def initialize(app, env)
|
10
10
|
@app = app
|
11
11
|
@machine = env[:machine]
|
12
|
-
@
|
13
|
-
@logger =
|
14
|
-
Log4r::Logger.new('vagrant_hostmanager::action::update')
|
12
|
+
@logger = Log4r::Logger.new('vagrant::hostmanager::update_hosts_file')
|
15
13
|
end
|
16
14
|
|
17
15
|
def call(env)
|
18
|
-
# check
|
19
|
-
if @machine.
|
20
|
-
# generate temporary hosts file
|
21
|
-
machines = generate(@machine.env, @machine.provider_name)
|
16
|
+
# check if machine is already active
|
17
|
+
return @app.call(env) if @machine.id
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
19
|
+
# check config to see if the hosts file should be update automatically
|
20
|
+
return @app.call(env) if !@machine.config.hostmanager.auto_update
|
21
|
+
@logger.info 'Updating /etc/hosts file automatically'
|
26
22
|
|
23
|
+
# continue the action stack so the machine will be created
|
27
24
|
@app.call(env)
|
25
|
+
|
26
|
+
# update /etc/hosts file on each active machine
|
27
|
+
machines = generate(@machine.env, @machine.provider_name)
|
28
|
+
machines.each { |machine| update(machine) }
|
28
29
|
end
|
29
30
|
end
|
30
31
|
end
|
@@ -18,9 +18,9 @@ module VagrantPlugins
|
|
18
18
|
argv = parse_options(opts)
|
19
19
|
options[:provider] ||= @env.default_provider
|
20
20
|
|
21
|
-
generate(@env, options[:provider])
|
21
|
+
generate(@env, options[:provider].to_sym)
|
22
22
|
|
23
|
-
with_target_vms(argv
|
23
|
+
with_target_vms(argv, options) do |machine|
|
24
24
|
update(machine)
|
25
25
|
end
|
26
26
|
end
|
@@ -2,18 +2,16 @@ module VagrantPlugins
|
|
2
2
|
module HostManager
|
3
3
|
class Config < Vagrant.plugin('2', :config)
|
4
4
|
attr_accessor :auto_update
|
5
|
+
attr_accessor :ignore_private_ip
|
5
6
|
|
6
7
|
def initialize
|
7
|
-
@auto_update =
|
8
|
+
@auto_update = UNSET_VALUE
|
9
|
+
@ignore_private_ip = UNSET_VALUE
|
8
10
|
end
|
9
11
|
|
10
|
-
def
|
11
|
-
|
12
|
-
if
|
13
|
-
errors << 'auto_update must be a boolean'
|
14
|
-
end
|
15
|
-
|
16
|
-
{ 'hostmanager' => errors }
|
12
|
+
def finalize!
|
13
|
+
@auto_update = true if @auto_update == UNSET_VALUE
|
14
|
+
@ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
@@ -10,10 +10,12 @@ module VagrantPlugins
|
|
10
10
|
# define a lambda for looking up a machine's ip address
|
11
11
|
get_ip_address = lambda do |machine|
|
12
12
|
ip = nil
|
13
|
-
machine.config.
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
unless machine.config.hostmanager.ignore_private_ip
|
14
|
+
machine.config.vm.networks.each do |network|
|
15
|
+
key, options = network[0], network[1]
|
16
|
+
ip = options[:ip] if key == :private_network
|
17
|
+
next if ip
|
18
|
+
end
|
17
19
|
end
|
18
20
|
ip || machine.ssh_info[:host]
|
19
21
|
end
|
@@ -43,15 +45,13 @@ module VagrantPlugins
|
|
43
45
|
def update(machine)
|
44
46
|
path = machine.env.tmp_path.join('hosts')
|
45
47
|
if machine.communicate.ready?
|
46
|
-
machine.env.ui.info
|
48
|
+
machine.env.ui.info I18n.t('vagrant_hostmanager.action.update', {
|
49
|
+
:name => machine.name
|
50
|
+
})
|
47
51
|
machine.communicate.upload(path, '/tmp/hosts')
|
48
52
|
machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
|
49
53
|
end
|
50
54
|
end
|
51
|
-
|
52
|
-
def translator
|
53
|
-
Helpers::Translator.new('hosts_file')
|
54
|
-
end
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require 'vagrant-hostmanager/helpers/translator'
|
2
1
|
require 'vagrant-hostmanager/action/update_hosts_file'
|
3
2
|
|
4
3
|
module VagrantPlugins
|
@@ -6,65 +5,26 @@ module VagrantPlugins
|
|
6
5
|
class Plugin < Vagrant.plugin('2')
|
7
6
|
name 'HostManager'
|
8
7
|
description <<-DESC
|
9
|
-
This plugin manages the /etc/hosts file for guest machines.
|
8
|
+
This plugin manages the /etc/hosts file for guest machines. An entry is
|
10
9
|
created for each active machine using the hostname attribute.
|
11
10
|
DESC
|
12
11
|
|
13
|
-
def self.update(hook)
|
14
|
-
setup_i18n
|
15
|
-
setup_logging
|
16
|
-
hook.append(Action::UpdateHostsFile)
|
17
|
-
end
|
18
|
-
|
19
12
|
config(:hostmanager) do
|
20
13
|
require_relative 'config'
|
21
14
|
Config
|
22
15
|
end
|
23
16
|
|
24
|
-
action_hook(:
|
25
|
-
|
26
|
-
|
27
|
-
# TODO remove duplication of i18n and logging setup
|
28
|
-
command(:hostmanager) do
|
29
|
-
setup_i18n
|
30
|
-
setup_logging
|
31
|
-
require_relative 'command'
|
32
|
-
Command
|
17
|
+
action_hook(:hostmanager, :machine_action_up) do |hook|
|
18
|
+
hook.prepend(Action::UpdateHostsFile)
|
33
19
|
end
|
34
20
|
|
35
|
-
|
36
|
-
|
37
|
-
'locales/en.yml',
|
38
|
-
HostManager.source_root)
|
39
|
-
I18n.reload!
|
40
|
-
|
41
|
-
Helpers::Translator.plugin_namespace = 'vagrant_hostmanager'
|
21
|
+
action_hook(:hostmanager, :machine_action_destroy) do |hook|
|
22
|
+
hook.append(Action::UpdateHostsFile)
|
42
23
|
end
|
43
24
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
|
48
|
-
rescue NameError
|
49
|
-
# This means that the logging constant wasn't found,
|
50
|
-
# which is fine. We just keep `level` as `nil`. But
|
51
|
-
# we tell the user.
|
52
|
-
level = nil
|
53
|
-
end
|
54
|
-
|
55
|
-
# Some constants, such as "true" resolve to booleans, so the
|
56
|
-
# above error checking doesn't catch it. This will check to make
|
57
|
-
# sure that the log level is an integer, as Log4r requires.
|
58
|
-
level = nil if !level.is_a?(Integer)
|
59
|
-
|
60
|
-
# Set the logging level on all "vagrant" namespaced
|
61
|
-
# logs as long as we have a valid level.
|
62
|
-
if level
|
63
|
-
logger = Log4r::Logger.new("vagrant_hostmanager")
|
64
|
-
logger.outputters = Log4r::Outputter.stderr
|
65
|
-
logger.level = level
|
66
|
-
logger = nil
|
67
|
-
end
|
25
|
+
command(:hostmanager) do
|
26
|
+
require_relative 'command'
|
27
|
+
Command
|
68
28
|
end
|
69
29
|
end
|
70
30
|
end
|
data/locales/en.yml
CHANGED
data/test/Vagrantfile
CHANGED
@@ -7,6 +7,8 @@ Vagrant.configure('2') do |config|
|
|
7
7
|
config.vm.box = 'precise64-chef11.2'
|
8
8
|
config.vm.box_url = 'https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_chef-11.2.0.box'
|
9
9
|
|
10
|
+
config.hostmanager.auto_update = false
|
11
|
+
|
10
12
|
config.vm.define :server1 do |server|
|
11
13
|
server.vm.hostname = 'fry'
|
12
14
|
server.vm.network :private_network, :ip => '10.0.5.2'
|
data/test/test.sh
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-hostmanager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A Vagrant plugin that manages the /etc/hosts file within a multi-machine
|
15
15
|
environment
|
@@ -29,7 +29,6 @@ files:
|
|
29
29
|
- lib/vagrant-hostmanager/command.rb
|
30
30
|
- lib/vagrant-hostmanager/config.rb
|
31
31
|
- lib/vagrant-hostmanager/errors.rb
|
32
|
-
- lib/vagrant-hostmanager/helpers/translator.rb
|
33
32
|
- lib/vagrant-hostmanager/hosts_file.rb
|
34
33
|
- lib/vagrant-hostmanager/plugin.rb
|
35
34
|
- lib/vagrant-hostmanager/version.rb
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module VagrantPlugins
|
2
|
-
module HostManager
|
3
|
-
module Helpers
|
4
|
-
class Translator
|
5
|
-
def self.plugin_namespace=(val)
|
6
|
-
@@plugin_namespace = val
|
7
|
-
end
|
8
|
-
|
9
|
-
def initialize(namespace)
|
10
|
-
@namespace = namespace
|
11
|
-
end
|
12
|
-
|
13
|
-
def t(keys, opts = {})
|
14
|
-
value = I18n.t("#{@@plugin_namespace}.#{@namespace}.#{keys}", opts)
|
15
|
-
opts[:progress] == false ? value : value + "..."
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|