Heroku and MongoDB enable the kind of frictionless devops workflow every mature software organization wants and spends mountains of money on. It begins with a 1-box developer setup in which I can checkout source code and be ready to start development after a bundle install, _and continues with a _git push to a Heroku staging environment that deploys the application for last minute integration testing and, finally, production. Each separate Heroku instance has its own configuration and resources. My code also needs sensible defaults for development. For example, mongoid.yml lists localhost:27017 as the development database host and ENV[‘MONGOHQ_HOST_LIST’] for production.
However, in true devops, I want to be able to reach into a remote environment programmatically, run commands remotely or connect two environments together, as long as we have access to them. In this post I’ll propose an implementation of three tasks that have become part of the daily routine of one of my projects.
- Execute a Rake task with local code modifications and the configuration of a production environment.
- Open a shell to the primary node of the MongoDB on my development Heroku environment.
- Dump a single table from a production database before doing something scary.
We’ll accomplish the above with help from two new gems: mongoid-shell and heroku-commander. Add those to your Gemfile in the :development section.
And, please remember, that with great power (and the word “production” in much of what follows) comes a lot of responsibility.
Execute a Rake task with local code modifications and the configuration of a production environment.
This is made possible by heroku-commander. The library wraps the Heroku CLI (intro here) and will run heroku config –s. It’s now easy to reach out to a Heroku application and retrieve its configuration programmatically without worrying about API keys (by default it will use the Heroku app defined via the “heroku” GIT remote). We will also need a bit of code to apply our application’s naming convention. This lets me change the execution environment to the one of a remote Heroku application, in Ruby.
So how do I run a task locally, but configured as production? With the following Rake task.
RAILS_ENV=production rake heroku:config_from_env my:task.
Open a shell to the primary node of the MongoDB on my development Heroku environment.
First, figure out the remote MongoDB configuration, then execute the mongo shell command. It’s important to know that the built-in system command doesn’t raise an error when the process returns a non-zero status code. Let’s add a system! function that fixes that.
Instead of making MongoDB command lines manually, I’ve used a new gem called mongoid-shell (intro here).
Dump a single table from a production database before doing something scary.
We all do backups and other important things, daily. But when manipulating production data I want to have the last safeguard with the freshest data from the collection I am about to update. Dump a MongoDB collection locally.
Thanks to @joeyAghion and @fancyremarker who are responsible for many of the core concepts above.