Rails Cloud Deployment [edit]

Contents

Short and Sweet

Here are the steps to configure and deploy a Rails application to Aptana Cloud. This short and sweet version assumes you've already gone through the deployment in Aptana Studio. It also comes with little explanation or commentary and is appropriate for the relatively experienced (e.g., you know whether or not you need to use sudo to run gem):

  1. Update rubygems if necessary
    gem install rubygems-update
    update_rubygems
  2. Install the aptana_cloud gem
    gem sources -a http://gems.aptana.com
    gem install aptana_cloud
  3. Create the Capistrano configuration from your project root directory
    apcloudify .
  4. Deploy your code to your staging site
    apcloud staging cloud:deploy
  5. And/or Deploy your code to your public site
    apcloud public cloud:deploy

Deploying Rails to Aptana Cloud: the Full Story

Rails deployments via Aptana Cloud are managed by the aptana_cloud gem. This gem builds upon the standard Ruby Capistrano gem to make deployments to Aptana Cloud simple.

Prerequisites

First, a note about sudo: most Unix-like systems including Linux and Mac OS X will require the gem command be run as root using the sudo command. Windows systems should not need (or have) sudo. We'll show examples with sudo, but Windows users should omit the sudo part.

You need to have Ruby and the standard Ruby package system rubygems installed. The rubygems package provides the gem command which is used to install the aptana_cloud gem. Most ruby installations will have rubygems already installed but the standard installations may have an old version of rubygems which may not work with aptana_cloud (aptana_cloud is tested against rubygems 1.3.1).

The short and sweet way of updating rubygems is

   sudo gem install rubygems-update
   sudo update_rubygems

There are other ways to update rubygems, but this should work with most any version.

aptana_cloud Gem Installation

First, you need to install the aptana_cloud gem on the machine you run Aptana Studio from, if you haven't already. The gem is available from the Aptana gem website, so you need to add the site as a gem source by opening a command line and entering the command

 sudo gem sources -a http://gems.aptana.com

Once you've added gems.aptana.com as a source, you install the aptana_cloud gem via

 sudo gem install aptana_cloud

You only need to run this once, but you may want to check for updates to the gem via

 sudo gem update aptana_cloud

periodically.

Deploying a new Aptana Cloud site

You deploy a new Rails site to Aptana Cloud via Studio. For example, you can right-click on your Rails project and select "Deploy to Aptana Cloud ...". The deployment wizard will lead you through the steps to set up your new cloud server. It does not set up your Rails environment or deploy your code. That's what we do here.

Configuring a Rails project for Aptana Cloud

The aptana_cloud gem uses two configuration files to describe your Aptana Cloud server and it can automatically set up these two files. To get a default setup, use the apcloudify command. From a command line, change to the root directory of your project (it should have a Rakefile and a config subdirectory) and enter

 apcloudify .

This command will prompt you for your Aptana User ID and password and once you give them, it will list your current cloud sites. Select the site you want to deploy to and hit enter. apcloudify will then create two files, Capfile and config/deploy.rb, which tell the apcloud command (which we'll use next) all it needs in order to configure and deploy to your server. You can further customize these files if you would like: comments in the created configuration files provide some examples of common customizations.

Note that if either of these files already exists when you run the apcloudify command, they will not be changed. If you wish to recreate the default configurations, simply delete the files and rerun the apcloudify command.

Preparing your Aptana Cloud site for Rails deployments

Before you can deploy your Rails project to your Aptana Cloud site, your cloud server needs to be customized for use with the aptana_cloud gem. This includes creating the standard aptana_cloud folder structure (which is the structure used by Capistrano, the standard Ruby deployment tool of which aptana_cloud is an extension), creating databases, and creating database configuration files. Additionally, most of these tasks need to be done for both your public and staging sites. All this can be done with a single apcloud command:

 apcloud cloud:setup

That's all you need to do. If necessary, apcloud will prompt your for you Aptana id and password in order to access your Aptana Cloud server.

Deploy and Run Migrations

Now that your sites are setup, you're ready to deploy your code. For a Rails application, this can be a three step process: first, copying your new code to your server, next running any necessary database migrations, and finally starting or restarting your web/Rails server. All this can be accomplished via the standard Capistrano deploy:migrations task. Because Aptana Cloud provides both public and staging sites, you need to additionally specify which site you wish to deploy to. This is done by adding a site task before the deploy task, so to deploy to staging, you would use

 apcloud staging deploy:migrations

Similarly, to deploy to public, you would use

 apcloud public deploy:migrations.

Each site is configured to use its own database. The Rails environment for the public site is production, for the staging site, development.

Beyond the Basics

As an extension of Capistrano, the aptana_cloud gem includes all the power of Capistrano, including support for deploying code directly out of a source code management system like Git or Subversion. See the comments in the generated config/deploy.rb file and the standard Capistrano documentation for more details and examples.

You can get a list of apcloud tasks by running "apcloud -T" and detailed documentation on a particular command by running "apcloud -e command"

Virtually all the standard Capistrano deployment tasks are available via apcloud. Commands that affect a site will require the addition of the "public" or "staging" task before the standard Capistrano task; you'll get an error if you try to run a command without specifying which site you wish to affect.

Gem Management

If your Rails application requires gems (or versions of gems) that are not installed by default on Aptana Cloud, you'll need to log in to your cloud server and manage them manually using the gem command. You will generally need to use sudo to run gem and you will need to enable root access to your cloud server in order to be able to use the sudo command. You configure root access to your site by selecting a particular site in Studio under My Sites, selecting Show Access Information, and then Enable Root Access. (This features is only available in Aptana Studio 1.2.2 and later and is not available in Aptana Studio 1.3 release candidates 1 or 2).