Capistrano deploy fails. Using bitbucket and SSH key - ruby-on-rails

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.

Related

Bitbucket Pipelines Inappropriate ioctl for device error using Capistrano

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 :)

Capistrano rails deploy - passphrase for SSH key not authenticating

I am trying to deploy my rails app with Capistrano. While deploying it asks for the passphrase for key '/home/gokul/.ssh/id_rsa' but, I can't able to type the passphrase the characters are visible and not authenticating.
deploy.rb
# Server
server 'xxx.xxx.xxx.xxx', roles: [:web, :app, :db], primary: true
# Repository
set :repo_url, 'git#bitbucket.org:gokul/testapp.git'
set :scm_passphrase, ''
set :application, 'testapp'
set :user, 'gokul'
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)}/APP/#{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), auth_methods: ['publickey'], keys: %w(~/.ssh/privatekey.pem) }
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}
set :linked_dirs, %w(tmp/pids)
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
Deployment Log:
It is asking for the passphrase but while entering the passphrase it is visible and not authenticating.
gokul$ cap production deploy
rvm 1.29.1 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
ruby-2.3.1
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
00:00 git:wrapper
01 mkdir -p /tmp
✔ 01 gokul#xxx.xxx.xxx.xxx 0.708s
Uploading /tmp/git-ssh-testapp-gokul.sh 100.0%
02 chmod 700 /tmp/git-ssh-testapp-gokul.sh
✔ 02 gokul#xxx.xxx.xxx.xxx 0.706s
00:03 git:check
01 git ls-remote git#bitbucket.org:gokul/testapp.git HEAD
01 Enter passphrase for key '/home/gokul/.ssh/id_rsa':
password
a
sde
we
ere
re
e
e
e
^C(Backtrace restricted to imported tasks)
cap aborted!
Interrupt:
Tasks: TOP => deploy:check => git:check
(See full trace by running task with --trace)
The deploy has failed with an error:
I tried setting pty as false as answered in https://stackoverflow.com/a/23227003/4172728. But this doesn't work for me.
Can anyone help me please.
Thank you.
I solved this issue by adding my local machine public key from ~/.ssh/id_rsa.pub to the list of access keys in the bitbucket repository settings.
And then, added the id_rsa to the ssh-agent as below:
gokul$ ssh-add ~/.ssh/id_rsa
Ref: https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html

Capistrano not creating puma pid when deploying to production

