Capistrano: Don't know how to build task 'deploy:new_release_path' - ruby-on-rails

I am deploying for the first time to a Digital Ocean Ubuntu droplet. I have configured everything and followed all the steps and am now on the step where I issue the command: cap production deploy:initial. For this command I am getting back this error message:
cap aborted!
Don't know how to build task 'deploy:new_release_path' (see --tasks)
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task_manager.rb:58:in `[]'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/task.rb:361:in `[]'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/dsl/task_enhancements.rb:12:in `after'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/scm/git.rb:21:in `register_hooks'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/configuration/plugin_installer.rb:28:in `install'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/configuration.rb:155:in `install_plugin'
/Users/Christopher/ClientProjects/PawBookings/Capfile:3:in `<top (required)>'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:687:in `raw_load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:96:in `block in load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:95:in `load_rakefile'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:79:in `block in run'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/lib/capistrano/application.rb:14:in `run'
/Users/Christopher/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/capistrano-3.7.1/bin/cap:3:in `<top (required)>'
/Users/Christopher/.rbenv/versions/2.3.1/bin/cap:22:in `load'
/Users/Christopher/.rbenv/versions/2.3.1/bin/cap:22:in `<main>'
Capfile
# Load DSL and Setup Up Stages
require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
config/deploy.rb
# Change these
server '12.34.56.789', port: 80, roles: [:web, :app, :db], primary: true
set :repo_url, 'my_ssh_github_url'
set :application, 'app_name'
set :user, 'deploy_username'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
config/nginx.conf
upstream puma {
server unix:///home/deploy_username/apps/app_name/shared/tmp/sockets/app_name-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy_username/apps/app_name/current/public;
access_log /home/deploy_username/apps/app_name/current/log/nginx.access.log;
error_log /home/deploy_username/apps/app_name/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
Update:
I moved a couple lines of code in the Capfile. The process seems to have started but now gives the following error message:
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke bundler:map_bins (first_time)
** Execute bundler:map_bins
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_linked_dirs (first_time)
** Execute deploy:set_linked_dirs
** Invoke deploy:set_rails_env
** Invoke rvm:hook (first_time)
** Execute rvm:hook
cap aborted!
Net::SSH::Disconnect: connection closed by remote host
New Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/scm/git'
install_plugin Capistrano::SCM::Git
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

The Git plugin relies on the deploy tasks being present. In your Capfile, you must install the Git plugin after requiring capistrano/deploy, like this:
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git

its mainly due to the ruby,rails and ubuntu updated version issues. I got the same problem.
Added in Capfile the below line before the last line and that solved my issue.
install_plugin Capistrano::Puma::Daemon
For reference follow the below tutorial. Would be helpful for someone. Because I spent lot of time to find a solution for this. https://matthewhoelter.com/2020/11/10/deploying-ruby-on-rails-for-ubuntu-2004.html

Related

Caddy not rendering assets

I have a rails application that is not rendering assets. It was previously working fine. When I added linked files it not only uploaded the files I specified but also precompiled assets. Even after clobbering all assets and recompiling it fails to render them even though that exist on the box.
error
Caddyfile
mydomain.com {
gzip
log stdout
root /home/deploy/apps/rails-app/current/public
proxy / unix:///home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock {
except /assets # this is /public/assets directory
except /solr
transparent
websocket
policy round_robin
}
errors stdout
header / {
Strict-Transport-Security "max-age=31536000"
}
proxy /solr localhost:8983 {
transparent
}
}
deploy.rb
# frozen_string_literal: true
# config valid only for current version of Capistrano
# lock '3.13.0'
# Change these
server '...', port: 2221, roles: %i[web app db], primary: true
set :repo_url, '...'
set :application, '...'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w[~/.ssh/id_rsa], auth_methods: %w(publickey) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :bundle_flags, '--no-cache'
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.6.6'
set :rails_env, 'production'
set :assets_dependencies, %w(app/assets lib/assets vendor/assets Gemfile.lock config/routes.rb)
## Defaults:
set :branch, 'master'
set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# set :linked_dirs, %w{data_files}
set :linked_dirs, fetch(:linked_dirs, []).push('public/system')
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
# before :start, :make_dirs
end
namespace :deploy do
desc 'Make sure local git is in sync with remote.'
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts 'WARNING: HEAD is not the same as origin/master'
puts 'Run `git push` to sync changes.'
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke! 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke! 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
I was able to get this working but using the fileserver directive.
https://example.com {
encode zstd gzip
root * /home/deploy/apps/rails-app/current/public
file_server
tls user#email.com
#notStatic {
not file
}
reverse_proxy #notStatic unix//home/deploy/apps/rails-app/shared/tmp/sockets/rails-app-puma.sock
header / {
Strict-Transport-Security "max-age=31536000"
}
}

