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.
-
#interactive ⇒ Object
readonly
Returns the value of attribute interactive.
-
#modulepath ⇒ Object
readonly
Returns the value of attribute modulepath.
-
#puppetdb ⇒ Object
readonly
Returns the value of attribute puppetdb.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#usedb ⇒ Object
readonly
Returns the value of attribute usedb.
Instance Method Summary collapse
- #build_path(path) ⇒ Object
- #defaultconfig ⇒ Object
-
#extract_params(str) ⇒ Object
str is like: something=xxx;another=yyy.
- #get_config(file) ⇒ Object
- #initconfig(file) ⇒ Object
- #load(options) ⇒ Object
- #load_facts(file, format) ⇒ Object
- #load_scope(values) ⇒ Object
- #pick_first(label, default) ⇒ Object
- #resolve_path(path) ⇒ Object
Methods included from Utils
#deep_sort, #local_merge!, #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 |
#interactive ⇒ Object (readonly)
Returns the value of attribute interactive.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def interactive @interactive end |
#modulepath ⇒ Object (readonly)
Returns the value of attribute modulepath.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def modulepath @modulepath end |
#puppetdb ⇒ Object (readonly)
Returns the value of attribute puppetdb.
12 13 14 |
# File 'lib/hieracles/config.rb', line 12 def puppetdb @puppetdb 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 |
#usedb ⇒ Object (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.(File.join(@basepath, path)) end |
#defaultconfig ⇒ Object
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 = @optionfile = @options[:config] || defaultconfig @extraparams = extract_params([: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.(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.(path)) File.(path) elsif File.exists?(File.(File.join(@basepath, path))) File.(File.join(@basepath, path)) else raise IOError, "File #{path} not found." end end |