This repository has been archived by the owner on Jan 25, 2018. It is now read-only.
forked from agiledivider/vagrant-hostsupdater
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Windows privilege elevation handling
- Loading branch information
1 parent
bb1a495
commit 521ab3c
Showing
3 changed files
with
19 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
@@ -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 = "" | ||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jeremiahsmall
|
||
else | ||
`sudo #{command}` | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type %1 >> %2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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.