Capistrano 3, using upload! in a task in lib/capistrano/tasks - ruby-on-rails

I'm using Capistrano 3 and I want to create my own task. So I created a file my_new_thing.rake in lib/capistrano/tasks and I can see the task when I run cap -T. But... some of the methods aren't available. When I try to use upload! I get
cap aborted!
NoMethodError: undefined method `upload!' for main:Object
But if I move the same task into config/deploy.rb then then upload! method is available.
So what's going on? How do I create new Capistrano tasks put them in separate file and have them work?

I had the same problem, I created my own recipe in a separate file which I loaded in deploy but couldn't get upload! to work.
What fixed it for me was adding a role filter inside the task making my final recipe look something like this:
namespace :figaro do
desc "Transfer Figaro's application.yml to shared/config"
task :upload do
on roles(:all) do
upload! "config/application.yml", "#{shared_path}/config/application.yml"
end
end
end
before "deploy:check", "figaro:upload"
I hope that helps!

You can create a folder config/recipes for your capistrano recipes if you want to keep them in separate files.
Use the .rb extension since this isnt a regular rake task.
In config/deploy.rb add this line
load File.expand_path('../recipes/my_new_thing.rb', __FILE__)

If you want to use the rake tasks then you will need to create a task in the deploy file which calls that rake tasks which is not that smart of a move. So as #Sharagoz suggested the best route will be to create your own recipe file and include that in.

Related

Rake task during application initialization rails

I want to execute a rake task when the server of my application starts.
In config/application.rb i put the following:
if !Rails.env.production?
Rake::Task[ "init:db_records" ].invoke
end
The rake task is well defined, and runs without a problem if i invode it from terminal
rake init:db_records
But when placed in config/application.rb (or even in any initializers/*) i got the following error.
Don't know how to build task 'init:db_records'
What is the way to execute a rake task when the server starts ?
Thanks!
Rails already has a mechanism for setting up a development database -- rake db:seed. It does not run automatically when you start the app, but it does run as part of rake db:setup.
Unless you have a good reason, it's usually best to stick the conventions that Rails provides.
For those who encounter the same problem in the future.
I achieved this by creating a new file in the initializers directory, where i put the code of the rake task.
The advantage of this at this point, is that the application is already loaded, so you have access to ActiveRecord functions...
Putting the code directly in config/application.rb didn't work, since my models were not loaded yet.
Hope it will help!
Your Rake tasks are (likely) defined in a Rakefile. The initializer has no idea that file even exists, so it doesn't know about the tasks within.
The easiest way to circumvent this is by doing something like this:
Dir.chdir(Rails.root) do
`rake init:db_records`
end
That is, change the working directory to the root rails directory, then running the command.

Capistrano custom task calling git:create_release

I have created a custom capistrano task located in the lib/capistrano/tasks directory.
I have placed the task under a namespace. When I run cap -T my task appears in the list.
I want to call the git:create_release task inside my task. Currently I have:
namespace :setup do
desc "Performs a setup"
task :run do
on roles(:all) do
git.create_release
end
end
end
However, the above does not work. It gives me the following error:
undefined local variable or method `git'
Now I assume the issue lies in not having some explicit import of the git submodules. I am not sure how to go about including them in the custom tasks module.
Instead of git.create_release I think you need invoke "git:create_release". See the Capistrano README.

Simple rails rake task refuse to run with error "Don't know how to build task", why?

I have this simple rake task which refuses to run. I just don't see why it looks correct. Who can pinpoint me to the probably very simple mistake I made? Thank you!
/lib/tasks/reindex.rb:
namespace :db do
desc "Tire reindex profiles"
task :reindex => :environment do
system "cd #{Rails.root} && rake environment tire:import CLASS='Profile' FORCE=true"
end
end
The error:
rake db:reindex
rake aborted!
Don't know how to build task 'db:reindex'
Rename your file to reindex.rake and it should work.
Related: How to build task 'db:populate'
You can also get this error if you forget to put the namespace before your task name. (i.e. :reindex instead of db:reindex)
The file extension for rake tasks must be '.rake'.
If you named your file as '.rb', then rake will not find it, and you will question your own sanity for several minutes before ending up here.
Don't forget to check that you call the name of the task and not the file name.
The best thing is that they be named the same.
This error happen to me is because the namespace name got underscore
As is: deploy_app (not work)
To be: deployapp (working)

vlad the deployer vlad:start_app with passenger issue

I'm trying to deploy a rails app using vlad the deployer.
I'm using nginx and passenger.
I have an issue with the vlad:start_app task.
When I deploy I get the following issue
touch: cannot touch `/var/www/mysite.com/releases/20100623130302/tmp/restart.txt': No such file or directory
rake aborted!
execution failed with status 1: ssh mysite.com touch /var/www/mysite.com/releases/20100623130302/tmp/restart.txt
The issue is obvious in that the 20100623130302 in releases does not exist.
I would rather use the following task but cant override the default vlad:start_app task by placing this in my config/deploy.rb file.
namespace :vlad do
desc 'Restart Passenger'
remote_task :start_app do
run "touch #{current_path}/tmp/restart.txt"
end
end
Any help appreciated. The options I though of are to either get the default vlad task to work or someway to override the default vlad task.
RAILS_ROOT/Rakefile is the file.
To override a task you have to remove the previous one first as defining the same task again just creates a second task that will run after the first one.
Here's an example from the vlad website how to replace a task:
namespace :vlad do
# Clear existing update task so that we can redefine instead of adding to it.
Rake.clear_tasks('vlad:update')
remote_task :update, :roles => :app do
#custom update stuff
end
end

How do I find the source file for a rake task?

I know you can view all possible rake tasks by typing
rake -T
But I need to know what exactly a task does. From the output, how can I find a source file that actually has the task? For example, I'm trying to find the source for the db:schema:dump task.
I know this is an old question, but in any case:
rake -W
This was introduced in rake 0.9.0.
http://rake.rubyforge.org/doc/release_notes/rake-0_9_0_rdoc.html
Support for the –where (-W) flag for showing where a task is defined.
Despite what others have said, you can programmatically get the source location of rake tasks in a rails application. To do this, just run something like the following in your code or from a console:
# load all the tasks associated with the rails app
Rails.application.load_tasks
# get the source locations of actions called by a task
task_name = 'db:schema:load' # fully scoped task name
Rake.application[task_name].actions.map(&:source_location)
This will return the source locations of any code that gets executed for this task. You can also use #prerequisites instead of #source_location to get a list of prerequisite task names (e.g. 'environment', etc).
You can also list all tasks loaded using:
Rake.application.tasks
UPDATE: See Magne's good answer below. For versions of rake >= 0.9.0 you can use rake -W to show the source location of your rake tasks.
There is no programmatic way to do this unfortunately. Rake tasks can be loaded either from rails itself, lib/tasks, or from any plugin with a tasks directory.
This should nab most everything not within Rails itself:
find . -name "*.rake" | xargs grep "whatever"
As for db:schema:dump, here's the source:
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
task :dump => :environment do
require 'active_record/schema_dumper'
File.open(ENV['SCHEMA'] || "#{RAILS_ROOT}/db/schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
It can be found on line 242 of lib/tasks/database.rake in the rails 2.2.2 gem. If you've got a different version of Rails, just search for "namespace :schema".
You probably actually want the source of the ActiveRecord::SchemaDumper, but I think you should have no trouble figuring out where that is. :-)
For most rake tasks in Rails, look in the Rails gem directory, in lib/tasks.
If you've vendored Rails into your app directory structure then look in vendor/rails/railties/lib/tasks instead
Either way, db:schema:dump is in databases.rake.

Resources