Capistrano bitbucket - Permission denied (publickey) - ruby-on-rails

I'm trying to deploy my application using Capistrano towards my DigitalOcean server.
This isn't the first time I've configured a RoR server on DigitalOcean with Capistrano deploys that's why I'm confused; I haven't changed anything in my workflow.
Here is my Capistrano configuration file:
require 'bundler/capistrano'
require 'rvm/capistrano'
set :application, "foobar"
set :repository, "git#bitbucket.org:sergiotapia/foobar.git"
set :ping_url, "http://192.168.1.1/"
set :scm, :git
set :scm_verbose, true
default_run_options[:pty] = true
set :user, "sergiotapia" # The user on the VPS server.
set :password, "hunter2"
set :use_sudo, false
set :deploy_to, "/home/sergiotapia/www/#{application}"
set :deploy_via, :remote_cache
set :keep_releases, 1
set :rails_env, "production"
set :migrate_target, :latest
role :web, "192.168.1.1"
role :app, "192.168.1.1"
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, roles: :app, except: { no_release: true } do
run "sudo touch #{File.join(current_path,'tmp','restart.txt')}"
end
end
# Add this to add the `deploy:ping` task:
namespace :deploy do
task :ping do
system "curl --silent #{fetch(:ping_url)}"
end
end
namespace :gems do
task :bundle, :roles => :app do
run "cd #{release_path} && bundle install --without development && rake db:migrate RAILS_ENV=production"
end
end
after "deploy:update_code", "gems:bundle"
# Add this to automatically ping the server after a restart:
after "deploy:restart", "deploy:ping"
When running a cap deploy:setup and cap deploy:check everything comes back green-lighted (working fine).
It fails on the actual cap deploy command.
** [192.168.1.1 :: out] Enter passphrase for key '/home/sergiotapia/.ssh/id_rsa':
Password:
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] Permission denied (publickey).
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] fatal: Could not read from remote repository.
** [192.168.1.1 :: out]
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] Please make sure you have the correct access rights
** [192.168.1.1 :: out]
** [192.168.1.1 :: out] and the repository exists.
** [192.168.1.1 :: out]
I've already added my id_rsa.pub file to BitBucket and also made sure it's added to my SSH agent using the ssh-add -l command.
Even testing out SSH from the remote server works fine:
sergiotapia#tappia:~/www$ ssh -T git#bitbucket.org
logged in as sergiotapia.
You can use git or hg to connect to Bitbucket. Shell access is disabled.
So what gives, why is denying me access to the repository on BitBucket?
Is Capistrano running as a user other than sergiotapia? Would that be the cause of it?

Make sure to add your ssh key to the authentication agent:
ssh-add ~/.ssh/id_rsa
and ensure in deploy.rb
ssh_options[:forward_agent] = true
Edit:
If you are losing the ssh-add configuration on reboots, you should do the following:
As of macOS Sierra 10.12.2 Apple added an ssh_config option called UseKeychain which allows a 'proper' resolution to the problem. Add the following to your ~/.ssh/config file:
Host *
AddKeysToAgent yes
UseKeychain yes

You can setup the SSH agent on the :app server,
Setup keys that do not require a passphrase between the :app server and bitbucket.
Change deploy_via to: :deploy_via, :copy (No need for the deployed server to checkout files, potentially slower though.)

Related

Capistrano deploy using remote_cache - Permission denied

