Capistrano on Media Temple

January 28th, 2007 by Trevor

I recently moved most of the sites I'm maintaining over to Media Temple's Grid Server hosting service because I was having no luck getting Rails apps deployed on Dreamhost. I gather that people much brighter than me have gotten Rails apps running on Dreamhost without any difficulties, but I just wasn't able to get it working myself and was looking to move hosts in any case.

I chose Media Temple in large part because of their focus on Rails Containers and because of the tools they've built that make it easy to get up and running. It doesn't hurt that they've got pretty reasonable prices, too.

One of the main things that got me interested in Rails is that its "opinionated" nature starts you off on the right foot. Rails apps have development, testing, and production environments from the start, which makes it very easy to make changes to live apps without worrying about screwing things up. Capistrano makes the deployment part of creating and maintaining Rails apps extremely easy. The only thing is that you've got to create a "deployment recipe" for use on your web host's servers.

Here is the Capistrano Deployment Recipe (deploy.rb) that I put together for Media Temple. If you've followed the instructions in the Server Guide for how to prepare your (gs) for Rails hosting, just set the proper passwords and stuff in the first few lines and you should be good to go. Enjoy!

 
require 'mt-capistrano'
 
set :site,         "0000"
set :application,  "appname"
set :webpath,      "app.com"
set :domain,       "primarydomain.com"
set :user,         "serveradmin%primarydomain.com"
set :password,     "password"
 
ssh_options[:username] = 'serveradmin%primarydomain.com'
 
set :repository, "svn+ssh://#{user}@#{domain}/home/#{site}/data/svn/#{application}/trunk"
set :deploy_to,  "/home/#{site}/containers/rails/#{application}"
 
set :checkout, "export"
 
role :web, "#{domain}"
role :app, "#{domain}"
role :db,  "#{domain}", :primary => true
 
task :after_update_code, :roles => :app do
  put(File.read('config/database.yml'), "#{release_path}/config/database.yml", :mode => 0444)
end
 
task :restart, :roles => :app do
  run "mtr restart #{application} -u #{user} -p #{password}"
  run "mtr generate_htaccess #{application} -u #{user} -p #{password}"
  migrate
end
 

I originally posted this code on the Rails Weenie forums, but I thought it might be good to post it here so that it doesn't get lost. This blog post also provided some serious help when I was setting things up, and I think it's worth a read as well.

If you're brand new to Capistrano, you'll have to first install the gems for "capistrano" and Media Temple's "mt-capistrano" like you would with any other gem. You'll also want to have Subversion set up for your code repository. If you don't have SSH keys set up, I think you can add ssh_options[:password] to the recipe instead.

Comments

Posting code? Please use Pastie.

Have a question? Please visit the Forum.

13 Comments

  1. Hey Trevor,

    Thanks for the recipe! It’s a very helpful in setting up Capistrano and deploying Rails on (mt) the ‘right’ way.

    thanks,
    Alastair

    Comment by Alastair on February 6, 2007

  2. [...] The Almost Effortless blog have posted a recipe for setting up Capistrano on Media Temple. [...]


  3. Thanks for this, very useful!

    I’m trying to get cap deploy working on my mt gs account and I’m having a little difficulty though; the capistrano deploy seems to run successfully until the restart command, when it complains that my application doesn’t exist. Checking the directory structure in the rails container it looks like it ought to do as far as i can see (the regular capistrano heirarchy). Any ideas for what i should check?

    ” * executing “mtr restart mylittleapp -u serveradmin%s1234.gridserver.com -p 123456″
    servers: ["s1234.gridserver.com"]
    [s1234.gridserver.com] executing command
    ** [out :: s1234.gridserver.com] Error executing command:
    ** [out :: s1234.gridserver.com] Application mylittleapp does not exist.”

    Comment by bitbutter on April 6, 2007

  4. bitbutter, it sound like you need to make sure you’ve followed the Media Temple instructions that you can find in the administration panel.

    I’d start by setting up the app without capistrano and make sure it works, then add capistrano to it.

    Comment by Trevor on April 7, 2007

  5. I found this to be very helpful, except that I have run into a problem. With Capistrano 2.0, it appears that the “%” sign in the username is causing problems. I can connect to the server and run all the Media Temple (cap mt_add) commands, but when I try to deploy I get an error that says that SSH cannot connect to the repository.

    It looks as though the % sign in the username variable of the deployment recipe is causing problems. Instead, it looks for:

    serveradmin/272lkjdslfaj.com

    where the ‘/272′ appears to be some kind of goof caused by the percent sign. Has anyone had any luck with Media Temple and Capistrano 2.0?


  6. Brian, I actually moved over to Slicehost, so I haven’t used Capistrano 2 on Media Temple at all. Sorry!

    Comment by Trevor on July 30, 2007

  7. [...] Since my Capistrano Recipe for Media Temple still gets a fair amount of traffic, I figured I might as well share my new Capistrano 2 Recipe for Slicehost - specifically Debian Etch. [...]


  8. [...] [RECIPE] Capistrano Recipe for Media Temple [...]

    Pingback by Capistrano on August 24, 2007

  9. Hi,

    Is there any official documentation for deploying rails application using capistrano on media temple grid servers


  10. I believe there’s some documentation in the MT knowledge base, but maybe not. I moved over to Slicehost a few months ago, so I can’t say for sure.

    Comment by Trevor on November 13, 2007

  11. Thanks for the advice.

    I’m not sure, but the recent OS X update (10.5.3) seems to have broken Capistrano with MediaTemple - I fixed this by uninstalling, then re-installing, the capistrano gem.

    More details:

    http://www.sevenoakjournal.co.uk/2008/06/02/os-x-capistrano-mediatemple/

    Cheers

    Comment by James on June 2, 2008

  12. Why did you move to Slicehost? I’m currently on (mt), and am also thinking to move to Slicehost. Just wondering how your experience has been.

    Comment by Arshad on November 2, 2008

  13. I had some small problems with Media Temple, and I thought the Slicehost pricing/limitation were much more reasonable. At the time I moved, Media Temple would only let me run 1 mongrel, whereas I could run at least 3 with Slicehost for the same cost. I’ve been really happy with Slicehost, and I wholeheartedly reccomend using them if you’re comfortable with server administration (which not everyone is).

    Comment by Trevor on November 2, 2008