Class: VagrantHosts::Cap::SyncHosts::Windows
- Defined in:
- lib/vagrant-hosts/cap/sync_hosts/windows.rb
Overview
Provide a base class for syncing hosts entries on Windows systems.
Instance Method Summary collapse
- #change_domain_name(domainname) ⇒ Object
-
#change_host_name(name) ⇒ Object
Windows needs a modification of the base method because Windows guest names cannot be fully-qualified domain names (they cannot contain the "." character).
- #update_hosts ⇒ Object
Methods inherited from Base
#initialize, #sync!, sync_hosts
Constructor Details
This class inherits a constructor from VagrantHosts::Cap::SyncHosts::Base
Instance Method Details
#change_domain_name(domainname) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vagrant-hosts/cap/sync_hosts/windows.rb', line 43 def change_domain_name(domainname) # Source: http://poshcode.org/2958 # Note that whitespace is important in this inline powershell script due # to the use of a here-string. powershell = "function Set-PrimaryDnsSuffix {\nparam ([string] $Suffix)\n\n# http://msdn.microsoft.com/en-us/library/ms724224(v=vs.85).aspx\n$ComputerNamePhysicalDnsDomain = 6\n\nAdd-Type -TypeDefinition @\"\nusing System;\nusing System.Runtime.InteropServices;\n\nnamespace ComputerSystem {\n public class Identification {\n [DllImport(\"kernel32.dll\", CharSet = CharSet.Auto)]\n static extern bool SetComputerNameEx(int NameType, string lpBuffer);\n\n public static bool SetPrimaryDnsSuffix(string suffix) {\n try {\n return SetComputerNameEx($ComputerNamePhysicalDnsDomain, suffix);\n }\n catch (Exception) {\n return false;\n }\n }\n }\n}\n\"@\n[ComputerSystem.Identification]::SetPrimaryDnsSuffix($Suffix)\n}\n\n$success = Set-PrimaryDnsSuffix \"\#{domainname}\"\nif ($success -eq $True) {exit 0} else {exit 1}\n END_OF_POWERSHELL\n\n @machine.communicate.sudo(powershell)\nend\n" |
#change_host_name(name) ⇒ Object
Windows needs a modification of the base method because Windows guest names cannot be fully-qualified domain names (they cannot contain the "." character). Therefore, modify the input name to convert illegal characters to legal replacements.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/vagrant-hosts/cap/sync_hosts/windows.rb', line 28 def change_host_name(name) # First set the machine name (hostname) components = name.split('.') hostname = components.first domainname = components.slice(1, components.size).join('.') super(hostname) # Next set the Primary DNS Suffix, if it makes sense (domainname) unless domainname.empty? change_domain_name(domainname) end end |
#update_hosts ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/vagrant-hosts/cap/sync_hosts/windows.rb', line 4 def update_hosts host_entries = [] all_hosts(@config).each do |(address, aliases)| aliases.each do |name| host_entries << "#{address} #{name}" end end script = [] script << '$HostsPath = "$env:windir\\System32\\drivers\\etc\\hosts"' script << '$Hosts = gc $HostsPath' host_defs = "'" + host_entries.join('\', \'') + "'" script << "@(#{host_defs}) | % { if (\$Hosts -notcontains \$_) { Add-Content -Path \$HostsPath -Value \$_ }}" @machine.communicate.sudo(script.join("; ")) end |