Permalink
Browse files

Add cf-runtime clients

- Add classes to create Mongo, Redis, AMQP, Carrot,
MySQL, and Postgresql connections to CF services

- Fix auto-reconfig log messages if 0 or more than
one services of a type are found

- Add cfruntime rdoc

- Move supported version constants to AutoReconfiguration module

- Remove complex passing of URL argument during AMQP auto-config

- Null path option during Redis auto-config

Change-Id: Iccab8a424dbb6e73b94feb6057859e17020005c5
  • Loading branch information...
Jennifer Hickey
Jennifer Hickey committed Dec 13, 2011
1 parent 25777a8 commit af4661218c4f7d4c079c397fa8b7801515af53ea
Showing with 1,042 additions and 202 deletions.
  1. +1 −0 auto-reconfiguration/cf-autoconfig.gemspec
  2. +0 −1 auto-reconfiguration/lib/cfautoconfig.rb
  3. +9 −7 auto-reconfiguration/lib/cfautoconfig/document/mongodb.rb
  4. +3 −3 auto-reconfiguration/lib/cfautoconfig/document/mongodb_configurer.rb
  5. +19 −24 auto-reconfiguration/lib/cfautoconfig/keyvalue/redis.rb
  6. +2 −3 auto-reconfiguration/lib/cfautoconfig/keyvalue/redis_configurer.rb
  7. +25 −34 auto-reconfiguration/lib/cfautoconfig/messaging/amqp.rb
  8. +3 −3 auto-reconfiguration/lib/cfautoconfig/messaging/amqp_configurer.rb
  9. +19 −22 auto-reconfiguration/lib/cfautoconfig/messaging/carrot.rb
  10. +3 −3 auto-reconfiguration/lib/cfautoconfig/messaging/carrot_configurer.rb
  11. +6 −9 auto-reconfiguration/lib/cfautoconfig/relational/mysql.rb
  12. +34 −22 auto-reconfiguration/lib/cfautoconfig/relational/postgres.rb
  13. +6 −6 auto-reconfiguration/lib/cfautoconfig/relational/postgres_configurer.rb
  14. +35 −0 auto-reconfiguration/spec/unit/document/mongo_configurer_spec.rb
  15. +18 −1 auto-reconfiguration/spec/unit/keyvalue/redis_configurer_spec.rb
  16. +17 −7 auto-reconfiguration/spec/unit/messaging/amqp_configurer_spec.rb
  17. +16 −17 auto-reconfiguration/spec/unit/messaging/carrot_configurer_spec.rb
  18. +22 −0 auto-reconfiguration/spec/unit/relational/mysql_configurer_spec.rb
  19. +17 −8 auto-reconfiguration/spec/unit/relational/postgres_configurer_spec.rb
  20. +7 −0 cfruntime/cf-runtime.gemspec
  21. +8 −5 cfruntime/lib/cfruntime.rb
  22. +43 −0 cfruntime/lib/cfruntime/amqp.rb
  23. +44 −0 cfruntime/lib/cfruntime/carrot.rb
  24. +58 −0 cfruntime/lib/cfruntime/mongodb.rb
  25. +44 −0 cfruntime/lib/cfruntime/mysql.rb
  26. +50 −0 cfruntime/lib/cfruntime/postgres.rb
  27. +26 −10 cfruntime/lib/cfruntime/properties.rb
  28. +44 −0 cfruntime/lib/cfruntime/redis.rb
  29. +0 −2 cfruntime/lib/cfruntime/version.rb
  30. +82 −0 cfruntime/spec/amqp_spec.rb
  31. +66 −0 cfruntime/spec/carrot_spec.rb
  32. +78 −0 cfruntime/spec/mongodb_spec.rb
  33. +74 −0 cfruntime/spec/mysql_spec.rb
  34. +61 −0 cfruntime/spec/postgres_spec.rb
  35. +6 −8 cfruntime/spec/properties_spec.rb
  36. +73 −0 cfruntime/spec/redis_spec.rb
  37. +23 −7 cfruntime/spec/spec_helper.rb
