The OpenShift team believes that the best way to figure out how something works is to get your hands dirty and do something real with it. One of our crackerjack developers (Ram Ranganathan) put this sample application together and we’re sharing it with the world to provide an easy way to test drive OpenShift’s new MongoDB support. Read Mike’s and Juan’s blog to get the skinny on what’s new in today’s release or if you’re really lazy, just watch this video detailing the announcement.
Spoiler alert — there is a video that accompanies this blog post at the bottom that will help you visualize walking through the steps outlined below.
The PHP application that we’ll deploy is called PHPMongoTweet. This application allows you to search for things on Twitter and have the resulting tweets automatically added to the MongoDB collection that is associated with the application. In the case of this app, the collection name is phpmongotweet. So without boring you too much, let’s get on with the geekfest!
Creating an app on OpenShift express is a quick oneliner with the rhc-create-app tool (or through the Express console):
rhc-create-app -a phpmongotweet -t php-5.3
Now that we have an application ready to go, we need to let OpenShift know that MongoDB is required for this app. Using the rhc-ctl-app we can control the applications runtime environment and add MongoDB 2.0:
rhc-ctl-app -a phpmongotweet -e add-mongodb-2.0
Note that the output of the above command will output the login credentials, database name, and connection URL – This application makes use of the $OPENSHIFT_NOSQL_DB_HOST, $OPENSHIFT_NOSQL_DB_USERNAME, and $OPENSHIFT_NOSQL_DB_PASSWORD environment variables so we don’t need to remember this information.
With our new application created and MongoDB support added, we are now ready to add some code to the app and see it in action. We’ll use a Github remote to pull down the phpmongotweet application:
git remote add upstream -m master git://github.com/openshift/phpmongotweet-example.git
git pull -s recursive -X theirs upstream master
These commands will pull a copy of the phpmongotweet app from from Github and place it in your applications git repository. The ‘git push’ command deploys your new application. You should now be able to visit http://phpmongotweet-$namespace.rhcloud.com. The output from the git push should include a link that you can copy and paste. Once the page loads you can search for anything on twitter and the tweets will be loaded into the MongoDB database associated with your application.
To confirm that the application is actually saving the tweets to the database, try out our experimental shell access to run mongo shell. You will want to run the following to get the connection URL for SSH:
The output will include a ‘Git URL’ that will look like this:
You will want to copy the ‘firstname.lastname@example.org’ piece of it and use it as the user@host for SSH and specify that you wan to run ‘rhcsh’. The full command becomes:
This will drop you in to a shell account that you can use to execute:
mongo -u $OPENSHIFT_NOSQL_DB_USERNAME -p $OPENSHIFT_NOSQL_DB_PASSWORD $OPENSHIFT_NOSQL_DB_HOST/$OPENSHIFT_APP_NAME
Once you are in the Mongo Shell you can run:
to dump the contents of the tweet database.
As of this writing, rhcsh functionality is well used internally but officially untested and should be used at your own risk.
Thanks for tuning in!