reminder. git. awesome.

posted by alexis reigel on july 19, 2012

This is just a quick reminder of how awesome git actually is. The story is short:

My disk crashed and I lost a couple of local commits that I hadn’t pushed (to github) yet. Luckily, I deployed them to the staging server already (actually, this is a reminder of how awesome git and capistrano actually are). Because capistrano is awesome and deploys the git repo to the server, and git is awesome too, I could restore my local repo from the staging server:

local:  $ ssh staging_server
server: $ # some `cd` action
server: $ tar czf asdf.tgz www_folder
server: $ # ctrl + D
local:  $ cd /tmp
local:  $ scp user@staging_server:/tmp/asdf.tgz .
local:  $ tar xf asdf.tgz
local:  $ cd my_local_repo
local:  $ git pull /tmp/asdf dev

A simple git pull from the copied repo and I’m all set again.

How awesome is your dev stack?

sinatra on capistrano

posted by alexis reigel on january 31, 2012

Capistrano is really great for deployment. The documentation however is somewhat scattered. The capistrano wiki has a more or less useful From The Beginning instruction and a Getting Started that doesn’t quite get you started.
However, the instructions on github are actually a very good starting point. Neverthelesse I want to show my sample here that has two additions to the one in the github help.

  1. Multistage support (i.e. staging and production server)
  2. Custom task to upload a config file

For further reading I recommend the Capistrano Handbook.

I assume that you’ve setup capistrano as described in the github help. The one thing I suggest is to ignore the part where the password is stored in the file. I highly advise to use ssh agent forwarding instead.

ssh_options[:forward_agent] = true # use local ssh key
set :user, "deployer"

Let’s setup multistage:

require 'capistrano/ext/multistage'
set :stages, %w(production staging)
set :default_stage, "staging"

Also, on most deployment server you may not have rights to sudo:

set :use_sudo, false

As I’m assuming you’re using passenger mod_rails:

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

The last part uploads our configuration file to the server. This configuration contains user and password for the database login. We don’t store that in the source code repository.

namespace :config do
  task :db do
    upload('db.yml', "#{deploy_to}/current/db.yml")
  end
end

before "deploy:restart", "config:db"

The whole capistrano configuration file is on github.