there is a lot of issues found in the web with this problem, but I don’t found one to resolve my issue and it is difficult understand all.
This is my single problematic.
I began rewrite an ruby on rails app using rbenv. I want try to deploy this app with Capistrano Puma and Nginx (does I need also Passenger or can I only use Puma ?)
I have write my project on a local machine. and now I am trying deploy it in a server. but from the beginning I have this issue with Capistrano config.
Here what’s wrong ?
$ cap --tasks --trace
** Invoke load:defaults (first_time)
** Execute load:defaults
cap aborted!
Don't know how to build task 'start' (See the list of available tasks with `cap --tasks`)
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task_manager.rb:59:in `[]'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task.rb:405:in `[]'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capistrano-3.14.1/lib/capistrano/dsl/task_enhancements.rb:7:in `before'
/home/deploy/ecran-village-plannings/config/deploy.rb:85:in `block in <top (required)>'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/task_manager.rb:232:in `in_namespace'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/dsl_definition.rb:141:in `namespace'
/home/deploy/ecran-village-plannings/config/deploy.rb:76:in `<top (required)>'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/rake_module.rb:29:in `load'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/rake_module.rb:29:in `load_rakefile'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/default_loader.rb:11:in `load'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:781:in `load_imports'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capistrano-3.14.1/lib/capistrano/application.rb:105:in `load_imports'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:711:in `raw_load_rakefile'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:104:in `block in load_rakefile'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:103:in `load_rakefile'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:82:in `block in run'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:186:in `standard_exception_handling'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/application.rb:80:in `run'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capistrano-3.14.1/lib/capistrano/application.rb:14:in `run'
/home/deploy/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/capistrano-3.14.1/bin/cap:3:in `<top (required)>'
/home/deploy/.rbenv/versions/2.7.2/bin/cap:23:in `load'
/home/deploy/.rbenv/versions/2.7.2/bin/cap:23:in `<main>'
Here are my files:
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.2'
gem 'rails-i18n'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem 'sorcery'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
# gem "dotenv-rails"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development, :production do
#Add Capistrano
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano-rbenv'
gem 'capistrano3-puma', github: "seuros/capistrano-puma"
gem 'capistrano-nginx'
gem 'capistrano-ext'
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
Capfile
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require "capistrano/rails/migrations"
require 'capistrano/rbenv'
require 'capistrano/nginx'
require 'capistrano/puma'
install_plugin Capistrano::Puma
install_plugin Capistrano::Puma::Nginx
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
# require "capistrano/scm/git"
# install_plugin Capistrano::SCM::Git
# Include tasks from other gems included in your Gemfile
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
#set :rbenv_type, :user
#set :rbenv_ruby, '2.7.2'
config/deploy.rb
# config valid for current version and patch releases of Capistrano
lock "~> 3.14.1"
server 'seances.ecranvillage.ovh', roles: [:web, :app, :db], primary: true
set :repo_url, "[email protected]:rodinux/ecran-village-plannings.git"
set :application, "ecran_village_plannings"
ask :branch, 'master'
set :bundle_without, %w{development test}.join(' ')
set user: 'deploy', roles: %w{app db web}
# Default value for :format is :airbrussh.
# set :format, :airbrussh
# You can configure the Airbrussh format using :format_options.
# These are the defaults.
# set :format_options, command_output: true, log_file: "log/capistrano.log", color: :auto, truncate: :auto
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
append :linked_files, "config/master.key", "config/database.yml"
# Default value for linked_dirs is []
append :linked_dirs, "bin", "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle" "public/system", "storage"
# Default value for default_env is {}
#set :default_env, { path: "/opt/ruby/bin:$PATH" }
#set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')
# Default value for linked_dirs is []
#set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads' )
# Rbenv specific settings
set :rbenv_path, '/home/deploy/.rbenv/'
set :rbenv_type, :system # or :system, depends on your rbenv setup
set :rbenv_ruby, 'ruby 2.7.2p137'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }
set :rails_env, 'production'
# Default value for keep_releases is 5
set :keep_releases, 5
# if you want to remove the dump file after loading
set :db_local_clean, true
# if you want to remove the dump file from the server after downloading
set :db_remote_clean, true
# if you are highly paranoid and want to prevent any push operation to the server
set :disallow_pushing, false
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
set :puma_threads, [4, 16]
set :puma_workers, 2
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 :nginx_config_name, 'ecran_village_plannings'
set :nginx_server_name, 'seances.ecranvillage.ovh'
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :puma_workers, 2
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
# when using db, you should add config/database.yml here
# set :linked_files, fetch(:linked_files, []).concat(%w{.rbenv-vars})
# set :linked_dirs, fetch(:linked_dirs, []).concat(%w{log tmp/pids tmp/cache tmp/sockets vendor/bundle})
/config/deploy/production.rb
# server-based syntax
# ======================
# Defines a single server with a list of roles and multiple properties.
# You can define all roles on a single server, or split them:
set :stage, :production # this defines production stage for deployment
set :branch, 'master'
server "seances.ecranvillage.ovh", user: "deploy", roles: %w{app db web}, my_property: :my_value
# server "example.com", user: "deploy", roles: %w{app web}, other_property: :other_value
# server "db.example.com", user: "deploy", roles: %w{db}
# role-based syntax
# ==================
# Defines a role with one or multiple servers. The primary server in each
# group is considered to be the first unless any hosts have the primary
# property set. Specify the username and a domain or IP for the server.
# Don't use `:all`, it's a meta role.
# role :app, %w{[email protected]}, my_property: :my_value
# role :web, %w{[email protected] [email protected]}, other_property: :other_value
# role :db, %w{[email protected]}
# Configuration
# =============
# You can set any configuration variable like in config/deploy.rb
# These variables are then only loaded and set in this stage.
# For available Capistrano configuration variables see the documentation page.
# http://capistranorb.com/documentation/getting-started/configuration/
# Feel free to add new variables to customise your setup.
# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
# limited set of options, consult the Net::SSH documentation.
# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start
#
# Global options
# --------------
set :ssh_options, {
keys: %w(/home/deploy/.ssh/id_rsa),
# forward_agent: false,
# auth_methods: %w(password)
}
#
# The server-based syntax can be used to override options:
# ------------------------------------
# server "example.com",
# user: "user_name",
# roles: %w{web app},
# ssh_options: {
# user: "user_name", # overrides user setting above
# keys: %w(/home/user_name/.ssh/id_rsa),
# forward_agent: false,
# auth_methods: %w(publickey password)
# # password: "please use keys"
# }
I have add files like /config/secrets.yml, etc…
I perhaps don’t use the good practise to use Capistrano… why can’t I have to do ?
3
Answers
You need to add this to your Capfile as per the docs
install_plugin Capistrano::Puma::Daemon # If you using puma daemonized (not supported in Puma 5+)
Cheers
bundle exec cap --tasks --trace
worksbut
cap <environment> deploy
failsFixed: add
bundle exec
tocap <environment> deploy
commandWhat changed?: I was using
oh-my-zsh
withbundler
plugin that auto-loadbundle exec
in my shell, but lately I removed it to speed up terminal startupits mainly due to the ruby,rails and ubuntu updated version issues. I got the same problem.
Added in Capfile this line before the last line and that solved my issue(only if your puma version is < 5 ).
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