vagrant-hostmanager 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ # Changelog
2
+
3
+ ## 1.4.0
4
+ ### Features
5
+ * supports vagrant 1.5 [[#80](https://github.com/smdahlen/vagrant-hostmanager/issues/80), [#81](https://github.com/smdahlen/vagrant-hostmanager/pull/81)]
6
+ * only updates hosts file if contents have changed [[#78](https://github.com/smdahlen/vagrant-hostmanager/pull/78)]
7
+ * custom ip resolver now has access to the machine whose hosts file is being updated [[#62](https://github.com/smdahlen/vagrant-hostmanager/pull/62)]
8
+
9
+ ### Bug fixes
10
+ * custom IP resolver result no longer ignored [[#57](https://github.com/smdahlen/vagrant-hostmanager/pull/57)]
11
+ * when multiple private_networks are configured, the first one is used [[#64](https://github.com/smdahlen/vagrant-hostmanager/pull/64)]
12
+ * destroyed machines are now removed from hosts file [[#52](https://github.com/smdahlen/vagrant-hostmanager/pull/52)]
13
+
14
+ [Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.3.0...v1.4.0)
15
+
16
+
17
+ ## 1.3.0
18
+ ### Features
19
+ * allow defining a custom IP resolver block [[#15](https://github.com/smdahlen/vagrant-hostmanager/pull/15)]
20
+ * handle removing destroyed machines from hosts file (currently only works with `include_offline = true`) [[#45](https://github.com/smdahlen/vagrant-hostmanager/pull/45)]
21
+ * attempt to elevate privileges when needed in Windows hosts [[#48](https://github.com/smdahlen/vagrant-hostmanager/pull/48)]
22
+
23
+ ### Bug fixes
24
+ * `--provider` command-line option now finds machines as expected [[#46](https://github.com/smdahlen/vagrant-hostmanager/pull/46)]
25
+ * uses proper `hosts` file location in Windows under cygwin [[#49](https://github.com/smdahlen/vagrant-hostmanager/pull/49)]
26
+
27
+ ### Miscelaneous
28
+ * MIT license added to gemspec
29
+
30
+ [Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.2.3...v1.3.0)
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  group :development do
4
- gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.2.7'
4
+ gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.5.1'
5
5
  end
6
6
 
7
- gemspec
7
+ group :plugins do
8
+ gemspec
9
+ end
data/README.md CHANGED
@@ -81,10 +81,11 @@ where host name is stored in ssh_info hash of each machine.
81
81
  This causes generation of invalid /etc/hosts file.
82
82
 
83
83
  Custom IP resolver gives you oportunity to calculate IP address
84
- for each machine by yourself. For example:
84
+ for each machine by yourself, giving You also access to the machine that is
85
+ updating /etc/hosts. For example:
85
86
 
86
87
  ```ruby
87
- config.hostmanager.ip_resolver = proc do |vm|
88
+ config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
88
89
  if hostname = (vm.ssh_info && vm.ssh_info[:host])
89
90
  `host #{hostname}`.split("\n").last[/(\d+\.\d+\.\d+\.\d+)/, 1]
90
91
  end
@@ -11,6 +11,14 @@ module VagrantPlugins
11
11
  @machine = env[:machine]
12
12
  @global_env = @machine.env
13
13
  @provider = @machine.provider_name
14
+
15
+ # config_global is deprecated from v1.5
16
+ if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
17
+ @config = @global_env.vagrantfile.config
18
+ else
19
+ @config = @global_env.config_global
20
+ end
21
+
14
22
  @logger = Log4r::Logger.new('vagrant::hostmanager::update_all')
15
23
  end
16
24
 
@@ -21,7 +29,7 @@ module VagrantPlugins
21
29
  return @app.call(env) if !@machine.id && env[:machine_action] == :destroy
22
30
 
23
31
  # check config to see if the hosts file should be update automatically
24
- return @app.call(env) unless @global_env.config_global.hostmanager.enabled?
32
+ return @app.call(env) unless @config.hostmanager.enabled?
25
33
  @logger.info 'Updating /etc/hosts file automatically'
26
34
 
27
35
  @app.call(env)
@@ -11,6 +11,14 @@ module VagrantPlugins
11
11
  @machine = env[:machine]
12
12
  @global_env = @machine.env
13
13
  @provider = env[:provider]
14
+
15
+ # config_global is deprecated from v1.5
16
+ if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
17
+ @config = @global_env.vagrantfile.config
18
+ else
19
+ @config = @global_env.config_global
20
+ end
21
+
14
22
  @logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
15
23
  end
16
24
 
@@ -10,11 +10,19 @@ module VagrantPlugins
10
10
  @app = app
11
11
  @global_env = env[:global_env]
12
12
  @provider = env[:provider]
13
+
14
+ # config_global is deprecated from v1.5
15
+ if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
16
+ @config = @global_env.vagrantfile.config
17
+ else
18
+ @config = @global_env.config_global
19
+ end
20
+
13
21
  @logger = Log4r::Logger.new('vagrant::hostmanager::update_host')
14
22
  end
15
23
 
16
24
  def call(env)
17
- if @global_env.config_global.hostmanager.manage_host?
25
+ if @config.hostmanager.manage_host?
18
26
  env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
19
27
  update_host
20
28
  end
@@ -19,11 +19,13 @@ module VagrantPlugins
19
19
  # download and modify file with Vagrant-managed entries
20
20
  file = @global_env.tmp_path.join("hosts.#{machine.name}")
21
21
  machine.communicate.download(realhostfile, file)
22
- update_file(file)
22
+ if update_file(file, machine, false)
23
+
24
+ # upload modified file and remove temporary file
25
+ machine.communicate.upload(file, '/tmp/hosts')
26
+ machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
27
+ end
23
28
 
24
- # upload modified file and remove temporary file
25
- machine.communicate.upload(file, '/tmp/hosts')
26
- machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
27
29
  # i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
28
30
  begin
29
31
  FileUtils.rm(file)
@@ -49,82 +51,99 @@ module VagrantPlugins
49
51
  end
50
52
 
51
53
  FileUtils.cp(hosts_location, file)
52
- update_file(file)
53
- copy_proc.call
54
+ if update_file(file)
55
+ copy_proc.call
56
+ end
54
57
  end
55
58
 
56
59
  private
57
60
 
58
- def update_file(file)
59
- # build array of host file entries from Vagrant configuration
60
- entries = []
61
- destroyed_entries = []
62
- ids = []
63
- get_machines.each do |name, p|
64
- if @provider == p
65
- machine = @global_env.machine(name, p)
66
- host = machine.config.vm.hostname || name
67
- id = machine.id
68
- ip = get_ip_address(machine)
69
- aliases = machine.config.hostmanager.aliases.join(' ').chomp
70
- if id.nil?
71
- destroyed_entries << "#{ip}\t#{host} #{aliases}"
72
- else
73
- entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n"
74
- ids << id unless ids.include?(id)
75
- end
76
- end
77
- end
61
+ def update_file(file, resolving_machine = nil, include_id = true)
62
+ file = Pathname.new(file)
63
+ old_file_content = file.read
64
+ new_file_content = update_content(old_file_content, resolving_machine, include_id)
65
+ file.open('w') { |io| io.write(new_file_content) }
66
+ old_file_content != new_file_content
67
+ end
78
68
 
79
- tmp_file = Tempfile.open('hostmanager', @global_env.tmp_path, 'a')
80
- begin
81
- # copy each line not managed by Vagrant
82
- File.open(file).each_line do |line|
83
- # Eliminate lines for machines that have been destroyed
84
- next if destroyed_entries.any? { |entry| line =~ /^#{entry}\t# VAGRANT ID: .*/ }
85
- tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ }
86
- end
69
+ def update_content(file_content, resolving_machine, include_id)
70
+ id = include_id ? " id: #{read_or_create_id}" : ""
71
+ header = "## vagrant-hostmanager-start#{id}\n"
72
+ footer = "## vagrant-hostmanager-end\n"
73
+ body = get_machines
74
+ .map { |machine| get_hosts_file_entry(machine, resolving_machine) }
75
+ .join
76
+ get_new_content(header, footer, body, file_content)
77
+ end
87
78
 
88
- # write a line for each Vagrant-managed entry
89
- entries.each { |entry| tmp_file << entry }
90
- ensure
91
- tmp_file.close
92
- FileUtils.cp(tmp_file, file)
93
- tmp_file.unlink
94
- end
79
+ def get_hosts_file_entry(machine, resolving_machine)
80
+ ip = get_ip_address(machine, resolving_machine)
81
+ host = machine.config.vm.hostname || machine.name
82
+ aliases = machine.config.hostmanager.aliases.join(' ').chomp
83
+ "#{ip}\t#{host} #{aliases}\n"
95
84
  end
96
85
 
97
- def get_ip_address(machine)
86
+ def get_ip_address(machine, resolving_machine)
98
87
  custom_ip_resolver = machine.config.hostmanager.ip_resolver
99
88
  if custom_ip_resolver
100
- custom_ip_resolver.call(machine)
89
+ custom_ip_resolver.call(machine, resolving_machine)
101
90
  else
102
91
  ip = nil
103
92
  if machine.config.hostmanager.ignore_private_ip != true
104
93
  machine.config.vm.networks.each do |network|
105
94
  key, options = network[0], network[1]
106
95
  ip = options[:ip] if key == :private_network
107
- next if ip
96
+ break if ip
108
97
  end
109
98
  end
99
+ ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
110
100
  end
111
- ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
112
101
  end
113
102
 
114
103
  def get_machines
115
- if @global_env.config_global.hostmanager.include_offline?
116
- machines = []
117
- @global_env.machine_names.each do |name|
118
- begin
119
- @global_env.machine(name, @provider)
120
- machines << [name, @provider]
121
- rescue Vagrant::Errors::MachineNotFound
104
+ if @config.hostmanager.include_offline?
105
+ machines = @global_env.machine_names
106
+ else
107
+ machines = @global_env.active_machines
108
+ .select { |name, provider| provider == @provider }
109
+ .collect { |name, provider| name }
110
+ end
111
+ # Collect only machines that exist for the current provider
112
+ machines.collect do |name|
113
+ begin
114
+ machine = @global_env.machine(name, @provider)
115
+ rescue Vagrant::Errors::MachineNotFound
116
+ # ignore
117
+ end
118
+ machine
122
119
  end
123
- end
124
- machines
120
+ .reject(&:nil?)
121
+ end
122
+
123
+ def get_new_content(header, footer, body, old_content)
124
+ if body.empty?
125
+ block = "\n"
126
+ else
127
+ block = "\n\n" + header + body + footer + "\n"
128
+ end
129
+ # Pattern for finding existing block
130
+ header_pattern = Regexp.quote(header)
131
+ footer_pattern = Regexp.quote(footer)
132
+ pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
133
+ # Replace existing block or append
134
+ old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
135
+ end
136
+
137
+ def read_or_create_id
138
+ file = Pathname.new("#{@global_env.local_data_path}/hostmanager/id")
139
+ if (file.file?)
140
+ id = file.read.strip
125
141
  else
126
- @global_env.active_machines
142
+ id = SecureRandom.uuid
143
+ file.dirname.mkpath
144
+ file.open('w') { |io| io.write(id) }
127
145
  end
146
+ id
128
147
  end
129
148
 
130
149
  ## Windows support for copying files, requesting elevated privileges if necessary
@@ -29,6 +29,12 @@ module VagrantPlugins
29
29
  Provisioner
30
30
  end
31
31
 
32
+ # Work-around for vagrant >= 1.5
33
+ # It breaks without a provisioner config, so we provide a dummy one
34
+ config(:hostmanager, :provisioner) do
35
+ ::Vagrant::Config::V2::DummyConfig.new
36
+ end
37
+
32
38
  command(:hostmanager) do
33
39
  require_relative 'command'
34
40
  Command
@@ -7,11 +7,19 @@ module VagrantPlugins
7
7
  super(machine, config)
8
8
  @global_env = machine.env
9
9
  @provider = machine.provider_name
10
+
11
+ # config_global is deprecated from v1.5
12
+ if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
13
+ @config = @global_env.vagrantfile.config
14
+ else
15
+ @config = @global_env.config_global
16
+ end
17
+
10
18
  end
11
19
 
12
20
  def provision
13
21
  update_guest(@machine)
14
- if @global_env.config_global.hostmanager.manage_host?
22
+ if @config.hostmanager.manage_host?
15
23
  update_host
16
24
  end
17
25
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module HostManager
3
- VERSION = '1.3.0'
3
+ VERSION = '1.4.0'
4
4
  end
5
5
  end
data/test/Vagrantfile CHANGED
@@ -1,11 +1,18 @@
1
1
  # -*- mode: ruby -*-
2
2
  # vi: set ft=ruby :
3
3
 
4
- Vagrant.require_plugin('vagrant-hostmanager')
4
+ if Gem::Version.new(::Vagrant::VERSION) < Gem::Version.new('1.5')
5
+ Vagrant.require_plugin('vagrant-hostmanager')
6
+ end
5
7
 
6
8
  Vagrant.configure('2') do |config|
7
- config.vm.box = 'precise64'
8
- config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box'
9
+
10
+ if ENV.key? 'VAGRANT_BOX'
11
+ config.vm.box = ENV['VAGRANT_BOX']
12
+ else
13
+ config.vm.box = 'precise64'
14
+ config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box'
15
+ end
9
16
 
10
17
  config.hostmanager.enabled = true
11
18
  config.hostmanager.manage_host = true
@@ -20,4 +27,10 @@ Vagrant.configure('2') do |config|
20
27
  server.vm.hostname = 'bender'
21
28
  server.vm.network :private_network, :ip => '10.0.5.3'
22
29
  end
23
- end
30
+
31
+ config.vm.define :server3 do |server|
32
+ server.vm.hostname = 'leena'
33
+ server.vm.network :private_network, :ip => '10.0.5.4'
34
+ server.vm.provision :hostmanager
35
+ end
36
+ end
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: 1.3.0
4
+ version: 1.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-12-02 00:00:00.000000000 Z
12
+ date: 2014-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -52,6 +52,7 @@ extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
54
  - .gitignore
55
+ - CHANGELOG.md
55
56
  - Gemfile
56
57
  - LICENSE.txt
57
58
  - README.md