Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix resolving windows hosts file in unix EOL #165

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Hotfix resolving windows hosts file in unix EOL.
  • Loading branch information
la-magra authored and magra committed Sep 26, 2015
commit a728f5ca693a7dc96179923639524540aa27ad9a
30 changes: 19 additions & 11 deletions lib/vagrant-hostmanager/hosts_file/updater.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ def initialize(global_env, provider)
def update_guest(machine)
return unless machine.communicate.ready?

if (machine.communicate.test("uname -s | grep SunOS"))
if machine.communicate.test("uname -s | grep SunOS")
realhostfile = '/etc/inet/hosts'
move_cmd = 'mv'
elsif (machine.communicate.test("test -d $Env:SystemRoot"))
elsif machine.communicate.test("test -d $Env:SystemRoot")
windir = ""
machine.communicate.execute("echo %SYSTEMROOT%", {:shell => :cmd}) do |type, contents|
windir << contents.gsub("\r\n", '') if type == :stdout
Expand Down Expand Up @@ -48,23 +48,26 @@ def update_guest(machine)
def update_host
# copy and modify hosts file on host with Vagrant-managed entries
file = @global_env.tmp_path.join('hosts.local')

if WindowsSupport.windows?
# lazily include windows Module
class << self
include WindowsSupport unless include? WindowsSupport
end

crlf_file = @global_env.tmp_path.join('hosts.local.crlf')
windows_convert_to_crlf(file, crlf_file)
hosts_location = "#{ENV['WINDIR']}\\System32\\drivers\\etc\\hosts"
copy_proc = Proc.new { windows_copy_file(file, hosts_location) }
copy_proc = Proc.new { windows_copy_file(crlf_file, hosts_location) }
FileUtils.cp(hosts_location, crlf_file)
if update_file(crlf_file)
copy_proc.call
end
else
hosts_location = '/etc/hosts'
copy_proc = Proc.new { `sudo cp #{file} #{hosts_location}` }
end

FileUtils.cp(hosts_location, file)
if update_file(file)
copy_proc.call
FileUtils.cp(hosts_location, file)
if update_file(file)
copy_proc.call
end
end
end

Expand Down Expand Up @@ -150,7 +153,7 @@ def get_new_content(header, footer, body, old_content)

def read_or_create_id
file = Pathname.new("#{@global_env.local_data_path}/hostmanager/id")
if (file.file?)
if file.file?
id = file.read.strip
else
id = SecureRandom.uuid
Expand All @@ -170,6 +173,11 @@ def self.windows?

require 'win32ole' if windows?

def windows_convert_to_crlf(source, dest)
convert_cmd = "type \"#{source.to_s.gsub('/', '\\')}\" | more /P > \"#{dest.to_s.gsub('/', '\\')}\""
WIN32OLE.new('Shell.Application').ShellExecute('cmd', "/C #{convert_cmd}", nil, nil, 7)
end

def windows_copy_file(source, dest)
begin
# First, try Ruby copy
Expand Down