diff options
author | Kapil Chouhan <kapil.chouhan@msystechnologies.com> | 2020-04-10 09:07:08 +0000 |
---|---|---|
committer | Kapil Chouhan <kapil.chouhan@msystechnologies.com> | 2020-04-22 09:31:20 +0000 |
commit | be1191b72d72887713048538e72a11b3940c6eb8 (patch) | |
tree | a564a3b23051a1f8a2573e94a7ce4334477ad23d /lib/chef/resource/locale.rb | |
parent | 16b6db24f54f4fa015b4939880a04abb9a8c256b (diff) | |
download | chef-be1191b72d72887713048538e72a11b3940c6eb8.tar.gz |
Update the locale resource to support WindowsKapil/GitHub-9450_Update_the_locale_resource_to_support_Windows
Signed-off-by: Kapil Chouhan <kapil.chouhan@msystechnologies.com>
Diffstat (limited to 'lib/chef/resource/locale.rb')
-rw-r--r-- | lib/chef/resource/locale.rb | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/lib/chef/resource/locale.rb b/lib/chef/resource/locale.rb index 0ecc440a80..5112e7e1df 100644 --- a/lib/chef/resource/locale.rb +++ b/lib/chef/resource/locale.rb @@ -30,7 +30,7 @@ class Chef LC_VARIABLES = %w{LC_ADDRESS LC_COLLATE LC_CTYPE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE LC_TIME}.freeze LOCALE_CONF = "/etc/locale.conf".freeze LOCALE_REGEX = /\A\S+/.freeze - LOCALE_PLATFORM_FAMILIES = %w{debian}.freeze + LOCALE_PLATFORM_FAMILIES = %w{debian windows}.freeze property :lang, String, description: "Sets the default system language.", @@ -65,13 +65,32 @@ class Chef end end + load_current_value do + if windows? + lang get_system_locale_windows + else + begin + old_content = ::File.read(LOCALE_CONF) + locale_values = Hash[old_content.split("\n").map { |v| v.split("=") }] + lang locale_values["LANG"] + rescue Errno::ENOENT => e + false + end + end + end + + # Gets the System-locale setting for the current computer. + # @see https://docs.microsoft.com/en-us/powershell/module/international/get-winsystemlocale + # @return [String] the current value of the System-locale setting. + # + def get_system_locale_windows + powershell_exec("Get-WinSystemLocale").result["Name"] + end + action :update do description "Update the system's locale." - unless up_to_date? - converge_by "Updating System Locale" do - generate_locales unless unavailable_locales.empty? - update_locale - end + converge_if_changed do + set_system_locale end end @@ -99,6 +118,21 @@ class Chef shell_out!("locale-gen #{unavailable_locales.join(" ")}") end + # Sets the system locale for the current computer. + # + def set_system_locale + if windows? + # Sets the system locale for the current computer. + # @see https://docs.microsoft.com/en-us/powershell/module/internationalcmdlets/set-winsystemlocale + # + response = powershell_exec("Set-WinSystemLocale -SystemLocale #{new_resource.lang}") + raise response.errors.join(" ") if response.error? + else + generate_locales unless unavailable_locales.empty? + update_locale + end + end + # Updates system locale by appropriately writing them in /etc/locale.conf # @note This locale change won't affect the current run. At this time it is an exercise # left to the user to restart or reboot if the locale change is required at @@ -134,15 +168,6 @@ class Chef content.sort.map { |t| t.join("=") }.join("\n") + "\n" end end - - # @return [Boolean] Whether any modification is required in /etc/locale.conf - # - def up_to_date? - old_content = ::File.read(LOCALE_CONF) - new_content == old_content - rescue - false # We need to create the file if it is not present - end end end end |