diff options
author | Tim Smith <tsmith@chef.io> | 2020-07-27 17:00:04 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-27 17:00:04 -0700 |
commit | 755ffdd8f0157343701f3847286aad7987d9be16 (patch) | |
tree | 301d9389b0043fdaafcdeccf52302004659fc624 | |
parent | 310dfe562134691a32c36bfcc6f07873ca36cd0b (diff) | |
parent | d2c066b572dbe892c10e50af42595606b75e8a0a (diff) | |
download | chef-755ffdd8f0157343701f3847286aad7987d9be16.tar.gz |
Merge pull request #10147 from chef/hostname
Use powershell_out vs. powershell_script in hostname
-rw-r--r-- | kitchen-tests/cookbooks/end_to_end/recipes/windows.rb | 4 | ||||
-rw-r--r-- | lib/chef/resource/hostname.rb | 36 |
2 files changed, 22 insertions, 18 deletions
diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb b/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb index 22f78db319..df176e8751 100644 --- a/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb +++ b/kitchen-tests/cookbooks/end_to_end/recipes/windows.rb @@ -68,3 +68,7 @@ locale "set system locale" do end include_recipe "::_ohai_hint" + +hostname "new-hostname" do + windows_reboot false +end
\ No newline at end of file diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb index f096b0e881..e76ea88e8d 100644 --- a/lib/chef/resource/hostname.rb +++ b/lib/chef/resource/hostname.rb @@ -87,8 +87,7 @@ class Chef def updated_ec2_config_xml begin require "rexml/document" unless defined?(REXML::Document) - config_file = 'C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml' - config = REXML::Document.new(::File.read(config_file)) + config = REXML::Document.new(::File.read(WINDOWS_EC2_CONFIG)) # find an element named State with a sibling element whose value is Ec2SetComputerName REXML::XPath.each(config, "//Plugin/State[../Name/text() = 'Ec2SetComputerName']") do |element| element.text = "Disabled" @@ -223,35 +222,36 @@ class Chef end else # windows + WINDOWS_EC2_CONFIG = 'C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml'.freeze + raise "Windows hostnames cannot contain a period." if new_resource.hostname.match?(/\./) # suppress EC2 config service from setting our hostname - if ::File.exist?('C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml') + if ::File.exist?(WINDOWS_EC2_CONFIG) xml_contents = updated_ec2_config_xml if xml_contents.empty? Chef::Log.warn('Unable to properly parse and update C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml contents. Skipping file update.') else - declare_resource(:file, 'C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml') do + file WINDOWS_EC2_CONFIG do content xml_contents end end end - # update via netdom - declare_resource(:powershell_script, "set hostname") do - code <<-EOH - $sysInfo = Get-WmiObject -Class Win32_ComputerSystem - $sysInfo.Rename("#{new_resource.hostname}") - EOH - notifies :request_reboot, "reboot[setting hostname]" - not_if { Socket.gethostbyname(Socket.gethostname).first == new_resource.hostname } - end + unless Socket.gethostbyname(Socket.gethostname).first == new_resource.hostname + converge_by "set hostname to #{new_resource.hostname}" do + powershell_out! <<~EOH + $sysInfo = Get-WmiObject -Class Win32_ComputerSystem + $sysInfo.Rename("#{new_resource.hostname}") + EOH + end - # reboot because $windows - declare_resource(:reboot, "setting hostname") do - reason "#{Chef::Dist::PRODUCT} updated system hostname" - action :nothing - only_if { new_resource.windows_reboot } + # reboot because $windows + reboot "setting hostname" do + reason "#{Chef::Dist::PRODUCT} updated system hostname" + only_if { new_resource.windows_reboot } + action :request_reboot + end end end end |