Deploying and maintaining web applications, APIs and mobile backends may require you to do one-off tasks -migrate a database, run a report, debug an application, etc.

Currently Pivotal Web Services, PivotalCF, IBM BlueMix & Open Source Cloud Foundry do not have a native way to run one-off tasks. There is now a way with cf-ssh script (1.5 min):

Solution

Recently Dan Higham discovered http://tmate.io/ - instance terminal sharing - and proceeded to create tmate-bootstrap to allow SSH sessions into Cloud Foundry containers.

Within the SSH session you can run one-off tasks.

To simplify the local setup & cleanup, there is the cf-ssh script.

Install

The only setup is to download the cf-ssh script into your $PATH. Assuming ~/bin is in your $PATH:

cd ~/bin  
wget https://raw.githubusercontent.com/danhigham/tmate-bootstrap/master/scripts/cf-ssh -O cf-ssh  
chmod +x cf-ssh  

To run:

$ cf-ssh APPNAME

To specify the buildpack name or URI (since it cannot be discovered):

$ cf-ssh APPNAME -b ruby_buildpack
$ cf-ssh APPNAME -b https://github.com/cloudfoundry/buildpack-ruby.git

To specify the project path or file to upload (Java jar file):

$ cf-ssh APPNAME -p path/to/project

Things to remember

  1. Have the cf CLI already installed locally and be logged in, and targeting the organization/space with the application
  2. Be within the application's source project

Limitations

At this time there are some limitations at this time:

  • Cannot discover the use of custom buildpacks, must explicitly specify buildpack with -b option
  • Cannot discover that you deploy the application with an uploaded file/tarball/jar file, must explicitly specify path with -p option
  • Cannot re-use a manifest.yml that you might already have

But it does:

  • Discover and bind the same service instances
  • Discover and set the same environment variables

Offline one-time tasks

Currently the solution uses the publicly hosted http://tmate.io servers.

For offline Cloud Foundry, you would need to run a tmate server (see "Host your own tmate server" in http://tmate.io/) and reconfigure tmate at the top of the cf-ssh script.

Thanks

Thanks to Nico Viennot for creating and hosting http://tmate.io, Dan Higham for creating tmate-bootstrap, and James Bayer for pointing me in the right direction over the weekend!