Update to do an update of hosts files while leaving existing host structure in place, rather than generating a local hosts entry automatically.
This commit is contained in:
parent
67644a6de0
commit
1e698e5912
|
@ -0,0 +1,32 @@
|
||||||
|
require 'vagrant-hostmanager/hosts_file'
|
||||||
|
|
||||||
|
module VagrantPlugins
|
||||||
|
module HostManager
|
||||||
|
module Action
|
||||||
|
class DeleteGuests
|
||||||
|
include HostsFile
|
||||||
|
|
||||||
|
def initialize(app, env)
|
||||||
|
@app = app
|
||||||
|
@machine = env[:machine]
|
||||||
|
@logger = Log4r::Logger.new('vagrant::hostmanager::delete_hosts_file')
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
# check if machine is already active
|
||||||
|
#return @app.call(env) if @machine.id
|
||||||
|
|
||||||
|
# check config to see if the hosts file should be update automatically
|
||||||
|
return @app.call(env) unless @machine.config.hostmanager.enabled?
|
||||||
|
@logger.info 'Updating /etc/hosts file automatically'
|
||||||
|
|
||||||
|
# continue the action stack so the machine will be created
|
||||||
|
@app.call(env)
|
||||||
|
|
||||||
|
# update /etc/hosts file on each active machine
|
||||||
|
delete_guests(@machine,@machine.provider_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -3,18 +3,18 @@ require 'vagrant-hostmanager/hosts_file'
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
module HostManager
|
module HostManager
|
||||||
module Action
|
module Action
|
||||||
class UpdateHostsFile
|
class UpdateGuests
|
||||||
include HostsFile
|
include HostsFile
|
||||||
|
|
||||||
def initialize(app, env)
|
def initialize(app, env)
|
||||||
@app = app
|
@app = app
|
||||||
@machine = env[:machine]
|
@machine = env[:machine]
|
||||||
@logger = Log4r::Logger.new('vagrant::hostmanager::update_hosts_file')
|
@logger = Log4r::Logger.new('vagrant::hostmanager::update_guests')
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
# check if machine is already active
|
# check if machine is already active
|
||||||
return @app.call(env) if @machine.id
|
#return @app.call(env) if @machine.id
|
||||||
|
|
||||||
# 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 @machine.config.hostmanager.enabled?
|
return @app.call(env) unless @machine.config.hostmanager.enabled?
|
||||||
|
@ -24,8 +24,7 @@ module VagrantPlugins
|
||||||
@app.call(env)
|
@app.call(env)
|
||||||
|
|
||||||
# update /etc/hosts file on each active machine
|
# update /etc/hosts file on each active machine
|
||||||
machines = generate(@machine.env, @machine.provider_name)
|
update_guests(@machine,@machine.provider_name)
|
||||||
machines.each { |machine| update(machine) }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -18,10 +18,8 @@ module VagrantPlugins
|
||||||
argv = parse_options(opts)
|
argv = parse_options(opts)
|
||||||
options[:provider] ||= @env.default_provider
|
options[:provider] ||= @env.default_provider
|
||||||
|
|
||||||
generate(@env, options[:provider].to_sym)
|
|
||||||
|
|
||||||
with_target_vms(argv, options) do |machine|
|
with_target_vms(argv, options) do |machine|
|
||||||
update(machine)
|
update_guests(machine, machine.provider_name)
|
||||||
update_local(machine)
|
update_local(machine)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,36 +1,64 @@
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
module HostManager
|
module HostManager
|
||||||
module HostsFile
|
module HostsFile
|
||||||
# Generate a hosts file containing the the active machines
|
def update_guests(machine, provider)
|
||||||
# in the Vagrant environment backed by the specified provider.
|
|
||||||
# The file is written to the Vagrant temporary path.
|
|
||||||
def generate(env, provider)
|
|
||||||
machines = []
|
machines = []
|
||||||
|
|
||||||
|
env = machine.env
|
||||||
# create the temporary hosts file
|
# create the temporary hosts file
|
||||||
path = env.tmp_path.join('hosts')
|
path = env.tmp_path
|
||||||
File.open(path, 'w') do |file|
|
|
||||||
file << "127.0.0.1\tlocalhost\slocalhost.localdomain\n"
|
|
||||||
|
|
||||||
# add a hosts entry for each active machine matching the provider
|
#fetch hosts file from each machine
|
||||||
env.active_machines.each do |name, p|
|
#for each machine, ensure all machine entries are updated
|
||||||
if provider == p
|
# add a hosts entry for each active machine matching the provider
|
||||||
machines << machine = env.machine(name, provider)
|
env.active_machines.each do |name, p|
|
||||||
host = machine.config.vm.hostname || name
|
if provider == p
|
||||||
id = machine.id
|
machines << machine = env.machine(name, provider)
|
||||||
ip = get_ip_address(machine)
|
machine.communicate.download('/etc/hosts',path.join("hosts.#{name}"))
|
||||||
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
|
|
||||||
host_entry = "#{ip}\t#{host}\s#{host_aliases}\n"
|
|
||||||
@logger.info "Adding /etc/hosts entry: #{ip} #{host} #{host_aliases} #{id}"
|
|
||||||
file << "#{host_entry}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
env.active_machines.each do |name, p|
|
||||||
|
if provider == p
|
||||||
|
machines.each do |m|
|
||||||
|
@logger.info "Adding entry for #{m.name} to hosts.#{name}"
|
||||||
|
update_entry(m,path.join("hosts.#{name}"))
|
||||||
|
m.communicate.upload(path.join("hosts.#{name}"), '/tmp/hosts')
|
||||||
|
m.communicate.sudo("mv /tmp/hosts /etc/hosts")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
machines
|
machines
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# delete victim machine from all guests
|
||||||
|
def delete_guests(victim, provider)
|
||||||
|
machines = []
|
||||||
|
|
||||||
|
env = victim.env
|
||||||
|
# create the temporary hosts file
|
||||||
|
path = env.tmp_path
|
||||||
|
|
||||||
|
#fetch hosts file from each machine
|
||||||
|
#for each machine, ensure all machine entries are updated
|
||||||
|
# add a hosts entry for each active machine matching the provider
|
||||||
|
env.active_machines.each do |name, p|
|
||||||
|
if provider == p
|
||||||
|
machines << machine = env.machine(name, provider)
|
||||||
|
machine.communicate.download('/etc/hosts',path.join("hosts.#{name}"))
|
||||||
|
delete_entry(victim,path.join("hosts.#{name}"))
|
||||||
|
if machine.communicate.ready?
|
||||||
|
machine.env.ui.info I18n.t('vagrant_hostmanager.action.update_guest', {
|
||||||
|
:name => machine.name
|
||||||
|
})
|
||||||
|
machine.communicate.upload(path.join("hosts.#{name}"), '/tmp/hosts')
|
||||||
|
machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# define a lambda for looking up a machine's ip address
|
# define a lambda for looking up a machine's ip address
|
||||||
def get_ip_address(machine)
|
def get_ip_address(machine)
|
||||||
ip = nil
|
ip = nil
|
||||||
|
@ -44,42 +72,65 @@ module VagrantPlugins
|
||||||
ip || machine.ssh_info[:host]
|
ip || machine.ssh_info[:host]
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_local(machine)
|
def update_entry(machine,file_name,sudo=false)
|
||||||
return if machine.id.nil?
|
delete_entry(machine,file_name,sudo)
|
||||||
tmplocal=machine.env.tmp_path.join('hosts.local')
|
|
||||||
delete_local(machine)
|
|
||||||
|
|
||||||
host = machine.config.vm.hostname || name
|
host = machine.config.vm.hostname || name
|
||||||
id = machine.id
|
id = machine.id
|
||||||
ip = get_ip_address(machine)
|
ip = get_ip_address(machine)
|
||||||
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
|
host_aliases = machine.config.hostmanager.aliases.join("\s").chomp
|
||||||
host_entry = "#{ip}\t#{host}\s#{host_aliases}\s# VAGRANT: #{id}\n"
|
host_entry = "#{ip}\t#{host}\s#{host_aliases}\s# VAGRANT: #{id}\n"
|
||||||
@logger.info "Adding /etc/hosts entry: #{ip} #{host} #{host_aliases} # #{id} - #{tmplocal}"
|
@logger.info "Adding /etc/hosts entry: #{host_entry}"
|
||||||
File.open(tmplocal,'a') do |tmpfile|
|
temp_file_name = Dir::Tmpname.make_tmpname(File.join(machine.env.tmp_path,'hostmanager'), nil)
|
||||||
tmpfile << host_entry
|
FileUtils.cp(file_name, temp_file_name)
|
||||||
|
File.open(temp_file_name,'a') do |tempfile|
|
||||||
|
@logger.info "writing #{host_entry} to #{tempfile.path}"
|
||||||
|
tempfile << host_entry
|
||||||
end
|
end
|
||||||
publish_local(machine.env)
|
|
||||||
|
|
||||||
|
if sudo == false
|
||||||
|
@logger.info "copy #{temp_file_name} #{file_name}"
|
||||||
|
FileUtils.cp(temp_file_name,file_name)
|
||||||
|
else
|
||||||
|
machine.env.ui.info I18n.t('vagrant_hostmanager.action.run_sudo')
|
||||||
|
@logger.warn "Running sudo to replace local hosts file, enter your local password if prompted..."
|
||||||
|
@logger.info `sudo cp -v #{temp_file_name} #{file_name}`
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_entry(machine,file_name,sudo=false)
|
||||||
|
host = machine.config.vm.hostname || name
|
||||||
|
temp_file_name = Dir::Tmpname.make_tmpname(File.join(machine.env.tmp_path,'hostmanager'), nil)
|
||||||
|
tempfile = File.open(temp_file_name,'w') do |f|
|
||||||
|
File.open(file_name,'r').each_line do |line|
|
||||||
|
if line.match(/#{machine.id}$/).nil?
|
||||||
|
f << line
|
||||||
|
else
|
||||||
|
@logger.info "Matched #{machine.id}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if sudo == false
|
||||||
|
@logger.info "copy #{temp_file_name} #{file_name}"
|
||||||
|
FileUtils.cp(temp_file_name,file_name)
|
||||||
|
else
|
||||||
|
machine.env.ui.info I18n.t('vagrant_hostmanager.action.run_sudo')
|
||||||
|
@logger.info `sudo cp -v #{temp_file_name} #{file_name}`
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_local(machine)
|
||||||
|
return if machine.id.nil?
|
||||||
|
update_entry(machine,'/etc/hosts',true)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_local(machine)
|
def delete_local(machine)
|
||||||
return if machine.id.nil?
|
return if machine.id.nil?
|
||||||
tmplocal=machine.env.tmp_path.join('hosts.local')
|
delete_entry(machine,'/etc/hosts',true)
|
||||||
File.open(tmplocal, 'w') do |tmpfile|
|
|
||||||
File.open('/etc/hosts','r').each_line do |line|
|
|
||||||
if line.match(/#{machine.id}$/).nil?
|
|
||||||
@logger.info "Found #{machine.id} in hosts file"
|
|
||||||
tmpfile << line
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
publish_local(machine.env)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_local(env)
|
def publish_local(tempfile)
|
||||||
@logger.info `sudo cp -v /etc/hosts /etc/hosts.bak`
|
@logger.info `sudo cp -v #{tempfile} /etc/hosts`
|
||||||
@logger.info `sudo cp -v #{env.tmp_path.join('hosts.local')} /etc/hosts`
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -88,11 +139,12 @@ module VagrantPlugins
|
||||||
def update(machine)
|
def update(machine)
|
||||||
path = machine.env.tmp_path.join('hosts')
|
path = machine.env.tmp_path.join('hosts')
|
||||||
if machine.communicate.ready?
|
if machine.communicate.ready?
|
||||||
machine.env.ui.info I18n.t('vagrant_hostmanager.action.update', {
|
machine.env.ui.info I18n.t('vagrant_hostmanager.action.update_guest', {
|
||||||
:name => machine.name
|
:name => machine.name
|
||||||
})
|
})
|
||||||
|
machine.communicate.download(path, '/etc/hosts')
|
||||||
machine.communicate.upload(path, '/tmp/hosts')
|
machine.communicate.upload(path, '/tmp/hosts')
|
||||||
machine.communicate.sudo("mv /tmp/hosts /etc/hosts.hostmanager")
|
machine.communicate.sudo("mv /tmp/hosts /etc/hosts")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'vagrant-hostmanager/action/update_hosts_file'
|
require 'vagrant-hostmanager/action/delete_guests'
|
||||||
|
require 'vagrant-hostmanager/action/update_guests'
|
||||||
require 'vagrant-hostmanager/action/update_local_entry'
|
require 'vagrant-hostmanager/action/update_local_entry'
|
||||||
require 'vagrant-hostmanager/action/delete_local_entry'
|
require 'vagrant-hostmanager/action/delete_local_entry'
|
||||||
|
|
||||||
|
@ -16,14 +17,18 @@ module VagrantPlugins
|
||||||
Config
|
Config
|
||||||
end
|
end
|
||||||
|
|
||||||
|
action_hook(self::ALL_ACTIONS) do |hook|
|
||||||
|
hook.after(VagrantPlugins::ProviderVirtualBox::Action::Boot, Action::UpdateGuests)
|
||||||
|
end
|
||||||
|
|
||||||
action_hook(:hostmanager, :machine_action_up) do |hook|
|
action_hook(:hostmanager, :machine_action_up) do |hook|
|
||||||
hook.prepend(Action::UpdateHostsFile)
|
hook.prepend(Action::UpdateGuests)
|
||||||
hook.prepend(Action::UpdateLocalEntry)
|
hook.prepend(Action::UpdateLocalEntry)
|
||||||
end
|
end
|
||||||
|
|
||||||
action_hook(:hostmanager, :machine_action_destroy) do |hook|
|
action_hook(:hostmanager, :machine_action_destroy) do |hook|
|
||||||
hook.prepend(Action::DeleteLocalEntry)
|
hook.prepend(Action::DeleteLocalEntry)
|
||||||
hook.append(Action::UpdateHostsFile)
|
hook.append(Action::DeleteGuests)
|
||||||
end
|
end
|
||||||
|
|
||||||
command(:hostmanager) do
|
command(:hostmanager) do
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
en:
|
en:
|
||||||
vagrant_hostmanager:
|
vagrant_hostmanager:
|
||||||
action:
|
action:
|
||||||
update: "[%{name}] Updating /etc/hosts file"
|
update_guest: "[%{name}] Updating /etc/hosts file"
|
||||||
|
update_host: "Updating local /etc/hosts file"
|
||||||
|
run_sudo: "Running sudo to modify host machine's /etc/hosts file"
|
||||||
|
|
Loading…
Reference in New Issue