Archive for March, 2008

Recipes: get (download) your log

Monday, March 24th, 2008

These are a few snippets for getting and viewing your log files

This task will download your production log and put a time stamped version in your local log directory

task :download_log do

TIMESTAMP = '%Y%m%d%H%M%S'

get "#{deploy_to}/current/log/production.log",

"log/production._#{Time.now.strftime(TIMESTAMP)}.log"

end

Dealing with user uploaded files between deployments

Monday, March 24th, 2008

If you are writing an application that allows your users to upload files, you have to work around the fact that their files will be deleted every time you deploy your application.

Usually if you want your file to available they will be uploaded to the public folder. The solution is to store those files in the “shared” directory.

Here is one example on how to set that up for a Rails app.

Create a directory called “uploads” in your.

When using attachment_fu or similar, use a :prefix=>”/public/uploads/#{tablename}

Add the following tasks to your deploy file

desc "link upload directory"
task :symlink_uploads, :roles => :app do
  run "ln -nfs /var/www/apps/#{application}/containers/rails/#{application}/shared/system/uploads #{release_path}/public/uploads"
end
end
desc "create upload directory"
task :create_uploads_directory, :roles => :app do
	run "mkdir #{deploy_to}/shared/system/media"
end
end
Hook them into your deployment
after "deploy","symlink_uploads"

after "deploy:setup","create_uploads_directory"

Any uploads will now be stored outside the versioned directories.

Recipes: Ferret

Monday, March 24th, 2008

Ferret (http://ferret.davebalmain.com/trac) is a high-performance, full-featured text search engine library written for Ruby.

It is inspired by Apache Lucene Java project.

acts_as_ferret comes with a built in DRb server that acts

as the central hub for all indexing and searching in your application.

Ferret now comes with a recipe for managing your Drb server. The Ferret config file is located at config/ferret_server.yml and

looks like this:

production:
host: ferret.yourdomain.com
port: 9009
pid_file: log/ferret.pid

The supplied recipe looks like this

namespace :ferret do
desc “Stop the Ferret server, and continue even if the operation fails.”
task :stop_safe, :roles => :app, :o n_error => :continue do
top.ferret.stop
end

desc “Start Ferret Server”
task :start, :roles => :app do
top.ferret.start
end

desc “Stop Ferret Server”
task :stop, :roles => :app do
top.ferret.stop
end

desc “Restart Ferret Server”
task :restart, :roles => :app do
top.ferret.restart
end

desc “Rebuild the Ferret index”
task :rebuild, :roles => :app do
run “cd #{current_path} && rake production ferret:rebuild”
end

desc “Destroy the Ferret index”
task :destroy, :roles => :app do
run “cd #{current_path} && rm -rf index/production index/development index/test”
end

end

.caprc (the capistrano dotfile)

Monday, March 24th, 2008

As of revision 5774, capistrano  has a  default  dotfile.

Here’s how dotfiles work in edge capistrano:

* The dotfile, by default, is called “.caprc” and will be searched
for in your home directory. On Unices, this is the ENV["HOME"]
variable. On windows, it is “#{ENV["HOMEDRIVE"]}#{ENV["HOMEPATH"]}”.
The environment is searched in that order (so if you have HOME set on
Windows, it will be used), and if none is found the root directory is
used.

* You can override the path to the config file via the -c switch:
“cap -c /path/to/.customrc …”

* You can specify that you don’t want to use the config file at all
via the -x switch: “cap -x …”

* The file will be loaded immediately after the “-S” options are
processed. Thus, variables you set via -S will be available to
the .caprc stuff.

The major difference between the patch you submitted and the way I
implemented it is that the .caprc will always be loaded, whether or
not other recipe files are specified. (As I mentioned above, you can
skip the config loading with -x.)

[From Jamis buck on the mailing list ]

Webservers : Lightspeed

Monday, March 24th, 2008

namespace :deploy do
task :restart do
sudo “/usr/local/lsws/bin/lswsctrl restart”
end

task :stop do
sudo “/usr/local/lsws/bin/lswsctrl stop”
end

task :start do
sudo “/usr/local/lsws/bin/lswsctrl start”
end

end

Webservers: Thin

Monday, March 24th, 2008

If you are using the Thin webserver you can add  the following to you deploy file to restart support restarting  Thin

namespace :deploy do
task :start do
sudo “/etc/init.d thin start”
end
task :stop do
sudo “/etc/init.d thin stop”
end
task :restart do
sudo “/etc/init.d thin restart”
end
end

Checking out from your local file system

Monday, March 24th, 2008

If you can’t use a SCM from your server for  whatever reason you can use

set :deploy_via, :copy

to compress and upload your code from your development machine.

Here is an example and some setting that you can use :

set :deploy_via, :copy

# the source is checked out first before it is compressed and uploaded

# this specified how this happens export or checkout

set :copy_strategy, :export

set :copy_remote_dir, “some_tmp_path” # it defaults to /tmp

set :copy_compression, :zip       # :zip or :gzip, :bz2

What is default_run_options[:pty] ??

Monday, March 24th, 2008

This setting specifies the value for pseudo terminals in Capistrano, the default value is false

As of version 2.1, Capistrano no longer requests that a pty be
allocated for each command. This means that your default profile
settings will be loaded automatically for every command.

You can override the default setting for an individual command by adding :pty => true.

For more information on ssh and shells including Pseudos terminals:  http://net-ssh.rubyforge.org/chapter-5.html

More on Pseudo terminals in general : http://en.wikipedia.org/wiki/Pseudo_terminal

Checking out from Git

Monday, March 24th, 2008

ssh_options[:port] = some_port_number
set :scm, :git
set :branch,  “stable”
set :scm_url, “staging” # In actuality, this is set to the IP address
set :scm_passphrase, proc { Capistrano::CLI.password_prompt(“SSH
passphrase: “) }

set :repository,  “#{scm_user}@#{scm_url}:project.git”
set :remote, scm_user # Set in ~/.caprc
set :repository_cache, “git_master”
set :deploy_via, :remote_cache

[to_cleanup]

Checking out from http+svn

Monday, March 24th, 2008

Setup your variables like so:

set :svn_username, “username”
set :svn_password, “password”
set :projectname, “my_project”

set :repository, “http://my_svn_server.org/repos/#{projectname}/trunk/”

To prevent being asked for password, you may need to check out a project from your repository once from the account you deploy with, on the server.

[to_clarify]