Module: Hieracles::Config
Overview
configuration singleton
Instance Attribute Summary collapse
-
#basepath ⇒ Object
readonly
Returns the value of attribute basepath.
-
#classpath ⇒ Object
readonly
Returns the value of attribute classpath.
-
#encpath ⇒ Object
readonly
Returns the value of attribute encpath.
-
#extraparams ⇒ Object
readonly
Returns the value of attribute extraparams.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#hierafile ⇒ Object
readonly
Returns the value of attribute hierafile.
-
#modulepath ⇒ Object
readonly
Returns the value of attribute modulepath.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #defaultconfig ⇒ Object
-
#extract_params(str) ⇒ Object
str is like: something=xxx;another=yyy.
- #initconfig(file) ⇒ Object
- #load(options) ⇒ Object
- #load_facts(file, format) ⇒ Object
- #path(what) ⇒ Object
Methods included from Utils
#deep_merge!, #deep_sort, #max_key_length, #sym_keys, #to_deep_hash, #to_shallow_hash
Instance Attribute Details
#basepath ⇒ Object (readonly)
Returns the value of attribute basepath.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def basepath @basepath end |
#classpath ⇒ Object (readonly)
Returns the value of attribute classpath.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def classpath @classpath end |
#encpath ⇒ Object (readonly)
Returns the value of attribute encpath.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def encpath @encpath end |
#extraparams ⇒ Object (readonly)
Returns the value of attribute extraparams.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def extraparams @extraparams end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def format @format end |
#hierafile ⇒ Object (readonly)
Returns the value of attribute hierafile.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def hierafile @hierafile end |
#modulepath ⇒ Object (readonly)
Returns the value of attribute modulepath.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def modulepath @modulepath end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def scope @scope end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def server @server end |
Instance Method Details
#defaultconfig ⇒ Object
45 46 47 |
# File 'lib/hieracles/config.rb', line 45 def defaultconfig File.join(ENV['HOME'], '.config', 'hieracles', 'config.yml') end |
#extract_params(str) ⇒ Object
str is like: something=xxx;another=yyy
50 51 52 53 54 55 56 |
# File 'lib/hieracles/config.rb', line 50 def extract_params(str) return {} unless str str.split(';').reduce({}) do |a, k| a["#{k[/^[^=]*/]}".to_sym] = k[/[^=]*$/] a end end |
#initconfig(file) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/hieracles/config.rb', line 32 def initconfig(file) FileUtils.mkdir_p(File.dirname(file)) File.open(file, 'w') do |f| f.puts '---' f.puts '# uncomment if you use the CGI method for discovery' f.puts '# server: puppetserver.example.com' 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 |
# File 'lib/hieracles/config.rb', line 15 def load() @optionfile = [:config] || defaultconfig @extraparams = extract_params([:params]) initconfig(@optionfile) unless File.exist? @optionfile values = YAML.load_file(@optionfile) @server = values['server'] @classpath = values['classpath'] @modulepath = values['modulepath'] || 'modules' @encpath = [:encpath] || values['encpath'] || 'enc' @basepath = File.([:basepath] || values['basepath'] || '.') @hierafile = [:hierafile] || values['hierafile'] || 'hiera.yaml' @format = ([:format] || values['format'] || 'console').capitalize facts_file = [:yaml_facts] || [:json_facts] facts_format = [:json_facts] ? :json : :yaml @scope = sym_keys((facts_file && load_facts(facts_file, facts_format)) || values['defaultscope'] || {}) end |
#load_facts(file, format) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/hieracles/config.rb', line 62 def load_facts(file, format) if format == :json JSON.parse(File.read(file)) else YAML.load_file(file) end end |
#path(what) ⇒ Object
58 59 60 |
# File 'lib/hieracles/config.rb', line 58 def path(what) File.join(@basepath, send(what.to_sym)) end |