vagrant-hostmaster 0.7.0 → 0.8.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
@@ -1,6 +1,6 @@
1
1
  # vagrant-hostmaster
2
2
 
3
- vagrant-hostmaster is a Vagrant plugin to manage /etc/hosts entries on both the host OS and guest VMs.
3
+ `vagrant-hostmaster` is a Vagrant plugin to manage /etc/hosts entries on both the host OS and guest VMs.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,6 +18,74 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ vagrant-hostmaster will automatically add/update /etc/hosts entries when you run `vagrant up`
22
+ (or `vagrant provision`).
23
+
24
+ The host entries will automatically be removed when you run `vagrant destroy`.
25
+
26
+ ### Configuration
27
+
28
+ By default, the name specified in the `vm.host_name` configuration option will be associated
29
+ with the address of the first network interface.
30
+
31
+ For example:
32
+
33
+ Vagrant::Config.run do |config|
34
+ ...
35
+
36
+ config.vm.host_name = "www.example.com"
37
+
38
+ config.vm.network :hostonly, "33.33.33.60"
39
+ end
40
+
41
+ This would result in the following hosts entry:
42
+
43
+ 33.33.33.60 www.example.com # VAGRANT: ...
44
+
45
+ ### Using a different host name
46
+
47
+ To use a different host name, specify it in your Vagrantfile using the `hosts.name` configuration
48
+ option.
49
+
50
+ For example:
51
+
52
+ Vagrant::Config.run do |config|
53
+ ...
54
+
55
+ config.vm.host_name = "www.example.com"
56
+ config.hosts.name = "example.com"
57
+
58
+ config.vm.network :hostonly, "33.33.33.60"
59
+ end
60
+
61
+ This would result in the following hosts entry:
62
+
63
+ 33.33.33.60 example.com # VAGRANT: ...
64
+
65
+ ### Host aliases
66
+
67
+ In addition, the `hosts.aliases` configuration option can be used to provide aliases for your host names.
68
+
69
+ For example:
70
+
71
+ Vagrant::Config.run do |config|
72
+ ...
73
+
74
+ config.vm.host_name = "www.example.com"
75
+ config.hosts.aliases = %w(example.com)
76
+
77
+ config.vm.network :hostonly, "33.33.33.60"
78
+ end
79
+
80
+ This would result in the following hosts entry:
81
+
82
+ 33.33.33.60 www.example.com example.com # VAGRANT: ...
83
+
84
+ ## Command Line
85
+
86
+ In addition to automatically updating the hosts file, the `hosts` command supports manual
87
+ modification (or verification) of the hosts entries.
88
+
21
89
  Usage: vagrant hosts <command> [<args>]
22
90
 
23
91
  Available subcommands:
@@ -29,21 +97,15 @@ Or install it yourself as:
29
97
 
30
98
  ### List Host Entries
31
99
 
32
- vagrant hosts list
100
+ vagrant hosts list [vm-name]
33
101
 
34
102
  ### Remove Host Entries
35
103
 
36
- vagrant hosts remove [<vm-name> [...]]
104
+ vagrant hosts remove [vm-name]
37
105
 
38
106
  ### Update Host Entries
39
107
 
40
- vagrant hosts update [<vm-name> [...]]
41
-
42
- ## TODO
43
-
44
- 1. add commands to modify guests
45
-
46
- 2. add provisioning support so that changes are made automatically
108
+ vagrant hosts update [vm-name]
47
109
 
48
110
  ## Contributing
49
111
 
@@ -1,4 +1,5 @@
1
1
  require 'vagrant/hostmaster/version'
2
+ require 'vagrant/hostmaster/command/base'
2
3
  require 'vagrant/hostmaster/command/root'
3
4
  require 'vagrant/hostmaster/config'
4
5
  require 'vagrant/hostmaster/vm'
@@ -11,5 +12,5 @@ Vagrant.commands.register(:hosts) { Vagrant::Hostmaster::Command::Root }
11
12
 
12
13
  Vagrant.actions[:destroy].insert_after(Vagrant::Action::VM::ProvisionerCleanup, Vagrant::Hostmaster::Middleware::Remove)
13
14
 
14
- Vagrant.actions[:provision].insert(Vagrant::Action::VM::Provision, Vagrant::Hostmaster::Middleware::Update)
15
- Vagrant.actions[:start].insert(Vagrant::Action::VM::Provision, Vagrant::Hostmaster::Middleware::Update)
15
+ Vagrant.actions[:provision].insert_after(Vagrant::Action::VM::Provision, Vagrant::Hostmaster::Middleware::Update)
16
+ Vagrant.actions[:start].insert_after(Vagrant::Action::VM::Provision, Vagrant::Hostmaster::Middleware::Update)
@@ -0,0 +1,30 @@
1
+ module Vagrant
2
+ module Hostmaster
3
+ module Command
4
+ class Base < Vagrant::Command::Base
5
+ def execute
6
+ sub_command = self.class.name.split('::').last.downcase
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: vagrant hosts #{sub_command} [vm-name]"
10
+ end
11
+
12
+ # Parse the options
13
+ argv = parse_options(parser)
14
+ return if !argv
15
+
16
+ with_target_vms(argv) do |vm|
17
+ if vm.created?
18
+ Hostmaster::VM.new(vm).send sub_command.to_sym
19
+ else
20
+ vm.ui.info I18n.t("vagrant.commands.common.vm_not_created")
21
+ end
22
+ end
23
+
24
+ # Success, exit status 0
25
+ 0
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,24 +1,7 @@
1
1
  module Vagrant
