Installing and starting daemon script via Capistrano 3 - ruby-on-rails

I've been trying this for days without success now. I am using Capistrano 3 to deploy my Ruby on Rails 4 code on my production server.
At the end of the deployment process, I want to restart my daemon scripts, which I would manually do by this command:
RAILS_ENV=production bundle exec ruby script/my_daemon restart
On my Capistrano 3 recipe (config/deploy.rb) I've tried a few different settings but none of those worked.
namespace :deploy do
desc 'Restart application'
task :restart do
invoke 'unicorn:restart'
end
after :publishing, :restart
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
within release_path do
execute :rake, 'tmp:cache:clear'
# Daemons
# This ends up with an error
execute :bundle, :exec, :ruby, "RAILS_ENV=production /var/www/MY_APP/current/script/my_daemon restart;"
# This starts the daemon, but in development environment
execute :bundle, :exec, :ruby, "/var/www/MY_APP/current/script/my_daemon restart RAILS_ENV=production;"
# This also starts the daemon, but in development environment
execute :bundle, :exec, :ruby, "/var/www/MY_APP/current/script/my_daemon restart;"
end
end
end
end
Could someone help me write the right recipe to restart my daemons in production environment? Thanks.

Try setting env via capistrano dsl
within release_path do
with rails_env: :production do
execute :bundle, :exec, :ruby, "#{current_path}/script/my_daemon restart"
end
end

Related

Nothing happens after Capistrano deploy command

I am using Capistrano gem for deploy management of my Ruby on Rails app. Its connected with AWS server, EC2, MySQL Redis. While I am putting my command "cap production deploy" or "cap staging deploy" nothing happens. I just got stuck there.
To add here my SSH key is properly added. And in my AWS security groups, only the authorized IPs are added to have the permission of deploying. My IPs are also added.
But when I add open ports 0.0.0.0/0 in all security groups it allows me to deploy. But I shouldn't add open ports for the sace of application security.
What can be the reason and how to solve these?
screenshot of my console, nothing happens
Below is my deploy.rb file:
SSHKit.config.command_map[:rake] = 'bundle exec rake'
# config valid only for current version of Capistrano
lock '3.8.1'
set :application, 'my_app'
set :repo_url, 'git#gitlab.com:_____'
set :deploy_via, :remote_cache
set :rvm_roles, [:app, :web]
set :rvm_type, :user
set :rvm_ruby_version, 'ruby-2.4.0'
set :log_level, :debug
set :pty, false
set :linked_files, %w{config/application.yml config/database.yml}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/assets public/uploads}
set :keep_releases, 10
set :whenever_roles, [:web, :app,:db]
set :whenever_identifier, "#{fetch(:application)}_#{fetch(:stage)}"
namespace :deploy do
desc 'restart (upgrade) unicorn server'
task :restart do
invoke 'unicorn:restart'
end
after :finishing, 'deploy:cleanup'
after 'deploy:publishing', 'deploy:restart'
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
within release_path do
execute :rake, 'tmp:cache:clear'
end
end
end
end
namespace :delayed_job do
desc 'List of running delayed job workers'
task :list do
on roles(:all) do |host|
execute :ps, 'aux | grep delayed_job'
end
end
desc 'Stop delayed_job workers forcefully'
task :kill do
on roles(:all) do |host|
execute :kill, "-9 $(ps aux | grep delayed_job | awk '{print $2}')"
end
end
end
task :upload_secret_files do
on roles(:all) do |host|
begin
execute "mkdir -p #{shared_path}/config"
rescue
end
upload! 'config/application.yml', "#{shared_path}/config/application.yml"
upload! 'config/database.yml', "#{shared_path}/config/database.yml"
end
end
task :log do
on roles(:all) do |host|
execute "tail -f #{current_path}/log/#{fetch(:rails_env)}.log"
end
end
desc 'Invoke a rake command on the remote server'
task :invoke, [:command] => 'deploy:set_rails_env' do |task, args|
on primary(:app) do
within current_path do
with rails_env: fetch(:rails_env) do
rake args[:command]
end
end
end
end
I was struggling with exactly the same issue and spent several hours. Didn't get what is the issue related to. It just started working after I reboot my local OS

Capistrano 3 does not restart my rails app after deployment

I use the following deploy.rb :
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, '*****'
set :rails_env, 'production'
set :repo_url, 'admin#test.*******.***:/srv/outils/repos/*****'
set :scm, :git
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
#after 'deploy:publishing', 'deploy:restart'
end
after 'deploy:publishing', 'deploy:restart'
It correctly deploy the app but does not restart it. What should i modify to make it restart? There is no message, error or otherwise, about the restart.
In Capistrano 3, the restart task is available and will be called, but it is empty. See the Capistrano source code in capistrano/lib/capistrano/tasks/deploy.rake for the code:
[...]
task :restart
task :failed
end
If you want the restart task to actually do something, you can change the default behavior for restart by adding this code to your deploy.rb:
namespace :deploy do
task :restart do
invoke rake-restart-something-task
end
end
Where the rake-restart-something-task can be something like deploy:service:restart_apache, or some other task that you defined in the capistrano rake file.