Capistrano + puma + nginx: Capistrano puma is not creating puma-sockets and puma ids

Puma sockets and pids are not created after deploying with Capistrano. I have rails 5.1.2 application with ruby 2.4.0. Getting following error:
unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock failed (2: No such file or directory) while connecting to upstream, client: client_id
cap staging deploy:initial successfully completes. Have googled it and followed many links and answers but nothing is working. The deploy.rb and other files are following.
config/deploy.rb
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"
# Change these
server 'my_iP-address', roles: [:web, :app, :db], primary: true
set :repo_url, 'git#github.com'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :deploy_via, :remote_cache
set :deploy_to, "/home/deploy/#{fetch(:application)}"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
set :keep_releases, 5
## Linked Files & Directories (Default None):
set :bundle_binstubs, nil
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
if fetch(:stage) == (:staging || 'staging')
if `git rev-parse HEAD` != `git rev-parse origin/staging`
puts "WARNING: HEAD is not the same as origin/staging"
puts "Run `git push` to sync changes."
exit
end
elsif `git rev-parse HEAD` != `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke!('puma:restart')
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
Setting up branch, application, and stage in config/deploy/staging.rb
set :application, 'affiliate-staging'
set :deploy_to, "/home/deploy/#{fetch(:application)}"
set :stage, :staging
set :rails_env, :staging
set :branch, :staging
config/nginx.conf
upstream puma {
#using same puma socket name which I am sertting up in staging.rb
server unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy/affiliate-staging/current/public;
access_log /home/deploy/affiliate-staging/current/log/nginx.access.log;
error_log /home/deploy/affiliate-staging/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 100M;
keepalive_timeout 10;
}
As you can see there should be pids and affiliate-saintlbeau-puma.sock but its not created. I am getting this error:
unix:///home/deploy/affiliate-staging/shared/tmp/sockets/affiliate-staging-puma.sock failed (2: No such file or directory) while connecting to upstream, client: client_id
shared/puma.rb
#!/usr/bin/env puma
directory '/home/deploy/affiliate-staging/current'
rackup "/home/deploy/affiliate-staging/current/config.ru"
environment 'staging'
tag ''
pidfile "/home/deploy/shared/tmp/pids/puma.pid"
state_path "/home/deploy/shared/tmp/pids/puma.state"
stdout_redirect '/home/deploy/current/log/puma.error.log', '/home/deploy/current/log/puma.access.log', true
threads 4,16
bind 'unix:///home/deploy/shared/tmp/sockets/puma.sock'
workers 0
preload_app!
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = ""
end
before_fork do
ActiveRecord::Base.connection_pool.disconnect!
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
I am unable to understand what I am missing here. I hope I haven't messed up the question.

Capistrano cap production deploy:initial Net::SSH::ConnectionTimeout

I am following this tutorial for setting up a webserver for a simple rails app: https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma with nginx and capistrano.
my deploy.rb file:
# Change these
server '45.33.11.11', port: 22, roles: [:web, :app, :db], primary: true
set :repo_url, 'git#bitbucket.org:slucha/supplement.git'
set :application, 'supplementtests'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
and Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require "capistrano/scm/git"
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
install_plugin Capistrano::Puma
install_plugin Capistrano::SCM::Git
Nginx is installed, ssh keys are set up for both bitbucket ssh and deploy keys for the repo and rvm ist also installed on the server
The cap staging deploy:check command passes without errors but when I try to run cap production deploy:initial I get the following error
** Invoke production (first_time)
** Execute production
** Invoke load:defaults (first_time)
** Execute load:defaults
** Invoke bundler:map_bins (first_time)
** Execute bundler:map_bins
** Invoke deploy:set_rails_env (first_time)
** Execute deploy:set_rails_env
** Invoke deploy:set_linked_dirs (first_time)
** Execute deploy:set_linked_dirs
** Invoke deploy:set_rails_env
** Invoke rvm:hook (first_time)
** Execute rvm:hook
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as root#45.79.147.60: Net::SSH::ConnectionTimeout
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
Net::SSH::ConnectionTimeout: Net::SSH::ConnectionTimeout
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh/transport/session.rb:90:in `rescue in initialize'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh/transport/session.rb:57:in `initialize'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh.rb:237:in `new'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh.rb:237:in `start'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/connection_pool.rb:59:in `call'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/connection_pool.rb:59:in `with'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/netssh.rb:176:in `with_ssh'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/netssh.rb:129:in `execute_command'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `block in create_command_and_execute'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `tap'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `create_command_and_execute'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:55:in `test'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/capistrano-rvm-0.1.2/lib/capistrano/tasks/rvm.rake:21:in `block (3 levels) in <top (required)>'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:29:in `instance_exec'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:29:in `run'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Errno::ETIMEDOUT: Connection timed out - connect(2) for 45.79.147.60:22
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:65:in `connect'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:65:in `connect_internal'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:140:in `connect'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:338:in `block in tcp'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:232:in `each'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:232:in `foreach'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/socket.rb:328:in `tcp'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh/transport/session.rb:70:in `initialize'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh.rb:237:in `new'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/net-ssh-4.2.0/lib/net/ssh.rb:237:in `start'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/connection_pool.rb:59:in `call'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/connection_pool.rb:59:in `with'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/netssh.rb:176:in `with_ssh'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/netssh.rb:129:in `execute_command'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `block in create_command_and_execute'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `tap'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:141:in `create_command_and_execute'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:55:in `test'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/capistrano-rvm-0.1.2/lib/capistrano/tasks/rvm.rake:21:in `block (3 levels) in <top (required)>'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:29:in `instance_exec'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/backends/abstract.rb:29:in `run'
/home/jan/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/sshkit-1.15.1/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
Tasks: TOP => rvm:hook
Any ideas why there is a timeout?
UPDATE
ssh -T git#bitbucket.org gives message:
logged in as slucha.
You can use git or hg to connect to Bitbucket. Shell access is disabled.
ssh -T deploy#45.33.95.53 gives
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.9.64-x86_64-linode88 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
The problem was that I had two conflicting settings between the deploy.rb and production.rb
make sure that your settings in both files are correct.
in my production.rb I had the following line:
server '53.33.95.53', user: 'deploy', roles: %w{web app}
Which was overwriting the current ip address and was causing the timeout.

Puma not creating socket file while deployed with Capistrano

I have followed the tutorial here on ow to deploy a Rails App using Capistrano and Puma. I had initially used with a Rails 4 and all worked fine, however I'm now trying with Rails 5 App and is not working. I've made some changes due to changes in Capistrano (I've also upgraded Capistrano) and now I'm stuck as the deployment does not seem to create the sock file while deploying and I get the following error:
*412 connect() to unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock failed (2: No such file or directory) while connecting to upstream, client: 172.245.92.157, server: api.myapp.com, request: "GET / HTTP/1.0", upstream: "http://unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock:/", host: "roboarticle.com"
Here's my Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
require 'capistrano/deploy'
# Include default deployment tasks
# require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
install_plugin Capistrano::Puma
require 'capistrano/secrets_yml'
require 'capistrano/sidekiq'
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Here's my deploy.rb:
# config valid only for current version of Capistrano
lock '3.8.0'
set :application, 'myapp'
# set :repo_url, 'git#example.com:me/my_repo.git'
# Change these
# server '178.62.117.38', roles: [:web, :app, :db], primary: true
set :repo_url, 'git#gitlab.com:WagnerMatos/myapp.git'
set :application, 'myapp'
set :user, 'deployer'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, false
set :use_sudo, false
set :deploy_to, "/var/www/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, false # Change to false when not using ActiveRecord
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/staging`
puts "WARNING: HEAD is not the same as origin/staging"
puts "Run `git push` to sync changes."
exit
end
end
end
desc "reload the database with seed data"
task :seed do
puts "\n=== Seeding Database ===\n"
on primary :db do
within current_path do
with rails_env: fetch(:stage) do
execute :rake, 'db:seed'
end
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
# execute :bundle, 'exec cap production setup'
end
end
after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:reverted', 'sidekiq:restart'
after 'deploy:published', 'sidekiq:restart'
# before :starting, :check_revision
# after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
# after("deploy:compile_assets", "deploy:build_missing_paperclip_styles")
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
And here's my nguni config file:
upstream puma {
server unix:///var/www/apps/myapp/shared/tmp/sockets/myapp-puma.sock;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
listen 443 default_server deferred;
server_name api.myapp.io;
ssl on;
ssl_certificate /etc/nginx/ssl/api.myapp.io/ssl-bundle.crt;
ssl_certificate_key /etc/nginx/ssl/api.myapp.io/api.myapp.io.key;
ssl_prefer_server_ciphers on;
root /var/www/apps/myapp/current/public;
access_log /var/www/apps/myapp/current/log/nginx.access.log;
error_log /var/www/apps/myapp/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri #puma;
location #puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
proxy_read_timeout 300;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4000M;
keepalive_timeout 10;
}
Any ideas of what I'm missing here?