I am modifying a functional Capistrano script trying to speed it up since my home internet upload speed is horrendous. I have a git server (not github), and a ubuntu dev server, when I run cap deploy it currently grabs the latest from my git repo and makes a local copy then uploads that to my ubuntu server and restarts passenger without an issue. My capistrano code contains...
set :deploy_via, :copy
But if I try to use this instead...
set :deploy_via, :remote_cache
I get this error...
** transaction: start
* ←[32m2014-03-13 08:43:36 executing `deploy:update_code'←[0m
updating the cached checkout on all servers
←[33mexecuting locally: "git ls-remote ssh://gitadmin#sub.example.com/opt/git/hub/app.git master"←[0m
gitadmin#sub.example.com's password:
←[2;37mcommand finished in 6880ms←[0m
* ←[33mexecuting "if [ -d /srv/www/app/shared/cached-copy ]; then cd /srv/www/app/shared/cached-copy && git fetch -q origin && git fetch --tags -q origin && git reset
-q --hard efe4a94f5a4f1354edb0f4b516e9ea1d627e5101 && git clean -q -d -x -f; else git clone -q -b master ssh://gitadmin#sub.example.com/opt/git/hub/app.git /s
rv/www/app/shared/cached-copy && cd /srv/www/app/shared/cached-copy && git checkout -q -b deploy efe4a94f5a4f1354edb0f4b516e9ea1d627e5101; fi"←[0m
servers: ["12.34.56.78"]
[12.34.56.78] executing command
** ←[31m[12.34.56.78 :: err] Permission denied, please try again.←[0m
** ←[31m[12.34.56.78 :: err] Permission denied, please try again.←[0m
** ←[31m[12.34.56.78 :: err] Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).←[0m
** [12.34.56.78 :: err] fatal: The remote end hung up unexpectedly
It prompts for the git password - gitadmin#sub.example.com's password:
which I enter and it proceeds ok, then it seems to error out on the next command above. I'm guessing I need to setup some ssh keys somehow? Again this is not hosted on github, the repo and dev server are two separate boxes on my company servers. Locally I am running windows 7. If I try adding...
set :ssh_options, { :forward_agent => true }
default_run_options[:pty] = true
I get the same distance but this time instead of automatically saying permission denied, it prompts for the gitadmin password, I enter it correctly and it says "permission denied".
It would help if you posted your full capistrano scipt (minus any passwords, ip addresses) but I suspect that you need to introduce your server to the remote repository.
You don't need deploy_via option at all
To introduce your server to your repo ssh into your server and from there ssh into the repository service, there should be documentation on the url to use for this. You would get a access denied message but the point is that this process adds ssh public key to your server
It might also be worth watching Ryan Bates Railscast on deploying, he uses github but the process is pretty similar for any remote repo
http://railscasts.com/episodes/335-deploying-to-a-vps
Pay attention to how he introduces his server to github, like I say, your service should provide you with instructions for similar introduction
There is a revised (pro) cast that you would need a subscription for but Ryan is on an extended break (possibly indefinitely right now) so a subscription would be extremely good value right now and well worth thinking about.
The revised (pro) cast on capstrano, how to set variables etc... is here http://railscasts.com/episodes/133-capistrano-tasks-revised
This is a working script for one of my apps just replace xxx and paths as you see fit, I assume you have all the start and restart stuff already set up but this should point you to at least the minimum settings needed. I say this because you possibly have other setting that you don't need, but as you haven't posted your script it is impossible to tell.
require "bundler/capistrano"
server "146.185.182.228", :web, :app, :db, primary: true
set :application, "xxx"
set :user, "xxxx"
# adjust if you are using RVM, remove if you are not
set :rvm_type, :user
set :rvm_ruby_string, 'ruby-2.0.0-p353'
set :ssh_options, {:forward_agent => true}
default_run_options[:pty] = true
# file paths
set :repository, "git#bitbucket.org:xxxx.git"
set :deploy_to, "/home/#{user}/apps/#{application}"
# set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
set :scm, :git
set :use_sudo, false
set :rails_env, :production
set :password, "xxxxxxx"
#role :web, "your web-server here" # Your HTTP server, Apache/etc
#role :app, "your app-server here" # This may be the same as your `Web` server
#role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run
#role :db, "your slave db-server here"
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "cd #{release_path} && bundle install"
run "/etc/init.d/unicorn_#{application} #{command}"
run "#{sudo} service nginx #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/init_unicorn.sh /etc/init.d/unicorn_#{application}"
sudo "ln -nfs #{current_path}/config/sidekiq.conf /etc/init/sidekiq.conf"
run "mkdir -p #{shared_path}/config"
end
after "deploy:setup", "deploy:setup_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web 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
before "deploy", "deploy:check_revision"
after "deploy:update_code", "deploy:symlink_shared"
end

Ruby on Rails AWS Cap Deploy Permission Denied

I'm trying to deploy an app to an amazon webserver. I have git installed and functioning (I already successfully made a push. But when I try to do cap deploy I get "permission denied"
Here's the error:
servers: ["22.229.78.34"]
[ubuntu#22.229.78.34] executing command
** [22.229.78.34 :: out] Cloning into '/home/ubuntu/liquid_admin/releases /20130703153332'...
** [22.229.78.34 :: out] Permission denied (publickey).
** fatal: The remote end hung up unexpectedly
command finished in 779ms
*** [deploy:update_code] rolling back
And here's the deploy file:
require 'bundler/capistrano'
set :user, 'ubuntu'
set :domain, 'ubuntu#22.229.78.34'
set :applicationdir, "~/liquid_admin"
set :scm, 'git'
set :repository, "ssh://ubuntu#22.229.78.34/~/liquid_admin.git"
set :git_enable_submodules, 1 # if you have vendored rails
set :branch, 'master'
set :git_shallow_clone, 1
set :scm_verbose, true
set :application, "liquid.radio"
# set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
Or: accurev, bzr, cvs, darcs, git, mercurial, perforce, subversion or none
role :web, "ubuntu#22.229.78.34" # Your HTTP server, Apache/etc
role :app, "ubuntu#22.229.78.34" # This may be the same as your ` Web` server
role :db, "ubuntu#22.229.78.34", :primary => true # This is where Rails migrations will run
set :deploy_to, "~/liquid_admin"
set :deploy_via, :export
default_run_options[:pty] = true
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
Probably a silly time to ask, but I'm wondering if I need to already have ruby and rails installed for this to work. I'm going to do it right now anyway... but I was just wondering (didn't know if cap does it for you.)
But other than that..why would it be failing?
Again git is setup and running fine. Push worked just great. SSH is working fine. Another thing I should note is that I tried it both with running ubuntu#22.229.78.34 and with just running 22.229.78.34. I did it the ubuntu way because that's usually the only way amazon will accept a connection. Any help would be appreciated.
** [22.229.78.34 :: out] Permission denied (publickey).
** fatal: The remote end hung up unexpectedly
Looks like you need to forward your Github ssh key through the ssh connection Capistrano opens to the AWS instance. Add set :ssh_options, { :forward_agent => true } to your deploy.rb

deploy via capistrano fail

2013-06-17 15:23:22 executing `deploy'
* 2013-06-17 15:23:22 executing `deploy:update'
** transaction: start
* 2013-06-17 15:23:22 executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "hg log --verbose -r tip --template \"{node|short}\""
command finished in 74ms
* executing "if [ -d /home/user/shared/cached-copy ]; then hg pull --verbose --repository /home/user/shared/cached-copy ssh://hg#bitbucket.org/user/myapp && hg update --verbose --repository /home/user/shared/cached-copy --clean 70d2fc5e4a40; else hg clone --verbose --noupdate ssh://hg#bitbucket.org/user/myapp /home/user/shared/cached-copy && hg update --verbose --repository /home/user/shared/cached-copy --clean 70d2fc5e4a40; fi"
[192.168.1.204] executing command
** [192.168.1.204 :: out] running ssh hg#bitbucket.org 'hg -R user/myapp serve --stdio'
** [192.168.1.204 :: out] pulling from ssh://hg#bitbucket.org/user/myapp ** [192.168.1.204 :: out] searching for changes
** no changes found
** [192.168.1.204 :: out] remote: Warning: Permanently added the RSA host key for IP address '207.223.240.182' to the list of known hosts.
** [192.168.1.204 :: out] abort: unknown revision '70d2fc5e4a40'!
i made a deploy.rb script to deploy a rails application via capistrano the problem is that when i run cap deploy there is something wrong with the cloning process and id on't know what this revision is. this is my deploy.rb :
require "rvm/capistrano"
require "bundler/capistrano"
set :application, "myapp"
set :use_sudo , false
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :rvm_type, :user
set :rvm_install_with_sudo, true
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
set :scm, :mercurial
set :repository, "ssh://hg#bitbucket.org/user/myapp"
set :deploy_to, "/home/user/"
set :user, "user"
set :scm_verbose, true
set :deploy_via, :remote_cache
ssh_options[:forward_agent] = true
can anyone help me?
PS : i forget to tell that my repository has two sub repositories would that be the problem or do i need to set any options for that?

Deploying Rails app with Capistrano

I just acquired a Media Temple (dv) 4.0 server to deploy a Rails app I created via a git repository. I'm new to Rails but Capistrano deployment seemed pretty straightforward. Nonetheless, I've been working all day on getting my first Rails project deployed. Here's the error I'm getting when I execute "cap production deploy -v" after setup and check:
** [out :: 64.207.184.51] rake aborted!
** [out :: 64.207.184.51] no such file to load -- rubygems
I have been installing gems all day on the server, so I don't think rubygems isn't installed. This is after hours of debugging why it was telling me my public key is invalid.
Here are my configuration files:
Capfile:
require 'capistrano/ext/multistage'
load 'deploy'
load 'deploy/assets'
load 'config/deploy'
set :application, 'snippet'
set :domain, '[server IP address]'
set :user, 'root'
config/deploy.rb
require 'capistrano/ext/multistage'
default_run_options[:pty] = true
set :stages, ["production"]
set :default_stage, "production"
set :application, "[app name]"
set :scm, :git
set :repository, "git#github.com:[github.com repository]"
set :branch, "master"
set :user, "root"
set :use_sudo, false
set :deploy_via, :remote_cache
set :scm_passphrase, "[public ssh key passphrase]"
config/deploy/production.rb
server "[server IP]", :app, :web, :db, :primary => true
set :deploy_to, "/var/www/vhosts/[domain]/httpdocs"
output of error:
vespertine:snippet nporteschaikin$ cap production deploy -v
** transaction: start
/Users/nporteschaikin/.rvm/gems/ruby-1.9.2-p320/gems/capistrano-2.13.4/lib/capistrano/recipes/deploy.rb:107: warning: Insecure world writable dir /Users/nporteschaikin/.rvm/gems/ruby-1.9.2-p320/bin in PATH, mode 040777
Password:
** [IP-ADDRESS :: out] Enter passphrase for key '/root/.ssh/id_rsa':
** [IP-ADDRESS :: out]
** [IP-ADDRESS :: out] Enter passphrase for key '/root/.ssh/id_rsa':
** [IP-ADDRESS :: out]
** [out :: IP-ADDRESS] (in /var/www/vhosts/[domain.com]/httpdocs/releases/20121021210706)
** [out :: IP-ADDRESS] rake aborted!
** [out :: IP-ADDRESS] no such file to load -- rubygems
** [out :: IP-ADDRESS] /var/www/vhosts/[domain.com]/httpdocs/releases/20121021210706/Rakefile:5:in `require'
** [out :: IP-ADDRESS] (See full trace by running task with --trace)
*** [deploy:update_code] rolling back
failed: "sh -c 'cd /var/www/vhosts/[domain.com]/httpdocs/releases/20121021210706 && rake RAILS_ENV=production RAILS_GROUPS=assets assets:precompile'" on IP-ADDRESS
Please help! I installed RVM, Ruby, Passenger, and Rails on the server.

cap deploy:cold fails with 'No such file or directory - git rev-parse master' error

I am trying to deploy my rails application for the first time.
Server is running Ubuntu 10.4 server (64bit)
Local machine is running Windows XP.
Repository is at github
I have successfully run
cap deploy:setup
but when I run
cap deploy:cold
I get the following error:
D:\Rails\rails_apps\fx>cap deploy:cold
You are running Ruby 1.8.6, which has a bug in its threading implementation.
You are liable to encounter deadlocks running Capistrano, unless you install
the fastthread library, which is available as a gem:
gem install fastthread
* executing `deploy:cold'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
*** [deploy:update_code] rolling back
* executing "rm -rf /var/www/fx/releases/20100818215651; true"
servers: ["pragmaticriskmanagement.gotdns.com"]
[pragmaticriskmanagement.gotdns.com] executing command
command finished
D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy.rb:37:in ``': No such file or directory - git rev-parse master (Errno::ENOENT)
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy.rb:37:in `load'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy.rb:87:in `with_env'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy.rb:37:in `load'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistran/recipes/deploy/scm/git.rb:154:in `query_revision'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy/scm/base.rb:35:in `send'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy/scm/base.rb:35:in `method_missing'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy/scm/base.rb:63:in `local'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/recipes/deploy/scm/base.rb:35:in `method_missing'
... 39 levels...
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/lib/capistrano/cli/execute.rb:14:in `execute'
from D:/Rails/ruby/lib/ruby/gems/1.8/gems/capistrano-2.1.0/bin/cap:4
from D:/Rails/ruby/bin/cap:19:in `load'
from D:/Rails/ruby/bin/cap:19
Here is my 'deploy.rb' file
set :application, "fx" #"pragmaticriskmanagement.gotdns.com"
set :repository, "git#github.com:jmedding/Fx.git"
set :deploy_to, "/var/www/#{application}"
set :git_enable_submodules, 1 # Make sure git submodules are populated
set :location, "pragmaticriskmanagement.gotdns.com"
set :user, "xxxxx"
set :use_sudo, false
set :scm, :git
role :app, location #"your app-server here"
role :web, location #"your web-server here"
role :db, location, :primary => true #"your db-server here", :primary => true
namespace :deploy do
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
desc "Make symlink for database.yml"
task :symlink_dbyaml do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
desc "Create empty database.yml in shared path"
task :create_dbyaml do
run "mkdir -p #{shared_path}/config"
put '', "#{shared_path}/config/database.yml"
end
end
after 'deploy:setup', 'deploy:create_dbyaml'
after 'deploy:update_code', 'deploy:symlink_dbyaml'
after "deploy", "deploy:cleanup"
and now I'm stuck...
Any help would be greatly appreciated.
Thanks,
Jon
Sorry to resurrect such an old thread, but I solved this by adding the default location of my git binaries (C:\Program Files (x86)\Git\bin) to my PATH variable.
Ok, I have made some progress.
It seems that the error indicates that the git ls-remote command could not be found on the local system (win xp). As a test, I opened the GIT Bash shell and tried the command - it worked. Next, I tried
cap deploy
from the GIT Bash shell. Command not found. OK, I added my rails path
D:\Rails\ruby\bin
to the windows 'path' variable. Now, try
cap deploy
from the bash shell. This seems to have solved this problem, which now leads to my next problem
** [xxxxxxxx.com :: out] Cloning into /var/www/fx/shared/cached-copy...
** [xxxxxxxx.com :: err] Permission denied (publickey).
** [xxxxxxxx.com :: err] fatal: The remote end hung up unexpectedly
I can successfully run cap deploy from my linux box with the same public key. Also, I can push to my Github repo from this laptop with this key. Of course, the key in question should be on the server, but if it works when I run 'cap' from the linux box, why wouldn't it run from here?

Resources