I have a DigitalOcean Droplet with a Rails application in which I wanted to automate deploys via Pipelines and Capistrano.
This is my Capfile:
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
# 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
#
# 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/apache/systemd"
require "capistrano/rails"
# require "capistrano/rails/migrations"
# require "capistrano/passenger"
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
deploy.rb:
# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"
server "xx.xx.xx.xx", port: 22, roles: [:web, :app, :db], primary: true
set :application, "myapp"
set :repo_url, "git#bitbucket.org:user/repo.git"
set :user, "deployuser"
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "customdir"
# 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/database.yml"
# Default value for linked_dirs is []
# append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for local_user is ENV['USER']
# set :local_user, -> { `git config user.name`.chomp }
# Default value for keep_releases is 5
# set :keep_releases, 5
# Uncomment the following to require manually verifying the host key before first deploy.
# set :ssh_options, verify_host_key: :secure
#
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 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'apache:reload'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :restart
after :rollback, :restart
end
bitbucket-pipelines.yml:
image: ruby:2.5.3
pipelines:
default:
- step:
script: # Modify the commands below to build your repository.
- apt-get update -y
- apt-get install -y build-essential git-core curl nodejs default-libmysqlclient-dev ssh
- gem install bundler
- bundle install
- bundle exec cap production deploy
I created a SSH under Settings -> Pipelines -> SSH keys and added it to the authorized_keys file in the droplet under the deployuser specified on the deploy.rb. When I run the pipeline this is the log trace:
+ bundle exec cap production deploy
00:00 git:wrapper
01 mkdir -p /tmp
#<Thread:0x000055ed4cf70528#/usr/local/bundle/gems/sshkit-1.18.2/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
/usr/local/bundle/gems/sshkit-1.18.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing on host xx.xx.xx.xx: Inappropriate ioctl for device (SSHKit::Runner::ExecuteError)
from /usr/local/bundle/gems/sshkit-1.18.2/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host xx.xx.xx.xx: Inappropriate ioctl for device
Caused by:
Errno::ENOTTY: Inappropriate ioctl for device
Tasks: TOP => deploy:check => git:check => git:wrapper
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing on host xx.xx.xx.xx: Inappropriate ioctl for device
** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:
# Logfile created on 2019-05-30 00:35:51 +0000 by logger.rb/61378
INFO ---------------------------------------------------------------------------
INFO START 2019-05-30 00:35:51 +0000 cap production deploy
INFO ---------------------------------------------------------------------------
INFO [5548bb68] Running /usr/bin/env mkdir -p /tmp on 142.93.227.140
DEBUG [5548bb68] Command: /usr/bin/env mkdir -p /tmp
#xx.xx.xx.xx's password:
I'm not sure if the last line has something to do with what's happening. I intend to make the deploy passwordless and as straightforward as possible.
Update:
I tried adding ssh -T deployUser#xx.xx.xx.xx to the script steps. Apparently there's nothing wrong:
ssh -T deployUser#xx.xx.xx.xx
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-50-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Fri May 31 02:14:16 UTC 2019
System load: 0.01 Processes: 140
Usage of /: 19.7% of 24.06GB Users logged in: 1
Memory usage: 74% IP address for eth0: xx.xx.xx.xx
Swap usage: 0%
Get cloud support with Ubuntu Advantage Cloud Guest:
http://www.ubuntu.com/business/services/cloud
* Canonical Livepatch is available for installation.
- Reduce system reboots and improve kernel security. Activate at:
https://ubuntu.com/livepatch
1 package can be updated.
0 updates are security updates.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Went back to the basics. I turned off Pipelines on Bitbucket and tried to run cap production deploy locally while pointing to the remote server. Turns out it was indeed asking for a password when it shouldn't have, and also it was not SSHing with the correct user, so a) Added only the necessary keys to the deployUser's authorized_keys file b) Modified deploy.rb like so:
lock "~> 3.11.0"
set :user, "deployUser" # Notice that set user was moved here so it can be fetched later
server "xx.xx.xx.xx", user: fetch(:user), port: 22, roles: [:app], primary: true
set :application, "my_app"
set :repo_url, "my_git_repo"
set :rvm_type, :system
set :rvm_ruby_version, '2.5.3'
set :bundle_jobs, 2 # Small modification so it doesn't eat up the Droplet's memory
I'm getting another error but that's a matter for more Googling :)
Related
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)
I'm having issues with running ruby gem commands while using Capistrano 3 with capistrano-rbenv (I'm assuming it can't find the ruby interpreter), specifically delayed_job. I've posted the necessary files associated with the issue:
`require': no such file to load -- rubygems (LoadError)
Any assistance would be greatly appreciated.
Gemfile
gem 'capistrano', "~> 3.6"
gem 'capistrano-rbenv', '~> 2.0'
Capfile
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
require "capistrano/rbenv"
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
deploy.rb
# config valid only for current version of Capistrano
lock '3.6.1'
set :application, 'youtube_rank_tracker'
set :repo_url, './.git'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/dogecoin'
# Default value for :scm is :git
set :scm, :git
# 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/database.yml', 'config/secrets.yml'
# Default value for linked_dirs is []
# append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'
# Default value for default_env is {}
# set :default_env, { path: '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' }
# Default value for keep_releases is 5
set :keep_releases, 5
# set :default_env, { path: "$HOME/.rbenv/bin:$PATH" }
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.2'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:all) do |host|
execute :mkdir, "-p #{release_path}/tmp"
execute :touch, release_path.join('tmp/restart.txt')
execute "cd #{release_path} && RAILS_ENV=production bin/delayed_job restart;"
# execute "cd #{release_path} && bundle exec rake -T"
end
end
end
after 'deploy:publishing', 'deploy:restart'
Error
he deploy has failed with an error: Exception while executing as root#.....: cd /.... && RAILS_ENV=production bin/delayed_job restart; exit status: 1
cd /.... && RAILS_ENV=production bin/delayed_job restart; stdout: /usr/lib/ruby/vendor_ruby/bundler/shared_helpers.rb:2:in `require': no such file to load -- rubygems (LoadError)
from /usr/lib/ruby/vendor_ruby/bundler/shared_helpers.rb:2
from /usr/lib/ruby/vendor_ruby/bundler/setup.rb:1:in `require'
from /usr/lib/ruby/vendor_ruby/bundler/setup.rb:1
Short answer
Instead of writing it yourself, use the semi-official Capistrano 3 Delayed Job integration Gem: https://github.com/AgileConsultingLLC/capistrano3-delayed-job
Long answer
If you were to actually write this yourself, capistrano-rbenv only affects a small default list of commands, as seen here: https://github.com/capistrano/rbenv/blob/master/lib/capistrano/tasks/rbenv.rake#L47
In this case, you'd probably want something like the following:
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:all) do |host|
within release_path do
execute :ruby, "bin/delayed_job", 'restart'
end
end
end
end
Instead of creating the tmp/ directory, add it to :linked_dirs in your config.
Also, it looks like you are touching tmp/restart.txt to restart Passenger. Alternatively, use the capistrano-passenger gem to do that. It can run in a newer mode which uses the passenger commands to restart your app, or you can tell it to use the touch file mechanism.
Finally, instead of using cd ${release_path}, use the within DSL method. That will accomplish the same thing and not break the command map.
I've been trying to solve this problem for some time.
I'm working with Ruby on Rails, using Capistrano to deploy from a BitBucket repository, and when I run cap production deploy it ends up with the following error:
...
...
DEBUG [71effca7] Running /usr/local/rvm/bin/rvm default do ruby --version as user#MyHostIP
DEBUG [71effca7] Command: /usr/local/rvm/bin/rvm default do ruby --version
DEBUG [71effca7] ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
DEBUG [71effca7] Finished in 0.750 seconds with exit status 0 (successful).
ruby 2.2.4p230 (2015-12-16 revision 53155) [x86_64-linux]
INFO [b9bac9f3] Running /usr/bin/env mkdir -p /tmp/MyAppName/ as user#MyHostIP
DEBUG [b9bac9f3] Command: /usr/bin/env mkdir -p /tmp/MyAppName/
INFO [b9bac9f3] Finished in 0.255 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/MyAppName/git-ssh.sh 0.0%
INFO Uploading /tmp/MyAppName/git-ssh.sh 100.0%
INFO [2cd4cd4c] Running /usr/bin/env chmod +x /tmp/MyAppName/git-ssh.sh as user#MyHostIP
DEBUG [2cd4cd4c] Command: /usr/bin/env chmod +x /tmp/MyAppName/git-ssh.sh
INFO [2cd4cd4c] Finished in 0.247 seconds with exit status 0 (successful).
INFO [1643a10f] Running /usr/bin/env git ls-remote --heads as user#MyHostIP
DEBUG [1643a10f] Command: ( export GIT_ASKPASS="/bin/echo" GIT_SSH="/tmp/MyAppName/git-ssh.sh" ; /usr/bin/env git ls-remote --heads )
DEBUG [1643a10f] usage: git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as user#MyHostIP: git exit status: 129
git stdout: usage: git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...
git stderr: Nothing written
SSHKit::Command::Failed: git exit status: 129
git stdout: usage: git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...
git stderr: Nothing written
Tasks: TOP => git:check
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as user#MyHostIP: git exit status: 129
git stdout: usage: git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...
git stderr: Nothing written
/var/www/html/current/config/deploy.rb file:
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'MyApp'
set :repo_url, 'git#bitbucket.org:user/repository.git'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/var/www/html'
# Default value for :scm is :git
set :scm, :git
set :branch, "master"
set :rails_env, "production"
# Default value for :format is :pretty
set :format, :pretty
set :passenger_restart_with_touch, true
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
set :pty, true
# Default value for :linked_files is []
# 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')
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for keep_releases is 5
set :keep_releases, 5
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
task :seed do
on roles(:all) do
execute "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{fetch(:rails_env)}"
end
end
task :reset do
on roles(:all) do
execute "cd #{current_path}; bundle exec rake db:reset RAILS_ENV=#{fetch(:rails_env)}"
end
end
end
/var/www/html/repo/config file:
[core]
repositoryformatversion = 0
filemode = true
bare = true
[remote "origin"]
fetch = +refs/*:refs/*
mirror = true
url = git#bitbucket.org:user/repository.git
/var/www/html/current/config/deploy/production.rb file:
# 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:
# server 'example.com', 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}
set :stage, :production
server 'MyHostIP', user: 'user', roles: %w{web app 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{deploy#example.com}, my_property: :my_value
# role :web, %w{user1#primary.com user2#additional.com}, other_property: :other_value
# role :db, %w{deploy#example.com}
role :app, %w{user#MyHostIP}
role :web, %w{user#MyHostIP}
role :db, %w{user#MyHostIP}
# 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, {
forward_agent: false,
auth_methods: %w(password),
password: 'password',
user: 'user',
port: 2178
}
I hope you can help me. I don't know what to do!!
Thank you!
I solved it. The problem was that I didn't configure Capistrano locally.
There is this app which has been upgraded from Rails 3 to 4. (In addition to upgraded from Capistrano 2 to 3). This is an app that was built, has worked fine for a couple years and now we are adding some features and updating things - but deployment is failing.
I've tried reinstalling gems, passenger apache module, various capistrano deployment configuration options (with sudo, without sudo, setting specific gem paths with default_env ) and working through various Capistrano upgrade documentation, guides, blog posts and other SO posts...
Deployment errors
When attempting to push the changes to the staging server the deployment keeps failing.
Here's the base error when running cap staging deploy:
/dependency.rb:315:in `to_specs': Could not find 'passenger' (>= 0) among 14 total gem(s) (Gem::LoadError)
There must be some simple conflict / confusion with paths / sudo / rvmsudo and has taken more time than I'd like to admit debugging...
Initially I tried doing everything without sudo but wasn't getting success. According to the rvm documentation (https://rvm.io/integration/sudo) I tried editng the sudoers file.
So following rvm.io documentation I comment out Default secure_path=... and add Default env_keep and the following error occurs:
Exception while executing as user#example.org: sudo exit status: 127
sudo stdout: /usr/bin/env: ruby: No such file or directory
gem env
Output of $ gem env on the staging server:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.6
- RUBY VERSION: 2.0.0 (2015-02-25 patchlevel 643) [x86_64-linux]
- INSTALLATION DIRECTORY: /home/user/.rvm/gems/ruby-2.0.0-p643
- RUBY EXECUTABLE: /home/user/.rvm/rubies/ruby-2.0.0-p643/bin/ruby
- EXECUTABLE DIRECTORY: /home/user/.rvm/gems/ruby-2.0.0-p643/bin
- SPEC CACHE DIRECTORY: /home/user/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /home/user/.rvm/rubies/ruby-2.0.0-p643/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /home/user/.rvm/gems/ruby-2.0.0-p643
- /home/user/.rvm/gems/ruby-2.0.0-p643#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /home/user/.rvm/gems/ruby-2.0.0-p643/bin
- /home/user/.rvm/gems/ruby-2.0.0-p643#global/bin
- /home/user/.rvm/rubies/ruby-2.0.0-p643/bin
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
- /usr/games
- /usr/local/games
- /home/user/.rvm/bin
- /home/user/.rvm/bin
config/deploy/staging.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:
# server 'example.com', 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}
set :stage, :staging
server "example.org", user: "user", roles: %w{web, app, 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{example.org}
role :web, %w{example.org}
role :db, %w{example.org}
# role :app, %w{deploy#example.com}, my_property: :my_value
# role :web, %w{user1#primary.com user2#additional.com}, other_property: :other_value
# role :db, %w{deploy#example.com}
# 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/rlisowski/.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'
# }
set :rails_env, "staging"
set :application, "res.example.org"
set :repo_url, "user#example.org:/home/user/webdocs/appname.git"
# set :repo_url, "ssh://user#example.org:3699/home/user/webdocs/appname.git"
set :ssh_options, {
port: 3699,
verbose: :debug
}
# set :rvm_map_bins, %w{gem rake ruby bundle rvmsudo}
set :rvm_map_bins, fetch(:rvm_map_bins, []).push('rvmsudo')
# set :rbenv_ruby, "2.0.0"
set :port, 3699
set :scm, :git
set :pty, true
# set :use_sudo, true
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
# set :user, 'user'
set :use_sudo, false #true
set :deploy_to, "/var/www/res"
set :branch, "staging"
set :deploy_via, :remote_cache
set :passenger_rvm_ruby_version, "2.0.0"
set :default_env, {
rvm_bin_path: '~/.rvm/bin',
path: "/home/user/.rvm/gems/ruby-2.0.0-p643/bin:/home/user/.rvm/rubies/ruby-2.0.0-p643/bin/ruby:/home/user/.rvm/gems/ruby-2.0.0-p643/bin:$PATH"
}
set :passenger_restart_with_sudo, true
set :log_level, :debug
# set :passenger_restart_with_sudo, true
set :passenger_environment_variables, { :path => '/home/user/.rvm/gems/ruby-2.0.0-p643/bin:/home/user/.rvm/rubies/ruby-2.0.0-p643/bin/ruby:/home/user/.rvm/gems/ruby-2.0.0-p643/bin:$PATH' }
=begin
set :default_environment, {
'PATH' => 'home/user/.rvm/gems/ruby-1.9.3-p385/bin:/home/user/.rvm/gems/ruby-1.9.3-p385#global/bin:/home/user/.rvm/rubies/ruby-1.9.3-p385/bin:/home/user/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/user/.rvm/bin:$PATH'
#'PATH' => '/home/user/.rvm/gems/default/bin:/home/user/.rvm/gems/default#global/bin:/home/user/.rvm/rubies/default/bin:/home/user/.rvm/bin:$PATH'
#'PATH' => '/home/user/.rvm/gems/ruby-1.9.3-p385/bin:/home/user/.rvm/gems/ruby-1.9.3-p385#global/bin:/home/user/.rvm/rubies/ruby-1.9.3-p385/bin:/home/user/.rvm/bin:$PATH'
}
=end
#default_environment["GEM_PATH"] ="/home/user/.rvm/gems/ruby-1.9.2-p320"
# /home/bb/.rvm/gems/ruby-1.9.2-p318/bin:/home/bb/.rvm/gems/ruby-1.9.2-p318#global/bin:/home/bb/.rvm/rubies/ruby-1.9.2-p318/bin:/home/bb/.rvm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/bb/.cabal/bin:/home/bb/.xmonad/bin
#default_environment["PATH"] = "/home/bb/.rvm/gems/ruby-1.9.2-p318/bin:/home/bb/.rvm/gems/ruby-1.9.2-p318#global/bin:/home/bb/.rvm/rubies/ruby-1.9.2-p318/bin:/home/bb/.rvm/bin:$PATH"
# role :web, "example.org" # Your HTTP server, Apache/etc
#role :app, "example.org" # This may be the same as your `Web` server
#role :db, "example.org", :primary => true # This is where Rails migrations will run
#role :db, "example.org"
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
#after "deploy", "deploy:bundle_gems"
#after "deploy:bundle_gems", "deploy:restart"
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
desc "Install gems"
task :bundle_gems do
on roles(:app), in: :sequence, wait: 5 do
run "cd #{deploy_to}/current && bundle install"
end
end
desc "Setup sym links for uploads..."
task :make_links do
on roles(:app), in: :sequence, wait: 5 do
run "ln -s #{shared_path}/uploads/ #{deploy_to}/current/public/uploads"
end
end
desc "Precompile assets...."
task :precompile_assets do
on roles(:app), in: :sequence, wait: 5 do
run "cd #{deploy_to}/current && bundle exec rake --trace assets:precompile RAILS_ENV=staging"
end
end
# task :start do ; end
# task :stop do ; end
=begin
desc "Restart App"
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
# run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
=end
end
after "deploy", "deploy:make_links"
after "deploy", "deploy:bundle_gems"
# after "deploy", "deploy:precompile_assets"
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'
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'appname'
set :repo_url, 'user#example.org:/home/user/webdocs/appname.git'
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_app_name'
# Default value for :scm is :git
set :scm, :git
# Default value for :format is :pretty
# set :format, :pretty
# Default value for :log_level is :debug
set :log_level, :debug
set :stages, %w(staging production)
set :default_stage, "staging"
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
# 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')
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# Default value for keep_releases is 5
# set :keep_releases, 5
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
Any suggestions / direction appreciated!
After more testing and further reviewing the Passenger & Capistrano documentation (https://www.phusionpassenger.com/documentation/Users%20guide%20Apache.html & http://capistranorb.com/documentation/getting-started/authentication-and-authorisation/ ) there were a few points of confusion resolved.
The gem installation of passenger was uninstalled. Then following the Passenger documentation, and starting over from scratch, Passenger was installed via the Ubuntu repositories / apt-get.
In addition, passwordless sudo access needed to be setup for the deploy user - and update the Capistrano deployment file accordingly.
Specifically making sure
set :passenger_restart_with_sudo, true was set
So the Could not find 'passenger' (>= 0) error is now gone.
Capistrano deployment is getting further. Now it's just hanging up on my rake tasks which is a different issue to work out.
I am trying to use capistrano (for the first time) to deploy my rails app. First the essentials, I am using:
ruby 1.9.3p362
Rails 3.2.13
rvm 1.24.7
Capistrano 3.0.1
Phusion Passenger 4.0.26
Ubuntu 12.04 LTS
I get the following error when attempting to run cap production deploy
DEBUG [679a47be] fatal: No remote configured to list refs from.
My full cap production deploy output is included below
INFO [488ba755] Running /usr/bin/env mkdir -p /tmp/AppName/ on sub.example.com
DEBUG [488ba755] Command: /usr/bin/env mkdir -p /tmp/AppName/
INFO [488ba755] Finished in 1.730 seconds with exit status 0 (successful).
DEBUG Uploading /tmp/AppName/git-ssh.sh 0.0%
INFO Uploading /tmp/AppName/git-ssh.sh 100.0%
INFO [c895f068] Running /usr/bin/env chmod +x /tmp/AppName/git-ssh.sh on sub.example.com
DEBUG [c895f068] Command: /usr/bin/env chmod +x /tmp/AppName/git-ssh.sh
INFO [c895f068] Finished in 0.217 seconds with exit status 0 (successful).
DEBUG [679a47be] Running /usr/bin/env git ls-remote on sub.example.com
DEBUG [679a47be] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/AppName/git-ssh.sh /usr/bin/env git ls-remote )
DEBUG [679a47be] fatal: No remote configured to list refs from.
DEBUG [679a47be] Finished in 1.775 seconds with exit status 128 (failed).
Gemfile
# Deploy with Capistrano
gem 'capistrano', '~> 3.0.0'
gem 'capistrano-rails', '~> 1.1.0'
gem 'rvm1-capistrano3', require: false
Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# require 'capistrano/rvm'
# require 'capistrano/rbenv'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
deploy.rb
I have altered this file to add my git url, the app name, the deploy_to path, and the tasks inside task :restart as directed to restart Phusion Passenger.
set :application, 'AppName'
set :deploy_to, '/var/www/appname'
set :repository, "git#github.com:username/appname.git" # Your clone URL
set :scm, "git"
set :user, "my-github-deploy-user" # The server's user for deploys
set :scm_passphrase, "correct-password" # The deploy user's password
set :branch, "master"
set :deploy_via, :remote_cache
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
set :ssh_options, {
verbose: :debug
}
set :format, :pretty
set :log_level, :debug
# set :pty, true
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
# set :keep_releases, 5
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# Your restart mechanism here, for example:
execute :touch, release_path.join('tmp/restart.txt')
# task :start do ; end
# task :stop do ; end
# task :restart, :roles => :app, :except => { :no_release => true } do
# run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
# end
end
end
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 :finishing, 'deploy:cleanup'
end
I tried to included all necessary information, please let me know if there is anything else I can add, thanks for any help you can provide!
In Capistrano 3, you use repo_url instead of repository
So in your deploy.rb, try replacing
set :repository, "git#github.com:username/appname.git" # Your clone URL
with
set :repo_url, "https://github.com/username/appname.git" # Your clone URL
Hopefully this helps.
Update: The ssh URL needs a key; the https URL does not. It's worked for me.