Bruno Miranda's Notebook

Personal Blog about Ruby on Rails, XHTML, CSS, and Design

As many of you are doing, I moved a couple of my source repositories to GitHub. At the moment I have one private and one public.

Here is a simple guide to deploy using vlad. You will need a condig/deploy.rb file:


set :application, "appname"
set :domain,      "domain.com"
set :deploy_to,   "/var/rails/#{application}"
set :scm,         "git"

# use the following with a private repos
# set :repository,  "git@github.com:user/appname.git"

# use the following with a public repos
# set :repository,   "git@github.com:user/appname.git"

set :mongrel_port, 8070
set :mongrel_servers, 2

namespace :vlad do
  desc 'Runs vlad:update, vlad:symlink, vlad:migrate and vlad:start'
  task :deploy => ['vlad:update', 'vlad:symlink', 'vlad:migrate', 'vlad:stop_app', 'vlad:start_app']

  desc 'Symlinks your custom directories'
  remote_task :symlink, :roles => :app do
    run "ln -s #{shared_path}/database.yml #{current_release}/config/database.yml"
  end
end

Possible Gotchas!

If you have Vlad 1.2 installed you will need to add the following to your Rakefile

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

If you run into the following error:

fatal: You must specify an archive format

It is probably because you installed git-core with sudo apt-get install git-core. The reason for that is apt-get install git-core installs a 1.4x version of git on your Ubuntu machine, and a default for git archive was added in a later version. Thankfully the solution is pretty simple. Remove the previously installed version of git-core and install a newer version from source.

sudo apt-get remove git-core
wget http://kernel.org/pub/software/scm/git/git-1.5.5.1.tar.bz2
sudo apt-get build-dep git-core
tar xjf git-1.5.5.1.tar.bz2
cd git-1.5.5.1/
./configure --with-tcltk
make
sudo make install

Use –with-tcltk since there is no need to install GUI on the server.

If you do however want to install GUI for some reason you will need to install gettext and start the the git-core installation from source from the beginning.

sudo apt-get install gettext

Now all you have left is to run setup and then deploy:

rake vlad:setup

and

rake vlad:deploy

Deploying a Private Repos

Make sure you go into the server, generate an ssh key for the deployment user and paste the public key into the Github’s deploy key. You will also need to go into the server and clone the repos so that the ssh key pair is generated. Make sure the public key on the server is owned by the deploy user and the permissions are correct:

chown deploy:deploy id_rsa.pub

and

chmod 600 id_rsa.pub

Let me know if you encounter any issues with the instructions above.

In preparation for my presentation next Monday, July 23, I set out to install and configure Capistrano 2 and ssh locally (OS X) so that I can demonstrate the deployment of a Rails app using Capistrano 2.

I followed the instructions online, setting up a new user called deploy, starting up the ssh server for that user and making sure that all PATH were correct.

A way to test your paths is running

echo $PATH

The above should return the location for the binaries for to ruby, rake, gem, etc are located.

Mine did so, I thought life was great. I created the following recipe:

load 'deploy' if respond_to?(:namespace)

require 'mongrel_cluster/recipes'

set :application, "hello"
set :domain, "localhost"
set :repository, "file:///Users/Bruno/svn/hello/"
set :deploy_to, "/Users/deploy/www/sites/#{application}" 
set :user, "deploy"
set :use_sudo, false
set :scm, :subversion
set :scm_command, "/usr/local/bin/svn"

role :app, domain
role :web, domain
role :db,  domain, :primary => true

namespace :deploy do

  desc "Run after every successful deployment." 
  task :after_default do
    cleanup
  end

  desc "Restart Mongrel"
  task :restart, :roles => :app do
     run "cd /Users/deploy/www/sites/hello/current/ && script/server"
   end

end

Which should be enough to properly deploy and start the mongrel server. That was not the case. I kept getting errors related to cap not being able to find the path to rake or rubygems.

With the help of my fried Chris Saylor, I created a task to inspect the system variables:

  desc "Debug task."
  task(:debug_env) { run "env" }

Upon running the task above I noticed that the path= line was not showing the proper path to where my binaries where located. It should have been:/usr/local/bin

It turns out that I had to set create an ssh environment path in ~/.ssh/environment

PATH=/usr/local/bin:/usr/bin:/usr/sbin

Also, you will need to edit /etc/sshd_config, uncommenting the line that states: PermitUserEnvironment no, and changing the value to yes.

#
PermitUserEnvironment yes
#

After those changes have been made, you should be able to run:

cap deploy:setup
cap deploy

Enjoy!

PS. Thanks to everyone that had the patience to put up with me while I pulled my hair out for 2 days trying to find the answer to this problem, that includes, voxdolo, cwsaylor and queso.