Ruby on Rails AWS Cap Deploy Permission Denied - ruby-on-rails

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

Related

Capistrano Many Private Repo Github

Very strange behavior and i cant understand where problems.
I have 2 private repositories on Github and i deploy with Capistrano.
First app normal deployed deploy.rb:
require "rvm/capistrano"
require 'bundler/capistrano'
set :rvm_ruby_string, "2.1.0"
set :assets_role, :app
set :normalize_asset_timestamps, false
set :application, "awesome_app_one"
set :scm, :git
set :repository, "git#github.com:myaccount/repo_one.git"
set :branch, :master
set :deploy_via, :remote_cache
set :ssh_options, { forward_agent: true }
set :user, "rails"
set :deploy_to, "/home/rails/#{application}"
set :shared_children, %w(public/system public/files public/uploads log tmp/pids tmp/sockets)
set :use_sudo, false
task :production do
role :web, "123.456.789.0"
role :app, "123.456.789.0"
role :db, "123.456.789.0", primary: true
set :branch, :master
set :deploy_to, "/home/rails/#{application}"
set :rails_env, 'production'
end
Another app have similar deploy.rb recipe and capistrano can`t found repo:
> ** [123.456.789.0 :: err] ERROR: Repository not found.
> ** [123.456.789.0 :: err] fatal: The remote end hung up unexpectedly
Then i try clone repo on server:
$> git clone git#github.com:myaccount/repo_two.git
$> Initialized empty Git repository in /home/rails/repo_two/.git/
$> ERROR: Repository not found.
$> fatal: The remote end hung up unexpectedly
I add my ssh keys from server on repo_one and repo_two. Look the same problem here but no answers.
Update:
I found some solution and now i can clone repo on server but somebody know how tell capistrano use my command to pull repo?
Had this error last night
We had to use this to get around that error:
set :repository, 'https://username:password#github.com/username/repo.git'
We're refactoring, but it worked for us

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

Capistrano bitbucket - Permission denied (publickey)

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

Capistrano using sudo git clone instead of git clone

Here's my Capistrano file:
set :application, "mydemoapp"
set :repository, "git#bitbucket.org:sergiotapia/mydemoapp.git"
set :scm, :git
set :user, "serg" # The user on the VPS server.
set :use_sudo, false
set :deploy_to, "/opt/nginx/html/#{application}"
set :deploy_via, :remote_cache
set :keep_releases, 1
set :rails_env, "production"
set :migrate_target, :latest
role :web, "192.168.1.1" # Your HTTP server, Apache/etc
role :app, "192.168.1.1" # This may be the same as your `Web` server
role :db, "192.168.1.1", :primary => true # This is where Rails migrations will run
The IP addresses above are obviously fake, I'm using the IP for my VPS
server in real life.
I ran this command, to verify that BitBucket is allowing me to access the repository:
sergiotapia#jarvis1:~/.ssh$ ssh -T hg#bitbucket.org
conq: logged in as sergiotapia.
You can use git or hg to connect to Bitbucket. Shell access is disabled.
However, running cap deploy again, I get:
[198.211.101.99] executing command
** [192.168.1.1:: err] Permission denied (publickey).
** [192.168.1.1:: err] fatal: The remote end hung up unexpectedly
command finished in 1026ms
*** [deploy:update_code] rolling back
* executing "rm -rf /opt/nginx/html/demoapp/releases/20130601025656; true"
I tried using git clone from my SSH terminal, manually and it worked fine as expected. My project cloned to a folder properly.
It seems Capistrano is running sudo git clone instead of git clone. This is verified by myself running a sudo git clone in the same folder manually on my SSH. Same Permission denied (publickey) error.
Why isn't my Capistrano setting for :use_sudo, false being used?

Deploy using Capistrano - is only run for servers matching

Im trying to deploy my application using Capistrano, but I get this error message:
`deploy:setup' is only run for servers matching {:except=>{:no_release=>true}}, but no servers matched
When running this command:
bundle exec cap deploy:setup
Here is my deploy.rb file.
set :application, "example.com"
set :repository, "git#github.com:username/repo.git"
set :use_sudo, false
set :scm, :git
set :web, application
set :app, application
set :db, application
set :branch, "master"
set :user, "webmaster"
set :deploy_to, "/opt/www/#{application}"
set :deploy_via, :remote_cache
set :domain, application
set :port, 2222
set :bundler_cmd, "bundle install --deployment --without=development,test"
ssh_options[:paranoid] = false
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart_stalker do
run "cd #{deploy_to}/current && thor stalker:kill && stalker:init"
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "cd #{deploy_to}/current && touch tmp/restart.txt"
end
after "bundler_cmd", "deploy:restart_stalker"
end
I'm using Rails 3.
You need to define some roles. E.g.:
role :app, 'myapphostname'
role :web, 'mywebhostname'
It seems you used "set" instead of "role", but you should confirm this before making the change.
Most people are probably using multistage with capistrano so you wouldnt put your roles in the deploy.rb, so if you have added environment specific roles in config/deploy/#env_name.rb then make sure to add these in your config/deploy.rb
set :stages, %w(#env_name1, #env_name2...)
require 'capistrano/ext/multistage'
and make sure the capistrano-ext gem is installed.
Seems that you've already set up your server with bundle exec cap deploy:setup.
If that's the case you should now run bundle exec cap deploy.
I'm going to leave an answer here that helped me that when none of the suggested answers here or elsewhere could help me - I spent days researching this issue before I found a fix.
Make sure that if using multistage that the environment specific config files (e.g. config/deploy/environment.rb) are the only files in the config/deploy directory. I had an environment, dev that I was unable to deploy too, turned out there somehow was a complete empty config/deploy/dev file that was getting loaded instead of my config/deploy/dev.rb file, causing every deployment to that environment fail with the posted error.

Resources