Created
May 30, 2011 22:07
-
-
Save sr3d/999548 to your computer and use it in GitHub Desktop.
Mongoid Initializer for CloudFoundry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alexle:pushpuppy sr3d$ vmc push pushpuppy --runtime ruby19 | |
Would you like to deploy from the current directory? [Yn]: | |
Application Deployed URL: 'pushpuppy.cloudfoundry.com'? | |
Detected a Rails Application, is this correct? [Yn]: | |
Memory Reservation [Default:256M] (64M, 128M, 256M, 512M or 1G) | |
Creating Application: OK | |
Would you like to bind any services to 'pushpuppy'? [yN]: y | |
Would you like to use an existing provisioned service [yN]? N | |
The following system services are available:: | |
1. mongodb | |
2. mysql | |
3. redis | |
Please select one you wish to provision: 1 | |
Specify the name of the service [mongodb-XXXXX]: | |
Creating Service: OK | |
Binding Service: OK | |
Uploading Application: | |
Checking for available resources: OK | |
Processing resources: OK | |
Packing application: OK | |
Uploading (8K): OK | |
Push Status: OK | |
Staging Application: OK | |
Starting Application: OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' # explicitly requires json | |
# initialize the environment for CloudFoundry | |
# you will have to remove the mongoid.yml file otherwise Mongoid | |
# will read that file and try to connect, and will error out. | |
if ENV['VCAP_SERVICES'] | |
# get the config from VCAP | |
# http://support.cloudfoundry.com/entries/20018322-alert-mongo-bindings-have-changed | |
def mongo_connection_info | |
services = JSON.parse(ENV['VCAP_SERVICES']) | |
services.each do |service_version, bindings| | |
bindings.each do |binding| | |
if binding['label'] =~ /mongo/i | |
res = binding['credentials'] | |
return res | |
end | |
end | |
end | |
raise "could not find connection info" | |
end | |
conn_info = mongo_connection_info | |
# configure Mongoid to connect to the db using VCAP connection info | |
Mongoid.configure do |config| | |
name = conn_info['db'] || 'app' | |
host = conn_info['hostname'] | |
options = { | |
"database" => conn_info['db'] || 'app', | |
"host" => conn_info['hostname'], | |
"port" => conn_info['port'].to_i, | |
"autocreate_indexes" => false, | |
"allow_dynamic_fields" => true, | |
"include_root_in_json" => false, | |
"parameterize_keys" => true, | |
"persist_in_safe_mode" => false, | |
"raise_not_found_error" => true, | |
"reconnect_time" => 3 | |
} | |
if conn_info['username'] and conn_info['password'] | |
options['username'] = conn_info['username'] | |
options['password'] = conn_info['password'] | |
end | |
config.from_hash(options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment