Class: OrchestratorClient::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/orchestrator_client/config.rb

Instance Method Summary collapse

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



124
125
126
# File 'lib/orchestrator_client/config.rb', line 124

def [](key)
  @config[key]
end

#cacertObject



31
32
33
# File 'lib/orchestrator_client/config.rb', line 31

def cacert
  "#{puppetlabs_root}/puppet/ssl/certs/ca.pem"
end

#configObject



87
88
89
# File 'lib/orchestrator_client/config.rb', line 87

def config
  @config ||= load_config
end

#defaultsObject



35
36
37
38
39
40
41
42
# File 'lib/orchestrator_client/config.rb', line 35

def defaults
  { 'cacert' => cacert,
    'token-file' => File.join(user_root, 'token'),
    'User-Agent' => "OrchestratorRubyClient/#{OrchestratorClient::VERSION}",
    'job-poll-interval' => 1,
    'job-poll-timeout' => 1000,
  }
end

#global_confObject



19
20
21
# File 'lib/orchestrator_client/config.rb', line 19

def global_conf
  File.join(puppetlabs_root, 'client-tools', 'orchestrator.conf')
end

#load_configObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/orchestrator_client/config.rb', line 44

def load_config
  config = defaults
  if @load_files
    if File.exist?(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.exist?(user_conf) && File.readable?(user_conf)
      config = config.merge(load_file(user_conf))
    end
  end

  config.merge!(@overrides)

  %w[token-file cacert].each do |f|
    config[f] = File.expand_path(config[f]) if config[f]
  end
  config
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_tokenObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/orchestrator_client/config.rb', line 95

def load_token
  if @config['token']
    @config['token']
  else
    validate_file('token-file', config['token-file'])
    token = File.open(config['token-file']) { |f| f.read.strip }
    # If the token file contains illegal characters
    if token =~ URI::UNSAFE
      raise OrchestratorClient::ConfigError.new("token-file '#{config['token-file']}' contains illegal characters")
    end
    @config['token'] = token
    @config['token']
  end
end

#overrides_onlyObject



91
92
93
# File 'lib/orchestrator_client/config.rb', line 91

def overrides_only
  @config = @overrides
end

#puppetlabs_rootObject



15
16
17
# File 'lib/orchestrator_client/config.rb', line 15

def puppetlabs_root
  "/etc/puppetlabs"
end

#root_urlObject



114
115
116
117
118
119
120
121
122
# File 'lib/orchestrator_client/config.rb', line 114

def root_url
  unless @root_url
   url = @config['service-url']
   url += '/' if url !~ /\/$/
   url += 'orchestrator/v1/'
   @root_url = url
  end
  @root_url
end

#tokenObject



110
111
112
# File 'lib/orchestrator_client/config.rb', line 110

def token
  @token ||= load_token
end

#user_confObject



27
28
29
# File 'lib/orchestrator_client/config.rb', line 27

def user_conf
  File.join(user_root, 'client-tools', 'orchestrator.conf')
end

#user_rootObject



23
24
25
# File 'lib/orchestrator_client/config.rb', line 23

def user_root
  File.join(Dir.home, '.puppetlabs')
end

#validateObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/orchestrator_client/config.rb', line 66

def validate
  if config['service-url'].nil?
    raise OrchestratorClient::ConfigError.new("'service-url' is required in config")
  end

  begin
    service_url = URI.parse(config['service-url'])
    unless service_url.kind_of?(URI::HTTP) || service_url.kind_of?(URI::HTTPS)
      raise OrchestratorClient::ConfigError.new("'#{config['service-url']}' is an invalid service-url")
    end
  rescue URI::InvalidURIError
    raise OrchestratorClient::ConfigError.new("'#{config['service-url']}' is an invalid service-url")
  end

  if config['cacert'].nil?
    raise  OrchestratorClient::ConfigError.new("'cacert' is required in config")
  end

  validate_file('cacert', config['cacert'])
end

#validate_file(type, path) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/orchestrator_client/config.rb', line 128

def validate_file(type, path)
  stat = File.stat(path)
  if !stat.readable?
    OrchestratorClient::ConfigError.new("#{type} '#{path}' is unreadable")
  elsif !stat.file?
    OrchestratorClient::ConfigError.new("#{type} '#{path}' is not a file")
  end
rescue Errno::ENOENT
  raise OrchestratorClient::ConfigError.new("#{type} '#{path}' does not exist")
end