@@ -13,6 +13,7 @@ spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.extra_rdoc_files = ["LICENSE"]
+ s.rdoc_options = ["-N", "--tab-width=2", "--exclude='cf-autoconfig.gemspec|spec'"]
s.add_dependency "cf-runtime"
@@ -1,3 +1,2 @@
-# Copyright (c) 2009-2011 VMware, Inc.
require 'cfautoconfig/configurer'
require 'cfautoconfig/version'
@@ -1,6 +1,7 @@
require 'cfruntime/properties'
module AutoReconfiguration
+ SUPPORTED_MONGO_VERSION = '1.2.0'
module Mongo
def self.included( base )
@@ -11,19 +12,20 @@ def self.included( base )
base.send( :alias_method, :original_shortcut, :[])
base.send( :alias_method, :[], :shortcut_with_cf )
end
-
+
def initialize_with_cf(host = nil, port = nil, opts = {})
- @service_props = CFRuntime::CloudApp.service_props('mongodb')
- if @service_props.nil?
- puts "No MongoDB service bound to app. Skipping auto-reconfiguration."
- @auto_config = false
- original_initialize host, port, opts
- else
+ service_names = CFRuntime::CloudApp.service_names_of_type('mongodb')
+ if service_names.length == 1
+ @service_props = CFRuntime::CloudApp.service_props('mongodb')
puts "Auto-reconfiguring MongoDB"
@auto_config = true
mongo_host = @service_props[:host]
mongo_port = @service_props[:port]
original_initialize mongo_host, mongo_port, opts
+ else
+ puts "Found #{service_names.length} mongo services. Skipping auto-reconfiguration."
+ @auto_config = false
+ original_initialize host, port, opts
end
end
@@ -1,10 +1,9 @@
require 'cfautoconfig/configuration_helper'
-SUPPORTED_MONGO_VERSION = '1.2.0'
begin
#Require mongo here is mandatory for configurer to ensure class is loaded before applying OpenClass
require "mongo"
require File.join(File.dirname(__FILE__), 'mongodb')
- if Gem::Version.new(Mongo::VERSION) >= Gem::Version.new(SUPPORTED_MONGO_VERSION)
+ if Gem::Version.new(Mongo::VERSION) >= Gem::Version.new(AutoReconfiguration::SUPPORTED_MONGO_VERSION)
if AutoReconfiguration::ConfigurationHelper.disabled? :mongodb
puts "MongoDB auto-reconfiguration disabled."
module Mongo
@@ -26,14 +25,15 @@ class Connection
puts "MongoDB AutoReconfiguration already included."
else
module Mongo
+ #Introduce around alias into Mongo Connection class
class Connection
include AutoReconfiguration::Mongo
end
end
end
else
puts "Auto-reconfiguration not supported for this Redis version. " +
- "Found: #{Mongo::VERSION}. Required: #{SUPPORTED_MONGO_VERSION} or higher."
+ "Found: #{Mongo::VERSION}. Required: #{AutoReconfiguration::SUPPORTED_MONGO_VERSION} or higher."
end
rescue LoadError
puts "No MongoDB Library Found. Skipping auto-reconfiguration."
@@ -1,29 +1,24 @@
require 'cfruntime/properties'
+require 'cfruntime/redis'
module AutoReconfiguration
- module Redis
- def self.included( base )
- base.send( :alias_method, :original_initialize, :initialize)
- base.send( :alias_method, :initialize, :initialize_with_cf )
- end
+ SUPPORTED_REDIS_VERSION = '2.0'
+ module Redis
+ def self.included( base )
+ base.send( :alias_method, :original_initialize, :initialize)
+ base.send( :alias_method, :initialize, :initialize_with_cf )
+ end
- def initialize_with_cf(options = {})
- service_props = CFRuntime::CloudApp.service_props('redis')
- if(service_props.nil?)
- puts "No Redis service bound to app. Skipping auto-reconfiguration."
- original_initialize options
- else
- puts "Auto-reconfiguring Redis."
- cfoptions = options
- if !cfoptions[:path].nil?
- #Host and port are ignored if a path is specified
- cfoptions[:path] = "#{service_props[:host]}:#{service_props[:port]}"
- end
- cfoptions[:host] = service_props[:host]
- cfoptions[:port] = service_props[:port]
- cfoptions[:password] = service_props[:password]
- original_initialize cfoptions
- end
- end
- end
+ def initialize_with_cf(options = {})
+ service_names = CFRuntime::CloudApp.service_names_of_type('redis')
+ if service_names.length == 1
+ puts "Auto-reconfiguring Redis."
+ cfoptions = CFRuntime::RedisClient.options_for_svc(service_names[0],options)
+ original_initialize cfoptions
+ else
+ puts "Found #{service_names.length} redis services. Skipping auto-reconfiguration."
+ original_initialize options
+ end
+ end
+ end
end
@@ -1,9 +1,8 @@
require 'cfautoconfig/configuration_helper'
-SUPPORTED_REDIS_VERSION = '2.0'
begin
require 'redis'
require File.join(File.dirname(__FILE__), 'redis')
- if Gem::Version.new(Redis::VERSION) >= Gem::Version.new(SUPPORTED_REDIS_VERSION)
+ if Gem::Version.new(Redis::VERSION) >= Gem::Version.new(AutoReconfiguration::SUPPORTED_REDIS_VERSION)
if AutoReconfiguration::ConfigurationHelper.disabled? :redis
puts "Redis auto-reconfiguration disabled."
class Redis
@@ -26,7 +25,7 @@ class Redis
end
else
puts "Auto-reconfiguration not supported for this Redis version. " +
- "Found: #{Redis::VERSION}. Required: #{SUPPORTED_REDIS_VERSION} or higher."
+ "Found: #{Redis::VERSION}. Required: #{AutoReconfiguration::SUPPORTED_REDIS_VERSION} or higher."
end
rescue LoadError
puts "No Redis Library Found. Skipping auto-reconfiguration."
@@ -1,39 +1,30 @@
require 'cfruntime/properties'
+require 'cfruntime/amqp'
module AutoReconfiguration
- module AMQP
- def self.included( base )
- base.send( :alias_method, :original_connect, :connect)
- base.send( :alias_method, :connect, :connect_with_cf )
- end
+ SUPPORTED_AMQP_VERSION = '0.8'
+ module AMQP
+ def self.included( base )
+ base.send( :alias_method, :original_connect, :connect)
+ base.send( :alias_method, :connect, :connect_with_cf )
+ end
- def connect_with_cf(connection_options_or_string = {}, other_options = {}, &block)
- service_props = CFRuntime::CloudApp.service_props('rabbitmq')
- if(service_props.nil?)
- puts "No RabbitMQ service bound to app. Skipping auto-reconfiguration."
- original_connect(connection_options_or_string, other_options, &block)
- else
- puts "Auto-reconfiguring AMQP."
- url_provided = false
- case connection_options_or_string
- when String then
- url_provided = true
- cfoptions = {}
- else
- cfoptions = connection_options_or_string
- end
- if service_props[:url] && url_provided
- #If user passed in a URL and we have a URL for service, use it
- original_connect(service_props[:url], other_options, &block)
- else
- cfoptions[:host] = service_props[:host]
- cfoptions[:port] = service_props[:port]
- cfoptions[:user] = service_props[:username]
- cfoptions[:pass] = service_props[:password]
- cfoptions[:vhost] = service_props[:vhost]
- original_connect(cfoptions, other_options, &block)
- end
- end
- end
- end
+ def connect_with_cf(connection_options_or_string = {}, other_options = {}, &block)
+ service_names = CFRuntime::CloudApp.service_names_of_type('rabbitmq')
+ if service_names.length == 1
+ puts "Auto-reconfiguring AMQP."
+ case connection_options_or_string
+ when String then
+ cfoptions = {}
+ else
+ cfoptions = connection_options_or_string
+ end
+ original_connect(CFRuntime::AMQPClient.options_for_svc(service_names[0],cfoptions),
+ other_options, &block)
+ else
+ puts "Found #{service_names.length} rabbitmq services. Skipping auto-reconfiguration."
+ original_connect(connection_options_or_string, other_options, &block)
+ end
+ end
+ end
end
@@ -1,10 +1,9 @@
require 'cfautoconfig/configuration_helper'
-SUPPORTED_AMQP_VERSION = '0.8'
begin
#Require amqp here is mandatory for configurer to ensure class is loaded before applying OpenClass
require "amqp"
require File.join(File.dirname(__FILE__), 'amqp')
- if Gem::Version.new(AMQP::VERSION) >= Gem::Version.new(SUPPORTED_AMQP_VERSION)
+ if Gem::Version.new(AMQP::VERSION) >= Gem::Version.new(AutoReconfiguration::SUPPORTED_AMQP_VERSION)
if AutoReconfiguration::ConfigurationHelper.disabled? :rabbitmq
puts "RabbitMQ auto-reconfiguration disabled."
class << AMQP
@@ -19,13 +18,14 @@ class << AMQP
elsif AMQP.public_methods.index :connect_with_cf
puts "AMQP AutoReconfiguration already included."
else
+ #Introduce around alias into AMQP class
class << AMQP
include AutoReconfiguration::AMQP
end
end
else
puts "Auto-reconfiguration not supported for this AMQP version. " +
- "Found: #{AMQP::VERSION}. Required: #{SUPPORTED_AMQP_VERSION} or higher."
+ "Found: #{AMQP::VERSION}. Required: #{AutoReconfiguration::SUPPORTED_AMQP_VERSION} or higher."
end
rescue LoadError
puts "No AMQP Library Found. Skipping auto-reconfiguration."
@@ -1,27 +1,24 @@
require 'cfruntime/properties'
+require 'cfruntime/carrot'
module AutoReconfiguration
- module Carrot
- def self.included( base )
- base.send( :alias_method, :original_initialize, :initialize)
- base.send( :alias_method, :initialize, :initialize_with_cf )
- end
+ SUPPORTED_CARROT_VERSION = '1.0'
+ module Carrot
+ def self.included( base )
+ base.send( :alias_method, :original_initialize, :initialize)
+ base.send( :alias_method, :initialize, :initialize_with_cf )
+ end
- def initialize_with_cf(opts = {})
- service_props = CFRuntime::CloudApp.service_props('rabbitmq')
- if(service_props.nil?)
- puts "No RabbitMQ service bound to app. Skipping auto-reconfiguration."
- original_initialize opts
- else
- puts "Auto-reconfiguring Carrot."
- cfoptions = opts
- cfoptions[:host] = service_props[:host]
- cfoptions[:port] = service_props[:port]
- cfoptions[:user] = service_props[:username]
- cfoptions[:pass] = service_props[:password]
- cfoptions[:vhost] = service_props[:vhost]
- original_initialize opts
- end
- end
- end
+ def initialize_with_cf(opts = {})
+ service_names = CFRuntime::CloudApp.service_names_of_type('rabbitmq')
+ if service_names.length == 1
+ puts "Auto-reconfiguring Carrot."
+ cfopts = CFRuntime::CarrotClient.options_for_svc(service_names[0],opts)
+ original_initialize cfopts
+ else
+ puts "Found #{service_names.length} rabbitmq services. Skipping auto-reconfiguration."
+ original_initialize opts
+ end
+ end
+ end
end
@@ -1,11 +1,10 @@
require 'cfautoconfig/configuration_helper'
-SUPPORTED_CARROT_VERSION = '1.0'
begin
#Require carrot here is mandatory for configurer to ensure class is loaded before applying OpenClass
require "carrot"
require File.join(File.dirname(__FILE__), 'carrot')
carrot_version = Gem.loaded_specs['carrot'].version
- if carrot_version >= Gem::Version.new(SUPPORTED_CARROT_VERSION)
+ if carrot_version >= Gem::Version.new(AutoReconfiguration::SUPPORTED_CARROT_VERSION)
if AutoReconfiguration::ConfigurationHelper.disabled? :rabbitmq
puts "RabbitMQ auto-reconfiguration disabled."
class Carrot
@@ -20,13 +19,14 @@ class Carrot
elsif Carrot.public_instance_methods.index :initialize_with_cf
puts "Carrot AutoReconfiguration already included."
else
+ #Introduce around alias into Carrot class
class Carrot
include AutoReconfiguration::Carrot
end
end
else
puts "Auto-reconfiguration not supported for this Carrot version. " +
- "Found: #{carrot_version}. Required: #{SUPPORTED_CARROT_VERSION} or higher."
+ "Found: #{carrot_version}. Required: #{AutoReconfiguration::SUPPORTED_CARROT_VERSION} or higher."
end
rescue LoadError
puts "No Carrot Library Found. Skipping auto-reconfiguration."
@@ -1,4 +1,5 @@
require 'cfruntime/properties'
+require 'cfruntime/mysql'
module AutoReconfiguration
SUPPORTED_MYSQL2_VERSION = '0.2.7'
@@ -10,17 +11,13 @@ def self.included( base )
end
def initialize_with_cf(opts = {})
- @service_props = CFRuntime::CloudApp.service_props('mysql')
- if @service_props.nil?
- @auto_config = false
- else
+ service_names = CFRuntime::CloudApp.service_names_of_type('mysql')
+ if service_names.length == 1
puts "Auto-reconfiguring MySQL"
- @auto_config = true
- end
- if @auto_config
- original_initialize @service_props
+ original_initialize(CFRuntime::Mysql2Client.options_for_svc(service_names[0],opts))
else
- original_initialize opts
+ puts "Found #{service_names.length} mysql services. Skipping auto-reconfiguration."
+ original_initialize opts
end
end
end
Oops, something went wrong.

0 comments on commit af46612

Please sign in to comment.