cpruntime 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/CPRuntime.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+
3
+ home = File.dirname(__FILE__)
4
+ $:.unshift(File.dirname(__FILE__))
5
+ require 'credential'
6
+
7
+ #require 'cpruntime/rabbitmqclient'
8
+ #require 'cpruntime/mongoclient'
9
+ #require 'cpruntime/redisclient'
10
+ #require 'cpruntime/memcacheclient'
11
+
12
+ module CPRuntime
13
+ def credential_map(service_name)
14
+ credential_map = {}
15
+ svc_env = ENV['VCAP_SERVICES']
16
+ #svc_env = '{"mongodb-2.0.20":[{"name":"sz-dedicated-v2_73_mongodb64-ubuntu1004_suzhen","label":"mongodb-2.0.20","plan":"free","credentials":[{"username":"scalr","host":"ec2-23-20-254-0.compute-1.amazonaws.com","password":"uUfigF6Dlq","name":"sz-dedicated-v2_73_mongodb64-ubuntu1004_suzhen","db":"cloudpidb","hostname":"ec2-23-20-254-0.compute-1.amazonaws.com","type":"slave","port":27017},{"username":"scalr","host":"ec2-107-22-7-246.compute-1.amazonaws.com","password":"uUfigF6Dlq","name":"sz-dedicated-v2_73_mongodb64-ubuntu1004_suzhen","db":"cloudpidb","hostname":"ec2-107-22-7-246.compute-1.amazonaws.com","type":"slave","port":27017}]}]}'
17
+ services = JSON.parse(svc_env, :symbolize_names => true)
18
+ services.values.map { |srvs|
19
+ srvs.map{ |svc|
20
+ if svc[:label] =~ /^#{service_name}/
21
+ for i in 0..svc[:credentials].length-1 do
22
+ c_map = svc[:credentials][i]
23
+ c = Credential.new
24
+ c.username = c_map[:username]
25
+ c.password = c_map[:password]
26
+ c.host = c_map[:host]!=nil ? c_map[:host] : c_map[:host_ip]
27
+ c.port = c_map[:port]
28
+ c.vhost = c_map[:vhost]
29
+ c.db = c_map[:db]!=nil ? c_map[:db] : c_map[:name]
30
+ credential_map[c.host] = c
31
+ # p c.host
32
+ end
33
+ end
34
+ }
35
+ }
36
+ return credential_map
37
+ end
38
+ end
39
+
40
+ #CPRuntime::MongoClient.new.create_from_svc("ec2-23-20-254-0.compute-1.amazonaws.com")
data/lib/Credential.rb ADDED
@@ -0,0 +1,17 @@
1
+
2
+ class Credential
3
+ attr_accessor :host,:port,:db,:username,:password,:vhost
4
+ def to_str
5
+ p @host
6
+ p @port
7
+ p @db
8
+ p @username
9
+ p @password
10
+ p @vhost
11
+ end
12
+ end
13
+
14
+ #
15
+ #c = Credential.new
16
+ #c.hostname= 1
17
+ #c.to_str
@@ -0,0 +1,21 @@
1
+ require 'memcache'
2
+ require File.join(File.dirname(__FILE__), '../cpruntime')
3
+
4
+ module CPRuntime
5
+ class MemcacheClient
6
+
7
+ end
8
+ end
9
+
10
+ class CPRuntime::MemcacheClient
11
+ include CPRuntime
12
+ def create_from_svc(hostname)
13
+ m = credential_map("membase")
14
+ c = m[hostname];
15
+ p c
16
+ client = MemCache.new(c.host+":"+c.port)
17
+ return client
18
+ end
19
+ end
20
+
21
+ #CPRuntime::MemcacheClient.new.create_from_svc("ec2-23-20-254-0.compute-1.amazonaws.com")
@@ -0,0 +1,28 @@
1
+ require 'mongo'
2
+
3
+ #$:.unshift(File.dirname(__FILE__))
4
+
5
+ require File.join(File.dirname(__FILE__), '../cpruntime')
6
+
7
+ module CPRuntime
8
+ class MongoClient
9
+
10
+ end
11
+ end
12
+
13
+ class CPRuntime::MongoClient
14
+ include CPRuntime
15
+ def create_from_svc(hostname)
16
+ m = credential_map("mongo")
17
+ c = m[hostname];
18
+ p c
19
+ db = Mongo::Connection.new(c.host,c.port).db(c.db)
20
+ auth = db.authenticate(c.username,c.password)
21
+ return db
22
+ end
23
+ end
24
+
25
+ #db = CPRuntime::MongoClient.new.create_from_svc("ec2-23-20-254-0.compute-1.amazonaws.com")
26
+ #coll = db.collection("users")
27
+ #coll.insert("name" => "tian","age" => "25")
28
+ #p coll.find("name" => "tian").to_a.to_s
@@ -0,0 +1,19 @@
1
+ require 'mysql2'
2
+ require File.join(File.dirname(__FILE__), '../cpruntime')
3
+
4
+ module CPRuntime
5
+ class MySql2Client
6
+ end
7
+ end
8
+
9
+ class CPRuntime::MySql2Client
10
+ include CPRuntime
11
+ def create_from_svc(hostname)
12
+ m = credential_map("mysql")
13
+ c = m[hostname]
14
+ p c
15
+ client = Mysql2::Client.new(:host => c.host,:username => c.username,:password=>c.password,:database=>c.db,:port=>c.port)
16
+ end
17
+ end
18
+
19
+ #CloudpiRuntime::MySql2Client.new.create_from_svc("ec2-23-20-254-0.compute-1.amazonaws.com")
@@ -0,0 +1,21 @@
1
+ require 'bunny'
2
+ require File.join(File.dirname(__FILE__), '../cpruntime')
3
+
4
+ module CPRuntime
5
+ class RabbitMQClient
6
+
7
+ end
8
+ end
9
+
10
+ class CPRuntime::RabbitMQClient
11
+ include CPRuntime
12
+ def create_from_svc(hostname)
13
+ m = credential_map("rabbit")
14
+ c = m[hostname];
15
+ p c
16
+ client = Bunny.new(:host => c.host,:port => c.port,:pass => c.password,:user=>c.username,:vhost=>c.vhost)
17
+ return client
18
+ end
19
+ end
20
+
21
+ #CloudpiRuntime::RabbitMQClient.new.create_from_svc("ec2-23-20-254-0.compute-1.amazonaws.com")
@@ -0,0 +1,21 @@
1
+ require 'redis'
2
+ require File.join(File.dirname(__FILE__), '../cpruntime')
3
+
4
+ module CPRuntime
5
+ class RedisClient
6
+
7
+ end
8
+ end
9
+
10
+ class CPRuntime::RedisClient
11
+ include CPRuntime
12
+ def create_from_svc(hostname)
13
+ m = credential_map("redis")
14
+ c = m[hostname];
15
+ p c
16
+ client = Redis.new(:host => c.host,:port => c.port,:password => c.password)
17
+ return client
18
+ end
19
+ end
20
+
21
+ #CloudpiRuntime::RedisClient.new.create_from_svc("ec2-23-20-254-0.compute-1.amazonaws.com")
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpruntime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - xiaoliang tian
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-09 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: get service from cloudpi
15
+ email: xiaoliang.tian@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/CPRuntime.rb
21
+ - lib/Credential.rb
22
+ - lib/cpruntime/MemcacheClient.rb
23
+ - lib/cpruntime/MongoClient.rb
24
+ - lib/cpruntime/MySql2Client.rb
25
+ - lib/cpruntime/RabbitMQClient.rb
26
+ - lib/cpruntime/RedisClient.rb
27
+ homepage: http://www.samsungcloud.org
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.7.2
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: support for cloudpi service
51
+ test_files: []