Deploying with capistrano and 'Host key verification failed.' promble - ruby-on-rails

I'm trying to setup my Rails app on Linode and I'm at the stage of trying to get Capistrano setup.
I have a git server setup on the same server as where I'm deploying. I have my SSH keys setup (I think) -- at least I can SSH in no problem.
Both 'cap deploy:setup' and 'cap deploy:check' run without error.
But then when I try to run 'cap deploy:migrations' I get the following:
ray#ray-ThinkPad-SL:~/mini-saler$ cap deploy:migrations
* executing `deploy:migrations'
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote git#XX.207.243.215:mini-saler.git master"
command finished in 12446ms
* executing "if [ -d /home/railsu/bianbian.com/shared/cached-copy ]; then cd /home/railsu/bianbian.com/shared/cached-copy && git fetch origin && git fetch --tags origin && git reset --hard 8e8aa8f849a9438851a3767b338ab711d6470299 && git clean -d -x -f; else git clone git#XX.207.243.215:mini-saler.git /home/railsu/bianbian.com/shared/cached-copy && cd /home/railsu/bianbian.com/shared/cached-copy && git checkout -b deploy 8e8aa8f849a9438851a3767b338ab711d6470299; fi"
servers: ["XX.207.243.215"]
[XX.207.243.215] executing command
** [XX.207.243.215 :: out] Cloning into /home/railsu/bianbian.com/shared/cached-copy...
** [XX.207.243.215 :: err] Host key verification failed.
** [XX.207.243.215 :: err] fatal: The remote end hung up unexpectedly
command finished in 2009ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '1.9.3' -c 'if [ -d /home/railsu/bianbian.com/shared/cached-copy ]; then cd /home/railsu/bianbian.com/shared/cached-copy && git fetch origin && git fetch --tags origin && git reset --hard 8e8aa8f849a9438851a3767b338ab711d6470299 && git clean -d -x -f; else git clone git#XX.207.243.215:mini-saler.git /home/railsu/bianbian.com/shared/cached-copy && cd /home/railsu/bianbian.com/shared/cached-copy && git checkout -b deploy 8e8aa8f849a9438851a3767b338ab711d6470299; fi'" on XX.207.243.215
deploy.rb
set :user, 'railsu'
set :domain, 'XX.207.243.215'
set :application, 'mini-saler'
# adjust if you are using RVM, remove if you are not
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user
# file paths
set :repository, "git##{domain}:mini-saler.git"
set :deploy_to, "/home/railsu/bianbian.com"
# distribute your applications across servers (the instructions below put them
# all on the same server, defined above as 'domain', adjust as necessary)
role :app, domain
role :web, domain
role :db, domain, :primary => true
# miscellaneous options
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
set :rails_env, :production
namespace :deploy do
desc "cause Passenger to initiate a restart"
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
desc "reload the database with seed data"
task :seed do
run "cd #{current_path}; rake db:seed RAILS_ENV=#{rails_env}"
end
end
after "deploy:update_code", :bundle_install
desc "install the necessary prerequisites"
task :bundle_install, :roles => :app do
run "cd #{release_path} && bundle install"
end
I can use ssh to connect my vps.

I don't recall if any git commands are called on cap deploy:setup or cap deploy:check - I don't think there are.
Assuming you're using gitolite, check to make sure that your configuration has your railsu user's public key is in the keydir directory.
Or perhaps agent forwarding is what you want? Deploying with Capistrano can help you there.

Related

Capistrano and Git Deploy Rails App

When trying to deploy a Rails app to a production server with Capistrano, it doesn't seem to recognize my project as being a git repo, despite me having cloned the project directly from GitHub.
THE GIT LOG:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
CAP LOG:
$ cap production deploy
triggering load callbacks
* 2016-06-01 16:30:26 executing `production'
triggering start callbacks for `deploy'
* 2016-06-01 16:30:26 executing `multistage:ensure'
* 2016-06-01 16:30:26 executing `deploy'
* 2016-06-01 16:30:26 executing `deploy:update'
** transaction: start
* 2016-06-01 16:30:26 executing `deploy:update_code'
executing locally: "git ls-remote git#github.com:mitigation/mpm.git r1"
command finished in 662ms
* refreshing local cache to revision 8c86d067abde1464f88902566324a99e22cd3147 at /var/folders/xs/5qz1glwj30v51rwpxhyclw880000gn/T/mpm
executing locally: cd /var/folders/xs/5qz1glwj30v51rwpxhyclw880000gn/T/mpm && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard 8c86d067abde1464f88902566324a99e22cd3147 && git clean -q -d -x -f
fatal: Not a git repository (or any of the parent directories): .git
command finished in 13ms
shell command failed with return code pid 52560 exit 128
Here's my Capfile:
load 'deploy'
load 'deploy/assets'
load 'config/deploy'
Here's my DEPLOY.RB:
require 'soprano'
require 'bundler/capistrano'
require 'capistrano/ext/multistage'
require 'whenever/capistrano'
require 'leipreachan/capistrano2'
set :default_environment, {
'PATH' => '/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:$PATH'
}
set :web_server, :nginx
set :keep_releases, 3
set :repository, 'git#github.com:mitigation/mpm.git'
set :deploy_via, :copy
set :copy_exclude, %w(.git .idea .yardoc tmp log .DS_Store doc/* public/uploads.tar db/*.sql vendor/cache)
set :copy_cache, true
set :bundle_without, [:development, :test]
set :bundle_flags, '--deployment --binstubs'
set :user, 'deploy'
before 'deploy:setup', :db
after 'deploy:create_symlink', 'utils:version'
after 'deploy:update_code', 'db:symlink'
#For troubleshooting only
namespace :deploy do
task :update_code, :except => { :no_release => true } do
#on_rollback { run "rm -rf #{release_path}; true" }
strategy.deploy!
finalize_update
end
end
During deploys to various environments, capistrano3 creates a directory tree and puts the git information in a folder called repo_path. You'd have to travel inside that directory on your production server and "$git log" would work indicated its a .git repo
You also shouldn't need .git in your copy_exclude.
Try running through the capistrano3 set up process all over again with your app for different environments.

Deploy a rails project with capistrano

I want to deploy my project with capistrano. Here is my settings file:
deploy.rb
require "capistrano/ext/multistage"
require "capistrano_colors"
require "bundler/capistrano"
require "rvm/capistrano" # Load RVM"s capistrano plugin.
set :application, "project"
set :copy_exclude, %w(.git .gitignore doc features log spec test tmp Capfile)
#set :shared_children, shared_children + %w(public/uploads)
set :use_sudo, false
set :user, "app"
set :stages, %w(staging production)
namespace :deploy do
task :start, roles: :app, except: { no_release: true } do
run "cd #{current_path} && bundle exec unicorn_rails -c config/unicorn.rb -E #{rails_env} -D"
end
task :stop, roles: :app, except: { no_release: true } do
run "kill -KILL -s QUIT `cat #{shared_path}/pids/unicorn.pid`"
end
task :restart, roles: :app, except: { no_release: true } do
stop
start
end
end
def confirm
puts "\n\e[0;36m#{stage}\e[0m\e[0;31m Do you really deploy? (yes/no) \e[0m\n"
proceed = STDIN.gets rescue nil
exit unless proceed.chomp! == "yes"
end
For the multistage, I have created two files (one by environment):
deploy/production.rb
server "myserver.net", :app, :web, :db, primary: true
set :rails_env, "production"
set :rvm_type, :user
set :rvm_ruby_string, "ruby-2.0.0-p0"
set :scm, :git
set :repository, 'ssh://xxxx#11.111.111.111:54333/~/git-workspace/myproject.git'
set :deploy_via, :remote_cache
confirm
and almost the same for the staging one.
( As you have guessed, I have changed project, server name and port number for security purposes)
I first executed :
bundle exe cap production deploy:check
then :
bundle exe cap production deploy:setup
without any problem, when calling the deploy command (bundle exe cap production deploy), i get the below message :
xxxx-no-MacBook-Air:myproject xxxx$ bundle exe cap deploy
triggering load callbacks
triggering start callbacks for `deploy'
* 2013-10-08 13:43:12 13:43:12 == Currently executing `multistage:ensure'
No stage specified. Please specify one of: staging, production (e.g. `cap staging deploy')
xxxx-no-MacBook-Air: xxxx$ bundle exe cap production deploy
triggering load callbacks
* 2013-10-08 13:43:18 13:43:18 == Currently executing `production'
production Do you really want to deploy? (yes/no)
yes
triggering start callbacks for `deploy'
* 2013-10-08 13:43:20 13:43:20 == Currently executing `multistage:ensure'
* 2013-10-08 13:43:20 13:43:20 == Currently executing `deploy'
* 2013-10-08 13:43:20 13:43:20 == Currently executing `deploy:update'
** transaction: start
* 2013-10-08 13:43:20 13:43:20 == Currently executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote ssh://xxxx#11.111.111.111:54333/~/git-workspace/myproject.git HEAD"
Bonjour xxxx
xxxx#11.111.111.111's password:
command finished in 6010ms
* executing "if [ -d /u/apps/myproject/shared/cached-copy ]; then cd /u/apps/myproject/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard e000681dc88244f04ac2e82dd2cf8d94bfa9d930 && git clean -q -d -x -f; else git clone -q ssh://xxxx#11.111.111.111:54333/~/git-workspace/myproject.git /u/apps/myproject/shared/cached-copy && cd /u/apps/myproject/shared/cached-copy && git checkout -q -b deploy e000681dc88244f04ac2e82dd2cf8d94bfa9d930; fi"
servers: ["myserver.net"]
Enter passphrase for /Users/myname/.ssh/id_rsa:
[myserver.net] executing command
** [myserver.net :: out] Bonjour xxxx
** [myserver.net :: out] xxxx#11.111.111.111's password:
Password:
** [myserver.net :: out]
** [myserver.net :: out] Permission denied, please try again.
** xxxx#11.111.111.111's password:
Password:
** [myserver.net :: out]
** [myserver.net :: out] Permission denied, please try again.
** xxxx#11.111.11.111's password:
Password:
** [myserver.net :: out]
** [myserver.net :: out] Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
** [myserver.net :: out] fatal: The remote end hung up unexpectedly
command finished in 36598ms
*** [deploy:update_code] rolling back
* executing "rm -rf /u/apps/myproject/releases/20131008044412; true"
servers: ["myserver.net"]
[myserver.net] executing command
command finished in 1182ms
failed: "rvm_path=$HOME/.rvm $HOME/.rvm/bin/rvm-shell 'ruby-2.0.0-p0' -c 'if [ -d /u/apps/myserver/shared/cached-copy ]; then cd /u/apps/myserver/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q --hard e000681dc88244f04ac2e82dd2cf8d94bfa9d930 && git clean -q -d -x -f; else git clone -q ssh://xxxx#11.111.111.111:54333/~/git-workspace/myproject.git /u/apps/myproject/shared/cached-copy && cd /u/apps/myproject/shared/cached-copy && git checkout -q -b deploy e000681dc88244f04ac2e82dd2cf8d94bfa9d930; fi'" on myserver.net
xxx-no-MacBook-Air:myprojectxxxxx$
I have generated keys (in my local environment) and put the public one in the authorized_keys file (server side).
You seem to have taken a lot of the right steps, so now with this kind of error, you should test the permissions directly:
Ensure that you can ssh from your development machine (where you're running capistrano) to the deployment server as that user. (E.g., that might be something along the lines of ssh deploy#myserver.com.
Once you've made sure that works, then while logged in on the server, as the deployment user, try connecting to the repository server. E.g., something like ssh -T git#github.com as described here.
99% of the time, if you can do these two things successfully, there will be no permissions problems.

Capistrano stage values do not override defaults

I define some stage specific capistrano variables but they do not override default values.
config/deploy/staging.rb:
set :user, 'ec2-user'
set :rails_env, 'staging'
set :domain, 'test.etask.me'
config/deploy.rb:
set :application, 'etask'
set :stages, %w(production staging)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
set :user, 'ec2-user'
set(:unicorn_env) { rails_env }
role(:web) { domain }
role(:app) { domain }
role(:db, :primary => true) { domain }
set(:deploy_to) { "/home/#{user}/#{application}/#{fetch :rails_env}" }
set(:current_path) { File.join(deploy_to, current_dir) }
default_run_options[:pty] = false
require 'rvm/capistrano'
require 'bundler/capistrano'
set :using_rvm, true
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user
set :repository, 'git#bitbucket.org:adaptiveservices/etask-website.git'
set :scm, :git
set :branch, 'master'
set :git_shallow_clone, 1
set :keep_releases, 3
set :use_sudo, false
set :deploy_via, :remote_cache
set :git_enable_submodules, 1
cap deploy output:
triggering load callbacks
* 2013-10-15 15:44:25 executing `staging'
triggering start callbacks for `deploy'
* 2013-10-15 15:44:25 executing `multistage:ensure'
* 2013-10-15 15:44:25 executing `deploy'
* 2013-10-15 15:44:25 executing `deploy:update'
** transaction: start
* 2013-10-15 15:44:25 executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote git#bitbucket.org:adaptiveservices/etask-website.git master"
command finished in 2270ms
* executing "if [ -d /home/ec2-user/etask/production/shared/cached-copy ]; then cd /home/ec2-user/etask/production/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset -q
--hard 88e8556a3cce96e77d51a40b96f2dcd1437c939a && git submodule -q init && git submodule -q sync && export GIT_RECURSIVE=$([ ! \"`git --version`\" \\< \"git version 1.6.5\" ] && echo --recursive) && git
submodule -q update --init $GIT_RECURSIVE && git clean -q -d -x -f; else git clone -q -b master --depth 1 git#bitbucket.org:adaptiveservices/etask-website.git /home/ec2-user/etask/production/shared/cache
d-copy && cd /home/ec2-user/etask/production/shared/cached-copy && git checkout -q -b deploy 88e8556a3cce96e77d51a40b96f2dcd1437c939a && git submodule -q init && git submodule -q sync && export GIT_RECURS
IVE=$([ ! \"`git --version`\" \\< \"git version 1.6.5\" ] && echo --recursive) && git submodule -q update --init $GIT_RECURSIVE; fi"
As you can see capistrano thinks that it is production environment (look at the cd /home/ec2-user/etask/production/shared/cached-copy). It uses default values instead of defined in deploy/staging.rb. Even if I check what the value of user variable - it throws an error (value is not defined) in spite the fact that I set user inside deploy/staging.rb.
I have done all the things as described in capistrano wiki. I looked through tons of other related posts and they do not solve my issue.
Finally I introduced a hack to make it work. I have replaced
set :stages, %w(production staging)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
with
set :stages, %w(production staging)
set :stage, ARGV.select { |arg| stages.include? arg }.first || 'staging'
load "config/deploy/#{stage}.rb"
It also takes three lines but it DOES work. I hope I will get rid of that code when migrating to capistrano 3.x
move this require 'capistrano/ext/multistage' to the bottom of deploy.rb:
and run
cap staging deploy(not cap deploy staging)
Try that way it will use overridden params from staging env.

Rails Capistrano Deploy - REVISION: No such file or directory

I'm using the Capistrano gem and this tutorial to deploy my application to my remote server. Everything works fine until the end of the cap deploy command. I'm receiving this error:
** [ps123456.dreamhostps.com :: out] sh: myapp.git/releases/20130916201449/REVISION: No such file or directory
command finished in 2266ms
*** [deploy:update_code] rolling back
* executing "rm -rf myapp.git/releases/20130916201449; true"
servers: ["ps123456.dreamhostps.com"]
[ps123456.dreamhostps.com] executing command
command finished in 254ms
failed: "sh -c 'git clone --depth 1 ssh://username#ps123456.dreamhostps.com/~/myapp.git myapp.git/releases/20130916201449 && cd myapp.git/releases/20130916201449 && git checkout -b deploy 497af4d996358f8d1f42dc9658e276ee8d9fa64f && git submodule init && git submodule sync && export GIT_RECURSIVE=$([ ! \"`git --version`\" \\< \"git version 1.6.5\" ] && echo --recursive) && git submodule update --init $GIT_RECURSIVE && rm -Rf myapp.git/releases/20130916201449/.git && (echo 497af4d996358f8d1f42dc9658e276ee8d9fa64f > myapp.git/releases/20130916201449/REVISION)'" on ps123456.dreamhostps.com
Here is my deploy.rb:
require 'bundler/capistrano'
set :user, 'username'
set :domain, 'ps123456.dreamhostps.com'
set :applicationdir, "myapp.git"
default_run_options[:pty] = true
set :scm, 'git'
set :repository, "ssh://username#ps123456.dreamhostps.com/~/myapp.git"
set :git_enable_submodules, 1 # if you have vendored rails
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
# roles (servers)
role :web, domain
role :app, domain
role :db, domain, :primary => true
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :export
# additional settings
default_run_options[:pty] = true # Forgo errors when deploying from windows
# Passenger
namespace :deploy do
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
What is going wrong?
It's hard to say exactly, but it's clear that your releases/#{revision} dir is missing by the time you try to push the git revision into the file REVISION.
One likely issue that should probably get fixed is your :deploy_to dir is the same directory on the same machine as the git repo you are cloning from. Try this instead:
In your deploy.rb
set :deploy_to, "/home/#{user}/app/#{application}"
Then make sure to run this the first time:
cap deploy:setup
Then feel free to:
cap deploy
After a lot of head scratching, oddly enough, it was cap deploy:setup that was my downfall. I am still unsure as to what was going on, but I have thoroughly tested and verified that deleting the deployment directory and redeploying the application without running cap deploy:setup will work.
For some reason, running cap deploy:setup, even after successful deploys, will cause permission errors and causes cap deploy to be unable to create and write to directories.

capistrano + git deployment: could not create work tree dir : Permission denied

I'm deploying using git and capistrano with passenger. I've been banging my head for a few hours trying to make this work, and haven't made much progress. cap deploy:setup works ok, but cap deploy is failing with Permission issues. I tried changing permissions/ownership on my slice, but it's still failing.
require 'bundler/capistrano'
set :user, 'some_user'
set :domain, 'example.com'
set :applicationdir, "/home/some_user/public_html/example"
set :port, 30000
set :scm, 'git'
set :repository, "ssh://git#123.45.678.910:50000/home/git/example"
set :branch, 'master'
set :scm_verbose, true
# roles (servers)
role :web, domain
role :app, domain
role :db, domain, :primary => true
# deploy config
set :deploy_to, applicationdir
set :deploy_via, :remote_cache
# additional settings
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
# Passenger
namespace :deploy do
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
results in the following error:
executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote ssh://git#123.45.678.910:50000/home/git/example.com master"
/Users/some_user/.rvm/gems/ruby-1.9.2-p0/gems/capistrano-2.6.0/lib/capistrano/recipes/deploy.rb:104: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777
command finished in 78068ms
* executing "if [ -d /home/some_user/public_html/example.com/shared/cached-copy ]; then cd /home/some_user/public_html/example.com/shared/cached-copy && git fetch origin && git fetch --tags origin && git reset --hard c7f73668d0656c665a6445c33870d05a8550ab2c && git clean -d -x -f; else git clone ssh://git#123.45.678.910:50000/home/git/example.com /home/some_user/public_html/example.com/shared/cached-copy && cd /home/some_user/public_html/example.com/shared/cached-copy && git checkout -b deploy c7f73668d0656c665a6445c33870d05a8550ab2c; fi"
servers: ["example.com"]
[example.com] executing command
** [example.com :: out] fatal: could not create work tree dir '/home/some_user/public_html/example.com/shared/cached-copy'.: Permission denied
command finished in 353ms
*** [deploy:update_code] rolling back
* executing "rm -rf /home/some_user/public_html/example.com/releases/20110610173027; true"
servers: ["example.com"]
[example.com] executing command
command finished in 218ms
failed: "sh -c 'if [ -d /home/some_user/public_html/example.com/shared/cached-copy ]; then cd /home/some_user/public_html/example.com/shared/cached-copy && git fetch origin && git fetch --tags origin && git reset --hard c7f73668d0656c665a6445c33870d05a8550ab2c && git clean -d -x -f; else git clone ssh://git#123.45.678.910:50000/home/git/example.com /home/some_user/public_html/example.com/shared/cached-copy && cd /home/some_user/public_html/example.com/shared/cached-copy && git checkout -b deploy c7f73668d0656c665a6445c33870d05a8550ab2c; fi'" on example.com
Not sure how much this will help, but I always have a permissions issue after a deploy:setup
When you run deploy:setup it creates the initial directories for you. However those folders it creates are usually owned by root (In most of my situations anyway).
webapp/
shared root:root
releases root:root
To remedy this I will change ownership of those new folders to the user that will be using.
webapp/
shared myuser:myuser
releases myuser:myuser
Once this is done, I'll continue with my deploy:update
I've just encountered this problem as well. Here's what I found out.
If you have not yet been able to deploy successfully, you might want to temporarily move your deploy folder (in your server) to another location (or simply rename it)
Make sure you have this inside your recipe: set :use_sudo, false
Run this again: cap deploy:setup
Then try another run of cap deploy
You will definitely run into other troubles with permissions if you have built your deploy folders using the wrong user and tell capistrano that it has sudo permissions.
I hope this works for you, as it worked for me.

Resources