Sha256: df5ee7abd4638d02f4f49e472531c27faf7c4e1d2a40aaef34b7f3940f78d13d

Contents?: true

Size: 1.43 KB

Versions: 14

Compression:

Stored size: 1.43 KB

Contents

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
            if ($retval -eq 0) {
              shutdown /r /t 5 /f /d p:4:1 /c "Vagrant Rename Computer"
            }
            exit $retval
          EOH

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

          # Don't continue until the machine has shutdown and rebooted
	  if machine.guest.capability?(:wait_for_reboot)
            machine.guest.capability(:wait_for_reboot)
          else
            # use graceful_halt_timeout only if guest cannot wait for reboot
            sleep(sleep_timeout)
	  end
        end
      end
    end
  end
end

Version data entries

14 entries across 10 versions & 3 rubygems

Version Path
vagrant-unbundled-2.2.0.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.1.4.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.1.2.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/windows/cap/change_host_name.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/windows/cap/change_host_name.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/windows/cap/change_host_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/windows/cap/change_host_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/windows/cap/change_host_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.1.1.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-unbundled-2.0.4.0 plugins/guests/windows/cap/change_host_name.rb
vagrant-aws-detiber-0.7.2.pre.4 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/windows/cap/change_host_name.rb
vagrant-aws-detiber-0.7.2.pre.3 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/windows/cap/change_host_name.rb
vagrant-aws-detiber-0.7.2.pre.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/windows/cap/change_host_name.rb