2014-03-14 17:51:00 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestWindows
|
|
|
|
module Cap
|
|
|
|
module ChangeHostName
|
2014-06-10 05:46:03 +00:00
|
|
|
|
2014-03-14 17:51:00 +00:00
|
|
|
def self.change_host_name(machine, name)
|
2018-03-15 14:38:07 +00:00
|
|
|
change_host_name_and_wait(machine, name)
|
2014-06-10 05:46:03 +00:00
|
|
|
end
|
|
|
|
|
2018-03-15 14:38:07 +00:00
|
|
|
def self.change_host_name_and_wait(machine, name)
|
2014-06-10 05:46:03 +00:00
|
|
|
# If the configured name matches the current name, then bail
|
2014-06-24 21:26:03 +00:00
|
|
|
# We cannot use %ComputerName% because it truncates at 15 chars
|
|
|
|
return if machine.communicate.test("if ([System.Net.Dns]::GetHostName() -eq '#{name}') { exit 0 } exit 1")
|
|
|
|
|
2015-01-29 01:00:24 +00:00
|
|
|
# Rename and reboot host if rename succeeded
|
2014-06-24 21:26:03 +00:00
|
|
|
script = <<-EOH
|
2015-01-29 01:00:24 +00:00
|
|
|
$computer = Get-WmiObject -Class Win32_ComputerSystem
|
|
|
|
$retval = $computer.rename("#{name}").returnvalue
|
|
|
|
if ($retval -eq 0) {
|
|
|
|
shutdown /r /t 5 /f /d p:4:1 /c "Vagrant Rename Computer"
|
2014-06-24 21:26:03 +00:00
|
|
|
}
|
2015-01-29 01:00:24 +00:00
|
|
|
exit $retval
|
2014-06-24 21:26:03 +00:00
|
|
|
EOH
|
2014-06-10 05:46:03 +00:00
|
|
|
|
2014-06-24 21:26:03 +00:00
|
|
|
machine.communicate.execute(
|
|
|
|
script,
|
|
|
|
error_class: Errors::RenameComputerFailed,
|
|
|
|
error_key: :rename_computer_failed)
|
2014-06-10 05:46:03 +00:00
|
|
|
|
|
|
|
# Don't continue until the machine has shutdown and rebooted
|
2018-03-15 15:45:42 +00:00
|
|
|
if machine.guest.capability?(:wait_for_reboot)
|
|
|
|
machine.guest.capability(:wait_for_reboot)
|
|
|
|
end
|
2014-03-14 17:51:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|