@@ -237,25 +237,11 @@ def install_hiera_config(logger, options)
237237
238238 # Munge datadir in hiera config file
239239 obj = YAML . load_file ( file_src )
240- ( [ obj [ :backends ] ] . flatten || %w( yaml json ) ) . each do |key |
241- next unless obj . key? ( key . to_sym )
242- if options [ :hiera_path_strip ] . is_a? ( String )
243- next if obj [ key . to_sym ] [ :datadir ] . nil?
244- rexp1 = Regexp . new ( '^' + options [ :hiera_path_strip ] )
245- obj [ key . to_sym ] [ :datadir ] . sub! ( rexp1 , @tempdir )
246- elsif options [ :hiera_path ] . is_a? ( String )
247- obj [ key . to_sym ] [ :datadir ] = File . join ( @tempdir , 'environments' , environment , options [ :hiera_path ] )
248- end
249- rexp2 = Regexp . new ( '%{(::)?environment}' )
250- obj [ key . to_sym ] [ :datadir ] . sub! ( rexp2 , environment )
251-
252- # Make sure the dirctory exists. If not, log a warning. This is *probably* a setup error, but we don't
253- # want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
254- # directory around or even setting up Hiera for the very first time.
255- unless File . directory? ( obj [ key . to_sym ] [ :datadir ] )
256- message = "WARNING: Hiera datadir for #{ key } doesn't seem to exist at #{ obj [ key . to_sym ] [ :datadir ] } "
257- logger . warn message
258- end
240+ version = obj [ 'version' ] || obj [ :version ] || 3
241+ if version . to_i == 5
242+ update_hiera_config_v5 ( logger , options , obj )
243+ else
244+ update_hiera_config_v3 ( logger , options , obj )
259245 end
260246
261247 # Write properly formatted hiera config file into temporary directory
@@ -292,6 +278,68 @@ def install_ssl(logger, options)
292278
293279 private
294280
281+ # Jump-off for hiera v3 (or earlier)
282+ # @param logger [Logger] Logger object
283+ # @param options [Hash] Options hash
284+ # @param obj [Hash] Parsed hiera.yaml file
285+ def update_hiera_config_v3 ( logger , options , obj )
286+ ( [ obj [ :backends ] ] . flatten || %w( yaml json ) ) . each do |key |
287+ next unless obj . key? ( key . to_sym )
288+ obj [ key . to_sym ] [ :datadir ] = hiera_munge ( options , obj [ key . to_sym ] [ :datadir ] )
289+
290+ # Make sure the directory exists. If not, log a warning. This is *probably* a setup error, but we don't
291+ # want it to be fatal in case (for example) someone is doing an octocatalog-diff to verify moving this
292+ # directory around or even setting up Hiera for the very first time.
293+ unless File . directory? ( obj [ key . to_sym ] [ :datadir ] )
294+ message = "WARNING: Hiera datadir for #{ key } doesn't seem to exist at #{ obj [ key . to_sym ] [ :datadir ] } "
295+ logger . warn message
296+ end
297+ end
298+ end
299+
300+ # Jump-off for hiera v5
301+ # @param logger [Logger] Logger object
302+ # @param options [Hash] Options hash
303+ # @param obj [Hash] Parsed hiera.yaml file
304+ def update_hiera_config_v5 ( _logger , options , obj )
305+ defaults_key = obj . key? ( :defaults ) ? :defaults : 'defaults'
306+ hierarchy_key = obj . key? ( :hierarchy ) ? :hierarchy : 'hierarchy'
307+
308+ # Fix defaults:datadir
309+ if obj [ defaults_key ] . is_a? ( Hash )
310+ [ :datadir , 'datadir' ] . each do |key |
311+ next unless obj [ defaults_key ] . key? ( key )
312+ obj [ defaults_key ] [ key ] = hiera_munge ( options , obj [ defaults_key ] [ key ] )
313+ end
314+ end
315+
316+ # Fix hierarchy:datadir
317+ if obj [ hierarchy_key ] . is_a? ( Array )
318+ obj [ hierarchy_key ] . each do |level |
319+ [ :datadir , 'datadir' ] . each do |key |
320+ next unless level . key? ( key )
321+ level [ key ] = hiera_munge ( options , level [ key ] )
322+ end
323+ end
324+ end
325+ end
326+
327+ # Hiera munge - shared method to apply :hiera_path_strip and :hiera_path
328+ def hiera_munge ( options , current_value )
329+ return if current_value . nil?
330+
331+ if options [ :hiera_path_strip ] . is_a? ( String )
332+ rexp1 = Regexp . new ( '^' + options [ :hiera_path_strip ] )
333+ current_value . sub! ( rexp1 , @tempdir )
334+ elsif options [ :hiera_path ] . is_a? ( String )
335+ current_value = File . join ( @tempdir , 'environments' , environment , options [ :hiera_path ] )
336+ end
337+ rexp2 = Regexp . new ( '%{(::)?environment}' )
338+ current_value . sub! ( rexp2 , environment )
339+
340+ current_value
341+ end
342+
295343 # Install SSL certificate authority certificate
296344 # @param logger [Logger] Logger object
297345 # @param options [Hash] Options hash
0 commit comments