Fixed issue 3987
Reboot the Windows guest after renaming the computer so changes take affect immediately before attempting to provision the box. - Changed rename from wmic to netdom since netdom seems to work correctly in Windows 2008R2 and newer OSs. - Fixed Windows guest error translations, the wrong namespace was specified in the yaml file.
This commit is contained in:
parent
0a5f6bb77e
commit
d4bd05883d
|
@ -2,11 +2,24 @@ module VagrantPlugins
|
||||||
module GuestWindows
|
module GuestWindows
|
||||||
module Cap
|
module Cap
|
||||||
module ChangeHostName
|
module ChangeHostName
|
||||||
|
|
||||||
def self.change_host_name(machine, name)
|
def self.change_host_name(machine, name)
|
||||||
# On windows, renaming a computer seems to require a reboot
|
change_host_name_and_wait(machine, name, machine.config.vm.graceful_halt_timeout)
|
||||||
machine.communicate.execute(
|
end
|
||||||
"wmic computersystem where name=\"%COMPUTERNAME%\" call rename name=\"#{name}\"",
|
|
||||||
shell: :cmd)
|
def self.change_host_name_and_wait(machine, name, sleep_timeout)
|
||||||
|
# If the configured name matches the current name, then bail
|
||||||
|
return if machine.communicate.test("if ($env:ComputerName -eq '#{name}') { exit 0 } exit 1")
|
||||||
|
|
||||||
|
# Rename and then reboot in one step
|
||||||
|
exit_code = machine.communicate.execute(
|
||||||
|
"netdom renamecomputer \"$Env:COMPUTERNAME\" /NewName:#{name} /Force /Reboot:0",
|
||||||
|
error_check: false)
|
||||||
|
|
||||||
|
raise Errors::RenameComputerFailed if exit_code != 0
|
||||||
|
|
||||||
|
# Don't continue until the machine has shutdown and rebooted
|
||||||
|
sleep(sleep_timeout)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,10 @@ module VagrantPlugins
|
||||||
class NetworkWinRMRequired < WindowsError
|
class NetworkWinRMRequired < WindowsError
|
||||||
error_key(:network_winrm_required)
|
error_key(:network_winrm_required)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class RenameComputerFailed < WindowsError
|
||||||
|
error_key(:rename_computer_failed)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
en:
|
en:
|
||||||
vagrant_winrm:
|
vagrant_windows:
|
||||||
errors:
|
errors:
|
||||||
cant_read_mac_addresses: |-
|
cant_read_mac_addresses: |-
|
||||||
The provider being used to start Windows ('%{provider}')
|
The provider being used to start Windows ('%{provider}')
|
||||||
|
@ -18,3 +18,10 @@ en:
|
||||||
Note that the Windows guest must be configured to accept insecure
|
Note that the Windows guest must be configured to accept insecure
|
||||||
WinRM connections, and the WinRM port must be forwarded properly from
|
WinRM connections, and the WinRM port must be forwarded properly from
|
||||||
the guest machine. This is not always done by default.
|
the guest machine. This is not always done by default.
|
||||||
|
rename_computer_failed: |-
|
||||||
|
Renaming the Windows guest failed. Most often this is because you've
|
||||||
|
specified a FQDN instead of just a host name.
|
||||||
|
|
||||||
|
Ensure the new guest name is properly formatted. Standard names may
|
||||||
|
contain letters (a-z, A-Z), numbers (0-9), and hypens (-), but no
|
||||||
|
spaces or periods (.). The name may not consist entirely of digits.
|
||||||
|
|
|
@ -8,7 +8,7 @@ describe "VagrantPlugins::GuestWindows::Cap::ChangeHostName" do
|
||||||
end
|
end
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
let(:old_hostname) {'oldhostname.olddomain.tld' }
|
let(:old_hostname) { 'oldhostname' }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(machine).to receive(:communicate).and_return(communicator)
|
allow(machine).to receive(:communicate).and_return(communicator)
|
||||||
|
@ -19,11 +19,19 @@ describe "VagrantPlugins::GuestWindows::Cap::ChangeHostName" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".change_host_name" do
|
describe ".change_host_name" do
|
||||||
|
|
||||||
it "changes the hostname" do
|
it "changes the hostname" do
|
||||||
communicator.expect_command('wmic computersystem where name="%COMPUTERNAME%" call rename name="newhostname.newdomain.tld"')
|
communicator.stub_command('if (!($env:ComputerName -eq \'newhostname\')) { exit 0 } exit 1', exit_code: 0)
|
||||||
described_class.change_host_name(machine, 'newhostname.newdomain.tld')
|
communicator.stub_command('netdom renamecomputer "$Env:COMPUTERNAME" /NewName:newhostname /Force /Reboot:0',
|
||||||
|
exit_code: 0)
|
||||||
|
described_class.change_host_name_and_wait(machine, 'newhostname', 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "raises RenameComputerFailed when exit code is non-zero" do
|
||||||
|
communicator.stub_command('if (!($env:ComputerName -eq \'newhostname\')) { exit 0 } exit 1', exit_code: 0)
|
||||||
|
communicator.stub_command('netdom renamecomputer "$Env:COMPUTERNAME" /NewName:newhostname /Force /Reboot:0',
|
||||||
|
exit_code: 123)
|
||||||
|
expect { described_class.change_host_name_and_wait(machine, 'newhostname', 0) }.
|
||||||
|
to raise_error(VagrantPlugins::GuestWindows::Errors::RenameComputerFailed)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue