Class: OrchestratorClient::Config
- Inherits:
-
Object
- Object
- OrchestratorClient::Config
- Defined in:
- lib/orchestrator_client/config.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #cacert ⇒ Object
- #config ⇒ Object
- #defaults ⇒ Object
- #global_conf ⇒ Object
-
#initialize(overrides = nil, load_files = false) ⇒ Config
constructor
A new instance of Config.
- #load_config ⇒ Object
- #load_file(path) ⇒ Object
- #load_token ⇒ Object
- #overrides_only ⇒ Object
- #puppetlabs_root ⇒ Object
- #root_url ⇒ Object
- #token ⇒ Object
- #user_conf ⇒ Object
- #user_root ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(overrides = nil, load_files = false) ⇒ Config
Returns a new instance of Config.
6 7 8 9 |
# File 'lib/orchestrator_client/config.rb', line 6 def initialize(overrides = nil, load_files=false) @overrides = overrides || {} @load_files = load_files end |
Instance Method Details
#[](key) ⇒ Object
95 96 97 |
# File 'lib/orchestrator_client/config.rb', line 95 def [](key) @config[key] end |
#cacert ⇒ Object
31 32 33 |
# File 'lib/orchestrator_client/config.rb', line 31 def cacert "#{puppetlabs_root}/puppet/ssl/certs/ca.pem" end |
#config ⇒ Object
69 70 71 |
# File 'lib/orchestrator_client/config.rb', line 69 def config @config ||= load_config end |
#defaults ⇒ Object
35 36 37 38 39 40 |
# File 'lib/orchestrator_client/config.rb', line 35 def defaults { 'cacert' => cacert, 'token-file' => File.join(user_root, 'token'), 'User-Agent' => "OrchestratorRubyClient/#{OrchestratorClient::VERSION}" } end |
#global_conf ⇒ Object
19 20 21 |
# File 'lib/orchestrator_client/config.rb', line 19 def global_conf File.join(puppetlabs_root, 'client-tools', 'orchestrator.conf') end |
#load_config ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/orchestrator_client/config.rb', line 42 def load_config config = defaults if @load_files if File.exists?(global_conf) && File.readable?(global_conf) config = config.merge(load_file(global_conf)) end if @overrides['config-file'] config = config.merge(load_file(@overrides['config-file'])) elsif File.exists?(user_conf) && File.readable?(user_conf) config = config.merge(load_file(user_conf)) end end config.merge(@overrides) end |
#load_file(path) ⇒ Object
11 12 13 |
# File 'lib/orchestrator_client/config.rb', line 11 def load_file(path) File.open(path) {|f| JSON.parse(f.read)['options']} end |
#load_token ⇒ Object
77 78 79 |
# File 'lib/orchestrator_client/config.rb', line 77 def load_token @config['token'] || File.open(config['token-file']) { |f| f.read.strip } end |
#overrides_only ⇒ Object
73 74 75 |
# File 'lib/orchestrator_client/config.rb', line 73 def overrides_only @config = @overrides end |
#puppetlabs_root ⇒ Object
15 16 17 |
# File 'lib/orchestrator_client/config.rb', line 15 def puppetlabs_root "/etc/puppetlabs" end |
#root_url ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/orchestrator_client/config.rb', line 85 def root_url unless @root_url url = @config['service-url'] url += '/' if url !~ /\/$/ url += 'orchestrator/v1/' @root_url = url end @root_url end |
#token ⇒ Object
81 82 83 |
# File 'lib/orchestrator_client/config.rb', line 81 def token @token ||= load_token end |
#user_conf ⇒ Object
27 28 29 |
# File 'lib/orchestrator_client/config.rb', line 27 def user_conf File.join(user_root, 'client-tools', 'orchestrator.conf') end |
#user_root ⇒ Object
23 24 25 |
# File 'lib/orchestrator_client/config.rb', line 23 def user_root File.join(Dir.home, '.puppetlabs') end |
#validate ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/orchestrator_client/config.rb', line 59 def validate if config['service-url'].nil? raise OrchestratorClient::ConfigError.new("'service-url' is required in config") end if config['cacert'].nil? raise OrchestratorClient::ConfigError.new("'cacert' is required in config") end end |