Capistrano 3 - Understand tasks - ruby-on-rails

i'm trying to understand how capistrano 3.1 is working, but because of its lack of documentation (its capistrano, so...), im running below my understanding.
Let me explain.
Here's a snippet taken from capistrano/rails gem
namespace :deploy do
desc 'Runs rake db:migrate if migrations are set'
task :migrate => [:set_rails_env] do
on primary fetch(:migration_role) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "db:migrate"
end
end
end
end
#[...]
end
when execute cap integration deploy:migrate, it sends the following command:
cd /srv/app/releases/20131106101722 && ( RAILS_ENV=integration /tmp/app/rvm-auto.sh . rake assets:precompile )
I changed a little bit the (non-working) code provided for delayed_job into that
namespace :delayed_job do
def args
fetch(:delayed_job_args, '')
end
def delayed_job_roles
fetch(:delayed_job_server_role, :app)
end
def delayed_job_bin
fetch(:delayed_job_bin, :'bin/delayed_job')
end
desc 'Restart the delayed_job process'
task :restart do
on roles(delayed_job_roles) do
within release_path do
with rails_env: fetch(:rails_env) do
execute delayed_job_bin, 'restart', args
end
end
end
end
end
And i get the following command cd /srv/winddle/current && ( RAILS_ENV=integration bin/delayed_job restart )
Obviously, it misses the bundle exec command.
I dive deeply into capistrano/bundler and capistrano/rails to look for some kind of hook that would add bundle exec automatically to any of these commands (or force the register of ssh kits commands) but couldnt find any.
The only solution i found is to use
execute :bundle, :exec, delayed_job_bin, :start, args which is not acceptable of course.
Anyone proper solution / explanation is welcomed.
Regards

Add the following line in deploy.rb, then use the code provided by delayed_job other than changing script to bin, which I see you already did:
set :bundle_bins, fetch(:bundle_bins, []).push('bin/delayed_job')
For users of RVM, add this instead:
set :rvm_map_bins, fetch(:rvm_map_bins, []).push('bin/delayed_job')
Source: https://github.com/capistrano/bundler#usage.

I'm literally just starting out with Capistrano and also struggling against a lack of documentation, so sorry if this post misses the mark.
v3 relies a lot on sshkit, so reading the documentation for that should be a big help. The readme gives an example that may solve your problem.
SSHKit.config.command_map.prefix[:rake].push("bundle exec")
puts SSHKit.config.command_map[:rake]
# => bundle exec rake
I also found an alternative solution in a Semaphore blog post.
SSHKit.config.command_map[:rake] = "bundle exec rake"
SSHKit.config.command_map[:rails] = "bundle exec rails"

Related

Run rake task from Capistrano if it exists

I need to a create a Capistrano pre-deploy step that runs a custom rake task.
in deploy.rb:
before 'deploy:starting', 'db:rollback_staging'
namespace :db do
desc 'Rollback staging db only if PR already deployed requires rollback'
task :rollback_staging do
on roles(:master) do
within current_path.to_s do
with rails_env: 'staging' do
execute :rake, 'release:rollback_staging'
end
end
end
end
end
The problem is that when deploying this code the rake task is not yet present on the server and therefore deploy fails with:
rake stdout: rake aborted!
Don't know how to build task 'release:rollback_staging' (See the list of available tasks with `rake --tasks`)
If there a way to check if the rake task exists from Capistrano?
smth like:
with rails_env: 'staging' do
execute :rake, 'release:rollback_staging' if rake_exists? 'release:rollback_staging'
end
I ended up just ignoring not 0 exit code from a rake task using raise_on_non_zero_exit: false:
with rails_env: 'staging' do
execute :rake, 'release:rollback_staging', raise_on_non_zero_exit: false
end
Would this work? Saw this pattern in https://github.com/AgileConsultingLLC/capistrano3-delayed-job
if Rake::Task.task_defined?('release:rollback_staging')

What does the rake task db:full_reset do?

I have the following in my deploy script in which I inherited from other devs. Does any one know what the rake task db:full_reset does? I would think it resets the db but I can't find that actual task anywhere in the code. Running rake -T doesn't give any clues. Would it be located in a gem?
namespace :db do
task :full_reset do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, "exec rake db:full_reset"
end
end
end
end
end
You can use rake -W db:full_reset to see where that task is defined

