Note: This guide uses the Cloud Foundry CLI version 5. The instructions below are not compatible with any cf version > 5. Click here to get a description of the differences between CLI v5 and CLI v6.
In some cases it is necessary to run certain 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.
You might know that anynines is build on top of Cloud Foundy which is an Open Source PaaS. 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. Deploy your application
git clone email@example.com:anynines/simple_rails_app.git
The “cf push” command will ask you some questions concerning your deployment setup. Have a look at the Getting Started guide for a detailed description. When asked if you would like to save the configuration, please answer with “yes”. This will to generate a deployment manifest file (manifest.yml).
2. Edit the deployment manifest
Next you can add a section to the manifest file, which was generated in the previous step. Open the file in a text-editor of your choice and add the last lines according to your existing manifest document:
--- applications: - name: simple_rails_app memory: 256M instances: 3 host: a9s-simple-rails-app domain: aws.ie.a9sapp.eu path: . services: mysql-a065a: label: mysql provider: core version: '5.5' plan: Pluto-free - name: one-off-instance memory: 256M instances: 1 path: . host: none domain: none services: mysql-a065a: label: mysql provider: core version: '5.5' plan: Pluto-free
As you can see we are going to deploy the same application a second time. The trick is to use the same service instance (mysql-a065a) as in the first application. Make sure that there is only one instance configured for the second app (one-off-instance).
3. Execute your tasks
That’s it! We are ready to execute the tasks. To run a database migration for example, type:
cf push one-off-instance --command "bundle exec rake db:migrate"
4. Shut down the one-off-instance
After you’ve finished your administrative tasks you can delete the "one-off-instance" application to avoid a waste of recourses. In case you need it again you just have to execute step three of this guide, since the one-off-instance is still configured in your manifest. To avoid the one-off-instance to be pushed every time you push your main application, just type “cf push” plus the name of your actual application:
cf push simple_rails_app
Please sign in to leave a comment.