Don't know how to build task 'environment' When i deploy my app to vps in Capistrano task

I deployed my app to vps with capistrano.
Everything works fine but only :environment task.
This is my code
namespace :deploy do
desc 'Clear memcache'
task clear_memcache: :environment do
on roles(:app) , in: :sequence, wait: 2 do
::Rails.cache.clear
CACHE.flush
end
end
after :finishing, :clear_memcache
end
But i always got this error.
#<RuntimeError: Don't know how to build task 'environment' (see --tasks)>
How can i fix this?
Thanks!
I think you are mixing two concepts. A rake task and a capistrano task. Rake tasks do use the :environment subtask, whereas the capistrano ones don't. Capistrano tasks cannot (AFAIK) directly call ruby code in the context of the rails application on the server, this is what rake tasks do.
What you actually probably want is to define both a rake task to clear the cache and a capistrano task that will call the rake task on the deployment server.
Try these:
The rake task to clear the cache:
# put this in Rakefile or any other rake file under lib/tasks
desc 'Clear memcache'
task clear_memcache: :environment do
::Rails.cache.clear
CACHE.flush
end
The capistrano task to call the rake on the server:
# config/deploy/production.rb
namespace :deploy do
desc 'Clear memcache'
task :clear_memcache do
on roles(:app) , in: :sequence, wait: 2 do
within release_path do
with rails_env: fetch(:rails_env) do
execute 'rake', 'clear_memcache'
end
end
end
end
after :finishing, :clear_memcache
end

How to define and run capistrano 3 task after deploy?

I have a capistrano script that works perfect, except it's not running a task after the deploy. I'm using rails_daemons to launch the rails application and a I need to restart the daemons.
#deploy.rb
namespace :deploy do
on roles :all do
execute :bundle, "exec rake daemons:restart"
end
end
Tryed this also:
task :restart_daemons, :roles => :app do
execute :bundle, "exec rake daemons:restart"
end
after "deploy", "deploy:restart_daemons"
First off, have you checked if bundle exec rake daemons:restart works locally? If so, try something like this:
namespace :deploy do
after :restart do
on roles(:web), in: :groups, limit: 3, wait: 10 do
within release_path do
execute :rake, 'daemons:restart'
end
end
end
end

Capistrano: linked file database.yml does not exist on my.server.ipadress

after i try to deploy my app via capistrano to my server i get this error message:
DEBUG [605f198a] Finished in 0.084 seconds with exit status 1 (failed).
ERROR linked file /home/deploy/myrailsapp/shared/config/database.yml does not exist on xx.xxx.xx.xxx
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#xx.xxx.xx.xxx: exit
SystemExit: exit
Tasks: TOP => deploy:check:linked_files
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as deploy#xx.xxx.xx.xxx: exit
my deploy.rb is:
set :deploy_to, '/home/deploy/myrailsapp'
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
i tried this tut https://www.gorails.com/deploy/ubuntu/14.04, this is my first try with capistrano.
Just create /home/deploy/myrailsapp/shared/config/database.yml file manually and adjust it.
Capistrano doesn't create (or manage) configuration file out of the box. So, you should do it manually or automate use own Capistrano scripts, Puppet, Chef, Ansible tools.
As i prefer to have my files central on the deployment server, i use this task to deploy the config files from the config dir to the linked files dir on the app server.
This uses rsync, since i use capistrano-rsync to deploy.
namespace :deploy do
task :copy_config do
on release_roles :app do |role|
fetch(:linked_files).each do |linked_file|
user = role.user + "#" if role.user
hostname = role.hostname
linked_files(shared_path).each do |file|
run_locally do
execute :rsync, "config/#{file.to_s.gsub(/.*\/(.*)$/,"\\1")}", "#{user}#{hostname}:#{file.to_s.gsub(/(.*)\/[^\/]*$/, "\\1")}/"
end
end
end
end
end
end
before "deploy:check:linked_files", "deploy:copy_config"
You can upload files using the rake task.
Add the gem to your Gemfile after setting up Capistrano, preferably in the :development group:
group :development do
gem 'capistrano', require: false
gem 'capistrano-rake', require: false
end
Аdd it to your Capfile:
require 'capistrano/rake'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
Create file lib/capistrano/tasks/setup.rake and add to it:
namespace :deploy do
namespace :check do
before :linked_files, :set_database_yml do
on roles(:app), in: :sequence, wait: 10 do
upload! 'config/database.yml', "#{shared_path}/config/database.yml"
end
end
end
end

Resources