Class: Lolitado::Pool
- Inherits:
-
Object
- Object
- Lolitado::Pool
- Defined in:
- lib/lolitado/pool.rb
Instance Attribute Summary collapse
-
#db_pool ⇒ Object
Returns the value of attribute db_pool.
-
#file ⇒ Object
Returns the value of attribute file.
-
#ssh_pool ⇒ Object
Returns the value of attribute ssh_pool.
Instance Method Summary collapse
-
#connect_database(conf, port = false) ⇒ Object
connect databse.
-
#connect_database_by_proxy(conf) ⇒ Object
connect database by ssh proxy.
-
#connect_remote_server(name) ⇒ Object
connect remote server.
-
#forward_port(conf) ⇒ Object
forward port.
-
#get_db(name) ⇒ Object
return existing db object or create new db object.
-
#get_ssh(name) ⇒ Object
return existing ssh object or create new ssh object.
-
#initialize(file_path) ⇒ Pool
constructor
initialize Lolitado::Pool, generate db_pool, ssh_pool object to use.
-
#use(params = {}) ⇒ Object
use existing db or ssh pool or switch to another db and ssh pool.
Constructor Details
#initialize(file_path) ⇒ Pool
initialize Lolitado::Pool, generate db_pool, ssh_pool object to use
17 18 19 20 21 |
# File 'lib/lolitado/pool.rb', line 17 def initialize file_path @db_pool = {} @ssh_pool = {} @file = Psych.load_file(file_path) end |
Instance Attribute Details
#db_pool ⇒ Object
Returns the value of attribute db_pool.
10 11 12 |
# File 'lib/lolitado/pool.rb', line 10 def db_pool @db_pool end |
#file ⇒ Object
Returns the value of attribute file.
10 11 12 |
# File 'lib/lolitado/pool.rb', line 10 def file @file end |
#ssh_pool ⇒ Object
Returns the value of attribute ssh_pool.
10 11 12 |
# File 'lib/lolitado/pool.rb', line 10 def ssh_pool @ssh_pool end |
Instance Method Details
#connect_database(conf, port = false) ⇒ Object
connect databse
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lolitado/pool.rb', line 91 def connect_database conf, port = false conf.delete('proxy') if port conf = conf.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} conf = conf.merge(:port => port) if port begin Sequel.connect(conf) rescue fail "database configuration \n #{conf} \n is not correct, please double check" end end |
#connect_database_by_proxy(conf) ⇒ Object
connect database by ssh proxy
80 81 82 83 |
# File 'lib/lolitado/pool.rb', line 80 def connect_database_by_proxy conf port = forward_port conf['proxy'] connect_database conf, port end |
#connect_remote_server(name) ⇒ Object
connect remote server
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lolitado/pool.rb', line 107 def connect_remote_server name ssh_conf = file[name] host = ssh_conf.delete('host') user = ssh_conf.delete('user') = ssh_conf.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} begin Net::SSH::Gateway.new(host, user, ) rescue fail "ssh configuration \n #{ssh_conf} \n is not correct, please double check" end end |
#forward_port(conf) ⇒ Object
forward port
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/lolitado/pool.rb', line 124 def forward_port conf ssh = get_ssh conf['ssh'] host = conf['database']['host'] remote_port = conf['database']['remote_port'] local_port = conf['database']['local_port'] # puts "forward remote port #{remote_port} to local port #{local_port}" begin ssh.open(host, remote_port, local_port) rescue Errno::EADDRINUSE return local_port rescue fail "fail to forward remote port #{remote_port} to local_port #{local_port}" end end |
#get_db(name) ⇒ Object
return existing db object or create new db object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lolitado/pool.rb', line 48 def get_db name if db_pool[name].nil? db_conf = file[name] if db_conf['proxy'].nil? db = connect_database db_conf else db = connect_database_by_proxy db_conf end db_pool[name] = Database.new(db) end return db_pool[name] end |
#get_ssh(name) ⇒ Object
return existing ssh object or create new ssh object
66 67 68 69 70 71 72 73 |
# File 'lib/lolitado/pool.rb', line 66 def get_ssh name if ssh_pool[name].nil? ssh = connect_remote_server name ssh_pool[name] = ssh else ssh_pool[name] end end |
#use(params = {}) ⇒ Object
use existing db or ssh pool or switch to another db and ssh pool
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lolitado/pool.rb', line 31 def use(params = {}) ssh_name = params.fetch(:ssh, false) db_name = params.fetch(:db, false) if db_name return get_db db_name elsif ssh_name return get_ssh ssh_name else fail "Please provide :ssh or :db" end end |