Capistrano – Deploying a Branch

When deploying from Capistrano, I like to have the following in my ‘deploy.rb’ file:

set :scm, "git"
set :repository, "git@www.example.com:project.git"
set :git_shallow_clone, 1
set :deploy_via, :remote_cache

This allows speeds deployments by keeping a versions checked out on the server in ‘shared/cached-copy’. When it comes to deploying, it is always best to deploy from a branch instead of the master/head/trunk. To handle this, add the following:

set :branch, fetch(:branch, “master”)

When you deploy, run the following:

$ cap deploy -S branch=REL_1.0

If you don’t specify the branch, the master is used.

Posted in Technology | Tagged , , , , | 2 Comments

Capistrano – Email on Deployment

It’s a good practice to send a notification to certain people when a new version is deployed. This process can easily be automated in Capistrano. Add the following file to the ‘lib’ directory with the filename ‘cap_mailer.rb’:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'rubygems'
require 'actionmailer'
 
ActionMailer::Base.smtp_settings = {:address => "localhost", :port => 25, :domain => 'example.com'}
 
class CapMailer < ActionMailer::Base
  def deploy_notification(options)    
    message = "Deployment\n\n"
    options.each_pair do |k,v|
      message << "  #{k}: #{v}\n"
    end
 
    recipients "notify-deployment@example.com"
    from       'noreply@example.com'
    subject    "Deployment to #{options[:rails_env]}"
    body       message
  end
end
 
module CapistranoMailer
  def send(options)
    CapMailer.deliver_deploy_notification(options) 
  end
end
 
Capistrano.plugin :mailer, CapistranoMailer

In the ‘deploy.rb’ file, add the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'lib/cap_mailer'
require 'etc'
 
namespace :deploy do
  desc "Sends an email upon successful deployment"
  task :notify do
    mailer.send(
      :rails_env => rails_env,
      :deploy_to => deploy_to,
      :branch => branch,
      :application => application,
      :repository => repository,
      :deployment_time => Time.new.to_s,
      :deployer_username => Etc.getlogin)
  end
end
 
after "deploy", "deploy:notify"
after "deploy:migrations", "deploy:notify"
Posted in Technology | Tagged , , | 1 Comment

Sprint Burndown Chart Template

Attached is a spreadsheet template for a Sprint Burndown Chart.

Features:

  • Color coding for the time estimate (green when estimate goes down and red when it goes up)
  • Color coding for the status (In Progress, Done, and Impediment)
  • Pace and 20% above/below lines on the chart
  • Supports multiple tasks per backlog item
  • Sort any column without loosing which backlog item tasks belong to
  • Prints nicely on Tabloid paper

Download it here: Sprint_Tasks_Burndown_Chart

Posted in agile | Tagged , , , | 1 Comment

Capistrano – Install Gems Remotely

Capistrano does not have out-of-the-box support for installing gems on your servers. You may not need this if you are packing them, but some of us would like to run the equivalent of ‘rake gems:install’. To do this in capistrano, add the following to your deploy.rb file:

namespace :gems do
  desc "Install gems"
  task :install, :roles => :app do
    run "cd #{current_release} && #{sudo} rake RAILS_ENV=#{rails_env} gems:install"
  end
end
Posted in Technology | Tagged , , , | Leave a comment

Capistrano – Adding Help to Deployments

If you don’t do something everyday, you tend to forget how to do it. The same is with the deployments. To help this, you can easily add a help section to Capistrano in your ‘config/deploy.rb’ file. Here is a sample:

desc "Print usage/help information"
task :help do
    puts "Usage: cap <task> -S stage=[staging|production] -S branch=<branch_name>"
    puts ""
    puts "Examples:"
    puts "----------------------------------------------------------------------"
    puts "  Production:"
    puts "    $ cap deploy:migrations -S stage=production"
    puts ""
    puts "  Staging:"
    puts "    $ cap deploy:migrations"
    puts ""
    puts "  Production with branch:"
    puts "    $ cap deploy:migrations -S stage=production -S branch=rel_001"
    puts ""
    puts "  Staging with branch:"
    puts "    $ cap deploy:migrations -S branch=rel_001"
end

When you forget how to do something, just run the following:

