vagrant-hostmanager 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -40,6 +40,10 @@ to true.
40
40
  A machine's host name is defined by `config.vm.hostname`. If this is not
41
41
  set, it falls back to the symbol defining the machine in the Vagrantfile.
42
42
 
43
+ When using include_offline set to true, only boxes that are up or have a
44
+ private ip configured will be added to the hosts file. You will receive a
45
+ warning on skipped boxes.
46
+
43
47
  In addition, the `hostmanager.aliases` configuration attribute can be used
44
48
  to provide aliases for your host names.
45
49
 
@@ -49,6 +53,7 @@ Example configuration:
49
53
  Vagrant.configure("2") do |config|
50
54
  config.hostmanager.enabled = true
51
55
  config.hostmanager.ignore_private_ip = false
56
+ config.hostmanager.include_offline = true
52
57
  config.vm.define "example-box" do |node|
53
58
  node.vm.hostname = "example-box-hostname"
54
59
  node.vm.network :private_network, ip: "192.168.42.42"
@@ -8,6 +8,8 @@ module VagrantPlugins
8
8
  opts = OptionParser.new do |o|
9
9
  o.banner = 'Usage: vagrant hostmanager [vm-name]'
10
10
  o.separator ''
11
+ o.version = VagrantPlugins::HostManager::VERSION
12
+ o.program_name = 'vagrant hostmanager'
11
13
 
12
14
  o.on('--provider provider', String,
13
15
  'Update machines with the specific provider.') do |provider|
@@ -4,40 +4,62 @@ module VagrantPlugins
4
4
  attr_accessor :enabled
5
5
  attr_accessor :ignore_private_ip
6
6
  attr_accessor :aliases
7
+ attr_accessor :include_offline
7
8
 
8
9
  alias_method :enabled?, :enabled
10
+ alias_method :include_offline?, :include_offline
9
11
 
10
12
  def initialize
11
13
  @enabled = false
12
14
  @ignore_private_ip = UNSET_VALUE
13
15
  @aliases = Array.new
16
+ @include_offline = false
14
17
  end
15
18
 
16
19
  def finalize!
17
20
  @ignore_private_ip = false if @ignore_private_ip == UNSET_VALUE
21
+ @aliases = [ @aliases ].flatten
18
22
  end
19
23
 
20
24
  def validate(machine)
21
25
  errors = Array.new
22
26
 
23
27
  # check if enabled option is either true or false
24
- if ![TrueClass, FalseClass].include?(enabled.class)
25
- errors << "A value for hostmanager.enabled can be true or false."
26
- end
28
+ errors << validate_bool('hostmanager.enabled', enabled)
29
+
30
+ # check if include_offline is either true or false
31
+ errors << validate_bool('hostmanager.include_offline', include_offline)
27
32
 
28
- # check if ignore_private_ip option is either true or false
29
- if ![TrueClass, FalseClass].include?(ignore_private_ip.class) &&
30
- @ignore_private_ip != UNSET_VALUE
31
- errors << "A value for hostmanager.ignore_private_ip can be true or false."
33
+ # check if ignore_private_ip option is either true or false (or UNSET_VALUE)
34
+ if @ignore_private_ip != UNSET_VALUE
35
+ errors << validate_bool('hostmanager.ignore_private_ip', ignore_private_ip)
32
36
  end
33
37
 
34
38
  # check if aliases option is an Array
35
- if !machine.config.hostmanager.aliases.kind_of?(Array)
36
- errors << "A value for hostmanager.aliases must be an Array."
39
+ if !machine.config.hostmanager.aliases.kind_of?(Array) and
40
+ !machine.config.hostmanager.aliases.kind_of?(String)
41
+ errors << I18n.t('vagrant_hostmanager.config.not_an_array_or_string', {
42
+ :config_key => 'hostmanager.aliases',
43
+ :is_class => aliases.class.to_s,
44
+ })
37
45
  end
38
46
 
47
+ errors.compact!
39
48
  { "HostManager configuration" => errors }
40
49
  end
50
+
51
+ private
52
+ def validate_bool(key, value)
53
+ if ![TrueClass, FalseClass].include?(value.class)
54
+ I18n.t('vagrant_hostmanager.config.not_a_bool', {
55
+ :config_key => key,
56
+ :value => value.class.to_s,
57
+ })
58
+ else
59
+ nil
60
+ end
61
+ end
62
+
41
63
  end
42
64
  end
43
65
  end
@@ -17,27 +17,34 @@ module VagrantPlugins
17
17
  next if ip
18
18
  end
19
19
  end
20
- ip || machine.ssh_info[:host]
20
+ ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
21
21
  end
22
22
 
23
23
  # create the temporary hosts file
24
24
  path = env.tmp_path.join('hosts')
25
25
  File.open(path, 'w') do |file|
26
26
  file << "127.0.0.1\tlocalhost\slocalhost.localdomain\n"
27
-
28
- # add a hosts entry for each active machine matching the provider
29
- env.active_machines.each do |name, p|
27
+ get_machines(env, provider).each do |name, p|
30
28
  if provider == p
31
29
  machines << machine = env.machine(name, provider)
32
30
  host = machine.config.vm.hostname || name
33
31
  ip = get_ip_address.call(machine)
34
- host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
35
- machine.env.ui.info "Adding /etc/hosts entry: #{ip} #{host} #{host_aliases}"
36
- file << "#{ip}\t#{host}\s#{host_aliases}\n"
32
+ if ip
33
+ host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
34
+ machine.env.ui.info I18n.t('vagrant_hostmanager.action.add_host', {
35
+ :ip => ip,
36
+ :host => host,
37
+ :aliases => host_aliases,
38
+ })
39
+ file << "#{ip}\t#{host}\s#{host_aliases}\n"
40
+ else
41
+ machine.env.ui.warn I18n.t('vagrant_hostmanager.action.host_no_ip', {
42
+ :name => name,
43
+ })
44
+ end
37
45
  end
38
46
  end
39
47
  end
40
-
41
48
  machines
42
49
  end
43
50
 
@@ -53,6 +60,30 @@ module VagrantPlugins
53
60
  machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
54
61
  end
55
62
  end
63
+
64
+ private
65
+ # Either use the active machines, or loop over all available machines and
66
+ # get those with the same provider (aka, ignore boxes that throw MachineNotFound errors).
67
+ #
68
+ # Returns an array with the same structure as env.active_machines:
69
+ # [ [:machine, :virtualbox], [:foo, :virtualbox] ]
70
+ def get_machines(env, provider)
71
+ if env.config_global.hostmanager.include_offline?
72
+ machines = []
73
+ env.machine_names.each do |name|
74
+ begin
75
+ m = env.machine(name, provider)
76
+ machines << [name, provider]
77
+ rescue Vagrant::Errors::MachineNotFound => ex
78
+ # ignore this box.
79
+ end
80
+ end
81
+ machines
82
+ else
83
+ env.active_machines
84
+ end
85
+ end
86
+
56
87
  end
57
88
  end
58
89
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module HostManager
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -1,4 +1,9 @@
1
1
  en:
2
2
  vagrant_hostmanager:
3
3
  action:
4
+ add_host: "Adding /etc/hosts entry: %{ip} %{host} %{aliases}"
5
+ host_no_ip: "Could not determine ip for machine '%{name}': no private ip configured or machine not up."
4
6
  update: "[%{name}] Updating /etc/hosts file"
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}'"
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.3.0
4
+ version: 0.4.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-05-02 00:00:00.000000000 Z
12
+ date: 2013-05-07 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