class RHC::Servers
Attributes
servers[R]
Public Class Methods
new(config=nil)
click to toggle source
# File lib/rhc/servers.rb, line 77 def initialize(config=nil) @servers ||= load || [] sync_from_config(config) end
to_host(hostname)
click to toggle source
# File lib/rhc/servers.rb, line 96 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 101 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 118 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 179 def backup FileUtils.cp(path, "#{path}.bak") if File.exists? path end
default()
click to toggle source
# File lib/rhc/servers.rb, line 147 def default list.select(&:default?).first || list.first end
exists?(server)
click to toggle source
# File lib/rhc/servers.rb, line 143 def exists?(server) hostname_exists?(server) || nickname_exists?(server) end
find(server)
click to toggle source
# File lib/rhc/servers.rb, line 130 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 138 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 126 def list @servers || [] end
nickname_exists?(nickname)
click to toggle source
# File lib/rhc/servers.rb, line 134 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 88 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 92 def present? File.exists?(path) end
reload(config=nil)
click to toggle source
# File lib/rhc/servers.rb, line 82 def reload(config=nil) @servers = load || [] sync_from_config(config) self end
remove(server)
click to toggle source
# File lib/rhc/servers.rb, line 122 def remove(server) @servers.delete(find(server)) end
save!()
click to toggle source
# File lib/rhc/servers.rb, line 171 def save! FileUtils.mkdir_p File.dirname(path) File.open(path, 'w') do |c| c.puts list.collect{|s| s.persisted = true; {'server' => s.to_yaml_hash}}.to_yaml end self end
sync_from_config(config)
click to toggle source
# File lib/rhc/servers.rb, line 151 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_client_key_file => o[:ssl_client_key_file], :ssl_ca_file => o[:ssl_ca_file]) list.each do |server| server.default = server.hostname == o[:server] server.persisted = true if !present? end end end
update(server, args={})
click to toggle source
# File lib/rhc/servers.rb, line 110 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 184 def load (YAML.load_file(path) || [] rescue []).collect do |e| Server.from_yaml_hash e['server'] end