2
2
  module Hostmaster
3
3
  module Command
4
- class List < Vagrant::Command::Base
5
- def execute
6
- options = OptionParser.new do |opts|
7
- opts.banner = "Usage: vagrant hosts list"
8
- end
9
-
10
- # Parse the options
11
- argv = parse_options(options)
12
- return if !argv
13
- raise Vagrant::Errors::CLIInvalidUsage, :help => opts.help.chomp if argv.length != 0
14
-
15
- with_target_vms do |vm|
16
- Hostmaster::VM.new(vm).list
17
- end
18
-
19
- # Success, exit status 0
20
- 0
21
- end
4
+ class List < Vagrant::Hostmaster::Command::Base
22
5
  end
23
6
  end
24
7
  end
@@ -1,23 +1,7 @@
1
1
  module Vagrant
2
2
  module Hostmaster
3
3
  module Command
4
- class Remove < Vagrant::Command::Base
5
- def execute
6
- options = OptionParser.new do |opts|
7
- opts.banner = "Usage: vagrant hosts remove [<vm-name> [...]]"
8
- end
9
-
10
- # Parse the options
11
- argv = parse_options(options)
12
- return if !argv
13
-
14
- with_target_vms(argv) do |vm|
15
- Hostmaster::VM.new(vm).remove
16
- end
17
-
18
- # Success, exit status 0
19
- 0
20
- end
4
+ class Remove < Vagrant::Hostmaster::Command::Base
21
5
  end
22
6
  end
23
7
  end
@@ -1,24 +1,7 @@
1
1
  module Vagrant
2
2
  module Hostmaster
3
3
  module Command
4
- class Update < Vagrant::Command::Base
5
- def execute
6
- options = OptionParser.new do |opts|
7
- opts.banner = "Usage: vagrant hosts update [<vm-name> [...]]"
8
- end
9
-
10
- # Parse the options
11
- argv = parse_options(options)
12
- return if !argv
13
-
14
- with_target_vms(argv) do |vm|
15
- @env.ui.info("Updating host entry for #{vm.name} VM. Administrator privileges will be required...", :prefix => false)
16
- Hostmaster::VM.new(vm).update
17
- end
18
-
19
- # Success, exit status 0
20
- 0
21
- end
4
+ class Update < Vagrant::Hostmaster::Command::Base
22
5
  end
23
6
  end
24
7
  end
@@ -13,7 +13,7 @@ module Vagrant
13
13
 
14
14
  protected
15
15
  def remove(vm)
16
- Hostmaster::VM.new(vm).remove
16
+ Hostmaster::VM.new(vm).remove(:guests => false)
17
17
  end
18
18
  end
19
19
  end
@@ -7,8 +7,8 @@ module Vagrant
7
7
  end
8
8
 
9
9
  def call(env)
10
- update env[:vm]
11
10
  @app.call(env)
11
+ update env[:vm]
12
12
  end
13
13
 
14
14
  protected
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module Hostmaster
3
- VERSION = "0.7.0"
3
+ VERSION = "0.8.0"
4
4
  end
5
5
  end
@@ -5,46 +5,73 @@ module Vagrant
5
5
  class VM
6
6
  extend Forwardable
7
7
 
8
- def_delegators :@vm, :config, :env, :name, :uuid
8
+ def_delegators :@vm, :channel, :config, :env, :name, :uuid
9
9
 
10
10
  def initialize(vm)
11
11
  @vm = vm
12
12
  end
13
13
 
14
14
  def add(options = {})
15
- env.ui.info("Adding host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
16
- sudo %Q(sh -c 'echo "#{host_entry}" >>/etc/hosts')
15
+ if process_local?(options)
16
+ env.ui.info("Adding host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
17
+ sudo add_command
18
+ end
19
+
20
+ with_other_vms { |vm| channel.sudo vm.add_command(uuid) } if process_guests?(options)
17
21
  end
18
22
 
19
23
  def list(options = {})
20
- system %Q(grep '#{signature}$' /etc/hosts)
24
+ if process_local?(options)
25
+ output = `#{list_command}`.chomp
26
+ env.ui.info("Local host entry for #{name}...\n#{output}\n\n", :prefix => false) unless output.empty?
27
+ end
28
+
29
+ if process_guests?(options)
30
+ entries = []
31
+ with_other_vms do |vm|
32
+ entry = ""
33
+ channel.execute(vm.list_command(uuid), :error_check => false) do |type, data|
34
+ entry << data if type == :stdout
35
+ end
36
+ entry.chomp!
37
+ entries << entry unless entry.empty?
38
+ end
39
+ env.ui.info("Guest host #{entries.size > 1 ? 'entries' : 'entry'} on #{name}...\n#{entries.join("\n")}\n\n", :prefix => false) unless entries.empty?
40
+ end
21
41
  end
22
42
 
23
43
  def remove(options = {})
24
- env.ui.info("Removing host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
25
- sudo %Q(sed -e '/#{signature}$/ d' -ibak /etc/hosts)
44
+ if process_local?(options)
45
+ env.ui.info("Removing host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
46
+ sudo remove_command
47
+ end
48
+ with_other_vms { |vm| channel.sudo vm.remove_command(uuid) } if process_guests?(options)
26
49
  end
27
50
 
28
51
  def update(options = {})
29
- env.ui.info("Updating host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
30
- remove(:quiet => true) && add(:quiet => true)
52
+ if process_local?(options)
53
+ env.ui.info("Updating host entry for #{name} VM. Administrator privileges will be required...") unless options[:quiet]
54
+ sudo(remove_command) && sudo(add_command)
55
+ end
56
+ with_other_vms { |vm| channel.sudo(vm.remove_command(uuid)) && channel.sudo(vm.add_command(uuid)) } if process_guests?(options)
31
57
  end
32
58
 
33
59
  protected
34
- def address
35
- @address ||= (addresses && addresses.first || '127.0.0.1')
60
+ def add_command(uuid = self.uuid)
61
+ %Q(sh -c 'echo "#{host_entry(uuid)}" >>/etc/hosts')
36
62
  end
37
63
 
38
- def addresses
39
- @network_addresses ||= (network && network.last)
64
+ def address
65
+ # network parameters consist of an address and a hash of options
66
+ @address ||= (network_parameters && network_parameters.first)
40
67
  end
41
68
 
42
69
  def host_aliases
43
70
  @host_aliases ||= Array(config.hosts.aliases)
44
71
  end
45
72
 
46
- def host_entry
47
- @host_entry ||= "#{address} #{host_names.join(' ')} #{signature}"
73
+ def host_entry(uuid = self.uuid)
74
+ %Q(#{address} #{host_names.join(' ')} #{signature(uuid)})
48
75
  end
49
76
 
50
77
  def host_name
@@ -55,16 +82,48 @@ module Vagrant
55
82
  @host_names ||= (Array(host_name) + host_aliases)
56
83
  end
57
84
 
85
+ def process_guests?(options = {})
86
+ {:guests => true}.merge(options)[:guests]
87
+ end
88
+
89
+ def process_local?(options = {})
90
+ {:local => true}.merge(options)[:local]
91
+ end
92
+
93
+ def list_command(uuid = self.uuid)
94
+ %Q(grep '#{signature(uuid)}$' /etc/hosts)
95
+ end
96
+
58
97
  def network
59
- @network ||= config.vm.networks.first
98
+ # hostonly networks are the only ones we're interested in
99
+ @network ||= networks.find { |type,network_parameters| type == :hostonly }
60
100
  end
61
101
 
62
- def signature
63
- @signature ||= "# VAGRANT: #{uuid}"
102
+ def network_parameters
103
+ # network is a pair of a network type and the network parameters
104
+ @network_parameters ||= (network && network.last)
105
+ end
106
+
107
+ def networks
108
+ @networks ||= config.vm.networks
109
+ end
110
+
111
+ def remove_command(uuid = self.uuid)
112
+ %Q(sed -e '/#{signature(uuid)}$/ d' -ibak /etc/hosts)
113
+ end
114
+
115
+ def signature(uuid = self.uuid)
116
+ %Q(# VAGRANT: #{uuid} (#{name}))
64
117
  end
65
118
 
66
119
  def sudo(command)
67
- system %Q(sudo #{command})
120
+ `sudo #{command}`
121
+ end
122
+
123
+ def with_other_vms
124
+ env.vms.each do |name,vm|
125
+ yield Hostmaster::VM.new(vm) if vm.config.vm.networks.any? { |type,network_parameters| type == :hostonly } && vm.name != self.name
126
+ end
68
127
  end
69
128
  end
70
129
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 7
7
+ - 8
8
8
  - 0
9
- version: 0.7.0
9
+ version: 0.8.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - S. Brent Faulkner
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-08-18 00:00:00 -04:00
17
+ date: 2012-08-19 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - README.md
60
60
  - Rakefile
61
61
  - lib/vagrant/hostmaster.rb
62
+ - lib/vagrant/hostmaster/command/base.rb
62
63
  - lib/vagrant/hostmaster/command/list.rb
63
64
  - lib/vagrant/hostmaster/command/remove.rb
64
65
  - lib/vagrant/hostmaster/command/root.rb