Class: Hieracles::Config
- Inherits:
-
Object
- Object
- Hieracles::Config
- Includes:
- Utils
- Defined in:
- lib/hieracles/config.rb
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
-
#initialize(options) ⇒ Config
constructor
A new instance of Config.
- #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, #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() = @optionfile = [:config] || defaultconfig @extraparams = extract_params([:params]) @values = get_config(@optionfile) @server = @values['server'] @usedb = if [:db] true elsif [: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 |
Instance Attribute Details
#basepath ⇒ Object (readonly)
Returns the value of attribute basepath.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def basepath @basepath end |
#classpath ⇒ Object (readonly)
Returns the value of attribute classpath.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def classpath @classpath end |
#encpath ⇒ Object (readonly)
Returns the value of attribute encpath.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def encpath @encpath end |
#extraparams ⇒ Object (readonly)
Returns the value of attribute extraparams.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def extraparams @extraparams end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def format @format end |
#hierafile ⇒ Object (readonly)
Returns the value of attribute hierafile.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def hierafile @hierafile end |
#interactive ⇒ Object (readonly)
Returns the value of attribute interactive.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def interactive @interactive end |
#modulepath ⇒ Object (readonly)
Returns the value of attribute modulepath.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def modulepath @modulepath end |
#puppetdb ⇒ Object (readonly)
Returns the value of attribute puppetdb.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def puppetdb @puppetdb end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def scope @scope end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
10 11 12 |
# File 'lib/hieracles/config.rb', line 10 def server @server end |
#usedb ⇒ Object (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.(File.join(@basepath, path)) end |
#defaultconfig ⇒ Object
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 = [:yaml_facts] || [:json_facts] facts_format = [: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) [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.(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 |