Merge branch 'master' of github.com:smdahlen/vagrant-hostmanager into windows_guests
This commit is contained in:
commit
18d3083f07
|
@ -0,0 +1,30 @@
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
## 1.4.0
|
||||||
|
### Features
|
||||||
|
* supports vagrant 1.5 [[#80](https://github.com/smdahlen/vagrant-hostmanager/issues/80), [#81](https://github.com/smdahlen/vagrant-hostmanager/pull/81)]
|
||||||
|
* only updates hosts file if contents have changed [[#78](https://github.com/smdahlen/vagrant-hostmanager/pull/78)]
|
||||||
|
* custom ip resolver now has access to the machine whose hosts file is being updated [[#62](https://github.com/smdahlen/vagrant-hostmanager/pull/62)]
|
||||||
|
|
||||||
|
### Bug fixes
|
||||||
|
* custom IP resolver result no longer ignored [[#57](https://github.com/smdahlen/vagrant-hostmanager/pull/57)]
|
||||||
|
* when multiple private_networks are configured, the first one is used [[#64](https://github.com/smdahlen/vagrant-hostmanager/pull/64)]
|
||||||
|
* destroyed machines are now removed from hosts file [[#52](https://github.com/smdahlen/vagrant-hostmanager/pull/52)]
|
||||||
|
|
||||||
|
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.3.0...v1.4.0)
|
||||||
|
|
||||||
|
|
||||||
|
## 1.3.0
|
||||||
|
### Features
|
||||||
|
* allow defining a custom IP resolver block [[#15](https://github.com/smdahlen/vagrant-hostmanager/pull/15)]
|
||||||
|
* handle removing destroyed machines from hosts file (currently only works with `include_offline = true`) [[#45](https://github.com/smdahlen/vagrant-hostmanager/pull/45)]
|
||||||
|
* attempt to elevate privileges when needed in Windows hosts [[#48](https://github.com/smdahlen/vagrant-hostmanager/pull/48)]
|
||||||
|
|
||||||
|
### Bug fixes
|
||||||
|
* `--provider` command-line option now finds machines as expected [[#46](https://github.com/smdahlen/vagrant-hostmanager/pull/46)]
|
||||||
|
* uses proper `hosts` file location in Windows under cygwin [[#49](https://github.com/smdahlen/vagrant-hostmanager/pull/49)]
|
||||||
|
|
||||||
|
### Miscelaneous
|
||||||
|
* MIT license added to gemspec
|
||||||
|
|
||||||
|
[Full diff](https://github.com/smdahlen/vagrant-hostmanager/compare/v1.2.3...v1.3.0)
|
6
Gemfile
6
Gemfile
|
@ -1,7 +1,9 @@
|
||||||
source 'https://rubygems.org'
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.2.7'
|
gem 'vagrant', :git => 'git://github.com/mitchellh/vagrant.git', :tag => 'v1.5.1'
|
||||||
end
|
end
|
||||||
|
|
||||||
gemspec
|
group :plugins do
|
||||||
|
gemspec
|
||||||
|
end
|
||||||
|
|
|
@ -11,6 +11,14 @@ module VagrantPlugins
|
||||||
@machine = env[:machine]
|
@machine = env[:machine]
|
||||||
@global_env = @machine.env
|
@global_env = @machine.env
|
||||||
@provider = @machine.provider_name
|
@provider = @machine.provider_name
|
||||||
|
|
||||||
|
# config_global is deprecated from v1.5
|
||||||
|
if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
|
||||||
|
@config = @global_env.vagrantfile.config
|
||||||
|
else
|
||||||
|
@config = @global_env.config_global
|
||||||
|
end
|
||||||
|
|
||||||
@logger = Log4r::Logger.new('vagrant::hostmanager::update_all')
|
@logger = Log4r::Logger.new('vagrant::hostmanager::update_all')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,7 +29,7 @@ module VagrantPlugins
|
||||||
return @app.call(env) if !@machine.id && env[:machine_action] == :destroy
|
return @app.call(env) if !@machine.id && env[:machine_action] == :destroy
|
||||||
|
|
||||||
# check config to see if the hosts file should be update automatically
|
# check config to see if the hosts file should be update automatically
|
||||||
return @app.call(env) unless @global_env.config_global.hostmanager.enabled?
|
return @app.call(env) unless @config.hostmanager.enabled?
|
||||||
@logger.info 'Updating /etc/hosts file automatically'
|
@logger.info 'Updating /etc/hosts file automatically'
|
||||||
|
|
||||||
@app.call(env)
|
@app.call(env)
|
||||||
|
|
|
@ -11,6 +11,14 @@ module VagrantPlugins
|
||||||
@machine = env[:machine]
|
@machine = env[:machine]
|
||||||
@global_env = @machine.env
|
@global_env = @machine.env
|
||||||
@provider = env[:provider]
|
@provider = env[:provider]
|
||||||
|
|
||||||
|
# config_global is deprecated from v1.5
|
||||||
|
if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
|
||||||
|
@config = @global_env.vagrantfile.config
|
||||||
|
else
|
||||||
|
@config = @global_env.config_global
|
||||||
|
end
|
||||||
|
|
||||||
@logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
|
@logger = Log4r::Logger.new('vagrant::hostmanager::update_guest')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,19 @@ module VagrantPlugins
|
||||||
@app = app
|
@app = app
|
||||||
@global_env = env[:global_env]
|
@global_env = env[:global_env]
|
||||||
@provider = env[:provider]
|
@provider = env[:provider]
|
||||||
|
|
||||||
|
# config_global is deprecated from v1.5
|
||||||
|
if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
|
||||||
|
@config = @global_env.vagrantfile.config
|
||||||
|
else
|
||||||
|
@config = @global_env.config_global
|
||||||
|
end
|
||||||
|
|
||||||
@logger = Log4r::Logger.new('vagrant::hostmanager::update_host')
|
@logger = Log4r::Logger.new('vagrant::hostmanager::update_host')
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
if @global_env.config_global.hostmanager.manage_host?
|
if @config.hostmanager.manage_host?
|
||||||
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
|
env[:ui].info I18n.t('vagrant_hostmanager.action.update_host')
|
||||||
update_host
|
update_host
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,11 +25,13 @@ module VagrantPlugins
|
||||||
# download and modify file with Vagrant-managed entries
|
# download and modify file with Vagrant-managed entries
|
||||||
file = @global_env.tmp_path.join("hosts.#{machine.name}")
|
file = @global_env.tmp_path.join("hosts.#{machine.name}")
|
||||||
machine.communicate.download(realhostfile, file)
|
machine.communicate.download(realhostfile, file)
|
||||||
update_file(file, machine)
|
if update_file(file, machine, false)
|
||||||
|
|
||||||
|
# upload modified file and remove temporary file
|
||||||
|
machine.communicate.upload(file, '/tmp/hosts')
|
||||||
|
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
|
||||||
|
end
|
||||||
|
|
||||||
# upload modified file and remove temporary file
|
|
||||||
machine.communicate.upload(file, '/tmp/hosts')
|
|
||||||
machine.communicate.sudo("#{move_cmd} /tmp/hosts #{realhostfile}")
|
|
||||||
# i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
|
# i have no idea if this is a windows competibility issue or not, but sometimes it dosen't work on my machine
|
||||||
begin
|
begin
|
||||||
FileUtils.rm(file)
|
FileUtils.rm(file)
|
||||||
|
@ -55,49 +57,36 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
|
|
||||||
FileUtils.cp(hosts_location, file)
|
FileUtils.cp(hosts_location, file)
|
||||||
update_file(file)
|
if update_file(file)
|
||||||
copy_proc.call
|
copy_proc.call
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def update_file(file, resolving_machine=nil)
|
def update_file(file, resolving_machine = nil, include_id = true)
|
||||||
# build array of host file entries from Vagrant configuration
|
file = Pathname.new(file)
|
||||||
entries = []
|
old_file_content = file.read
|
||||||
destroyed_entries = []
|
new_file_content = update_content(old_file_content, resolving_machine, include_id)
|
||||||
ids = []
|
file.open('w') { |io| io.write(new_file_content) }
|
||||||
get_machines.each do |name, p|
|
old_file_content != new_file_content
|
||||||
if @provider == p
|
end
|
||||||
machine = @global_env.machine(name, p)
|
|
||||||
host = machine.config.vm.hostname || name
|
|
||||||
id = machine.id
|
|
||||||
ip = get_ip_address(machine, resolving_machine)
|
|
||||||
aliases = machine.config.hostmanager.aliases.join(' ').chomp
|
|
||||||
if id.nil?
|
|
||||||
destroyed_entries << "#{ip}\t#{host} #{aliases}"
|
|
||||||
else
|
|
||||||
entries << "#{ip}\t#{host} #{aliases}\t# VAGRANT ID: #{id}\n"
|
|
||||||
ids << id unless ids.include?(id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
tmp_file = Tempfile.open('hostmanager', @global_env.tmp_path, 'a')
|
def update_content(file_content, resolving_machine, include_id)
|
||||||
begin
|
id = include_id ? " id: #{read_or_create_id}" : ""
|
||||||
# copy each line not managed by Vagrant
|
header = "## vagrant-hostmanager-start#{id}\n"
|
||||||
File.open(file).each_line do |line|
|
footer = "## vagrant-hostmanager-end\n"
|
||||||
# Eliminate lines for machines that have been destroyed
|
body = get_machines
|
||||||
next if destroyed_entries.any? { |entry| line =~ /^#{entry}\t# VAGRANT ID: .*/ }
|
.map { |machine| get_hosts_file_entry(machine, resolving_machine) }
|
||||||
tmp_file << line unless ids.any? { |id| line =~ /# VAGRANT ID: #{id}/ }
|
.join
|
||||||
end
|
get_new_content(header, footer, body, file_content)
|
||||||
|
end
|
||||||
|
|
||||||
# write a line for each Vagrant-managed entry
|
def get_hosts_file_entry(machine, resolving_machine)
|
||||||
entries.each { |entry| tmp_file << entry }
|
ip = get_ip_address(machine, resolving_machine)
|
||||||
ensure
|
host = machine.config.vm.hostname || machine.name
|
||||||
tmp_file.close
|
aliases = machine.config.hostmanager.aliases.join(' ').chomp
|
||||||
FileUtils.cp(tmp_file, file)
|
"#{ip}\t#{host} #{aliases}\n"
|
||||||
tmp_file.unlink
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_ip_address(machine, resolving_machine)
|
def get_ip_address(machine, resolving_machine)
|
||||||
|
@ -110,27 +99,57 @@ module VagrantPlugins
|
||||||
machine.config.vm.networks.each do |network|
|
machine.config.vm.networks.each do |network|
|
||||||
key, options = network[0], network[1]
|
key, options = network[0], network[1]
|
||||||
ip = options[:ip] if key == :private_network
|
ip = options[:ip] if key == :private_network
|
||||||
next if ip
|
break if ip
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
ip || (machine.ssh_info ? machine.ssh_info[:host] : nil)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_machines
|
def get_machines
|
||||||
if @global_env.config_global.hostmanager.include_offline?
|
if @config.hostmanager.include_offline?
|
||||||
machines = []
|
machines = @global_env.machine_names
|
||||||
@global_env.machine_names.each do |name|
|
|
||||||
begin
|
|
||||||
@global_env.machine(name, @provider)
|
|
||||||
machines << [name, @provider]
|
|
||||||
rescue Vagrant::Errors::MachineNotFound
|
|
||||||
end
|
|
||||||
end
|
|
||||||
machines
|
|
||||||
else
|
else
|
||||||
@global_env.active_machines
|
machines = @global_env.active_machines
|
||||||
|
.select { |name, provider| provider == @provider }
|
||||||
|
.collect { |name, provider| name }
|
||||||
end
|
end
|
||||||
|
# Collect only machines that exist for the current provider
|
||||||
|
machines.collect do |name|
|
||||||
|
begin
|
||||||
|
machine = @global_env.machine(name, @provider)
|
||||||
|
rescue Vagrant::Errors::MachineNotFound
|
||||||
|
# ignore
|
||||||
|
end
|
||||||
|
machine
|
||||||
|
end
|
||||||
|
.reject(&:nil?)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_new_content(header, footer, body, old_content)
|
||||||
|
if body.empty?
|
||||||
|
block = "\n"
|
||||||
|
else
|
||||||
|
block = "\n\n" + header + body + footer + "\n"
|
||||||
|
end
|
||||||
|
# Pattern for finding existing block
|
||||||
|
header_pattern = Regexp.quote(header)
|
||||||
|
footer_pattern = Regexp.quote(footer)
|
||||||
|
pattern = Regexp.new("\n*#{header_pattern}.*?#{footer_pattern}\n*", Regexp::MULTILINE)
|
||||||
|
# Replace existing block or append
|
||||||
|
old_content.match(pattern) ? old_content.sub(pattern, block) : old_content.rstrip + block
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_or_create_id
|
||||||
|
file = Pathname.new("#{@global_env.local_data_path}/hostmanager/id")
|
||||||
|
if (file.file?)
|
||||||
|
id = file.read.strip
|
||||||
|
else
|
||||||
|
id = SecureRandom.uuid
|
||||||
|
file.dirname.mkpath
|
||||||
|
file.open('w') { |io| io.write(id) }
|
||||||
|
end
|
||||||
|
id
|
||||||
end
|
end
|
||||||
|
|
||||||
## Windows support for copying files, requesting elevated privileges if necessary
|
## Windows support for copying files, requesting elevated privileges if necessary
|
||||||
|
|
|
@ -29,6 +29,12 @@ module VagrantPlugins
|
||||||
Provisioner
|
Provisioner
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Work-around for vagrant >= 1.5
|
||||||
|
# It breaks without a provisioner config, so we provide a dummy one
|
||||||
|
config(:hostmanager, :provisioner) do
|
||||||
|
::Vagrant::Config::V2::DummyConfig.new
|
||||||
|
end
|
||||||
|
|
||||||
command(:hostmanager) do
|
command(:hostmanager) do
|
||||||
require_relative 'command'
|
require_relative 'command'
|
||||||
Command
|
Command
|
||||||
|
|
|
@ -7,11 +7,19 @@ module VagrantPlugins
|
||||||
super(machine, config)
|
super(machine, config)
|
||||||
@global_env = machine.env
|
@global_env = machine.env
|
||||||
@provider = machine.provider_name
|
@provider = machine.provider_name
|
||||||
|
|
||||||
|
# config_global is deprecated from v1.5
|
||||||
|
if Gem::Version.new(::Vagrant::VERSION) >= Gem::Version.new('1.5')
|
||||||
|
@config = @global_env.vagrantfile.config
|
||||||
|
else
|
||||||
|
@config = @global_env.config_global
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def provision
|
def provision
|
||||||
update_guest(@machine)
|
update_guest(@machine)
|
||||||
if @global_env.config_global.hostmanager.manage_host?
|
if @config.hostmanager.manage_host?
|
||||||
update_host
|
update_host
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
module HostManager
|
module HostManager
|
||||||
VERSION = '1.3.0'
|
VERSION = '1.4.0'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
# -*- mode: ruby -*-
|
# -*- mode: ruby -*-
|
||||||
# vi: set ft=ruby :
|
# vi: set ft=ruby :
|
||||||
|
|
||||||
Vagrant.require_plugin('vagrant-hostmanager')
|
if Gem::Version.new(::Vagrant::VERSION) < Gem::Version.new('1.5')
|
||||||
|
Vagrant.require_plugin('vagrant-hostmanager')
|
||||||
|
end
|
||||||
|
|
||||||
Vagrant.configure('2') do |config|
|
Vagrant.configure('2') do |config|
|
||||||
config.vm.box = 'precise64'
|
|
||||||
config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box'
|
if ENV.key? 'VAGRANT_BOX'
|
||||||
|
config.vm.box = ENV['VAGRANT_BOX']
|
||||||
|
else
|
||||||
|
config.vm.box = 'precise64'
|
||||||
|
config.vm.box_url = 'http://cloud-images.ubuntu.com/precise/current/precise-server-cloudimg-vagrant-amd64-disk1.box'
|
||||||
|
end
|
||||||
|
|
||||||
config.hostmanager.enabled = true
|
config.hostmanager.enabled = true
|
||||||
config.hostmanager.manage_host = true
|
config.hostmanager.manage_host = true
|
||||||
|
@ -20,4 +27,10 @@ Vagrant.configure('2') do |config|
|
||||||
server.vm.hostname = 'bender'
|
server.vm.hostname = 'bender'
|
||||||
server.vm.network :private_network, :ip => '10.0.5.3'
|
server.vm.network :private_network, :ip => '10.0.5.3'
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
config.vm.define :server3 do |server|
|
||||||
|
server.vm.hostname = 'leena'
|
||||||
|
server.vm.network :private_network, :ip => '10.0.5.4'
|
||||||
|
server.vm.provision :hostmanager
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue