Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

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
Expand Up @@ -13,6 +13,7 @@ spec = Gem::Specification.new do |s|


s.platform = Gem::Platform::RUBY s.platform = Gem::Platform::RUBY
s.extra_rdoc_files = ["LICENSE"] s.extra_rdoc_files = ["LICENSE"]
s.rdoc_options = ["-N", "--tab-width=2", "--exclude='cf-autoconfig.gemspec|spec'"]


s.add_dependency "cf-runtime" s.add_dependency "cf-runtime"


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


module AutoReconfiguration module AutoReconfiguration
SUPPORTED_MONGO_VERSION = '1.2.0'
module Mongo module Mongo


def self.included( base ) def self.included( base )
Expand All @@ -11,19 +12,20 @@ def self.included( base )
base.send( :alias_method, :original_shortcut, :[]) base.send( :alias_method, :original_shortcut, :[])
base.send( :alias_method, :[], :shortcut_with_cf ) base.send( :alias_method, :[], :shortcut_with_cf )
end end

def initialize_with_cf(host = nil, port = nil, opts = {}) def initialize_with_cf(host = nil, port = nil, opts = {})
@service_props = CFRuntime::CloudApp.service_props('mongodb') service_names = CFRuntime::CloudApp.service_names_of_type('mongodb')
if @service_props.nil? if service_names.length == 1
puts "No MongoDB service bound to app. Skipping auto-reconfiguration." @service_props = CFRuntime::CloudApp.service_props('mongodb')
@auto_config = false
original_initialize host, port, opts
else
puts "Auto-reconfiguring MongoDB" puts "Auto-reconfiguring MongoDB"
@auto_config = true @auto_config = true
mongo_host = @service_props[:host] mongo_host = @service_props[:host]
mongo_port = @service_props[:port] mongo_port = @service_props[:port]
original_initialize mongo_host, mongo_port, opts 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
end end


Expand Down
@@ -1,10 +1,9 @@
require 'cfautoconfig/configuration_helper' require 'cfautoconfig/configuration_helper'
SUPPORTED_MONGO_VERSION = '1.2.0'
begin begin
#Require mongo here is mandatory for configurer to ensure class is loaded before applying OpenClass #Require mongo here is mandatory for configurer to ensure class is loaded before applying OpenClass
require "mongo" require "mongo"
require File.join(File.dirname(__FILE__), 'mongodb') 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 if AutoReconfiguration::ConfigurationHelper.disabled? :mongodb
puts "MongoDB auto-reconfiguration disabled." puts "MongoDB auto-reconfiguration disabled."
module Mongo module Mongo
Expand All @@ -26,14 +25,15 @@ class Connection
puts "MongoDB AutoReconfiguration already included." puts "MongoDB AutoReconfiguration already included."
else else
module Mongo module Mongo
#Introduce around alias into Mongo Connection class
class Connection class Connection
include AutoReconfiguration::Mongo include AutoReconfiguration::Mongo
end end
end end
end end
else else
puts "Auto-reconfiguration not supported for this Redis version. " + 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 end
rescue LoadError rescue LoadError
puts "No MongoDB Library Found. Skipping auto-reconfiguration." puts "No MongoDB Library Found. Skipping auto-reconfiguration."
Expand Down
43 changes: 19 additions & 24 deletions auto-reconfiguration/lib/cfautoconfig/keyvalue/redis.rb
@@ -1,29 +1,24 @@
require 'cfruntime/properties' require 'cfruntime/properties'
require 'cfruntime/redis'


module AutoReconfiguration module AutoReconfiguration
module Redis SUPPORTED_REDIS_VERSION = '2.0'
def self.included( base ) module Redis
base.send( :alias_method, :original_initialize, :initialize) def self.included( base )
base.send( :alias_method, :initialize, :initialize_with_cf ) base.send( :alias_method, :original_initialize, :initialize)
end base.send( :alias_method, :initialize, :initialize_with_cf )
end


