Class: RHC::Config

Inherits:
Object show all
Includes:
ConfigEnv
Defined in:
lib/rhc/config.rb

Overview

Responsible for encapsulating the loading and retrieval of OpenShift configuration files and converting them to commandline option equivalents. It also provides the converse option - converting a set of commandline options back into a config file.

In general, the values stored in the config should be identical (require little or no type conversion) to their option form. As new global options are added, only this class should have to change to persist that option.

During normal use, a new Config object should load the appropriate settings and those settings should be converted into commandline option defaults.

TODO: Encapsulate config writing to the home location TODO: Allow the config object to initialized with a path TODO: Remove deprecated methods, remove extra sources.

Constant Summary collapse

OPTIONS =

Option name [config_key type comment_string_for_config]

if nil, == key  nil == string  won't be written to file if nil
{
  :server =>      ['libra_server', nil, 'The StartApp server to connect to'],
  :rhlogin =>     ['default_rhlogin', nil, 'Your StartApp login name'],
  :password =>    nil,
  :use_authorization_tokens =>
                  [nil, :boolean, 'If true, the server will attempt to create and use authorization tokens to connect to the server'],
  :timeout =>     [nil, :integer, 'The default timeout for network operations'],
  :insecure =>    [nil, :boolean, "If true, certificate errors will be ignored.\nWARNING: This may allow others to eavesdrop on your communication with OpenShift."],
  :ssl_version => [nil, nil, 'The SSL protocol version to use when connecting to this server'],
  :ssl_client_cert_file => [nil, :path_to_file, 'A client certificate file for use with your server'],
  :ssl_ca_file => [nil, :path_to_file, 'A file containing CA one or more certificates'],
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigEnv

#conf_name, #home_conf_dir, #local_config_path, #ssh_dir, #ssh_priv_key_file_path, #ssh_pub_key_file_path

Constructor Details

#initializeConfig

DEPRECATED - will be removed when old commands are gone



106
107
108
# File 'lib/rhc/config.rb', line 106

def initialize
  set_defaults
end

Class Method Details

.defaultObject

DEPRECATED - will be removed when old commands are gone



86
87
88
# File 'lib/rhc/config.rb', line 86

def self.default
  @default ||= RHC::Config.new
end

.initializeObject

DEPRECATED - will be removed when old commands are gone



100
101
102
103
# File 'lib/rhc/config.rb', line 100

def self.initialize
  @default = nil
  default
end

.method_missing(method, *args, &block) ⇒ Object

DEPRECATED - will be removed when old commands are gone



91
92
93
94
95
96
97
# File 'lib/rhc/config.rb', line 91

def self.method_missing(method, *args, &block)
  if default.respond_to?(method)
    default.send(method, *args, &block)
  else
    raise NoMethodError, method
  end
end

.options_to_config(options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rhc/config.rb', line 64

def self.options_to_config(options)
  OPTIONS.inject([]) do |arr, (name, opts)|
    opts ||= []
    next arr unless opts[2]
    value = options[name]
    arr.concat(opts[2].each_line.to_a.map(&:strip).map{ |s| "# #{s}" })
    arr << "#{value.nil? ? '#' : ''}#{opts[0] || name}=#{self.type_to_config(opts[1], value)}"
    arr << ""
    arr
  end.join("\n")
end

.type_to_config(type, value) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/rhc/config.rb', line 76

def self.type_to_config(type, value)
  case type
  when :integer, :boolean
    value.nil? ? "<#{type}>" : value
  else
    value.nil? ? "<#{type || 'string'}>" : value
  end
end

Instance Method Details

#[](key) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rhc/config.rb', line 160

def [](key)
  lazy_init

  # evaluate in cascading order
  configs = [@opts, @opts_config, @env_config, @local_config, @global_config, @defaults]
  result = nil
  configs.each do |conf|
    result = conf[key] if !conf.nil?
    break if !result.nil?
  end

  result
end

#check_cpath(opts) ⇒ Object

DEPRECATED - will be removed when old commands are gone



210
211
212
213
214
215
216
217
218
219
# File 'lib/rhc/config.rb', line 210

def check_cpath(opts)
  unless opts["config"].nil?
    opts_config_path = File.expand_path(opts["config"])
    if !File.readable?(opts_config_path)
      raise Errno::EACCES.new "Could not open config file: #{@opts_config_path}"
    else
      set_opts_config(opts_config_path)
    end
  end
end

#config_pathObject

config_path

authoritive configuration path this is used to determine where config options should be written to when a script modifies the config such as in rhc setup



252
253
254
# File 'lib/rhc/config.rb', line 252

def config_path
  @config_path ||= local_config_path
end

#default_proxyObject

DEPRECATED - will be removed when old commands are gone



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rhc/config.rb', line 273

def default_proxy
  @default_proxy ||= (
    proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
    if proxy
      if proxy !~ /^(\w+):\/\// then
        proxy = "http://#{proxy}"
      end
      ENV['http_proxy'] = proxy
      proxy_uri = URI.parse(ENV['http_proxy'])
      Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
    else
      Net::HTTP
    end
  )
end

#default_rhloginObject

DEPRECATED - will be removed when old commands are gone



268
269
270
# File 'lib/rhc/config.rb', line 268

def default_rhlogin
  get_value('default_rhlogin')
end

#get_value(key) ⇒ Object

DEPRECATED - will be removed when old commands are gone



175
176
177
# File 'lib/rhc/config.rb', line 175

def get_value(key)
  self[key]
end

#global_config_pathObject

DEPRECATED - may be made private



222
223
224
225
# File 'lib/rhc/config.rb', line 222

def global_config_path
  linux_cfg = '/etc/openshift/' + conf_name
  File.exists?(linux_cfg) ? linux_cfg : File.join(File.expand_path(File.dirname(__FILE__) + "/../../conf"), conf_name)
end

#has_global_config?Boolean

Returns:

  • (Boolean)


227
228
229
230
# File 'lib/rhc/config.rb', line 227

def has_global_config?
  lazy_init
  !@global_config.nil?
end

#has_local_config?Boolean

Returns:

  • (Boolean)


232
233
234
235
# File 'lib/rhc/config.rb', line 232

def has_local_config?
  lazy_init
  !@local_config.nil?
end

#has_opts_config?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/rhc/config.rb', line 237

def has_opts_config?
  !@opts_config.nil?
end

#home_conf_pathObject



263
264
265
# File 'lib/rhc/config.rb', line 263

def home_conf_path
  home_conf_dir
end

#home_dirObject



259
260
261
# File 'lib/rhc/config.rb', line 259

def home_dir
  RHC::Config.home_dir
end

#pathObject



255
256
257
# File 'lib/rhc/config.rb', line 255

def path
  config_path
end

#proxy_varsObject

DEPRECATED - will be removed when old commands are gone



295
296
297
298
299
# File 'lib/rhc/config.rb', line 295

def proxy_vars
  Hash[[:address,:user,:pass,:port].map do |x|
    [x,default_proxy.instance_variable_get("@proxy_#{x}")]
  end]
end

#read_config_filesObject

DEPRECATED - will be removed when old commands are gone



111
112
113
# File 'lib/rhc/config.rb', line 111

def read_config_files
  load_config_files
end

#save!(options) ⇒ Object



153
154
155
156
157
158
# File 'lib/rhc/config.rb', line 153

def save!(options)
  File.open(path, 'w'){ |f| f.puts self.class.options_to_config(options) }
  @opts, @opts_config, @local_config, @global_config = nil
  load_config_files
  self
end

#set_defaultsObject

DEPRECATED - will be removed when old commands are gone



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rhc/config.rb', line 116

def set_defaults
  @defaults = RHC::Vendor::ParseConfig.new()
  @opts  = RHC::Vendor::ParseConfig.new() # option switches that override config file

  @env_config = RHC::Vendor::ParseConfig.new()
  @global_config = nil
  @local_config = nil
  @opts_config = nil # config file passed in the options

  @default_proxy = nil

  @defaults.add('libra_server', 'api.startappcloud.com')
  @env_config.add('libra_server', ENV['LIBRA_SERVER']) if ENV['LIBRA_SERVER']
  @env_config.add('libra_server', ENV['RHC_SERVER']) if ENV['RHC_SERVER']

  @opts_config_path = nil
end

#set_local_config(conf_path, must_exist = true) ⇒ Object

DEPRECATED - will be removed when old commands are gone



185
186
187
188
189
190
191
# File 'lib/rhc/config.rb', line 185

def set_local_config(conf_path, must_exist=true)
  conf_path = File.expand_path(conf_path)
  @config_path = conf_path if @opts_config_path.nil?
  @local_config = RHC::Vendor::ParseConfig.new(conf_path)
rescue Errno::EACCES => e
  raise Errno::EACCES.new "Could not open config file: #{e.message}" if must_exist
end

#set_opts_config(conf_path) ⇒ Object

DEPRECATED - needs to be renamed to something cleaner



194
195
196
197
198
199
200
# File 'lib/rhc/config.rb', line 194

def set_opts_config(conf_path)
  @opts_config_path = File.expand_path(conf_path)
  @config_path = @opts_config_path
  @opts_config = RHC::Vendor::ParseConfig.new(@opts_config_path) if File.exists?(@opts_config_path)
rescue Errno::EACCES => e
  raise Errno::EACCES.new "Could not open config file: #{e.message}"
end

#should_run_ssh_wizard?Boolean

DEPRECATED - should be moved to Helpers

Returns:

  • (Boolean)


242
243
244
# File 'lib/rhc/config.rb', line 242

def should_run_ssh_wizard?
  not File.exists? ssh_priv_key_file_path
end

#to_optionsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rhc/config.rb', line 134

def to_options
  OPTIONS.inject({}) do |h, (name, opts)|
      opts = Array(opts)
      value = self[opts[0] || name.to_s]
      if value
        value = case opts[1]
                when :integer
                  Integer(value)
                when :boolean
                  !!(value =~ /^\s*(y|yes|1|t|true)\s*$/i)
                else
                  value unless value.blank?
                end
        h[name] = value unless value.nil?
      end
      h
    end
end

#use_config(path) ⇒ Object



202
203
204
205
206
207
# File 'lib/rhc/config.rb', line 202

def use_config(path)
  path = File.expand_path(path)
  set_opts_config(path)
rescue => e
  raise ArgumentError, "Unable to read configuration file: #{e.message}", $!.backtrace
end

#usernameObject

DEPRECATED - underlying value and command option needs to be migrated to login



180
181
182
# File 'lib/rhc/config.rb', line 180

def username
  self['default_rhlogin']
end

#using_proxy?Boolean

DEPRECATED - will be removed when old commands are gone

Returns:

  • (Boolean)


290
291
292
# File 'lib/rhc/config.rb', line 290

def using_proxy?
  default_proxy.instance_variable_get(:@is_proxy_class) || false
end