Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Commit

Permalink
Add Windows privilege elevation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
itsananderson committed May 29, 2014
1 parent bb1a495 commit 521ab3c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/vagrant-hostsupdater/HostsUpdater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ def addToHosts(entries)
content = entries.join("\n").strip
if !File.writable?(@@hosts_path)
sudo(%Q(sh -c 'echo "#{content}" >> #@@hosts_path'))
elsif Vagrant::Util::Platform.windows?
require 'tmpdir'
uuid = @machine.id || @machine.config.hostsupdater.id
hashedId = Digest::MD5.hexdigest(uuid)
tmpPath = File.join(Dir.tmpdir, 'hosts-' + uuid)
tmpFile = File.open(tmpPath, "w")
tmpFile.write(content)
tmpFile.close()
win32AppendFallback = File.join(File.dirname(__FILE__), "fallbacks", "win32-append.cmd")
win32TmpPath = tmpPath.gsub('/', '\\')
puts win32TmpPath
sudo(%Q(#{win32AppendFallback} #{win32TmpPath} #@@hosts_path))
else
content = "\n" + content
hostsFile = File.open(@@hosts_path, "a")
Expand All @@ -88,7 +100,7 @@ def addToHosts(entries)
def removeFromHosts(options = {})
uuid = @machine.id || @machine.config.hostsupdater.id
hashedId = Digest::MD5.hexdigest(uuid)
if !File.writable?(@@hosts_path)
if !File.writable?(@@hosts_path) || Vagrant::Util::Platform.windows?
sudo(%Q(sed -i -e '/#{hashedId}/ d' #@@hosts_path))
else
hosts = ""
Expand All @@ -111,7 +123,8 @@ def signature(name, uuid = self.uuid)
def sudo(command)
return if !command
if Vagrant::Util::Platform.windows?
`#{command}`
win32SudoFallback = File.join(File.dirname(__FILE__), "fallbacks", "win32-sudo.ps1")
puts `powershell -ExecutionPolicy Unrestricted -File #{win32SudoFallback} #{command}`

This comment has been minimized.

Copy link
@tkdb

tkdb Jun 2, 2014

powershell is not a standard windows component across the superset of windows versions you check against here, see Installing Windows PowerShell - Microsoft Technet - I'd consider to check for powershell explicitly then otherwise this will just crash and I don't see any error checking here so will turn out hurting in case it's not available.

This comment has been minimized.

Copy link
@jeremiahsmall

jeremiahsmall Apr 21, 2015

I think this is a worthwhile change to PR. I hope you have time to remove powershell dependency and PR it.

else
`sudo #{command}`
end
Expand Down
1 change: 1 addition & 0 deletions lib/vagrant-hostsupdater/fallbacks/win32-append.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type %1 >> %2
3 changes: 3 additions & 0 deletions lib/vagrant-hostsupdater/fallbacks/win32-sudo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$exe = $args[0]
$argArray = $args[1..($args.length-1)]
Start-Process $exe $argArray -Verb runas

0 comments on commit 521ab3c

Please sign in to comment.