class RHC::Config

Public Class Methods

default() click to toggle source
# File lib/rhc/config.rb, line 7
def self.default
  @default ||= begin
    RHC::Config.new
  end.tap do |c|
    c.read_config_files
  end
end
initialize() click to toggle source
# File lib/rhc/config.rb, line 23
def self.initialize
  @default = nil
  default
end
method_missing(method, *args, &block) click to toggle source
# File lib/rhc/config.rb, line 15
def self.method_missing(method, *args, &block)
  if default.respond_to?(method)
    default.send(method, *args, &block)
  else
    raise NoMethodError, method
  end
end
new() click to toggle source
# File lib/rhc/config.rb, line 28
def initialize
  set_defaults

  _gem_cfg = File.join(File.expand_path(File.dirname(__FILE__) + "/../../conf"), @conf_name)
  @global_config_path = File.exists?(@_linux_cfg) ? @_linux_cfg : _gem_cfg
end

Public Instance Methods

[](key) click to toggle source
# File lib/rhc/config.rb, line 89
def [](key)
  raise KeyError("Please use RHC::Config.password to access the password config") if key == "password"

  # 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) click to toggle source
# File lib/rhc/config.rb, line 155
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_path() click to toggle source

#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

# File lib/rhc/config.rb, line 192
def config_path
  @config_path
end
config_user(username) click to toggle source

Public: configures the default user for this session

# File lib/rhc/config.rb, line 112
def config_user(username)
  @defaults.add('default_rhlogin', username)
end
default_proxy() click to toggle source
# File lib/rhc/config.rb, line 216
def default_proxy
  @default_proxy ||= (
    proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']
    if proxy
      if proxy !~ %r^(\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_rhlogin() click to toggle source
# File lib/rhc/config.rb, line 212
def default_rhlogin
  get_value('default_rhlogin')
end
get_value(key) click to toggle source
# File lib/rhc/config.rb, line 103
def get_value(key)
  self[key]
end
has_global_config?() click to toggle source
# File lib/rhc/config.rb, line 166
def has_global_config?
  !@global_config.nil?
end
has_local_config?() click to toggle source
# File lib/rhc/config.rb, line 170
def has_local_config?
  !@local_config.nil?
end
has_opts?() click to toggle source
# File lib/rhc/config.rb, line 178
def has_opts?
  !@opts.nil?
end
has_opts_config?() click to toggle source
# File lib/rhc/config.rb, line 174
def has_opts_config?
  !@opts_config.nil?
end
home_conf_path() click to toggle source
# File lib/rhc/config.rb, line 200
def home_conf_path
  @home_conf_path
end
home_dir() click to toggle source
# File lib/rhc/config.rb, line 204
def home_dir
  @home_dir
end
home_dir=(home_dir) click to toggle source

used for tests

# File lib/rhc/config.rb, line 79
def home_dir=(home_dir)
  @home_dir=home_dir
  @home_conf_path = File.join(@home_dir, '.openshift')
  @local_config_path = File.join(@home_conf_path, @conf_name)
  @local_config = nil
  @local_config = RHC::Vendor::ParseConfig.new(File.expand_path(@local_config_path)) if File.exists?(@local_config_path)
  @ssh_priv_key_file_path = "#{@home_dir}/.ssh/id_rsa"
  @ssh_pub_key_file_path = "#{@home_dir}/.ssh/id_rsa.pub"
end
local_config_path() click to toggle source
# File lib/rhc/config.rb, line 196
def local_config_path
  @local_config_path
end
opts_login() click to toggle source
# File lib/rhc/config.rb, line 120
def opts_login
  @opts['default_rhlogin']
end
opts_login=(username) click to toggle source
# File lib/rhc/config.rb, line 116
def opts_login=(username)
  @opts.add('default_rhlogin', username)
end
password() click to toggle source
# File lib/rhc/config.rb, line 129
def password
  @opts['password']
end
password=(password) click to toggle source

password is not allowed in config files and can only be passed on comman line

# File lib/rhc/config.rb, line 125
def password=(password)
  @opts.add('password', password)
end
proxy_vars() click to toggle source
# File lib/rhc/config.rb, line 236
def proxy_vars
  Hash[[:address,:user,:pass,:port].map do |x|
    [x,default_proxy.instance_variable_get("@proxy_#{x}")]
  end]
end
read_config_files() click to toggle source
# File lib/rhc/config.rb, line 35
def read_config_files
  @global_config = RHC::Vendor::ParseConfig.new(@global_config_path) if File.exists?(@global_config_path)
  @local_config = RHC::Vendor::ParseConfig.new(File.expand_path(@local_config_path)) if File.exists?(@local_config_path)
rescue Errno::EACCES => e
  raise Errno::EACCES.new("Could not open config file: #{e.message}")
end
set_defaults() click to toggle source
# File lib/rhc/config.rb, line 42
def set_defaults
  @defaults = RHC::Vendor::ParseConfig.new()
  @global_config = nil
  @local_config = nil
  @opts_config = nil # config file passed in the options
  @opts  = RHC::Vendor::ParseConfig.new() # option switches that override config file
  @default_proxy = nil
  @env_config = RHC::Vendor::ParseConfig.new()

  @defaults.add('libra_server', 'openshift.redhat.com')
  @env_config.add('libra_server', ENV['LIBRA_SERVER']) if ENV['LIBRA_SERVER']
  #
  # Config paths... /etc/openshift/express.conf or $GEM/conf/express.conf -> ~/.openshift/express.conf
  #
  @conf_name = 'express.conf'
  @home_dir = File.expand_path("~")
  @home_conf_path = File.join(@home_dir, '.openshift')
  @local_config_path = File.join(@home_conf_path, @conf_name)

  # config path passed in on the command line
  @opts_config_path = nil

  # authoritive config path
  # this can be @local_config_path or @opts_config_path
  # @opts_config_path trumps
  # this is used to determine where config options should be written to
  # when a script modifies the config such as in rhc setup
  @config_path = @local_config_path

  @ssh_priv_key_file_path = "#{@home_dir}/.ssh/id_rsa"
  @ssh_pub_key_file_path = "#{@home_dir}/.ssh/id_rsa.pub"

  @_linux_cfg = '/etc/openshift/' + @conf_name
  @global_config_path = @_linux_cfg
end
set_local_config(confpath, must_exist=true) click to toggle source
# File lib/rhc/config.rb, line 133
def set_local_config(confpath, must_exist=true)
  begin
    @local_config_path = File.expand_path(confpath)
    @config_path = @local_config_path if @opts_config_path.nil?
    @local_config = RHC::Vendor::ParseConfig.new(@local_config_path)
  rescue Errno::EACCES => e
    if must_exist
      raise Errno::EACCES.new "Could not open config file: #{e.message}"
    end
  end
end
set_opts_config(confpath) click to toggle source
# File lib/rhc/config.rb, line 145
def set_opts_config(confpath)
  begin
    @opts_config_path = File.expand_path(confpath)
    @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
end
should_run_ssh_wizard?() click to toggle source
# File lib/rhc/config.rb, line 182
def should_run_ssh_wizard?
  not File.exists? @ssh_priv_key_file_path
end
ssh_pub_key_file_path() click to toggle source
# File lib/rhc/config.rb, line 208
def ssh_pub_key_file_path
  @ssh_pub_key_file_path
end
username() click to toggle source
# File lib/rhc/config.rb, line 107
def username
  self['default_rhlogin']
end
using_proxy?() click to toggle source
# File lib/rhc/config.rb, line 232
def using_proxy?
  default_proxy.instance_variable_get(:@is_proxy_class) || false
end