I have a simple rails app that I am trying to deploy to a production server using Capistrano, but it appears to fail when trying to create the puma.pid.
When I check the the puma.access.log I see the following,
/home/capin/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/uri/rfc3986_parser.rb:66:in `split': bad URI(is not URI?): unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock (URI::InvalidURIError)
from /home/capin/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/uri/rfc3986_parser.rb:72:in `parse'
from /home/capin/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/uri/common.rb:226:in `parse'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/lib/puma/binder.rb:85:in `block in parse'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/lib/puma/binder.rb:84:in `each'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/lib/puma/binder.rb:84:in `parse'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/lib/puma/runner.rb:119:in `load_and_bind'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/lib/puma/single.rb:79:in `run'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/lib/puma/cli.rb:215:in `run'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/gems/puma-2.15.3/bin/puma:10:in `<top (required)>'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/bin/puma:23:in `load'
from /home/capin/apps/Tshirt/shared/bundle/ruby/2.2.0/bin/puma:23:in `<main>'
Does anybody know why puma would display this error? I can post more configuration files if need be too.
The console output when running the deploy command,
INFO [5680e746] Finished in 2.087 seconds with exit status 0 (successful).
DEBUG [d53f0457] Running /usr/bin/env if test ! -d /home/capin/apps/Tshirt/current; then echo "Directory does not exist '/home/capin/apps/Tshirt/current'" 1>&2; false; fi on youtee.io
DEBUG [d53f0457] Command: if test ! -d /home/capin/apps/Tshirt/current; then echo "Directory does not exist '/home/capin/apps/Tshirt/current'" 1>&2; false; fi
DEBUG [d53f0457] Finished in 0.296 seconds with exit status 0 (successful).
DEBUG [54524c04] Running /usr/bin/env [ -f /home/capin/apps/Tshirt/shared/tmp/pids/puma.pid ] on youtee.io
DEBUG [54524c04] Command: [ -f /home/capin/apps/Tshirt/shared/tmp/pids/puma.pid ]
DEBUG [54524c04] Finished in 0.310 seconds with exit status 1 (failed).
DEBUG [287a3b10] Running /usr/bin/env if test ! -d /home/capin/apps/Tshirt/releases; then echo "Directory does not exist '/home/capin/apps/Tshirt/releases'" 1>&2; false; fi on youtee.io
DEBUG [287a3b10] Command: if test ! -d /home/capin/apps/Tshirt/releases; then echo "Directory does not exist '/home/capin/apps/Tshirt/releases'" 1>&2; false; fi
DEBUG [287a3b10] Finished in 0.217 seconds with exit status 0 (successful).
INFO [5a813829] Running /usr/bin/env echo "Branch master (at 538f125)
deployed as release 20160101233746 by capin" >> /home/capin/apps/Tshirt/revisions.log on youtee.io
DEBUG [5a813829] Command: echo "Branch master (at 538f125) deployed as release 20160101233746 by capin" >> /home/capin/apps/Tshirt/revisions.log
INFO [5a813829] Finished in 0.248 seconds with exit status 0 (successful).
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:
# 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}
server 'youtee.io', port: 4321, roles: [:web, :app, :db], primary: true
set :rvm_ruby_string, '2.2.1'
set :repo_url, 'git#bitbucket.org:ipatch/tshirt.git'
set :application, 'Tshirt'
set :user, 'capin'
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)}"
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml config/secrets.yml}
# see this SO answer, http://stackoverflow.com/a/32011351/708807
# set :linked_dirs, fetch(:linked_dirs, []).push('public/uploads')
set :puma_bind, "tcp://0.0.0.0:9294"
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
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
# preserve uploaded files through Capistrano deployments
# ln -n = ln -h, which if target dir is a sym link don't follow
# ln -f, if already linked unlink so new link can be created
# ln -s, creates a symlink
# execute :ln, "-nfs #{shared_path}/public/uploads/store #{release_path}/public/uploads/store"
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
# 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}
# 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(/Users/capin/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey password)
# port: 4321,
}
# 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'
# }
shared/puma.rb
#!/usr/bin/env puma
directory '/home/capin/apps/Tshirt/current'
rackup "/home/capin/apps/Tshirt/current/config.ru"
environment 'production'
pidfile "/home/capin/apps/Tshirt/shared/tmp/pids/puma.pid"
state_path "/home/capin/apps/Tshirt/shared/tmp/pids/puma.state"
stdout_redirect '/home/capin/apps/Tshirt/current/log/puma.error.log', '/home/capin/apps/Tshirt/current/log/puma.access.log', true
threads 4,16
bind 'unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock'
workers 0
preload_app!
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = "/home/capin/apps/Tshirt/current/Gemfile"
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
Looks like mistake in your puma/capistrano configuration file. I think you put the line unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock into single quotas instead of double and string interpolation is not working :)
UPDATE:
check your puma_bind value, should be
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
in double quotas

rails project deploying server died few time

I deploying rails project using capistrano 3.0
ror + nginx + unicorn
deploy is so not bad.
but server died during unicorn restart time.
1~2 second died server..
I dont know what i todo..
this is my deploy.rb source..
# -*- encoding : utf-8 -*-
# config valid only for Capistrano 3.1
lock '3.1.0'
require 'unicorn'
#set :whenever_command, "bundle exec whenever"
#require "whenever/capistrano"
set :application, 'projectname'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
# Default deploy_to directory is /var/www/my_app
# set :deploy_to, '/var/www/my_app'
# Default value for :scm is :git
set :scm, :git
set :repo_url, 'git#github.com:gitURL'
# Default value for :format is :pretty
set :branch, 'master'
set :deploy_via, :remote_cache
# set :format, :pretty
# 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, %w{config/database.yml}
# Default value for linked_dirs is []
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# Default value for default_env is {}
set :default_env, { path: "/root/.rbenv/versions/2.1.0/lib/ruby/gems:/root/.rbenv/versions/2.1.0/bin:$PATH" }
#set :current_deploy_path, "DEPLOY_PATH"
# Default value for keep_releases is 5
# set :keep_releases, 5
set :ssh_options, { :forward_agent => true}
set :user, "user"
set :password, "passwod"
namespace :whenever do
task :start do
on roles(:app) do
execute("cd #{fetch :current_deploy_path}")
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, 'exec whenever --update-crontab'
end
end
end
end
end
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')
end
end
after :publishing, :restart
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
before 'deploy:assets:precompile', :link_assets
task :link_assets do
on roles(:app), :roles => :app, :except => { :no_release => true } do
execute("ln -fs #{shared_path}/database.yml #{release_path}/config/database.yml")
end
end
#before 'deploy:publishing', :kill_unicorns
task :kill_unicorns do
on roles(:app), in: :sequence, wait: 3 do
execute("if [ -f #{fetch :current_deploy_path}/tmp/pids/unicorn.pid ]; then kill -s QUIT `cat #{fetch :current_deploy_path}/tmp/pids/unicorn.pid`; fi")
end
end
task :make_unicorn do
on roles(:app), in: :sequence, wait: 3 do
execute :mkdir, '-p', "#{fetch :current_deploy_path}/tmp/pids"
execute("cd #{fetch :current_deploy_path}")
execute("if [ -f #{shared_path}/unicorn.pid ]; then kill -s QUIT `cat #{shared_path}/unicorn.pid`; fi")
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, 'exec unicorn -D -c config/unicorn.rb -E production'
end
end
execute("cp #{fetch :current_deploy_path}/tmp/pids/unicorn.pid #{shared_path}/unicorn.pid")
end
end
# desc "Update the crontab file"
# task :update_crontab do #, :roles => :db do
# on roles(:app), in: :sequence, wait: 3 do
# execute("cd #{release_path} && bundle exec whenever --update-crontab #{application}")
# end
# end
# after 'deploy:publishing', :kill_unicorns
after 'deploy:publishing', :make_unicorn
after 'deploy:finishing', 'whenever:start'
end
and this is my deploy/production.rb
set :domain, "14.63.165.216"
set :rails_env, "production"
set :current_deploy_path, "/geuinea_pig/priday/current"
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
server '14.63.165.216', user: 'root', roles: %w{web app db}, primary: true#, my_property: :my_value
set :deploy_to, '/geuinea_pig/priday'
set :ssh_options, {
user: 'root', # overrides user setting above
# keys: %w(/home/user_name/.ssh/id_rsa),
forward_agent: false,
#auth_methods: %w(publickey password),
auth_methods: %w(password),
password: 'dPffhdPffh1!'
}
this is deploy log in capistrano log
INFO [193203be] Running /usr/bin/env if [ -f /path/shared/unicorn.pid ]; then kill -s QUIT `cat /path/shared/unicorn.pid`; fi on 14.63.165.216
DEBUG [193203be] Command: if [ -f /path/shared/unicorn.pid ]; then kill -s QUIT `cat /path/shared/unicorn.pid`; fi
#---------------------------------
#THIS TIME DIED SERVER 2~3 second
#---------------------------------
INFO [193203be] Finished in 0.227 seconds with exit status 0 (successful).
DEBUG [05a0c865] Running /usr/bin/env if test ! -d /path/releases/20140706164800; then echo "Directory does not exist '/path/releases/20140706164800'" 1>&2; false; fi on 14.63.165.216
DEBUG [05a0c865] Command: if test ! -d /path/releases/20140706164800; then echo "Directory does not exist '/path/releases/20140706164800'" 1>&2; false; fi
DEBUG [05a0c865] Finished in 0.229 seconds with exit status 0 (successful).
INFO [3cf3c052] Running /usr/bin/env bundle exec unicorn -D -c config/unicorn.rb -E production on 14.63.165.216
DEBUG [3cf3c052] Command: cd /path/releases/20140706164800 && ( PATH=/root/.rbenv/versions/2.1.0/lib/ruby/gems:/root/.rbenv/versions/2.1.0/bin:$PATH RAILS_ENV=production /usr/bin/env bundle exec unicorn -D -c config/unicorn.rb -E production )
INFO [3cf3c052] Finished in 1.061 seconds with exit status 0 (successful).
INFO [07718210] Running /usr/bin/env cp /path/current/tmp/pids/unicorn.pid /path/shared/unicorn.pid on 14.63.165.216
DEBUG [07718210] Command: cp /path/current/tmp/pids/unicorn.pid /path/shared/unicorn.pid
INFO [07718210] Finished in 0.197 seconds with exit status 0 (successful).
I would suggest a slower start on Capistrano, if you're new to it? I find it challenging. In fact, I think you're way beyond me. There are so many little things that can go wrong and the logs/output can be a bit overwhelming that I try some simpler cap tasks. I create a weird directory under the home of my deployer user and then run this:
task :dir_exists do
ask(:dir_to_test, "$home")
on roles(:all) do |host|
if test("[ -d #{fetch(:dir_to_test)} ]")
info "Phew, it's ok, the directory exists!"
else
info "Directory #{fetch(:dir_to_test)} does not exist"
end
end
end
If that runs, at least I know that I'm communicating with the server and can run things. I had big problems at first mostly having to do with the authentication. I didn't get the whole ssh-add thing. Even yesterday I had to run some new commands on my database and it took me a about two hours to figure out that my syntax was complete crap.
Anyway, the more specific you can be about what is going wrong, the more help that will be offered. By the way, do you really have a top-level directory called "/geuinea_pig/friday/current"? If all of this is capistrano, maybe save these files off and start simpler? I say that because my deploy is alot simpler, maybe you don't need all that? Also, won't backtick execution happen on the local machine (you have backtick cat /path/shared/unicorn.pid backtick)?
Anyway, good luck!

How do I configure remote refs for git + capistrano 3 in ROR deployment?

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.

Resources