Sha256: 31367575b5aab250899b93ac627882decaf1bd3c3789792e73c0466a5cd0b45c

Contents?: true

Size: 1.06 KB

Versions: 20

Compression:

Stored size: 1.06 KB

Contents

require "log4r"

module VagrantPlugins
  module GuestWindows
    module Cap
      module ChangeHostName
        def self.change_host_name(machine, name)
          change_host_name_and_wait(machine, name, machine.config.vm.graceful_halt_timeout)
        end

        def self.change_host_name_and_wait(machine, name, sleep_timeout)
          # If the configured name matches the current name, then bail
          # 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")

          # Rename and reboot host if rename succeeded
          script = <<-EOH
            $computer = Get-WmiObject -Class Win32_ComputerSystem
            $retval = $computer.rename("#{name}").returnvalue
            exit $retval
          EOH

          machine.communicate.execute(
            script,
            error_class: Errors::RenameComputerFailed,
            error_key: :rename_computer_failed)

          machine.guest.capability(:reboot)
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/guests/windows/cap/change_host_name.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.3.3.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.3.2.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.19.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.18.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.16.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.14.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.10.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.9.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.8.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.7.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.6.2 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.6.1 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.6.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.5.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.4.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.3.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.2.2.0 plugins/guests/windows/cap/change_host_name.rb