$ cap help
 
Usage: cap <task> -S stage=[staging|production] -S branch=<branch_name>
  * executing `help'
Examples:
----------------------------------------------------------------------
  Production:
    $ cap deploy:migrations -S stage=production
 
  Staging:
    $ cap deploy:migrations
 
  Production with branch:
    $ cap deploy:migrations -S stage=production -S branch=rel_001
 
  Staging with branch:
    $ cap deploy:migrations -S branch=rel_001
Posted in Technology | Tagged , , | Leave a comment

Capistrano – Multiple Environments

Capistrano is designed to deploy your Rails application (yes it can handle others, but more work is required) to production.  However, most applications need to be deployed to more than one environment.  For example, we deploy to a staging environment in addition to production.  This lets select users and internal employees kick the tires before we release it in the wild.  After many complaints, capistrano-ext was born.  I am all in favor of using plug ins, but this is such a trivial issue, it’s easier to add the code than worry about the plugin.

set :stage, "staging" unless variables[:stage]
set :deploy_to, "/var/www/#{application}"
 
case stage
when "staging"
  set :rails_env, "staging"
  role :web, "staging.example.com"
  role :app, "staging.example.com"
  role :db"staging.example.com", :primary => true
when "production"
  set :rails_env, "production"
  role :web, "production.example.com"
  role :app, "production.example.com"
  role :db,  "production.example.com", :primary => true
end

To deploy to the correct environment call capistrano:

$ cap deploy -S stage=staging

If your staging and production share the same machine (not the best idea):

set :stage, "staging" unless variables[:stage]
server "example.com", :app, :web: db
 
case stage
when "staging"
  set :rails_env, "staging"
  set :deploy_to, "/var/www/staging-#{application}"
when "production"
  set :rails_env, "production"
  set :deploy_to, "/var/www/production-#{application}"
end
Posted in Technology | Tagged , , | Leave a comment

Google Wave Invite Users Still Waiting

google_wave_logoIt looks like people who were invited to Google Wave by other users (the ones that received the original invite from Google) are still waiting to get their invite.  If Google sent out 100,000 invites and each user could invite 8 more people, that’s about 800,000 people who are waiting around getting frustrated.

Has anyone received their invite yet?

Posted in Technology | Tagged , , | Leave a comment

Twitter Moderators?

Looking at the results from a trending topic in Twitter, I saw the following:

twitter_moderator

Notice the description of the results, “Google Wave was released September 30, 2009. People are discussing it and begging for beta invites.”  I tried a few others and they all seem to have a bit of information relevant to the topic.  Is Twitter adding commentary to the discussions?  Is their commentary accurate?  I wonder what sampling size they used to determine that people were begging for a beta vs. offering a beta to other people?

As it turns out, they are leveraging data from WhatTheTrend.com.  This UGC can be modified by anyone, I just modified the meaning of #googlewave.  How long until people start abusing this to spread rumors.  From the position of the content, it appears this is official information about a trend when in reality, it is the rambling from a random user.

Posted in Technology | 2 Comments

How to get invited to Google Wave

I got some more invites, if you want one, follow me on Twitter, retweet this post, and DM me your email address.  First-come, first-served.

Update: 10/10 – I am out of invites.

Update: 11/3 – I have 10 more invites, post a comment if you want one.

Posted in Technology | Tagged , , | 22 Comments

InvalidAuthenticityToken Error in Rails

Working with a RESTful API in Rails can be a bit challenging at times.  One such problem is the ActionController::InvalidAuthenticityToken error.  This error is thrown when authenticity token is not present or invalid.  (This is how Rails protects itself from CSRF.) This token should only be checked with HTML and AJAX requests.  XML requests do not need this check.  However, if the following code is run:

$ curl -d "<status>...</status>" -X POST http://localhost:3000/statuses.xml

the InvalidAuthenticityToken error is raised.  The problem stems from an incorrect content type.  If you force the content type in the header:

$ curl -H "Content-Type: application/xml" -d "<status>...</status>" -X POST http://localhost:3000/statuses.xml

everything works as expected. This turns out to be a gotcha since GET requests don’t need the header.

Posted in Technology | Tagged , , , , | Leave a comment