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.
5 6 7 8 |
# File 'lib/orchestrator_client/config.rb', line 5 def initialize(overrides = nil, load_files=false) @overrides = overrides || {} @load_files = load_files end |
Instance Method Details
#[](key) ⇒ Object
94 95 96 |
# File 'lib/orchestrator_client/config.rb', line 94 def [](key) @config[key] end |
#cacert ⇒ Object
30 31 32 |
# File 'lib/orchestrator_client/config.rb', line 30 def cacert "#{puppetlabs_root}/puppet/ssl/certs/ca.pem" end |
#config ⇒ Object
68 69 70 |
# File 'lib/orchestrator_client/config.rb', line 68 def config @config ||= load_config end |
#defaults ⇒ Object
34 35 36 37 38 39 |
# File 'lib/orchestrator_client/config.rb', line 34 def defaults { 'cacert' => cacert, 'token-file' => File.join(user_root, 'token'), 'User-Agent' => "OrchestratorRubyClient/#{OrchestratorClient::VERSION}" } end |
#global_conf ⇒ Object
18 19 20 |
# File 'lib/orchestrator_client/config.rb', line 18 def global_conf File.join(puppetlabs_root, 'client-tools', 'orchestrator.conf') end |
#load_config ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/orchestrator_client/config.rb', line 41 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
10 11 12 |
# File 'lib/orchestrator_client/config.rb', line 10 def load_file(path) File.open(path) {|f| JSON.parse(f.read)['options']} end |
#load_token ⇒ Object
76 77 78 |
# File 'lib/orchestrator_client/config.rb', line 76 def load_token @config['token'] || File.open(config['token-file']) { |f| f.read } end |
#overrides_only ⇒ Object
72 73 74 |
# File 'lib/orchestrator_client/config.rb', line 72 def overrides_only @config = @overrides end |
#puppetlabs_root ⇒ Object
14 15 16 |
# File 'lib/orchestrator_client/config.rb', line 14 def puppetlabs_root "/etc/puppetlabs" end |
#root_url ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/orchestrator_client/config.rb', line 84 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
80 81 82 |
# File 'lib/orchestrator_client/config.rb', line 80 def token @token ||= load_token end |
#user_conf ⇒ Object
26 27 28 |
# File 'lib/orchestrator_client/config.rb', line 26 def user_conf File.join(user_root, 'client-tools', 'orchestrator.conf') end |
#user_root ⇒ Object
22 23 24 |
# File 'lib/orchestrator_client/config.rb', line 22 def user_root File.join(Dir.home, '.puppetlabs') end |
#validate ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/orchestrator_client/config.rb', line 58 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 |