def initialize_with_cf(options = {}) def initialize_with_cf(options = {})
service_props = CFRuntime::CloudApp.service_props('redis') service_names = CFRuntime::CloudApp.service_names_of_type('redis')
if(service_props.nil?) if service_names.length == 1
puts "No Redis service bound to app. Skipping auto-reconfiguration." puts "Auto-reconfiguring Redis."
original_initialize options cfoptions = CFRuntime::RedisClient.options_for_svc(service_names[0],options)
else original_initialize cfoptions
puts "Auto-reconfiguring Redis." else
cfoptions = options puts "Found #{service_names.length} redis services. Skipping auto-reconfiguration."
if !cfoptions[:path].nil? original_initialize options
#Host and port are ignored if a path is specified end
cfoptions[:path] = "#{service_props[:host]}:#{service_props[:port]}" end
end end
cfoptions[:host] = service_props[:host]
cfoptions[:port] = service_props[:port]
cfoptions[:password] = service_props[:password]
original_initialize cfoptions
end
end
end
end end
@@ -1,9 +1,8 @@
require 'cfautoconfig/configuration_helper' require 'cfautoconfig/configuration_helper'
SUPPORTED_REDIS_VERSION = '2.0'
begin begin
require 'redis' require 'redis'
require File.join(File.dirname(__FILE__), '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 if AutoReconfiguration::ConfigurationHelper.disabled? :redis
puts "Redis auto-reconfiguration disabled." puts "Redis auto-reconfiguration disabled."
class Redis class Redis
Expand All @@ -26,7 +25,7 @@ class Redis
end end
else else
puts "Auto-reconfiguration not supported for this Redis version. " + 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 end
rescue LoadError rescue LoadError
puts "No Redis Library Found. Skipping auto-reconfiguration." puts "No Redis Library Found. Skipping auto-reconfiguration."
Expand Down
59 changes: 25 additions & 34 deletions auto-reconfiguration/lib/cfautoconfig/messaging/amqp.rb
@@ -1,39 +1,30 @@
require 'cfruntime/properties' require 'cfruntime/properties'
require 'cfruntime/amqp'


module AutoReconfiguration module AutoReconfiguration
module AMQP SUPPORTED_AMQP_VERSION = '0.8'
def self.included( base ) module AMQP
base.send( :alias_method, :original_connect, :connect) def self.included( base )
base.send( :alias_method, :connect, :connect_with_cf ) base.send( :alias_method, :original_connect, :connect)
end base.send( :alias_method, :connect, :connect_with_cf )
end


def connect_with_cf(connection_options_or_string = {}, other_options = {}, &block) def connect_with_cf(connection_options_or_string = {}, other_options = {}, &block)
service_props = CFRuntime::CloudApp.service_props('rabbitmq') service_names = CFRuntime::CloudApp.service_names_of_type('rabbitmq')
if(service_props.nil?) if service_names.length == 1
puts "No RabbitMQ service bound to app. Skipping auto-reconfiguration." puts "Auto-reconfiguring AMQP."
original_connect(connection_options_or_string, other_options, &block) case connection_options_or_string
else when String then
puts "Auto-reconfiguring AMQP." cfoptions = {}
url_provided = false else
case connection_options_or_string cfoptions = connection_options_or_string
when String then end
url_provided = true original_connect(CFRuntime::AMQPClient.options_for_svc(service_names[0],cfoptions),
cfoptions = {} other_options, &block)
else else
cfoptions = connection_options_or_string puts "Found #{service_names.length} rabbitmq services. Skipping auto-reconfiguration."
end original_connect(connection_options_or_string, other_options, &block)
if service_props[:url] && url_provided end
#If user passed in a URL and we have a URL for service, use it end
original_connect(service_props[:url], other_options, &block) end
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
end end
@@ -1,10 +1,9 @@
require 'cfautoconfig/configuration_helper' require 'cfautoconfig/configuration_helper'
SUPPORTED_AMQP_VERSION = '0.8'
begin begin
#Require amqp here is mandatory for configurer to ensure class is loaded before applying OpenClass #Require amqp here is mandatory for configurer to ensure class is loaded before applying OpenClass
require "amqp" require "amqp"
require File.join(File.dirname(__FILE__), '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 if AutoReconfiguration::ConfigurationHelper.disabled? :rabbitmq
puts "RabbitMQ auto-reconfiguration disabled." puts "RabbitMQ auto-reconfiguration disabled."
class << AMQP class << AMQP
Expand All @@ -19,13 +18,14 @@ class << AMQP
elsif AMQP.public_methods.index :connect_with_cf elsif AMQP.public_methods.index :connect_with_cf
puts "AMQP AutoReconfiguration already included." puts "AMQP AutoReconfiguration already included."
else else
#Introduce around alias into AMQP class
class << AMQP class << AMQP
include AutoReconfiguration::AMQP include AutoReconfiguration::AMQP
end end
end end
else else
puts "Auto-reconfiguration not supported for this AMQP version. " + 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 end
rescue LoadError rescue LoadError
puts "No AMQP Library Found. Skipping auto-reconfiguration." puts "No AMQP Library Found. Skipping auto-reconfiguration."
Expand Down
41 changes: 19 additions & 22 deletions auto-reconfiguration/lib/cfautoconfig/messaging/carrot.rb
@@ -1,27 +1,24 @@
require 'cfruntime/properties' require 'cfruntime/properties'
require 'cfruntime/carrot'


module AutoReconfiguration module AutoReconfiguration
module Carrot SUPPORTED_CARROT_VERSION = '1.0'
def self.included( base ) module Carrot
base.send( :alias_method, :original_initialize, :initialize) def self.included( base )
base.send( :alias_method, :initialize, :initialize_with_cf ) base.send( :alias_method, :original_initialize, :initialize)
end base.send( :alias_method, :initialize, :initialize_with_cf )
end


def initialize_with_cf(opts = {}) def initialize_with_cf(opts = {})
service_props = CFRuntime::CloudApp.service_props('rabbitmq') service_names = CFRuntime::CloudApp.service_names_of_type('rabbitmq')
if(service_props.nil?) if service_names.length == 1
puts "No RabbitMQ service bound to app. Skipping auto-reconfiguration." puts "Auto-reconfiguring Carrot."
original_initialize opts cfopts = CFRuntime::CarrotClient.options_for_svc(service_names[0],opts)
else original_initialize cfopts
puts "Auto-reconfiguring Carrot." else
cfoptions = opts puts "Found #{service_names.length} rabbitmq services. Skipping auto-reconfiguration."
cfoptions[:host] = service_props[:host] original_initialize opts
cfoptions[:port] = service_props[:port] end
cfoptions[:user] = service_props[:username] end
cfoptions[:pass] = service_props[:password] end
cfoptions[:vhost] = service_props[:vhost]
original_initialize opts
end
end
end
end end
@@ -1,11 +1,10 @@
require 'cfautoconfig/configuration_helper' require 'cfautoconfig/configuration_helper'
SUPPORTED_CARROT_VERSION = '1.0'
begin begin
#Require carrot here is mandatory for configurer to ensure class is loaded before applying OpenClass #Require carrot here is mandatory for configurer to ensure class is loaded before applying OpenClass
require "carrot" require "carrot"
require File.join(File.dirname(__FILE__), 'carrot') require File.join(File.dirname(__FILE__), 'carrot')
carrot_version = Gem.loaded_specs['carrot'].version 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 if AutoReconfiguration::ConfigurationHelper.disabled? :rabbitmq
puts "RabbitMQ auto-reconfiguration disabled." puts "RabbitMQ auto-reconfiguration disabled."
class Carrot class Carrot
Expand All @@ -20,13 +19,14 @@ class Carrot
elsif Carrot.public_instance_methods.index :initialize_with_cf elsif Carrot.public_instance_methods.index :initialize_with_cf
puts "Carrot AutoReconfiguration already included." puts "Carrot AutoReconfiguration already included."
else else
#Introduce around alias into Carrot class
class Carrot class Carrot
include AutoReconfiguration::Carrot include AutoReconfiguration::Carrot
end end
end end
else else
puts "Auto-reconfiguration not supported for this Carrot version. " + 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 end
rescue LoadError rescue LoadError
puts "No Carrot Library Found. Skipping auto-reconfiguration." puts "No Carrot Library Found. Skipping auto-reconfiguration."
Expand Down
15 changes: 6 additions & 9 deletions auto-reconfiguration/lib/cfautoconfig/relational/mysql.rb
@@ -1,4 +1,5 @@
require 'cfruntime/properties' require 'cfruntime/properties'
require 'cfruntime/mysql'


module AutoReconfiguration module AutoReconfiguration
SUPPORTED_MYSQL2_VERSION = '0.2.7' SUPPORTED_MYSQL2_VERSION = '0.2.7'
Expand All @@ -10,17 +11,13 @@ def self.included( base )
end end


def initialize_with_cf(opts = {}) def initialize_with_cf(opts = {})
@service_props = CFRuntime::CloudApp.service_props('mysql') service_names = CFRuntime::CloudApp.service_names_of_type('mysql')
if @service_props.nil? if service_names.length == 1
@auto_config = false
else
puts "Auto-reconfiguring MySQL" puts "Auto-reconfiguring MySQL"
@auto_config = true original_initialize(CFRuntime::Mysql2Client.options_for_svc(service_names[0],opts))
end
if @auto_config
original_initialize @service_props
else else
original_initialize opts puts "Found #{service_names.length} mysql services. Skipping auto-reconfiguration."
original_initialize opts
end end
end end
end end
Expand Down

0 comments on commit af46612

Please sign in to comment.