vagrant-hostmanager 0.0.4 → 0.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/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
- To update the `/etc/hosts` file on each active machine, run the following
25
- command:
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
- $ vagrant hostmanager
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
- ```ruby
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.
@@ -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
- @translator = Helpers::Translator.new('action.update_hosts_file')
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 config to see if the hosts file should updated automatically
19
- if @machine.config.hostmanager.auto_update
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
- # update /etc/hosts file on each active machine
24
- machines.each { |machine| update(machine) }
25
- end
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[1..-1], :provider => options[:provider]) do |machine|
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 = false
8
+ @auto_update = UNSET_VALUE
9
+ @ignore_private_ip = UNSET_VALUE
8
10
  end
9
11
 
10
- def validate(machine)
11
- errors = []
12
- if !(!!@auto_update == @auto_update)
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.vm.networks.each do |network|
14
- key, options = network[0], network[1]
15
- ip = options[:ip] if key == :private_network
16
- next if ip
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 translator.t('update', { :name => machine.name })
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. A entry is
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(:hostmanager_up, :machine_action_up, &method(:update))
25
- action_hook(:hostmanger_destroy, :machine_action_destroy, &method(:update))
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
- def self.setup_i18n
36
- I18n.load_path << File.expand_path(
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
- def self.setup_logging
45
- level = nil
46
- begin
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
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module HostManager
3
- VERSION = '0.0.4'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
1
  en:
2
2
  vagrant_hostmanager:
3
- hosts_file:
3
+ action:
4
4
  update: "[%{name}] Updating /etc/hosts file"
@@ -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'
@@ -1,10 +1,6 @@
1
1
  cd test
2
2
 
3
3
  vagrant up
4
- echo "[server1] /etc/hosts file:"
5
- vagrant ssh server1 -c 'cat /etc/hosts'
6
- echo "[server2] /etc/hosts file:"
7
- vagrant ssh server2 -c 'cat /etc/hosts'
8
4
 
9
5
  vagrant hostmanager
10
6
 
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
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-06 00:00:00.000000000 Z
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