Skip to content

Commit

Permalink
Add cf-runtime clients
Browse files Browse the repository at this point in the history
- 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 committed Dec 16, 2011
1 parent 25777a8 commit af46612
Show file tree
Hide file tree
Showing 37 changed files with 1,042 additions and 202 deletions.
1 change: 1 addition & 0 deletions auto-reconfiguration/cf-autoconfig.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 0 additions & 1 deletion auto-reconfiguration/lib/cfautoconfig.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Copyright (c) 2009-2011 VMware, Inc.
require 'cfautoconfig/configurer'
require 'cfautoconfig/version'
16 changes: 9 additions & 7 deletions auto-reconfiguration/lib/cfautoconfig/document/mongodb.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'cfruntime/properties'

module AutoReconfiguration
SUPPORTED_MONGO_VERSION = '1.2.0'
module Mongo

def self.included( base )
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."
Expand Down
43 changes: 19 additions & 24 deletions auto-reconfiguration/lib/cfautoconfig/keyvalue/redis.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."
Expand Down
59 changes: 25 additions & 34 deletions auto-reconfiguration/lib/cfautoconfig/messaging/amqp.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."
Expand Down
41 changes: 19 additions & 22 deletions auto-reconfiguration/lib/cfautoconfig/messaging/carrot.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."
Expand Down
15 changes: 6 additions & 9 deletions auto-reconfiguration/lib/cfautoconfig/relational/mysql.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'cfruntime/properties'
require 'cfruntime/mysql'

module AutoReconfiguration
SUPPORTED_MYSQL2_VERSION = '0.2.7'
Expand All @@ -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
Expand Down
Loading

0 comments on commit af46612

Please sign in to comment.