How to fix "uninitialized constant" in a Rake task

I have a problem when I do:
namespace :xaaron do
task :get_roles do
roles = Xaaron::Role.all
puts roles
end
task :get_role, [:name] do |t, args|
role = Xaaron::Role.find(args[:name].parameterize)
puts role
end
end
The first task will work fine. I can even add binding.pry and run Xaaron::Role and get information about Roles back. But the second task fails with:
NameError: uninitialized constant Xaaron::Role
I run each task in my main app because these tasks are inside an engine, using:
bin/rake xaaron:get_roles` and `bin/rake xaaron:get_role
I can run bin/rails c in the main application that uses the engine and run Xaaron::Role and get information about Roles table.
Why is the second one failing but the first one is not? Is there scoping with arguments?
I'm not sure why either works, but if this is Rails and those are Rails models, your tasks should depend on the environment:
task :get_roles => [ :environment ] do
By depending on the :environment task, it first loads Rails.
Also see: What's the 'environment' task in Rake?.
You can also run a Rake task as
bundle exec rake environment xaaron:get_role
This will load the Rails environment first.
I kept getting uninitialized constant errors for a Rake task, even after depending on :environment and running with bundle exec.
The issue was that I was making a Rake::TestTask and, even though the Rake task had access to all constants, the test files themselves did not have access to constants.
The solution was to add this line to the top of my test file:
require_relative '../config/environment'
This is the Rake task:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
end
To add, as of Ruby 1.9 and above, you can use this hash syntax:
namespace :xaaron do
desc "Rake task to get roles"
task get_roles: :environment do
roles = Xaaron::Role.all
puts roles
end
#####
end
And then you can run the command below to run the Rake task:
rake xaaron:get_roles
or
bundle exec rake xaaron:get_roles

How to execute a Rails "Rake Task"

what's up?
My friend created a rake task to update our data in the database (because we have db changes). Following is the task:
namespace :db do
task :update_database => :environment do
puts "Update do banco"
posts = Post.where("source_id is null").order("id")
done = Array.new
posts.each do |post|
if post.source_id.nil? and !done.include?(post)
posts2 = Post.where("content LIKE ? AND id != ?", post.content, post.id)
done.concat(posts2)
posts2.each do |post2|
post2.source_id = post.id
post2.save
end
end
end
end
end
I already executed this rake task in my localhost, but I deploy my project to heroku and now my project won't open online. I don't remember what's the command to execute rake tasks and I can't find it in no place.
My questions is:
What's the command to execute rake tasks?
What's the command to execute rake tasks on heroku? Just "heroku run "?
Thanks!
heroku run bundle exec rake db:update_database
should do.
bundle exec ensures that the script is run in the context of current bundle.

'rake' "in order to" 'rake'

To prepare database for my Ruby on Rails 3 application I need to run the following steps in the Terminal:
rake db:create
rake db:migrate
rake db:seed
Is it possible to do all those steps in one? Maybe it is possible running a 'rake' command that will "fire" another 'rake' command... but how?!
You can define your own rake tasks which call other tasks as prerequisites:
# lib/tasks/my_tasks.rake
namespace :db do
desc "create, migrate and seed"
task :do_all => [:create,:migrate,:seed] do
end
end
Normally the body of the task would contain Ruby code to do something, but in this case we are just invoking the three prerequisite tasks in turn (db:create,db:migrate,db:seed).
The empty do-end blocks are not needed, e.g. (for zetetic's answer)
$ cat lib/tasks/my_tasks.rake
# lib/tasks/my_tasks.rake
namespace :db do
desc "create, migrate and seed"
task :do_all => [:create,:migrate,:seed]
end
rake db:create db:migrate db:seed will do all that.
zeteitic got it right, but in the event you don't want to namespace this task under "db", you'd want something more like this:
desc "Bootstrap database."
task :bootstrap => ["db:create", "db:migrate", "db:seed"] do; end
And on the command line:
rake bootstrap
# => create, migrate and seed db

Resources