Class: Hiera::Backend::Fragment_backend

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/fragment_backend.rb

Constant Summary collapse

SUPPORTED_EXTENSIONS =
%w{yaml eyaml}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFragment_backend

Returns a new instance of Fragment_backend.



23
24
25
# File 'lib/hiera/backend/fragment_backend.rb', line 23

def initialize
  Fragment_backend::debug 'Hiera fragment backend starting'
end

Class Method Details

.process_filesObject



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
# File 'lib/hiera/backend/fragment_backend.rb', line 40

def self.process_files
  conf = Config[:fragment]
  data_dir = conf[:datadir]
  # restrict to yaml extensions only

  if conf[:extensions] and not conf[:extensions].to_set.subset? SUPPORTED_EXTENSIONS.to_set
    raise "Unsupported extensions #{conf[:extensions]}. Only #{SUPPORTED_EXTENSIONS} are supported"
  end

  extensions = [conf[:extensions] || SUPPORTED_EXTENSIONS].flatten.join(',')

  fragments = [conf[:fragments]].flatten.map do |fragment|
    path_join_glob(data_dir, "#{fragment}.{#{extensions}}")
  end.flatten.select do |file|
    File.file? file
  end

  if fragments.empty?
    warn "No fragments found in #{data_dir}"
  end

  dest_dir = conf[:destdir] || Config[:yaml][:datadir]

  input_dirs = [conf[:inputdirs]].flatten.map { |file| Pathname.new(file).realpath.to_s }

  debug "Cleaning output dir: #{dest_dir}"
  FileUtils.rm_rf dest_dir
  FileUtils.mkdir_p dest_dir

  glob_pattern = "**/*.{#{extensions}}"
  debug "Using fragments #{fragments.join(',')}"
  merge_input(fragments, glob_pattern, input_dirs) do |yaml_file, input_dir, yaml|
    output_file = Pathname.new(yaml_file).realpath.to_s.sub(input_dir, dest_dir)
    debug "Writing to #{output_file}"

    FileUtils.mkdir_p File.dirname(output_file)
    File.open(output_file, 'w+') { |file| YAML.dump(yaml, file) }
  end
end

Instance Method Details

#lookup(key, scope, order_override, resolution_type) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/hiera/backend/fragment_backend.rb', line 27

def lookup(key, scope, order_override, resolution_type)
  answer = nil
  case resolution_type
    when :array
      answer = []
    when :hash
      answer = {}
    end
  answer
end