Question

Grails works great with the mongodb plugin locally but I'm having issues on AppFog.

The instructions for deploying Grails on AppFog are fairly clear for databases other than MongoDB. The documentation seem to allude to auto-config for MongoDB but if I have my configuration set to the following, it doesn't work.

//DataSource.groovy

grails {
  mongo {
    host = "localhost"
    port = 27017
    databaseName = "dbname"
  }
}

Auto-config doesn't seem to replace localhost with the proper hostname. It seems like I need to set the values from the VCAP_SERVICES.

Was it helpful?

Solution

Do you have the CloudFoundry plugin installed in your Grails project? The plugin does the auto-reconfig for the MongoDB and other data sources in a Grails app.

If for some reason you can't, or don't want to, use the Grails CloudFoundry plugin, another alternative to using the environment variables as you showed above is to use the cloudfoundry-runtime Java API. This API gives you access to the same info as is stored in the environment variables, but it is a little cleaner than parsing the environment variables directly.

OTHER TIPS

After contacting support, the answer was to use the VCAP_SERVICE environment variable. After digging around on the web, I figured out how to retrieve and use VCAP_SERVICES in the config.

grails {
    def vcap = System.env.VCAP_SERVICES
    def credentials = vcap ? grails.converters.JSON.parse(vcap)["mongodb-1.8"][0]["credentials"] : null
    mongo {
        host = credentials ? credentials.hostname : "localhost"
        port = credentials ? credentials.port : "27017"
        username = credentials ? credentials.username : null
        password = credentials ? credentials.password : null
        databaseName = credentials ? credentials.db : "dbname"
    }
}

I also created a github gist this configuration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top