summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2020-08-18 08:56:21 -0700
committerGitHub <noreply@github.com>2020-08-18 08:56:21 -0700
commitfa55aaa32d3f34704de0303987378039a3c465cf (patch)
tree70eac76fc17a5f578ac451d16446ea74de6b5fa1
parent02a51c541ad12e1910c0ab2b5b8f3bca04f2057f (diff)
parent29ed4ff2c7bbc2098c11a8d93a5ffab4fa1b8a30 (diff)
downloadchef-fa55aaa32d3f34704de0303987378039a3c465cf.tar.gz
Merge pull request #10317 from chef/hostname
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/resource/hostname.rb36
1 files changed, 18 insertions, 18 deletions
diff --git a/lib/chef/resource/hostname.rb b/lib/chef/resource/hostname.rb
index 7329dd34bf..a4f694f391 100644
--- a/lib/chef/resource/hostname.rb
+++ b/lib/chef/resource/hostname.rb
@@ -67,8 +67,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"
@@ -220,35 +219,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