Deploy Sinatra with Vlad and Git

Deploying Sinatra with Vlad the Deployer and Git is basically just deploying with Vlad.

Step One - Install Vlad!

Install the gem:

gem install vlad
gem install vlad-git
gem install vlad-unicorn # if you're using unicorn

Or via bundler:

$ vi Gemfile

gem 'vlad', :require => false
gem 'vlad-git', :require => false
gem 'vlad-unicorn', :require => false  
#   again, if you're using unicorn

Step Two - Configure Vlad

First, you have make sure that rake is loading Vlad correctly, so open up your 'APP_ROOT/Rakefile' and add the following lines:

begin
  require 'vlad'
  Vlad.load :scm => :git, :app => :unicorn
rescue LoadError
  # do nothing
end

Note:

':scm => :git' and ':app => :unicorn' are used to to configure the plugins I've installed above. If you weren't using Unicorn, for example, you would omit ':app => :unicorn'.

Second, you have setup a deploy.rb (much like Capistrano, for those that know it). Create 'APP_ROOT/config/deploy.rb' with the following data:

set :application, "ExampleApp"
set :domain, "www.example.com"
set :deploy_to, "/home/username/example_app"
set :repository, 'git://github.com/path_to/example_app.git'

# unicorn if used
set :unicorn_pid, "#{deploy_to}/shared/log/unicorn.pid"
# to ensure RACK_ENV 
set :unicorn_command, "cd #{deploy_to}/current && RACK_ENV=production unicorn"
# if bundler is being used...
# set :unicorn_command, "cd #{deploy_to}/current && RACK_ENV=production bundle exec unicorn"

Thrid, you have to ensure that you can SSH to your remote host (www.example.com per the example above). I'm not going to turn this in to a long SSH tutorial, but before running Vlad for the first time be sure you can type 'ssh hostname' with no additional params, otherwise you'll either need to update your '~/.ssh/config' file with something like:

Hostname www.example.com
User <your_remote_user>
IdentityFile ~/.ssh/<remote_id_file>
ForwardAgent yes

Optionally, Vlad has methods of configuring ssh params:

set :user, "username"
# ... and ...
set :domain, "#{user}@www.example.com"

But I find this to be less pretty, personally.

Step Three - Vlad Deployment

Once Vlad is configured by following the above, you simply have to set it up and deploy it. For a full list of Vlad commands use: $ rake -T vlad However, here are the priamry ones I use:

To setup the directory struction for deployment (first time use only): $ rake vlad:setup

To push code to the remote server (used on every deploy): $ rake vlad:update

To (re)start the application server (used on every deploy): $ rake vlad:start_app

That's it.

Note:

For Sinatra app's you can ignore 'vlad:migrate', it is designed to use Rails migrations, which you don't have.

What's Vlad do remotely?

'setup' and 'update' together creates the following tree starting at the previously set 'deploy_to' location:

~/example_app/
        ./current -> ~/example_app/releases/<current version datestamp>
        ./releases/
        ./releases/<release datestamp>
        ./revisions.log
        ./scm/
        ./scm/repo/
        ./scm/repo/<git clone contents>
        ./shared/
        ./shared/log/
        ./shared/pids/
        ./shared/system/

On each subsequent update, a new folder will be created in './releases/' and the './current' symlink will be updated to point to it.

Note to Bundler users!

By default, 'rake vlad:update' will not run bundle install. You'll need to create a custom vlad deployment task for that by adding the following to your 'Rakefile':

task "vlad:deploy" => %w[ vlad:update vlad:bundle:install ]

And adding the following to the very top of your 'config/deploy.rb':

require "bundler/vlad"

You would then run... rake vlad:deploy ...instead of... rake vlad:update ...when installing your application.