How to run rails commands in production with capistrano 3 and rvm installed

I'm struggling to get a decent understanding of capistrano. I want to run rails commands in production but it seems that the corresponding binstub is nowhere to be found. As a matter of fact, I have the current/ and shared/ directories under my app name, but none of both has a bin/ directory with a rails binstub.
I'm also a complete newbie at building capistrano tasks. I found out this gem for example to run rails c with capistrano, but it requires the rails binstub in the current/bin directory, which of course I don't have.
EDIT: I tried the capistrano-rails-console gem but even if I add the ssh_options like this, I end up with:
00:00 rails:console
01 ~/.rvm/bin/rvm default do bundle exec rails console production
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/ubuntu/.rvm/rubies/ruby-2.3.1/bin/ruby
...
as if any binstub is not recognized.
I also followed the answers of this question, but none of the approaches seems to work for me. I run the app on Linux so iterm is not an option for me, and the GitHub snippets linked in the other answers either end up with:
cap aborted!
NameError: undefined local variable or method `current_task' for #<SSHKit::Backend::Netssh:0x00000001f15800>
Did you mean? current_path
or:
00:00 rails:console
Connecting with <my_username>#<my_host>
bash: bundle: command not found
Connection to <my_host> closed.
So I believe it's an rvm problem, but I totally don't know how to cope with it.
I noticed that, when I run cap production deploy, following commands are run among others:
~/.rvm/bin/rvm default do bundle exec rake assets:precompile
~/.rvm/bin/rvm default do bundle exec rake db:migrate
but if I run them on my production server, I get the following response:
Could not locate Gemfile or .bundle/ directory
For your reference, here's my config/deploy.rb:
set :scm, :git
set :repo_url, '<git_repo>'
set :application, '<app_name>'
set :user, '<production_user>'
set :puma_threads, [4, 16]
set :puma_workers, 0
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
set :linked_files, %w{config/application.yml config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
# Bonus! Colors are pretty!
def red(str)
"\e[31m#{str}\e[0m"
end
# Figure out the name of the current local branch
def current_git_branch
branch = `git symbolic-ref HEAD 2> /dev/null`.strip.gsub(/^refs\/heads\//, '')
puts "Deploying branch #{red branch}"
branch
end
# Set the deploy branch to the current branch
set :branch, current_git_branch
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :assets do
desc "compile assets locally and upload before finalize_update"
task :deploy do
%x[bundle exec rake assets:clean && bundle exec rake assets:precompile]
ENV['COMMAND'] = " mkdir '#{release_path}/public/assets'"
invoke
upload '/#{app_dir}/public/assets', "#{release_path}/public/assets", {:recursive => true}
end
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
#unless `git rev-parse HEAD` == `git rev-parse origin/master`
# puts "WARNING: HEAD is not the same as origin/master"
# puts "Run `git push` to sync changes."
# exit
#end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
task :fix_absent_manifest_bug do
on roles(:web) do
within release_path do execute :touch,
release_path.join('public', fetch(:assets_prefix), 'manifest-fix.temp')
end
end
end
# desc 'Restart application'
# task :restart do
# on roles(:app), in: :sequence, wait: 5 do
# invoke 'puma:restart'
# end
# end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
after :updating, 'deploy:fix_absent_manifest_bug'
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
and my Capfile:
# Load DSL and set up stages
require 'capistrano/setup'
# Include default deployment tasks
require 'capistrano/deploy'
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
# require 'capistrano/bundler'
# require 'capistrano/rails/assets'
# require 'capistrano/rails/migrations'
# require 'capistrano/passenger'
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/rails/collection'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
Am I missing something? Thanks in advance
Solved with the help of this Upwork freelancer.
The solution steps were:
remove the binstubs locally
set :bundle_binstubs, nil in config/deploy.rb
remove the bin directory from the :linked_dirs list (adding also /bin in .gitignore)
push the changes and run cap production deploy
recreate the binstubs with rake rails:update:bin
comment out the set :bundle_binstubs, nil line
add the bin directory in :linked_dirs again
modify the config/deploy.rb file like this:
namespace :deploy do
task :regenerate_bins do
on roles(:web) do
within release_path do
execute :bundle, 'exec rake rails:update:bin'
end
end
end
...
...
after :finishing, :regenerate_bins
...
uncomment set :bundle_binstubs, nil and remove bin from :linked_dirs once more
push changes and deploy
After this, the binstubs are found in the current/bin directory instead of the shared/bin one (in Rails 4 and 5)

Resources