vagrant-hostmanager 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -11,19 +11,31 @@ The current implementation is a proof-of-concept supporting the larger
11
11
  objective of using Vagrant as a cloud management interface for development
12
12
  and production environments.
13
13
 
14
- The plugin has been tested with Vagrant 1.1.4.
14
+ The plugin has been tested with Vagrant 1.1.5.
15
15
 
16
16
  Installation
17
17
  ------------
18
18
  Install the plugin following the typical Vagrant 1.1 procedure:
19
19
 
20
- vagrant plugin install vagrant-hostmanager
20
+ $ vagrant plugin install vagrant-hostmanager
21
21
 
22
22
  Usage
23
23
  -----
24
- The plugin hooks into the `vagrant up` and `vagrant destroy` commands
25
- automatically updating the `/etc/hosts` file on each active machine that
26
- is using the same provider.
24
+ To update the `/etc/hosts` file on each active machine, run the following
25
+ command:
26
+
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:
33
+
34
+ ```ruby
35
+ Vagrant.configure('2') do |config|
36
+ config.hostmanager.auto_update = true
37
+ end
38
+ ```
27
39
 
28
40
  A machine's IP address is defined by either the static IP for a private
29
41
  network configuration or by the SSH host configuration.
@@ -1,66 +1,31 @@
1
+ require 'vagrant-hostmanager/hosts_file'
2
+
1
3
  module VagrantPlugins
2
4
  module HostManager
3
5
  module Action
4
6
  class UpdateHostsFile
7
+ include HostsFile
8
+
5
9
  def initialize(app, env)
6
- @app, @env = app, env
10
+ @app = app
11
+ @machine = env[:machine]
7
12
  @translator = Helpers::Translator.new('action.update_hosts_file')
8
- @logger =
13
+ @logger =
9
14
  Log4r::Logger.new('vagrant_hostmanager::action::update')
10
15
  end
11
16
 
12
17
  def call(env)
13
- global_env = env[:machine].env
14
- current_provider = env[:machine].provider_name
15
-
16
- # build a list of host entries based on active machines that
17
- # are using the same provider as the current one
18
- matching_machines = []
19
- entries = {}
20
- entries['127.0.0.1'] = 'localhost'
21
- global_env.active_machines.each do |name, provider|
22
- if provider == current_provider
23
- machine = global_env.machine(name, provider)
24
- host = machine.config.vm.hostname || name
25
- entries[get_ip_address(machine)] = host
26
- matching_machines << machine
27
- end
28
- end
29
-
30
- # generate hosts file
31
- path = env[:tmp_path].join('hosts')
32
- File.open(path, 'w') do |file|
33
- entries.each_pair do |ip, host|
34
- @logger.info "Adding /etc/hosts entry: #{ip} #{host}"
35
- file << "#{ip}\t#{host}\n"
36
- end
37
- end
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)
38
22
 
39
- # copy the hosts file to each matching machine
40
- # TODO append hostname to loopback address
41
- matching_machines.each do |machine|
42
- if machine.communicate.ready?
43
- env[:ui].info @translator.t('update', { :name => machine.name })
44
- machine.communicate.upload(path, '/tmp/hosts')
45
- machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
46
- end
23
+ # update /etc/hosts file on each active machine
24
+ machines.each { |machine| update(machine) }
47
25
  end
48
26
 
49
27
  @app.call(env)
50
28
  end
51
-
52
- protected
53
-
54
- def get_ip_address(machine)
55
- ip = nil
56
- machine.config.vm.networks.each do |network|
57
- key, options = network[0], network[1]
58
- ip = options[:ip] if key == :private_network
59
- next if ip
60
- end
61
-
62
- ip || machine.ssh_info[:host]
63
- end
64
29
  end
65
30
  end
66
31
  end
