class RHC::Servers

Attributes

servers[R]

Public Class Methods

new(config=nil) click to toggle source
# File lib/rhc/servers.rb, line 70
def initialize(config=nil)
  @servers ||= load || []
  sync_from_config(config)
end
to_host(hostname) click to toggle source
# File lib/rhc/servers.rb, line 89
def self.to_host(hostname)
  uri = RHC::Helpers.to_uri(hostname)
  uri.scheme == 'https' && uri.port == URI::HTTPS::DEFAULT_PORT ? uri.host : hostname
end

Public Instance Methods

add(hostname, args={}) click to toggle source
# File lib/rhc/servers.rb, line 94
def add(hostname, args={})
  raise RHC::ServerHostnameExistsException.new(hostname) if hostname_exists?(hostname)
  raise RHC::ServerNicknameExistsException.new(args[:nickname]) if args[:nickname] && nickname_exists?(args[:nickname])

  args[:nickname] = suggest_server_nickname(Servers.to_host(hostname)) unless args[:nickname].present?

  Server.new(hostname, args).tap{ |server| @servers << server }
end
add_or_update(hostname, args={}) click to toggle source
# File lib/rhc/servers.rb, line 111
def add_or_update(hostname, args={})
  update(hostname, args) rescue add(hostname, args)
end
backup() click to toggle source
# File lib/rhc/servers.rb, line 168
def backup
  FileUtils.cp(path, "#{path}.bak") if File.exists? path
end
default() click to toggle source
# File lib/rhc/servers.rb, line 140
def default
  list.select(&:default?).first || list.first
end
exists?(server) click to toggle source
# File lib/rhc/servers.rb, line 136
def exists?(server)
  hostname_exists?(server) || nickname_exists?(server)
end
find(server) click to toggle source
# File lib/rhc/servers.rb, line 123
def find(server)
  exists?(server).tap{|s| raise RHC::ServerNotConfiguredException.new(server) unless s }
end
hostname_exists?(hostname) click to toggle source
# File lib/rhc/servers.rb, line 131
def hostname_exists?(hostname)
  hostname = Servers.to_host(hostname)
  list.select{|s| s.hostname == hostname}.first
end
list() click to toggle source
# File lib/rhc/servers.rb, line 119
def list
  @servers || []
end
nickname_exists?(nickname) click to toggle source
# File lib/rhc/servers.rb, line 127
def nickname_exists?(nickname)
  list.select{|s| s.nickname.present? && s.nickname == nickname}.first
end
path() click to toggle source
# File lib/rhc/servers.rb, line 81
def path
  File.join(RHC::Config.home_dir, '.openshift', "#{ENV['OPENSHIFT_SERVERS'].presence || 'servers'}.yml")
end
present?() click to toggle source
# File lib/rhc/servers.rb, line 85
def present?
  File.exists?(path)
end
reload(config=nil) click to toggle source
# File lib/rhc/servers.rb, line 75
def reload(config=nil)
  @servers = load || []
  sync_from_config(config)
  self
end
remove(server) click to toggle source
# File lib/rhc/servers.rb, line 115
def remove(server)
  @servers.delete(find(server))
end
save!() click to toggle source
# File lib/rhc/servers.rb, line 160
def save!
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, 'w') do |c| 
    c.puts list.collect{|s| {'server' => s.to_yaml_hash}}.to_yaml
  end
  self
end
sync_from_config(config) click to toggle source
# File lib/rhc/servers.rb, line 144
def sync_from_config(config)
  unless config.nil? || !config.has_configs_from_files?
    o = config.to_options
    add_or_update(
      o[:server], 
      :login                    => o[:rhlogin], 
      :use_authorization_tokens => o[:use_authorization_tokens],
      :insecure                 => o[:insecure],
      :timeout                  => o[:timeout],
      :ssl_version              => o[:ssl_version],
      :ssl_client_cert_file     => o[:ssl_client_cert_file],
      :ssl_ca_file              => o[:ssl_ca_file])
    list.each{|server| server.default = server.hostname == o[:server]}
  end
end
update(server, args={}) click to toggle source
# File lib/rhc/servers.rb, line 103
def update(server, args={})
  find(server).tap do |s|
    args.each do |k, v|
      s.send("#{k}=", v) unless v.nil?
    end
  end
end

Protected Instance Methods

load() click to toggle source
# File lib/rhc/servers.rb, line 173
def load
  (YAML.load_file(path) || [] rescue []).collect do |e|
    Server.from_yaml_hash e['server']
  end