Capistrano using sudo git clone instead of git clone - ruby-on-rails

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?

Related

Rails : Capistrano. Permisssion Denied(public key)

I've been looking at a way to deploy my app using capistrano. I currently am hosting a small private repo on github and a local server to try deploying my test. I come upon a problem and an error message below.
I have done the following
Generate an ssh key on server and added it sucessfully to deploy keys in repo and tested(git#github.com)
Generate an ssh key on client and add it sucessfully to deploy keys in repo
setup a private repository. And have account deployer with rights to deploy
configed deploy.rb and production rb to follow mimic many other templates out there.
I still cannnot figure why it is giving me an error like this
DEBUG [a5554d3d] Command: /usr/bin/env chmod +x /tmp/App/git-ssh.sh
INFO [a5554d3d] Finished in 0.020 seconds with exit status 0 (successful).
INFO [b1517df1] Running /usr/bin/env git ls-remote --heads git#github.com:aceofw
ings/App.git as deploy#192.168.1.84
DEBUG [b1517df1] Command: ( GIT_ASKPASS=/bin/echo GIT_SSH=/tmp/App/git-ssh
.sh /usr/bin/env git ls-remote --heads git#github.com:aceofwings/App.git )
DEBUG [b1517df1] Permission denied (publickey).
DEBUG [b1517df1] fatal: Could not read from remote repository.
DEBUG [b1517df1]
DEBUG [b1517df1] Please make sure you have the correct access rights
DEBUG [b1517df1] and the repository exists.
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#192.168.1.84:
git exit status: 128
git stdout: Nothing written
git stderr: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
SSHKit::Command::Failed: git exit status: 128
git stdout: Nothing written
The Deploy.rb file
###############Deploy.rb##################
# config valid only for current version of Capistrano
lock '3.4.0'
set :repo_url, 'git#github.com:aceofwings/App.git'
set :application, 'App'
set :user, 'deploy'
#set :pty, true
# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')
namespace :deploy do
after :restart, :clear_cache do
on roles(:web), in: :groups, limit: 3, wait: 10 do
# Here we can do anything such as:
# within release_path do
# execute :rake, 'cache:clear'
# end
end
end
end
Production.rb
server '192.168.1.84', user: 'deploy', roles: %w{app db web}
#set :stage, :production
role :app, %w{deploy#192.168.1.84}
role :web, %w{deploy#192.168.1.84}
role :db, %w{deploy#192.168.1.84}
set :ssh_options, {
forward_agent: false,
auth_methods: %w(password),
password: 'Deploy4Real',
user: 'deploy'
}
I had a similar problem. It turns out that the SSH agent was not running. I found this out looking at the Capistrano documentation: http://capistranorb.com/documentation/getting-started/authentication-and-authorisation/
Running this command:
$ ssh-add -l
showed me that my public key was not added to the agent so I had to add it:
$ ssh-add
Identity added: /Users/me/.ssh/id_rsa (/Users/me/.ssh/id_rsa)
After this my Capistrano deployment worked.
You should set in production.rb:
forward_agent: true
I was having the same problem with Capistrano 3.4. With Capistrano 3.2.1 it seemed to ignore this parameter.
You can use capistrano-ssh-doctor to troubleshoot possible config problems.
I think your capistrano may have some old files in its git cache, probably from renaming the repository or something like that. Try to delete the cached-copy folder in the shared folder on the server, so capistrano will pull your repo again.

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

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.

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

Error running capistrano in ubuntu 10.04 64 bits

I'm a novice with capistrano, and I'm going to deploy my app in linode on SO ubuntu 10.04 lucid 64 bits.
I'm trying to make my deploy with capistrano gem, but I get the error:
[000.00.000.00] executing command
[000.00.000.00] rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'default' -c 'cd /home/user/project/current; git fetch origin; git reset --hard master'
** [out :: 109.74.195.41] bash: /home/user/.rvm/bin/rvm-shell: No such file or directory
command finished in 330ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'default' -c 'cd /home/user/project/current; git fetch origin; git reset --hard master'" on 000.00.000.00
ubuntu#ubuntu:~/Desktop/project$
This is my deploy.rb
# RVM
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, 'default'
set :rvm_type, :user
# Bundler
require "bundler/capistrano"
# General
set :application, "project"
set :user, "user"
set :deploy_to, "/home/user/project"
set :deploy_via, :copy
set :use_sudo, false
# Git
set :scm, :git
set :repository, "~/project/.git"
set :branch, "master"
# VPS
role :web, "my vps ip"
role :app, "my vps ip"
role :db, "my vps ip", :primary => true
set :server, :nginx
require 'capistrano_recipes'
I have installed nginx and I want to use unicorn with nginx.
I have a directory created in home/user/project in remote vps to deploy my application there.
My ruby version in vps remote host is:
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-linux]
Can anyone help shed some light on what I'm doing wrong?
RVM (head or 1.11.0) now supports tasks for installing RVM/Ruby, I have an article to describe the process http://niczsoft.com/2012/03/fast-deployment-using-capistrano-rvm-and-more/
You can find my deploy.rb here: https://github.com/mpapis/ad/blob/master/config/deploy.rb
And here is description on RVM site: http://beginrescueend.com/integration/capistrano/
Also worth mentioning are the new file formats not requiring trusting: http://beginrescueend.com/workflow/projects/#ruby-versions

Resources