@@ -0,0 +1,29 @@
1
+ module VagrantPlugins
2
+ module HostManager
3
+ class Command < Vagrant.plugin('2', :command)
4
+ include HostsFile
5
+
6
+ def execute
7
+ options = {}
8
+ opts = OptionParser.new do |o|
9
+ o.banner = 'Usage: vagrant hostmanager [vm-name]'
10
+ o.separator ''
11
+
12
+ o.on('--provider provider', String,
13
+ 'Update machines with the specific provider.') do |provider|
14
+ options[:provider] = provider
15
+ end
16
+ end
17
+
18
+ argv = parse_options(opts)
19
+ options[:provider] ||= @env.default_provider
20
+
21
+ generate(@env, options[:provider])
22
+
23
+ with_target_vms(argv[1..-1], :provider => options[:provider]) do |machine|
24
+ update(machine)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ module VagrantPlugins
2
+ module HostManager
3
+ class Config < Vagrant.plugin('2', :config)
4
+ attr_accessor :auto_update
5
+
6
+ def initialize
7
+ @auto_update = false
8
+ end
9
+
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 }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,57 @@
1
+ module VagrantPlugins
2
+ module HostManager
3
+ module HostsFile
4
+ # Generate a hosts file containing the the active machines
5
+ # in the Vagrant environment backed by the specified provider.
6
+ # The file is written to the Vagrant temporary path.
7
+ def generate(env, provider)
8
+ machines = []
9
+
10
+ # define a lambda for looking up a machine's ip address
11
+ get_ip_address = lambda do |machine|
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
17
+ end
18
+ ip || machine.ssh_info[:host]
19
+ end
20
+
21
+ # create the temporary hosts file
22
+ path = env.tmp_path.join('hosts')
23
+ File.open(path, 'w') do |file|
24
+ file << "127.0.0.1\tlocalhost\n"
25
+
26
+ # add a hosts entry for each active machine matching the provider
27
+ env.active_machines.each do |name, p|
28
+ if provider == p
29
+ machines << machine = env.machine(name, provider)
30
+ host = machine.config.vm.hostname || name
31
+ ip = get_ip_address.call(machine)
32
+ @logger.info "Adding /etc/hosts entry: #{ip} #{host}"
33
+ file << "#{ip}\t#{host}\n"
34
+ end
35
+ end
36
+ end
37
+
38
+ machines
39
+ end
40
+
41
+ # Copy the temporary hosts file to the specified machine overwritting
42
+ # the existing /etc/hosts file.
43
+ def update(machine)
44
+ path = machine.env.tmp_path.join('hosts')
45
+ if machine.communicate.ready?
46
+ machine.env.ui.info translator.t('update', { :name => machine.name })
47
+ machine.communicate.upload(path, '/tmp/hosts')
48
+ machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
49
+ end
50
+ end
51
+
52
+ def translator
53
+ Helpers::Translator.new('hosts_file')
54
+ end
55
+ end
56
+ end
57
+ end
@@ -10,16 +10,26 @@ module VagrantPlugins
10
10
  created for each active machine using the hostname attribute.
11
11
  DESC
12
12
 
13
- action_hook(:hostmanager_up, :machine_action_up) do |hook|
13
+ def self.update(hook)
14
14
  setup_i18n
15
15
  setup_logging
16
16
  hook.append(Action::UpdateHostsFile)
17
17
  end
18
18
 
19
- action_hook(:hostmanger_destroy, :machine_action_destroy) do |hook|
19
+ config(:hostmanager) do
20
+ require_relative 'config'
21
+ Config
22
+ end
23
+
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
20
29
  setup_i18n
21
30
  setup_logging
22
- hook.append(Action::UpdateHostsFile)
31
+ require_relative 'command'
32
+ Command
23
33
  end
24
34
 
25
35
  def self.setup_i18n
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module HostManager
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -1,5 +1,4 @@
1
1
  en:
2
2
  vagrant_hostmanager:
3
- action:
4
- update_hosts_file:
5
- update: "Updating /etc/hosts file on %{name}"
3
+ hosts_file:
4
+ update: "[%{name}] Updating /etc/hosts file"
@@ -6,12 +6,13 @@ vagrant ssh server1 -c 'cat /etc/hosts'
6
6
  echo "[server2] /etc/hosts file:"
7
7
  vagrant ssh server2 -c 'cat /etc/hosts'
8
8
 
9
- vagrant halt
10
- vagrant up
9
+ vagrant hostmanager
11
10
 
12
- vagrant destroy server1 -f
11
+ echo "[server1] /etc/hosts file:"
12
+ vagrant ssh server1 -c 'cat /etc/hosts'
13
13
  echo "[server2] /etc/hosts file:"
14
14
  vagrant ssh server2 -c 'cat /etc/hosts'
15
- vagrant destroy server2 -f
15
+
16
+ vagrant destroy -f
16
17
 
17
18
  cd ..
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.3
4
+ version: 0.0.4
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-04 00:00:00.000000000 Z
12
+ date: 2013-04-06 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
@@ -26,8 +26,11 @@ files:
26
26
  - Rakefile
27
27
  - lib/vagrant-hostmanager.rb
28
28
  - lib/vagrant-hostmanager/action/update_hosts_file.rb
29
+ - lib/vagrant-hostmanager/command.rb
30
+ - lib/vagrant-hostmanager/config.rb
29
31
  - lib/vagrant-hostmanager/errors.rb
30
32
  - lib/vagrant-hostmanager/helpers/translator.rb
33
+ - lib/vagrant-hostmanager/hosts_file.rb
31
34
  - lib/vagrant-hostmanager/plugin.rb
32
35
  - lib/vagrant-hostmanager/version.rb
33
36
  - locales/en.yml
@@ -62,3 +65,4 @@ summary: A Vagrant plugin that manages the /etc/hosts file within a multi-machin
62
65
  test_files:
63
66
  - test/Vagrantfile
64
67
  - test/test.sh
68
+ has_rdoc: