Module: Hieracles::Config

Extended by:
Config
Includes:
Utils
Included in:
Config
Defined in:
lib/hieracles/config.rb

Overview

configuration singleton

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#deep_sort, #local_merge!, #max_key_length, #sym_keys, #to_deep_hash, #to_shallow_hash

Instance Attribute Details

#basepathObject (readonly)

Returns the value of attribute basepath.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def basepath
  @basepath
end

#classpathObject (readonly)

Returns the value of attribute classpath.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def classpath
  @classpath
end

#encpathObject (readonly)

Returns the value of attribute encpath.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def encpath
  @encpath
end

#extraparamsObject (readonly)

Returns the value of attribute extraparams.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def extraparams
  @extraparams
end

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def format
  @format
end

#hierafileObject (readonly)

Returns the value of attribute hierafile.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def hierafile
  @hierafile
end

#interactiveObject (readonly)

Returns the value of attribute interactive.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def interactive
  @interactive
end

#modulepathObject (readonly)

Returns the value of attribute modulepath.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def modulepath
  @modulepath
end

#puppetdbObject (readonly)

Returns the value of attribute puppetdb.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def puppetdb
  @puppetdb
end

#scopeObject (readonly)

Returns the value of attribute scope.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def scope
  @scope
end

#serverObject (readonly)

Returns the value of attribute server.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def server
  @server
end

#usedbObject (readonly)

Returns the value of attribute usedb.



12
13
14
# File 'lib/hieracles/config.rb', line 12

def usedb
  @usedb
end

Instance Method Details

#build_path(path) ⇒ Object



97
98
99
# File 'lib/hieracles/config.rb', line 97

def build_path(path)
  File.expand_path(File.join(@basepath, path))
end

#defaultconfigObject



60
61
62
# File 'lib/hieracles/config.rb', line 60

def defaultconfig
  File.join(ENV['HOME'], '.config', 'hieracles', 'config.yml')
end

#extract_params(str) ⇒ Object

str is like: something=xxx;another=yyy



65
66
67
68
69
70
71
# File 'lib/hieracles/config.rb', line 65

def extract_params(str)
  return {} unless str
  str.split(';').reduce({}) do |a, k|
    a["#{k[/^[^=]*/]}".to_sym] = k[/[^=]*$/]
    a
  end
end

#get_config(file) ⇒ Object



44
45
46
47
# File 'lib/hieracles/config.rb', line 44

def get_config(file)
  initconfig(file) unless File.exist? file
  YAML.load_file(file)
end

#initconfig(file) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/hieracles/config.rb', line 49

def initconfig(file)
  FileUtils.mkdir_p(File.dirname(file))
  File.open(file, 'w') do |f|
    f.puts '---'
    f.puts 'classpath: manifests/classes/%s.pp'
    f.puts 'modulepath: modules'
    f.puts 'encpath: enc'
    f.puts 'hierafile: hiera.yaml'
  end
end

#load(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hieracles/config.rb', line 15

def load(options)
  @options = options
  @optionfile = @options[:config] || defaultconfig
  @extraparams = extract_params(options[:params])
  @values = get_config(@optionfile)
  @server = @values['server']
  @usedb = if @options[:db]
    true
  elsif @options[:nodb]
    false
  else
    @values['usedb']
  end
  @puppetdb = @values['puppetdb']
  @values['basepath'] ||= @values['localpath']
  @basepath = File.expand_path(pick_first(:basepath, '.'))
  @classpath = build_path(@values['classpath'])
  @modulepath = resolve_path(pick_first(:modulepath, 'modules'))
  @encpath = resolve_path(pick_first(:encpath, 'enc'))
  @hierafile = resolve_path(pick_first(:hierafile, 'hiera.yaml'))
  @format = pick_first(:format, 'console').capitalize
  @scope = load_scope(@values)
  @interactive = pick_first(:interactive, false)
end

#load_facts(file, format) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/hieracles/config.rb', line 79

def load_facts(file, format)
  if format == :json
    JSON.parse(File.read(file))
  else
    YAML.load_file(file)
  end
end

#load_scope(values) ⇒ Object



73
74
75
76
77
# File 'lib/hieracles/config.rb', line 73

def load_scope(values)
  facts_file = @options[:yaml_facts] || @options[:json_facts]
  facts_format = @options[:json_facts] ? :json : :yaml
  sym_keys((facts_file && load_facts(facts_file, facts_format)) || values['defaultscope'] || {})
end

#pick_first(label, default) ⇒ Object



40
41
42
# File 'lib/hieracles/config.rb', line 40

def pick_first(label, default)
  @options[label] || @values[label.to_s] || default
end

#resolve_path(path) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/hieracles/config.rb', line 87

def resolve_path(path)
  if File.exists?(File.expand_path(path))
    File.expand_path(path)
  elsif File.exists?(File.expand_path(File.join(@basepath, path)))
    File.expand_path(File.join(@basepath, path))
  else
    raise IOError, "File #{path} not found."
  end
end