Class: Hieracles::Config

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/hieracles/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

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

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



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

def initialize(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

Instance Attribute Details

#basepathObject (readonly)

Returns the value of attribute basepath.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def basepath
  @basepath
end

#classpathObject (readonly)

Returns the value of attribute classpath.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def classpath
  @classpath
end

#encpathObject (readonly)

Returns the value of attribute encpath.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def encpath
  @encpath
end

#extraparamsObject (readonly)

Returns the value of attribute extraparams.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def extraparams
  @extraparams
end

#formatObject (readonly)

Returns the value of attribute format.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def format
  @format
end

#hierafileObject (readonly)

Returns the value of attribute hierafile.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def hierafile
  @hierafile
end

#interactiveObject (readonly)

Returns the value of attribute interactive.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def interactive
  @interactive
end

#modulepathObject (readonly)

Returns the value of attribute modulepath.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def modulepath
  @modulepath
end

#puppetdbObject (readonly)

Returns the value of attribute puppetdb.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def puppetdb
  @puppetdb
end

#scopeObject (readonly)

Returns the value of attribute scope.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def scope
  @scope
end

#serverObject (readonly)

Returns the value of attribute server.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def server
  @server
end

#usedbObject (readonly)

Returns the value of attribute usedb.



10
11
12
# File 'lib/hieracles/config.rb', line 10

def usedb
  @usedb
end

Instance Method Details

#build_path(path) ⇒ Object



95
96
97
# File 'lib/hieracles/config.rb', line 95

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

#defaultconfigObject



58
59
60
# File 'lib/hieracles/config.rb', line 58

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

#extract_params(str) ⇒ Object

str is like: something=xxx;another=yyy



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

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



42
43
44
45
# File 'lib/hieracles/config.rb', line 42

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

#initconfig(file) ⇒ Object



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

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_facts(file, format) ⇒ Object



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

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

#load_scope(values) ⇒ Object



71
72
73
74
75
# File 'lib/hieracles/config.rb', line 71

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



38
39
40
# File 'lib/hieracles/config.rb', line 38

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

#resolve_path(path) ⇒ Object



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

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