Christian Vermeulen Christian Vermeulen - 4 months ago 18
Ruby Question

NoMethodError: undefined method `within' for main:Object

I'm trying to make Capistrano deploy script.

In my Capfile I make sure all rake tasks are included

# Load tasks
Dir.glob('config/capistrano_tasks/*.rake').each { |r| import r }


Next I have a 'migrations.rake' containing:

namespace :fileservice do
task :migrate do
within release_path do
info 'Doing migrations'

execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
end
end
end


In my deploy.rb I call the task at the very end with:

after 'deploy:publishing', 'fileservice:migrate'


For some reason I keep getting an error saying:

NoMethodError: undefined method `within' for main:Object


I have no idea where to look or what might be wrong... When googling I get a lot of "NoMethodError" hits but none about the 'within' method and most are general Ruby errors.

Where should "within" be defined? I dat a ruby on rails thing? Or capistrano?

Hopefully someone knows where to start looking or which library / script to include!

UPDATE: I just discovered that none of the methods work. When removing lines I got the same error for "info" and "execute".... So I guess somewhere, something is missing.....

Answer

You need to tell Capistrano where (i.e. on what servers) to run your SSH commands. Do this using an on block, as follows:

namespace :fileservice do
  task :migrate do
    on roles(:db) do
      within release_path do
        info 'Doing migrations'
        execute :php, fetch(:symfony_console_path), 'doctrine:migrations:migrate', '--no-interaction', fetch(:symfony_console_flags)
      end
    end
  end
end

Replace roles(:db) as appropriate for your task, depending on where you want the commands to be run. The expression on roles(:all) do ... end, for example, will run the commands on all servers.


You can also follow the official documentation at http://capistranorb.com, or the Capistrano README, both of which show examples of the task/on/execute syntax.

Comments