Does anyone actually use Capistrano?

I think it’s great, but I have a problem. Before using it, I was deploying using subversion - which is lovely, but being able to do everything locally from your own machine without shelling in is sweet.

Set-up was a bitch though. On two machines (my Win-box and the Debian Linux server itself) I experienced behaviour which was completely contrary to the way I understood the instructions - and the tutorial in the Beta version of Agile Web Development with Ruby On Rails 2nd Edition. (There was an error in their instructions which I corrected. The pragmatic guys pragmatically updated the PDF the next day, so cudos to them!)

The problem is this. If I create a new task in my deploy.rb file, I can’t use it. I can see it in ‘rake remote:show_tasks’, but rake remote:<task_name> throws a ‘I don’t know how to do that, Dave’ style error.

This also applies to hooks - before and after tasks don’t work.

I DID get it to work (after a very frustrating day) by finding the original file deploy.rb is inherited from, finding the ‘update_code’ task, copying it into my deploy file, and editing it. If I called my tasks from THERE, it worked.

I then found that my hooks worked - but only hooks into the function I’d copied over.

Not as much use now, as I have the code there to edit! ;^)

So… I’m guessing this isn’t mormal behaviour, as no instructions mentioned copying code. Copying code is Evil anyway, and I should probably be burned as a sacrifice to the Gods of Good Programming. (Forgive me mighty OO, may you purge my soul DRY.)

So what IS going on? Any ideas?

Ben

Capistrano isn't rake.

If you create a new Capistrano task then you have to run

cap <task>

to get the task to work.

Unless and until you modify the 'capistrano.rake' file in your lib/tasks directory to delegate the task for you.

Regards

NeilW

Ben Dunkley wrote:

The problem is this. If I create a new task in my deploy.rb file, I can't use it. I can see it in 'rake remote:show_tasks', but rake remote:<task_name> throws a 'I don't know how to do that, Dave' style error.

When you call "rake remote:foo", you're actually running a rake task 'remote:foo' that internally calls the Capistrano task 'foo'.

So if I add this to my deploy.rb:

desc "Print foo" task :foo do   puts "foo" end

then I can execute that Capistrano task by typing "cap foo" at the command line. But rake won't know about it yet; if I type "rake remote:foo", it'll complain.

The trick is to tell rake about this new task by adding it to your capistrano.rake file, which is in the lib/tasks directory in your Rails app. Add this inside the "namespace :remote" block:

desc "Print foo" task(:foo) { cap :foo }

Now you'll be able to run "rake remote:foo" without any problems.

This also applies to hooks - before and after tasks don't work.

Can you post your code? I have this kind of thing in most of my deploy.rb files, and it works fine:

desc "Link in production database config" task :after_update_code do   run <<-CMD     ln -nfs #{deploy_to}/#{shared_dir}/config/database.yml #{release_path}/config/database.yml   CMD end

Chris

Thanks so much for the help on this. I really was tearing out my hair last week... Now I'll be able to do things properly. :^D

I may mention it on the Agile Developers erratum page, as they suggest both using rake remote:blah AND using before and after hooks... Although they don't go into hooks very deeply. Hence the start of my troubles...

I've yet to update my code to reflect my new knowledge, as I have a working system and some tight deadlines ;^)

FTR, my code is this:

desc "Copy the database config files over and set up sym links to the content" task :after_update_code, :roles => :app do   db_config = "#{shared_path}/config/database.yml"   puts "Creating config files and sym links... ************************************************"   run "cp #{db_config} #{release_path}/config/"   run "ln -s #{shared_path}/public/product #{release_path}/public/product"   run "ln -s #{shared_path}/public/preview #{release_path}/public/preview" end

The stars were there to readily identify it in the task output. As I said, when called from rake remote:deploy, it didn't work - until I copied the 'update_code' code in. I can kinda vaguely sorta somehow see how rake would know less about the inheritance structure of the Cap file. I don't know how tasks tie together behind the scenes though, or how they differ from functions, or what kind of inheritance holds true, or what mechanism Rake uses to determine the living place of a task in a heirarchy of files..

I see you use more link options. -f I can understand, but Debian's man description of -n doesn't really tell me, as a realtive *nix newbie (9 months and counting) what it actually does or why I would want it...

Fun fun fun. :slight_smile:

Ben

Chris Mear wrote: