Deploy a rails project with capistrano - ruby-on-rails

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.

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 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?

Whenever Gem Fails to Run "bundle exec" Correctly in Capistrano

I'm having problems deploying the Whenever gem to my production environment using Capistrano.
The problem is stemming from a bundle exec whenever command that's triggering some 'missing gem' issues (yet running bundle install from the shell shows that everything is in fact there!).
My feeling is that one of two things is happening: that Bundler isn't fully loading before bundle exec is being called, or that somehow there's a path issue that's running it in the wrong place.
Here's what's going on:
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
executing locally: "git ls-remote git#my-source-repository:mysource.git HEAD"
command finished in 1847ms
* executing "git clone -q git#my-source-repository:mysource.git /domains/myapp/releases/20130124211036 && cd /domains/myapp/releases/20130124211036 && git checkout -q -b deploy 90238bbcb993e3e7df2374ffaa13e7ed701c202e && (echo 90238bbcb993e3e7df2374ffaa13e7ed701c202e > /domains/myapp/releases/20130124211036/REVISION)"
servers: ["myip"]
[myip] executing command
** [myip :: out] Enter passphrase for key '/home/deploy/.ssh/id_rsa':
** [myip :: out]
command finished in 9294ms
* executing `deploy:finalize_update'
triggering before callbacks for `deploy:finalize_update'
* executing `whenever:update_crontab'
* executing "cd /domains/myapp/releases/20130124211036 && bundle exec whenever --update-crontab My App --set environment=production --roles db"
servers: ["myip"]
[myip] executing command
** [out :: myip] Could not find carrierwave-0.5.8 in any of the sources
** [out :: myip] Run `bundle install` to install missing gems.
command finished in 1967ms
*** [deploy:update_code] rolling back
* executing "rm -rf /domains/myapp/releases/20130124211036; true"
servers: ["myip"]
[myip] executing command
command finished in 998ms
failed: "rvm_path=/usr/local/rvm /usr/local/rvm/bin/rvm-shell '1.9.3' -c 'cd /domains/myapp/releases/20130124211036 && bundle exec whenever --update-crontab My App --set environment=production --roles db'" on myip
You can see that the failure is coming from this line:
bundle exec whenever --update-crontab
Which is in turn being run by my Capistrano deploy.rb.
# Your Application's "Name"
set :application, [My App]
# The URL to your application's repository
set :repository, [repo]
set :scm, :git
set :scm_passphrase, [password]
# The user you are using to deploy with (This user should have SSH access to your server)
set :user, "deploy"
# We want to deploy everything under your user, and we don't want to use sudo
set :use_sudo, false
# Where to deploy your application to.
set :deploy_to, "/domains/myapp/"
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :system # use system-wide RVM
# Require that we use bundler to install necessary gem dependencies
require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
# -------------------------------- Server Definitions --------------------------------
# Define the hostname of your server. If you have multiple servers for multiple purposes, we can define those below as well.
set :server_name, [server]
# We're assuming you're using a single server for your site, but if you have a seperate asset server or database server, you can specify that here.
role :app, server_name
role :web, server_name
role :db, server_name, :primary => true
# -------------------------------- Final Config --------------------------------
# This configuration option is helpful when using svn+ssh but doesn't hurt anything to leave it enabled always.
default_run_options[:pty] = true
namespace :deploy do
desc "Tell Passenger to restart the app."
task :restart do
run "touch #{deploy_to}current/tmp/restart.txt"
end
task :start do
run "cd #{deploy_to} && ln -s current/public public_html"
run "touch #{deploy_to}current/tmp/restart.txt"
end
task :symlink do
run "cd #{deploy_to} && rm current ; ln -s releases/#{release_name} current"
end
task :stop do
run "rm #{deploy_to}public_html"
end
namespace :web do
task :disable do
run "cd #{deploy_to} && rm public_html && ln -s static_site public_html"
end
task :enable do
run "cd #{deploy_to} && rm public_html && ln -s current/public public_html"
end
end
desc "Symlink shared configs and folders on each release."
task :symlink_shared do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
run "ln -nfs #{shared_path}/assets #{release_path}/public/assets"
# create symlink to uploads folder for Carrierwave resources (i.e., book cover images)
run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
end
desc "Sync the public/assets directory."
task :assets do
system "rsync -vr --exclude='.DS_Store' public/assets #{user}##{application}:#{shared_path}/"
end
end
after "deploy", "deploy:migrate"
after 'deploy:update_code', 'deploy:symlink_shared'
after(:setup) do
run "rm -rf #{deploy_to}public_html"
run "mkdir #{deploy_to}static_site"
end
Now, the error that I'm getting about CarrierWave is bogus - I've been using CarrierWave for ages - and the issue clearly stems (as you can see by the command that triggers the error) from either Whenever itself or Bundler.
And I'm thinking that it actually may have something to do with paths related to RVM. At any rate, the bundle exec whenever isn't working.
Any ideas?
It turns out there were a couple problems, both of which needed addressing:
Whenever ~> 0.8 introduced a bug with Capistrano roles
The Whenever cap task was firing before bundle:install (this is confirmed in your code above)
See issues here: https://github.com/javan/whenever/issues/275 and here: https://github.com/javan/whenever/issues/269
To fix the problem, you most likely need to
Update bundler on your server (-v 1.2.3)
Update bundler locally
Update to the newest Whenever (-v 0.8.2)
#ideaoforder's answer helped me too, previously though I had the same trouble caused by require 'whenever/capistrano' being on the top of the config/deploy.rb followed by require 'bundler/capistrano'. Swapping those two helped.
I have require 'bundler/capistrano' always on top of the deploy script as a rule.

Deploying with capistrano and 'Host key verification failed.' promble

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.

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