summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2018-09-12 11:08:39 -0700
committerGitHub <noreply@github.com>2018-09-12 11:08:39 -0700
commit93eb41597cb045a442789494448ac3a07356eda0 (patch)
treec80c0e0240d44f21cccab59ee7df3a5efd65290c
parent7eb2bfefbc4b138b5b8ce19239ebf70f83de8ca9 (diff)
parent9d8c46e4e9422d769d6dc4238f48204ee259aeee (diff)
downloadchef-93eb41597cb045a442789494448ac3a07356eda0.tar.gz
Merge pull request #7633 from vincentaubert/locale
Add locale resource more managing the system's locale
-rw-r--r--lib/chef/resource/locale.rb92
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/resource/locale_spec.rb60
3 files changed, 153 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"
diff --git a/spec/unit/resource/locale_spec.rb b/spec/unit/resource/locale_spec.rb
new file mode 100644
index 0000000000..7e1292d211
--- /dev/null
+++ b/spec/unit/resource/locale_spec.rb
@@ -0,0 +1,60 @@
+#
+# Author:: Vincent AUBERT (<vincentaubert88@gmail.com>)
+# Copyright:: Copyright 2008-2018, Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# 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 language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+
+describe Chef::Resource::Locale do
+
+ let(:resource) { Chef::Resource::Locale.new("fakey_fakerton") }
+
+ it "has a name of locale" do
+ expect(resource.resource_name).to eq(:locale)
+ end
+
+ it "the lang property is equal to en_US.utf8" do
+ expect(resource.lang).to eql("en_US.utf8")
+ end
+
+ it "the lc_all property is equal to en_US.utf8" do
+ expect(resource.lc_all).to eql("en_US.utf8")
+ end
+
+ it "sets the default action as :update" do
+ expect(resource.action).to eql([:update])
+ end
+
+ it "supports :update action" do
+ expect { resource.action :update }.not_to raise_error
+ end
+
+ describe "when the language is not the default one" do
+ let(:resource) { Chef::Resource::Locale.new("fakey_fakerton") }
+ before do
+ resource.lang("fr_FR.utf8")
+ resource.lc_all("fr_FR.utf8")
+ end
+
+ it "the lang property is equal to fr_FR.utf8" do
+ expect(resource.lang).to eql("fr_FR.utf8")
+ end
+
+ it "the lc_all property is equal to fr_FR.utf8" do
+ expect(resource.lc_all).to eql("fr_FR.utf8")
+ end
+ end
+end