lolitado 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/accessors.rb +22 -0
- data/lib/db.rb +57 -0
- data/lib/pool.rb +109 -0
- data/lib/request.rb +46 -0
- data/lib/version.rb +3 -0
- data/lib/yaml.rb +31 -0
- metadata +7 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3fd234304855f6114d300e141b4a73a32853571a76ff75b0ca9a27098fa8d75
|
4
|
+
data.tar.gz: a0e6606f7325ee49ab9a023b2960cb3cc2942e3590a093813789a3d9c16ee9dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a14021644154f53d50844fb227bc96f65c0e9f9cb2cdce35de9b247b2305596bed93e08a823d467faceb6754b1fb584c03689085513c8d58a90eb8d24a341e9f
|
7
|
+
data.tar.gz: 44059dd091f273aef49f4053e3dd0b2190e86f6b365d6485a57f45e73bfc934c34a736e753193c4ff51afe56441e831fc483cfdc9fcbeea324cf9a1d3ce32b13
|
data/lib/accessors.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
#
|
2
|
+
# Contains the class level methods that are inserted into your api class
|
3
|
+
#
|
4
|
+
module Accessors
|
5
|
+
|
6
|
+
# just for example "how to create method in accesors"
|
7
|
+
def row name, identifier
|
8
|
+
define_method("delete_#{name}") do |value|
|
9
|
+
db[identifier[:table].to_sym].filter(value).delete
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def column name, identifier
|
14
|
+
define_method("avg_#{name}") do |value|
|
15
|
+
db[identifier[:table].to_sym].filter(value).avg(identifier.values.last.to_sym)
|
16
|
+
end
|
17
|
+
define_method("query_#{name}") do |value|
|
18
|
+
db[identifier[:table].to_sym].filter(value).all
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/db.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require './accessors'
|
2
|
+
|
3
|
+
class DBFactory
|
4
|
+
extend Accessors
|
5
|
+
|
6
|
+
attr_accessor :db
|
7
|
+
|
8
|
+
def initialize db
|
9
|
+
@db = db
|
10
|
+
end
|
11
|
+
|
12
|
+
def query_duration sql, type = true, waiting_time = 10
|
13
|
+
start = Time.now
|
14
|
+
if type
|
15
|
+
result = query(sql, waiting_time)
|
16
|
+
else
|
17
|
+
result = query_empty(sql, waiting_time)
|
18
|
+
end
|
19
|
+
finish = Time.now
|
20
|
+
msecs = (finish - start) * 1000.0
|
21
|
+
hash = {'result' => result, 'duration' => msecs}
|
22
|
+
return hash
|
23
|
+
end
|
24
|
+
|
25
|
+
def query sql, waiting_time = 10
|
26
|
+
result = db[sql].all
|
27
|
+
if result.length == 0
|
28
|
+
if waiting_time != 0
|
29
|
+
sleep 1
|
30
|
+
result = query(sql, waiting_time - 1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
return result
|
34
|
+
end
|
35
|
+
|
36
|
+
def multiple_query sql
|
37
|
+
splited_sql = sql.split(';')
|
38
|
+
splited_sql.each do |each_sql|
|
39
|
+
query_empty(each_sql)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def query_empty sql, waiting_time = 10
|
44
|
+
result = db[sql].all
|
45
|
+
if result.length != 0
|
46
|
+
if waiting_time != 0
|
47
|
+
sleep 1
|
48
|
+
result = query(sql, waiting_time - 1)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
return result
|
52
|
+
end
|
53
|
+
|
54
|
+
def insert sql
|
55
|
+
db[sql]
|
56
|
+
end
|
57
|
+
end
|
data/lib/pool.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'mysql2'
|
2
|
+
require 'sequel'
|
3
|
+
require 'net/ssh/gateway'
|
4
|
+
|
5
|
+
class Pool
|
6
|
+
|
7
|
+
attr_accessor :db_pool, :ssh_pool, :file_path
|
8
|
+
|
9
|
+
def initialize file_path
|
10
|
+
@db_pool = {}
|
11
|
+
@ssh_pool = {}
|
12
|
+
@file_path = file_path
|
13
|
+
end
|
14
|
+
|
15
|
+
def use(params ={})
|
16
|
+
ssh_name = params.fetch(:ssh, false)
|
17
|
+
db_name = params.fetch(:db, false)
|
18
|
+
if db_pool[db_name].nil? && db_name
|
19
|
+
ssh_key = fetch_ssh_config db_name
|
20
|
+
unless ssh_key.nil?
|
21
|
+
ssh = create_or_use_ssh ssh_key
|
22
|
+
port = forward_port ssh, ssh_key
|
23
|
+
db = connect_database db_name, port
|
24
|
+
db_pool[db_name] = db
|
25
|
+
else
|
26
|
+
db = connect_database_directly db_name
|
27
|
+
db_pool[db_name] = db
|
28
|
+
end
|
29
|
+
elsif ssh_name
|
30
|
+
create_or_use_ssh ssh_name
|
31
|
+
else
|
32
|
+
db_pool[db_name]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_or_use_ssh name
|
37
|
+
if ssh_pool[name].nil?
|
38
|
+
ssh = connect_remote_server name
|
39
|
+
ssh_pool[name] = ssh
|
40
|
+
else
|
41
|
+
ssh_pool[name]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def connect_database name, port=false
|
46
|
+
database_file = YAML.load_file(file_path)
|
47
|
+
db_conf = database_file[name]
|
48
|
+
db_conf = db_conf.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
49
|
+
db_conf = db_conf.merge(:port => port) if port
|
50
|
+
db = get_ready_for_database db_conf
|
51
|
+
end
|
52
|
+
|
53
|
+
def get_ready_for_database conf
|
54
|
+
begin
|
55
|
+
Sequel.connect(conf)
|
56
|
+
rescue
|
57
|
+
fail "database configuration \n #{conf} \n is not correct, please double check"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def connect_remote_server name
|
62
|
+
database_file = YAML.load_file(file_path)
|
63
|
+
ssh_conf = database_file[name]
|
64
|
+
ssh_conf = ssh_conf.delete_if { |key,value| key == 'database'}
|
65
|
+
ssh = get_ready_for_ssh ssh_conf
|
66
|
+
return ssh
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_ready_for_ssh conf
|
70
|
+
host = conf.delete('host')
|
71
|
+
user = conf.delete('user')
|
72
|
+
options = conf.delete_if {|key,value| key == 'host' && 'user'}
|
73
|
+
options = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
74
|
+
begin
|
75
|
+
Net::SSH::Gateway.new(host, user, options)
|
76
|
+
rescue
|
77
|
+
fail "ssh configuration \n #{conf} \n is not correct, please double check"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def forward_port ssh, name
|
82
|
+
database_file = YAML.load_file(file_path)
|
83
|
+
ssh_conf = database_file[name]
|
84
|
+
new_conf = ssh_conf.delete('database')
|
85
|
+
host = new_conf.delete('host')
|
86
|
+
remote_port = new_conf.delete('remote_port')
|
87
|
+
local_port = new_conf.delete('local_port')
|
88
|
+
begin
|
89
|
+
ssh.open(host, remote_port, local_port)
|
90
|
+
rescue Errno::EADDRINUSE
|
91
|
+
return local_port
|
92
|
+
rescue
|
93
|
+
fail "fail to forward remote port #{remote_port} to local_port #{local_port}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def fetch_ssh_config db_name
|
98
|
+
database_file = YAML.load_file(file_path)
|
99
|
+
db_conf = database_file[db_name]
|
100
|
+
ssh_key = db_conf.delete('ssh')
|
101
|
+
end
|
102
|
+
|
103
|
+
def connect_database_directly db_name
|
104
|
+
database_file = YAML.load_file(file_path)
|
105
|
+
db_conf = database_file[db_name]
|
106
|
+
db_conf = db_conf.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
107
|
+
db = Sequel.connect(db_conf)
|
108
|
+
end
|
109
|
+
end
|
data/lib/request.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
class Request
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
attr_accessor :new_headers
|
7
|
+
# debug_output $stdout
|
8
|
+
|
9
|
+
def new_response response, msecs
|
10
|
+
new_response = {}
|
11
|
+
new_response.merge({:response => response, :message => response.parsed_response, :status => response.code, :duration => msecs})
|
12
|
+
end
|
13
|
+
|
14
|
+
def request base_uri, method, endpoint, body = false
|
15
|
+
url = base_uri + endpoint
|
16
|
+
case method
|
17
|
+
when "get"
|
18
|
+
start = Time.now
|
19
|
+
response = self.class.get(url, :body => body, :headers => new_headers, :verify => false)
|
20
|
+
finish = Time.now
|
21
|
+
when "post"
|
22
|
+
start = Time.now
|
23
|
+
response = self.class.post(url, :body => body, :headers => new_headers, :verify => false)
|
24
|
+
finish = Time.now
|
25
|
+
when "put"
|
26
|
+
start = Time.now
|
27
|
+
response = self.class.put(url, :body => body, :headers => new_headers, :verify => false)
|
28
|
+
finish = Time.now
|
29
|
+
when "delete"
|
30
|
+
start = Time.now
|
31
|
+
response = self.class.delete(url, :headers => new_headers, :verify => false)
|
32
|
+
finish = Time.now
|
33
|
+
else
|
34
|
+
puts "#{method} is invalid method."
|
35
|
+
exit
|
36
|
+
end
|
37
|
+
msecs = (finish - start) * 1000.0
|
38
|
+
return new_response(response, msecs)
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_headers value
|
42
|
+
@new_headers ||= {}
|
43
|
+
@new_headers = @new_headers.merge(value.to_hash)
|
44
|
+
return @new_headers
|
45
|
+
end
|
46
|
+
end
|
data/lib/version.rb
ADDED
data/lib/yaml.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'psych'
|
2
|
+
|
3
|
+
module Cleric
|
4
|
+
module YAML
|
5
|
+
|
6
|
+
CONFIGURE_PATH = File.expand_path("../data/stage.yml",__FILE__)
|
7
|
+
|
8
|
+
def self.fetch_corresponding_conf_by name
|
9
|
+
all_hash_values = load_yml(CONFIGURE_PATH)
|
10
|
+
conf_value = fetch_value_by all_hash_values, name
|
11
|
+
return conf_value
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.load_yml path
|
15
|
+
Psych.load_file(path)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.fetch_value_by hash, key
|
19
|
+
if hash == false
|
20
|
+
fail "no configuration in #{ENV['PLATFORM']}.yml"
|
21
|
+
else
|
22
|
+
if hash[key].nil?
|
23
|
+
fail "can not find corresponding configure value for #{key}"
|
24
|
+
else
|
25
|
+
hash[key]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lolitado
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dolores Zhang
|
@@ -73,7 +73,13 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- lib/accessors.rb
|
77
|
+
- lib/db.rb
|
76
78
|
- lib/lolitado.rb
|
79
|
+
- lib/pool.rb
|
80
|
+
- lib/request.rb
|
81
|
+
- lib/version.rb
|
82
|
+
- lib/yaml.rb
|
77
83
|
homepage: http://github.com/doloreszhang/lolitado
|
78
84
|
licenses:
|
79
85
|
- MIT
|