summaryrefslogtreecommitdiff
path: root/lib/chef/resource/locale.rb
blob: b16cfe7e3c307ab5341fb39efc928620a5120acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#
# 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
      resource_name :locale

      description "Use the locale resource to set the system's locale."
      introduced "14.5"

      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

      property :lang, String,
               description: "Sets the default system language.",
               regex: [LOCALE_REGEX],
               validation_message: "The provided lang is not valid. It should be a non-empty string without any leading whitespaces."

      property :lc_env, Hash,
               description: "A Hash of LC_* env variables in the form of ({ 'LC_ENV_VARIABLE' => 'VALUE' }).",
               default: lazy { {} },
               coerce: proc { |h|
                         if h.respond_to?(:keys)
                           invalid_keys = h.keys - LC_VARIABLES
                           unless invalid_keys.empty?
                             error_msg = "Key of option lc_env must be equal to one of: \"#{LC_VARIABLES.join('", "')}\"!  You passed \"#{invalid_keys.join(', ')}\"."
                             raise Chef::Exceptions::ValidationFailed, error_msg
                           end
                         end
                         unless h.values.all? { |x| x =~ LOCALE_REGEX }
                           error_msg = "Values of option lc_env should be non-empty string without any leading whitespaces."
                           raise Chef::Exceptions::ValidationFailed, error_msg
                         end
                         h
                       }

      # @deprecated Use {#lc_env} instead of this property.
      #   {#lc_env} uses Hash with specific LC var as key.
      # @raise [Chef::Deprecated]
      #
      def lc_all(arg = nil)
        unless arg.nil?
          Chef.deprecated(:locale_lc_all, "Changing LC_ALL can break Chef's parsing of command output in unexpected ways.\n Use one of the more specific LC_ properties as needed.")
        end
      end

      action :update do
        description "Update the system's locale."
        begin
          unless up_to_date?
            converge_by "Updating System Locale" do
              generate_locales unless unavailable_locales.empty?
              update_locale
            end
          end
        rescue
          # It might affect debugging
          raise "#{node['platform']} platform is not supported by the chef locale resource. " +
            "If you believe this is in error please file an issue at https://github.com/chef/chef/issues"
        end
      end

      action_class do

        # Generates the localisation files from templates using locale-gen.
        # @see http://manpages.ubuntu.com/manpages/cosmic/man8/locale-gen.8.html
        # @raise [Mixlib::ShellOut::ShellCommandFailed] not a supported language or locale
        #
        def generate_locales
          bash "Generating locales: #{unavailable_locales.join(' ')}" do
            code <<~CODE
              if type locale-gen >/dev/null 2>&1
              then
                locale-gen #{unavailable_locales.join(' ')}
              fi
            CODE
          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
        # later part of the client run.
        # @see https://wiki.archlinux.org/index.php/locale#Setting_the_system_locale
        #
        def update_locale
          file "Updating system locale" do
            path LOCALE_CONF
            content new_content
          end
        end

        # @return [Array<String>] Locales that user wants to set but are not available on
        #   the system. They are required to be generated.
        #
        def unavailable_locales
          @unavailable_locales ||= begin
            available = shell_out!("locale -a").stdout.split("\n")
            required = [new_resource.lang, new_resource.lc_env.values].flatten.compact.uniq
            required - available
          end
        end

        # @return [String] Contents that are required to be
        #   updated in /etc/locale.conf
        #
        def new_content
          @new_content ||= begin
            content = {}
            content = new_resource.lc_env.dup if new_resource.lc_env
            content["LANG"] = new_resource.lang if new_resource.lang
            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
end