Running admin tasks (e.g. database migrations) with the CLI v6
Note: This guide uses the Cloud Foundry CLI version 6. The instructions below are not compatible with any cf version < 6. Click here to read how to install the cf CLI v6.
Every now and then you might need to run admin tasks (also known as One-Off commands). These tasks get executed in the context of the application and thus have access to all databases and services bound to the application as well as to the application’s source code. The purpose of these tasks could be a database migration, wich requires the execution of "rake db:migrate" or "manage.py syncdb" inside your application directory. For this use case anynines provides the "cf console [appname]" command. Running this command will provide you access to a Rails Console. The drawback of the “cf console” command is that it is very framework specific and as such does not work for every framework.
anynines is build on top of Cloud Foundy which is an Open Source PaaS (Platform as a Service). There is ongoing work to provide a mechanism to run arbitrary commands, much like in every SSH console. Thankfully you don't have to wait untill the Cloud Foundry team has completed this feature. The instructions below should show you how to run these tasks for the time being.
1. Setup a demo application
First we clone and deploy a demo application that will be used to demonstrate the workflow:
$ git clone git@github.com:anynines/simple_rails_app.git
$ cd simple_rails_app
$ cf push simple-rails-app
$ cf create-service mysql Pluto-free simple-rails-app-mysql-service
$ cf bind-service simple-rails-app simple-rails-app-mysql-service
$ cf restart simple-rails-app
2. Deploy an one-off instance
In this step we deploy the application a second time. Since this deployment will execute the administrative tasks make sure there is only one instance for this application specified. Otherwise the commands get executed multiple times.
$ cf push simple-rails-app-one-off-instance --no-route -i 1
$ cf bind-service simple-rails-app-one-off-instance simple-rails-app-mysql-service
3. Run your commands
That’s it! We are ready to execute the tasks. To run a database migration for example, make sure you are still in the source code folder and type:
$ cf push simple-rails-app-one-off-instance -c "bundle exec rake db:migrate" --no-route
4. Shut down the one-off-instance
After you’ve finished your administrative tasks you can delete or stop the "one-off-instance" application to avoid a waste of recourses.
Please sign in to leave a comment.
Comments
0 comments