diff options
author | Vincent AUBERT <vincentaubert88@gmail.com> | 2018-09-09 18:23:31 +0200 |
---|---|---|
committer | Vincent AUBERT <vincentaubert88@gmail.com> | 2018-09-11 21:26:17 +0200 |
commit | 9d8c46e4e9422d769d6dc4238f48204ee259aeee (patch) | |
tree | c80c0e0240d44f21cccab59ee7df3a5efd65290c /lib/chef | |
parent | 7eb2bfefbc4b138b5b8ce19239ebf70f83de8ca9 (diff) | |
download | chef-9d8c46e4e9422d769d6dc4238f48204ee259aeee.tar.gz |
add locale resource to chef
Signed-off-by: Vincent AUBERT <vincentaubert88@gmail.com>
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/resource/locale.rb | 92 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 |
2 files changed, 93 insertions, 0 deletions
diff --git a/lib/chef/resource/locale.rb b/lib/chef/resource/locale.rb new file mode 100644 index 0000000000..01a4577c43 --- /dev/null +++ b/lib/chef/resource/locale.rb @@ -0,0 +1,92 @@ +# +# Copyright:: 2011-2016, Heavy Water Software Inc. +# Copyright:: 2016-2018, Chef Software Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific slanguage governing permissions and +# limitations under the License. +# + +require "chef/resource" + +class Chef + class Resource + class Locale < Chef::Resource + preview_resource true + resource_name :locale + + description "Use the locale resource to set the system's locale" + introduced "14.5" + + property :lang, String, + default: "en_US.utf8", + description: "Sets the default system language." + property :lc_all, String, + default: "en_US.utf8", + description: "Sets the fallback system language." + + action :update do + description "Updates the system locale" + if node["init_package"] == "systemd" + # on systemd settings LC_ALL is (correctly) reserved only for testing and cannot be set globally + execute "localectl set-locale LANG=#{new_resource.lang}" do + # RHEL uses /etc/locale.conf + not_if { up_to_date?("/etc/locale.conf", new_resource.lang) } if ::File.exist?("/etc/locale.conf") + # Ubuntu 16.04 still uses /etc/default/locale + not_if { up_to_date?("/etc/default/locale", new_resource.lang) } if ::File.exist?("/etc/default/locale") + end + elsif ::File.exist?("/etc/sysconfig/i18n") + locale_file_path = "/etc/sysconfig/i18n" + + updated = up_to_date?(locale_file_path, new_resource.lang, new_resource.lc_all) + + file locale_file_path do + content lazy { + locale = IO.read(locale_file_path) + variables = Hash[locale.lines.map { |line| line.strip.split("=") }] + variables["LANG"] = new_resource.lang + variables["LC_ALL"] = + variables.map { |pairs| pairs.join("=") }.join("\n") + "\n" + } + not_if { updated } + end + + execute "reload root's lang profile script" do + command "source source /etc/sysconfig/i18n; source /etc/profile.d/lang.sh" + not_if { updated } + end + elsif ::File.exist?("/usr/sbin/update-locale") + execute "Generate locales" do + command "locale-gen" + not_if { up_to_date?("/etc/default/locale", new_resource.lang, new_resource.lc_all) } + end + + execute "Update locale" do + command "update-locale LANG=#{new_resource.lang} LC_ALL=#{new_resource.lc_all}" + not_if { up_to_date?("/etc/default/locale", new_resource.lang, new_resource.lc_all) } + end + else + raise "#{node["platform"]} platform not supported by the chef locale resource." + end + end + + action_class do + def up_to_date?(file_path, lang, lc_all = nil) + locale = IO.read(file_path) + locale.include?("LANG=#{lang}") && + (node["init_package"] == "systemd" || lc_all.nil? || locale.include?("LC_ALL=#{lc_all}")) + rescue + false + end + end + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index 614ef74ade..a96dcdda4a 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -139,3 +139,4 @@ require "chef/resource/windows_printer" require "chef/resource/windows_printer_port" require "chef/resource/windows_shortcut" require "chef/resource/windows_task" +require "chef/resource/locale" |