cf-runtime 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cfruntime.rb +4 -0
- data/lib/cfruntime/amqp.rb +1 -0
- data/lib/cfruntime/aws_s3.rb +43 -0
- data/lib/cfruntime/okjson.rb +3 -2
- data/lib/cfruntime/parser.rb +7 -0
- data/lib/cfruntime/parser/blob_parser.rb +15 -0
- data/lib/cfruntime/parser/default_parser.rb +21 -0
- data/lib/cfruntime/parser/mongodb_parser.rb +32 -0
- data/lib/cfruntime/parser/mysql_parser.rb +19 -0
- data/lib/cfruntime/parser/postgresql_parser.rb +19 -0
- data/lib/cfruntime/parser/rabbitmq_parser.rb +34 -0
- data/lib/cfruntime/parser/redis_parser.rb +19 -0
- data/lib/cfruntime/properties.rb +12 -48
- data/lib/cfruntime/version.rb +1 -1
- metadata +27 -2
data/lib/cfruntime.rb
CHANGED
data/lib/cfruntime/amqp.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'aws/s3'
|
2
|
+
require 'cfruntime/properties'
|
3
|
+
module CFRuntime
|
4
|
+
class AWSS3Client
|
5
|
+
|
6
|
+
# Creates and returns an AWS-S3 +Client+ instance.
|
7
|
+
# Passes optional Hash of non-connection-related options to +AWS::S3::Base.establish_connection!+.
|
8
|
+
# Raises +ArgumentError+ If zero or multiple blob services are found.
|
9
|
+
def self.create(options={})
|
10
|
+
service_names = CloudApp.service_names_of_type('blob')
|
11
|
+
if service_names.length != 1
|
12
|
+
raise ArgumentError.new("Expected 1 service of blob type, " +
|
13
|
+
"but found #{service_names.length}. " +
|
14
|
+
"Consider using create_from_svc(service_name) instead.")
|
15
|
+
end
|
16
|
+
create_from_svc(service_names[0],options)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Creates and returns a AWS-S3 +Client+ instance connected to a blob service with the
|
20
|
+
# specified name.
|
21
|
+
# Passes optional Hash of non-connection-related options to +AWS::S3::Base.establish_connection!+.
|
22
|
+
# Raises +ArgumentError+ If specified blob service is not found.
|
23
|
+
def self.create_from_svc(service_name, options={})
|
24
|
+
AWS::S3::Base.establish_connection!(options_for_svc(service_name,options))
|
25
|
+
end
|
26
|
+
|
27
|
+
# Merges provided options with connection options for specified blob service.
|
28
|
+
# Returns merged Hash containing (:access_key_id, :secret_access_key, :server, :port).
|
29
|
+
# Raises +ArgumentError+ If specified blob service is not found.
|
30
|
+
def self.options_for_svc(service_name,options={})
|
31
|
+
service_props = CFRuntime::CloudApp.service_props(service_name)
|
32
|
+
if service_props.nil?
|
33
|
+
raise ArgumentError.new("Service with name #{service_name} not found")
|
34
|
+
end
|
35
|
+
cfoptions = options
|
36
|
+
cfoptions[:server] = service_props[:host]
|
37
|
+
cfoptions[:port] = service_props[:port]
|
38
|
+
cfoptions[:access_key_id ] = service_props[:username]
|
39
|
+
cfoptions[:secret_access_key] = service_props[:password]
|
40
|
+
cfoptions
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/cfruntime/okjson.rb
CHANGED
@@ -27,10 +27,10 @@ require 'stringio'
|
|
27
27
|
# Some parts adapted from
|
28
28
|
# http://golang.org/src/pkg/json/decode.go and
|
29
29
|
# http://golang.org/src/pkg/utf8/utf8.go
|
30
|
-
module CFRuntime
|
30
|
+
module CFRuntime
|
31
|
+
module OkJson
|
31
32
|
extend self
|
32
33
|
|
33
|
-
|
34
34
|
# Decodes a json document in string s and
|
35
35
|
# returns the corresponding ruby value.
|
36
36
|
# String s must be valid UTF-8. If you have
|
@@ -593,4 +593,5 @@ module CFRuntime::OkJson
|
|
593
593
|
|
594
594
|
Spc = ' '[0]
|
595
595
|
Unesc = {?b=>?\b, ?f=>?\f, ?n=>?\n, ?r=>?\r, ?t=>?\t}
|
596
|
+
end
|
596
597
|
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require "cfruntime/parser/blob_parser"
|
2
|
+
require "cfruntime/parser/default_parser"
|
3
|
+
require "cfruntime/parser/mongodb_parser"
|
4
|
+
require "cfruntime/parser/mysql_parser"
|
5
|
+
require "cfruntime/parser/postgresql_parser"
|
6
|
+
require "cfruntime/parser/rabbitmq_parser"
|
7
|
+
require "cfruntime/parser/redis_parser"
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class BlobParser
|
3
|
+
def self.parse(svc)
|
4
|
+
serviceopts = {}
|
5
|
+
{ :username => :username,
|
6
|
+
:password => :password,
|
7
|
+
:hostname => :host,
|
8
|
+
:port => :port
|
9
|
+
}.each do |from, to|
|
10
|
+
serviceopts[to] = svc["credentials"][from.to_s]
|
11
|
+
end
|
12
|
+
serviceopts
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class DefaultParser
|
3
|
+
# Default parsing behavior simply returns
|
4
|
+
# the passed-in map with all keys converted
|
5
|
+
# to symbols
|
6
|
+
def self.parse(svc)
|
7
|
+
symbolize_keys(svc)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
def self.symbolize_keys(hash)
|
12
|
+
if hash.is_a? Hash
|
13
|
+
new_hash = {}
|
14
|
+
hash.each {|k, v| new_hash[k.to_sym] = symbolize_keys(v) }
|
15
|
+
new_hash
|
16
|
+
else
|
17
|
+
hash
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class MongodbParser
|
3
|
+
def self.parse(svc)
|
4
|
+
serviceopts = {}
|
5
|
+
cred = svc["credentials"]
|
6
|
+
if cred["url"]
|
7
|
+
uri=URI.parse(cred["url"])
|
8
|
+
user=URI.unescape(uri.user) if uri.user
|
9
|
+
passwd=URI.unescape(uri.password) if uri.password
|
10
|
+
host=uri.host
|
11
|
+
port=uri.port
|
12
|
+
if uri.path =~ %r{^/(.*)}
|
13
|
+
raise ArgumentError.new("multiple segments in path of mongo URI: #{uri}") if $1.index('/')
|
14
|
+
db = URI.unescape($1)
|
15
|
+
end
|
16
|
+
url = cred["url"]
|
17
|
+
else
|
18
|
+
# The old credentials format with no URL
|
19
|
+
user,passwd,host,port, db = %w(username password hostname port db).map {|key|
|
20
|
+
cred[key]}
|
21
|
+
url = "mongodb://#{user}:#{passwd}@#{host}:#{port}/#{db}"
|
22
|
+
end
|
23
|
+
serviceopts[:username] = user
|
24
|
+
serviceopts[:password] = passwd
|
25
|
+
serviceopts[:host] = host
|
26
|
+
serviceopts[:port] = port
|
27
|
+
serviceopts[:url] = url
|
28
|
+
serviceopts[:db] = db
|
29
|
+
serviceopts
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class MysqlParser
|
3
|
+
def self.parse(svc)
|
4
|
+
serviceopts = {}
|
5
|
+
{ :username => :username,
|
6
|
+
:password => :password,
|
7
|
+
:hostname => :host,
|
8
|
+
:port => :port,
|
9
|
+
:name => :database
|
10
|
+
}.each do |from, to|
|
11
|
+
serviceopts[to] = svc["credentials"][from.to_s]
|
12
|
+
end
|
13
|
+
serviceopts[:url] = svc["credentials"]["url"] ||
|
14
|
+
"mysql://#{serviceopts[:username]}:#{serviceopts[:password]}@" +
|
15
|
+
"#{serviceopts[:host]}:#{serviceopts[:port]}/#{serviceopts[:database]}"
|
16
|
+
serviceopts
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class PostgresqlParser
|
3
|
+
def self.parse(svc)
|
4
|
+
serviceopts = {}
|
5
|
+
{ :username => :username,
|
6
|
+
:password => :password,
|
7
|
+
:hostname => :host,
|
8
|
+
:port => :port,
|
9
|
+
:name => :database
|
10
|
+
}.each do |from, to|
|
11
|
+
serviceopts[to] = svc["credentials"][from.to_s]
|
12
|
+
end
|
13
|
+
serviceopts[:url] = svc["credentials"]["url"] ||
|
14
|
+
"postgresql://#{serviceopts[:username]}:#{serviceopts[:password]}@" +
|
15
|
+
"#{serviceopts[:host]}:#{serviceopts[:port]}/#{serviceopts[:database]}"
|
16
|
+
serviceopts
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class RabbitmqParser
|
3
|
+
def self.parse(svc)
|
4
|
+
serviceopts = {}
|
5
|
+
cred = svc["credentials"]
|
6
|
+
vhost = cred["vhost"] || "/" #The RabbitMQ default vhost
|
7
|
+
if cred["url"]
|
8
|
+
# The new "srs" credentials format
|
9
|
+
uri=URI.parse(cred["url"])
|
10
|
+
user=URI.unescape(uri.user) if uri.user
|
11
|
+
passwd=URI.unescape(uri.password) if uri.password
|
12
|
+
host=uri.host
|
13
|
+
port=uri.port
|
14
|
+
if uri.path =~ %r{^/(.*)}
|
15
|
+
raise ArgumentError.new("multiple segments in path of amqp URI: #{uri}") if $1.index('/')
|
16
|
+
vhost = URI.unescape($1)
|
17
|
+
end
|
18
|
+
serviceopts[:url] = cred["url"]
|
19
|
+
else
|
20
|
+
# The "old" credentials format
|
21
|
+
user,passwd,host,port = %w(user pass hostname port).map {|key|
|
22
|
+
cred[key]}
|
23
|
+
serviceopts[:url] = "amqp://#{user}:#{passwd}@#{host}:#{port}"
|
24
|
+
serviceopts[:url] = "#{serviceopts[:url]}/#{vhost}" if vhost != "/"
|
25
|
+
end
|
26
|
+
serviceopts[:username] = user
|
27
|
+
serviceopts[:password] = passwd
|
28
|
+
serviceopts[:host] = host
|
29
|
+
serviceopts[:port] = port
|
30
|
+
serviceopts[:vhost] = vhost
|
31
|
+
serviceopts
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CFRuntime
|
2
|
+
class RedisParser
|
3
|
+
def self.parse(svc)
|
4
|
+
serviceopts = {}
|
5
|
+
{ :username => :username,
|
6
|
+
:password => :password,
|
7
|
+
:hostname => :host,
|
8
|
+
:port => :port,
|
9
|
+
:name => :database
|
10
|
+
}.each do |from, to|
|
11
|
+
serviceopts[to] = svc["credentials"][from.to_s]
|
12
|
+
end
|
13
|
+
serviceopts[:url] = svc["credentials"]["url"] ||
|
14
|
+
"redis://#{serviceopts[:username]}:#{serviceopts[:password]}@" +
|
15
|
+
"#{serviceopts[:host]}:#{serviceopts[:port]}/#{serviceopts[:database]}"
|
16
|
+
serviceopts
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/cfruntime/properties.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require File.join(File.dirname(__FILE__), 'okjson')
|
1
|
+
require "uri"
|
2
|
+
require "cfruntime/okjson"
|
3
|
+
require "cfruntime/parser"
|
5
4
|
|
5
|
+
module CFRuntime
|
6
6
|
class CloudApp
|
7
|
-
|
8
7
|
class << self
|
9
8
|
# Returns true if this code is running on Cloud Foundry
|
10
9
|
def running_in_cloud?()
|
@@ -29,54 +28,21 @@ module CFRuntime
|
|
29
28
|
# of a specified type are found.
|
30
29
|
def service_props(service_name)
|
31
30
|
registered_svcs = {}
|
32
|
-
|
33
|
-
svcs = CFRuntime::OkJson.decode(ENV['VCAP_SERVICES'])
|
34
|
-
else
|
35
|
-
svcs = {}
|
36
|
-
end
|
31
|
+
svcs = ENV['VCAP_SERVICES'] ? CFRuntime::OkJson.decode(ENV['VCAP_SERVICES']) : {}
|
37
32
|
svcs.each do |key,list|
|
38
33
|
label, version = key.split('-')
|
34
|
+
begin
|
35
|
+
parser = Object.const_get("CFRuntime").const_get("#{label.capitalize}Parser")
|
36
|
+
rescue NameError
|
37
|
+
parser = Object.const_get("CFRuntime").const_get("DefaultParser")
|
38
|
+
end
|
39
39
|
list.each do |svc|
|
40
40
|
name = svc["name"]
|
41
41
|
serviceopts = {}
|
42
42
|
serviceopts[:label] = label
|
43
43
|
serviceopts[:version] = version
|
44
44
|
serviceopts[:name] = name
|
45
|
-
|
46
|
-
if label =~ /rabbitmq/
|
47
|
-
if cred['url']
|
48
|
-
#The RabbitMQ default vhost
|
49
|
-
vhost = '/'
|
50
|
-
# The new "srs" credentials format
|
51
|
-
uri=URI.parse(cred['url'])
|
52
|
-
user=URI.unescape(uri.user) if uri.user
|
53
|
-
passwd=URI.unescape(uri.password) if uri.password
|
54
|
-
host=uri.host
|
55
|
-
port=uri.port
|
56
|
-
if uri.path =~ %r{^/(.*)}
|
57
|
-
raise ArgumentError.new("multiple segments in path of amqp URI: #{uri}") if $1.index('/')
|
58
|
-
vhost = URI.unescape($1)
|
59
|
-
end
|
60
|
-
serviceopts[:url] = cred['url']
|
61
|
-
else
|
62
|
-
# The "old" credentials format
|
63
|
-
user,passwd,host,port,vhost = %w(user pass hostname port vhost).map {|key|
|
64
|
-
cred[key]}
|
65
|
-
end
|
66
|
-
serviceopts[:vhost] = vhost
|
67
|
-
else
|
68
|
-
user,passwd,host,port,dbname,db = %w(username password hostname port name db).map {|key|
|
69
|
-
cred[key]}
|
70
|
-
if label == "mongodb"
|
71
|
-
serviceopts[:db] = db
|
72
|
-
else
|
73
|
-
serviceopts[:database] = dbname
|
74
|
-
end
|
75
|
-
end
|
76
|
-
serviceopts[:username] = user
|
77
|
-
serviceopts[:password] = passwd
|
78
|
-
serviceopts[:host] = host
|
79
|
-
serviceopts[:port] = port
|
45
|
+
serviceopts.merge!(parser.parse(svc))
|
80
46
|
registered_svcs[name] = serviceopts
|
81
47
|
if list.count == 1
|
82
48
|
registered_svcs[label] = serviceopts
|
@@ -118,7 +84,5 @@ module CFRuntime
|
|
118
84
|
service_names
|
119
85
|
end
|
120
86
|
end
|
121
|
-
|
122
87
|
end
|
123
|
-
|
124
|
-
end
|
88
|
+
end
|
data/lib/cfruntime/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cf-runtime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|
@@ -107,6 +107,22 @@ dependencies:
|
|
107
107
|
- - ~>
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: 0.2.7
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: aws-s3
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.6.3
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.6.3
|
110
126
|
- !ruby/object:Gem::Dependency
|
111
127
|
name: rake
|
112
128
|
requirement: !ruby/object:Gem::Requirement
|
@@ -214,10 +230,19 @@ files:
|
|
214
230
|
- LICENSE
|
215
231
|
- README.md
|
216
232
|
- lib/cfruntime/amqp.rb
|
233
|
+
- lib/cfruntime/aws_s3.rb
|
217
234
|
- lib/cfruntime/carrot.rb
|
218
235
|
- lib/cfruntime/mongodb.rb
|
219
236
|
- lib/cfruntime/mysql.rb
|
220
237
|
- lib/cfruntime/okjson.rb
|
238
|
+
- lib/cfruntime/parser/blob_parser.rb
|
239
|
+
- lib/cfruntime/parser/default_parser.rb
|
240
|
+
- lib/cfruntime/parser/mongodb_parser.rb
|
241
|
+
- lib/cfruntime/parser/mysql_parser.rb
|
242
|
+
- lib/cfruntime/parser/postgresql_parser.rb
|
243
|
+
- lib/cfruntime/parser/rabbitmq_parser.rb
|
244
|
+
- lib/cfruntime/parser/redis_parser.rb
|
245
|
+
- lib/cfruntime/parser.rb
|
221
246
|
- lib/cfruntime/postgres.rb
|
222
247
|
- lib/cfruntime/properties.rb
|
223
248
|
- lib/cfruntime/redis.rb
|