Deploying to RVM-enviroment with Capistrano (RoR App) - ruby-on-rails

I've been looking around a lot for answers to my problem but I can't find a solution that works...so here is my first post ever on stackoverflow!
A while ago I setup an apache server with RVM in order to host one of my client's website. By then, my development environment also had ruby installed through rvm. In the meanwhile, I changed my development environment and now started using VM VirtualBox with Ubuntu, and installed ruby directly from source using apt-get install ruby. I've used Capistrano to deploy all my projects.
Now I've done some work back on that project, but when I tried to deploy it to the production server, I get this error:
2013-07-08 08:12:50 executing `bundle:install'
* executing "cd /var/www/project/releases/20130708061242 && bundle install --gemfile /var/www/project/releases/20130708061242/Gemfile --path /var/www/project/shared/bundle --deployment --quiet --without development test"
servers: ["xxx.xx.xxx.xxx"]
[xxx.xx.xxx.xxx] executing command
*** [err :: xxx.xx.xxx.xxx] tput:
*** [err :: xxx.xx.xxx.xxx] No value for $TERM and no -T specified
*** [err :: xxx.xx.xxx.xxx]
*** [err :: xxx.xx.xxx.xxx] tput:
*** [err :: xxx.xx.xxx.xxx] No value for $TERM and no -T specified
*** [err :: xxx.xx.xxx.xxx]
** [out :: xxx.xx.xxx.xxx] ERROR: Gem bundler is not installed, run `gem install bundler` first.
command finished in 818ms
*** [deploy:update_code] rolling back
* executing "rm -rf /var/www/project/releases/20130708061242; true"
servers: ["xxx.xx.xxx.xxx"]
[xxx.xx.xxx.xxx] executing command
command finished in 693ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'default' -c 'cd /var/www/project/releases/20130708061242 && bundle install --gemfile /var/www/project/releases/20130708061242/Gemfile --path /var/www/project/shared/bundle --deployment --quiet --without development test'" on xxx.xx.xxx.xxx
I also have my own testing server and I have no problem when deploying on this one (which has rbenv instead).
Server end stuff is my least knowledgeable area :) Let me know if I can give any additional information that you might need.
Thanks!
EDIT
Here is my deploy.rb
require "bundler/capistrano"
require "rvm/capistrano"
server "xxx.xx.xxx.xxx", :app, :web, :db, :primary => true
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
set :rvm_install_ruby_params, '--1.9' # for jruby/rbx default to 1.9 mode
set :rvm_install_pkgs, %w[libyaml openssl] # package list from https://rvm.io/packages
set :rvm_install_ruby_params, '--with-opt-dir=/usr/local/rvm/usr' # package support
before 'deploy:setup', 'rvm:install_rvm' # install RVM
before 'deploy:setup', 'rvm:install_pkgs' # install RVM packages before Ruby
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset, or:
before 'deploy:setup', 'rvm:create_gemset' # only create gemset
before 'deploy:setup', 'rvm:import_gemset' # import gemset from file
#General settings
set :ssh_options, { :forward_agent => true }
set :application, "project"
set :repository, "git#repo.com:project.git"
set :deploy_to, "/var/www/#{application}"
set :deploy_via, :remote_cache
set :scm, :git
set :scm_user, "deploy"
set :user, :deploy
set :use_sudo, false
set :keep_releases, 5
after "deploy:update_code", "deploy:migrate"
load "deploy/assets"

change top part of your deploy.rb to:
require "bundler/capistrano"
require "rvm/capistrano"
server "xxx.xx.xxx.xxx", :app, :web, :db, :primary => true
set :rvm_ruby_string, :local
before 'deploy:setup', 'rvm:install_rvm' # install RVM
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset
#General settings

Related

Capistrano Production Deploy -> rbenv

I think I have set up Capistrano3 correctly on my machine and that it should be connecting to my server. When I run cap production deploy (on my machine) I receive the following error messenger:
DEBUG [2959f403] Running /usr/bin/env [ -d ~/.rbenv/versions/2.1.2 ] as [user#myIP]
DEBUG [2959f403] Command: [ -d ~/.rbenv/versions/2.1.2 ]
DEBUG [2959f403] Finished in 0.420 seconds with exit status 1 (failed).
ERROR rbenv: 2.1.2 is not installed or not found in ~/.rbenv/versions/2.1.2
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as [user#myIP]: exit
SystemExit: exit
Tasks: TOP => rbenv:validate
(See full trace by running task with --trace)
On my machine (iMac):
$ ruby -v
ruby 2.1.2p95
$ rbenv -v
rbenv 0.4.0
$ which rebind
/usr/local/bin/rbenv
On my server (ubuntu)
$ ruby -v
ruby 2.1.2p95
$ rbenv -v
rbenv 0.4.0-151-g83ac0fb
$ which rebind
/home/[user]/.rbenv/bin/rbenv
Any ideas how to fix this?
** More Info From Comments **
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, [app name]
set :repo_url, [repo location]
set :repository, [repo location]
set :scm, :git
set :deploy_user, [user]
set :user, [user]
set :use_sudo, false
set :rails_env, "production"
set :deploy_via, :copy
# setup rvm.
# how many old releases do we want to keep
set :keep_releases, 5
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml config/application.yml}
# dirs we want symlinking to shared
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# what specs should be run before deployment is allowed to
# continue, see lib/capistrano/tasks/run_tests.cap
set :tests, []
# which config files should be copied by deploy:setup_config
# see documentation in lib/capistrano/tasks/setup_config.cap
# for details of operations
set(:config_files, %w(
nginx.conf
database.example.yml
log_rotation
monit
unicorn.rb
unicorn_init.sh
))
# which config files should be made executable after copying
# by deploy:setup_config
set(:executable_config_files, %w(
unicorn_init.sh
))
# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
set(:symlinks, [
{
source: "nginx.conf",
link: "/etc/nginx/sites-enabled/#{fetch(:full_app_name)}"
},
{
source: "unicorn_init.sh",
link: "/etc/init.d/unicorn_#{fetch(:full_app_name)}"
},
{
source: "log_rotation",
link: "/etc/logrotate.d/#{fetch(:full_app_name)}"
},
{
source: "monit",
link: "/etc/monit/conf.d/#{fetch(:full_app_name)}.conf"
}
])
namespace :deploy do
# make sure we're deploying what we think we're deploying
before :deploy, "deploy:check_revision"
# only allow a deploy with passing tests to deployed
before :deploy, "deploy:run_tests"
# compile assets locally then rsync
after :finishing, 'deploy:cleanup'
after 'deploy:publishing', 'deploy:restart'
end
Server
$ ls /home/[user]/.rbenv/versions/
[blank... ]
$
If /home/[user]/.rbenv/versions is an empty directory on the server, then you have no versions of ruby installed.
Try running rbenv install 2.1.2 on the server. You may need to install the ruby-build plugin first.

Deploying with RVM on Linode Ubuntu 10.4 Does not install RVM

I'm trying to deploy with Capistrano and RVM and even though when running cap bundler:install_bundler which is supposed to install bundler, and succeeds. After, when running cap deploy it fails with an error that bundler is not installed.
** [out :: victorstan.com] ERROR: Gem bundler is not installed, run `gem install bundler` first.
Any ideas why it installs bundler, but then can't find it, or installs it in the wrong place (i guess?). Thanks!
here is part of my deploy.rb script:
set :application, "dinner"
set :domain, "victorstan.com"
set :repository, "ssh://#{domain}/~/#{application}.git"
set :use_sudo, false
set :user, "victorstan" # The server's user for deploys
set :scm, :git
set :scm_username, "passenger"
set :deploy_to, "/srv/www/#{application}"
set :keep_releases, 2
set :branch, "master"
set :deploy_via, :copy # won't make cap prompt for password when deploying
set :scm_verbose, true
set :rvm_ruby_string, 'ruby-2.0.0-p195#dinner'
set :rvm_type, :user
set :rvm_install_type, :stable
set :whenever_command, "bundle exec whenever"
require "bundler/capistrano"
require "rvm/capistrano"
require "whenever/capistrano"
If I try to use cap to install bundler, it works:
cap bundler:install_bundler
* 2013-06-08 15:33:54 executing `bundler:install_bundler'
* executing "gem install bundler"
servers: ["victorstan.com"]
[victorstan.com] executing command
** [out :: victorstan.com] Successfully installed bundler-1.3.5
** [out :: victorstan.com] 1 gem installed
command finished in 1373ms
And here is the output when running cap deploy:
Switched to a new branch 'deploy'
command finished in 984ms
* Compressing /var/folders/w4/_6g_9lqd491_6cmhzb0kcmzh0000gn/T/20130608192600 to /var/folders/w4/_6g_9lqd491_6cmhzb0kcmzh0000gn/T/20130608192600.tar.gz
executing locally: tar czf 20130608192600.tar.gz 20130608192600
command finished in 53ms
servers: ["victorstan.com"]
** sftp upload /var/folders/w4/_6g_9lqd491_6cmhzb0kcmzh0000gn/T/20130608192600.tar.gz -> /tmp/20130608192600.tar.gz
[victorstan.com] /tmp/20130608192600.tar.gz
[victorstan.com] done
* sftp upload complete
* executing "cd /srv/www/dinner/releases && tar xzf /tmp/20130608192600.tar.gz && rm /tmp/20130608192600.tar.gz"
servers: ["victorstan.com"]
[victorstan.com] executing command
command finished in 467ms
* 2013-06-08 15:26:05 executing `deploy:finalize_update'
triggering before callbacks for `deploy:finalize_update'
* 2013-06-08 15:26:05 executing `bundle:install'
* executing "cd /srv/www/dinner/releases/20130608192600 && bundle install --gemfile /srv/www/dinner/releases/20130608192600/Gemfile --path /srv/www/dinner/shared/bundle --deployment --quiet --without development test"
servers: ["victorstan.com"]
[victorstan.com] executing command
** [out :: victorstan.com] ERROR: Gem bundler is not installed, run `gem install bundler` first.
command finished in 598ms
*** [deploy:update_code] rolling back
* executing "rm -rf /srv/www/dinner/releases/20130608192600; true"
servers: ["victorstan.com"]
[victorstan.com] executing command
command finished in 513ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'ruby-2.0.0-p195#dinner' -c 'cd /srv/www/dinner/releases/20130608192600 && bundle install --gemfile /srv/www/dinner/releases/20130608192600/Gemfile --path /srv/www/dinner/shared/bundle --deployment --quiet --without development test'" on victorstan.com
is ruby-2.0.0-p195#dinner installed / created on the server?
you can ensure it is with adding:
before 'deploy:setup', 'rvm:install_ruby'
more details on using rvm-capistrano can be found here: https://github.com/wayneeseguin/rvm-capistrano#readme
I have read some more examples on setting up deploy script and i've re-arranged the order of elements of my deploy script, as well as ena
set :application, "dinner"
set :domain, "victorstan.com"
set :repository, "ssh://#{domain}/~/#{application}.git"
set :use_sudo, false
set :user, "victorstan" # The server's user for deploys
set :scm, :git
set :scm_username, "passenger"
set :scm_passphrase, "*********" # The deploy user's password
set :deploy_to, "/srv/www/#{application}"
set :keep_releases, 2
set :branch, "master"
set :deploy_via, :copy # won't make cap prompt for password when deploying
set :scm_verbose, true
set :rvm_ruby_string, :local
set :rvm_type, :user
set :rvm_install_type, :stable
set :default_shell, :bash
set :whenever_command, "bundle exec whenever"
default_run_options[:pty] = true
# before 'deploy', 'rvm:install_ruby'
before 'deploy:setup', 'rvm:install_ruby'
require "rvm/capistrano"
require "bundler/capistrano"
require "whenever/capistrano"
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true # This is where Rails migrations will run
Some notable elements that have changed:
set :rvm_ruby_string set to :local instead of explicitly naming it: 'ruby-2.0.0-p195#dinner', not sure if this has any implication on the issue though, seems unlikely.
set :default_shell, :bash was another parameter I added.
And, as I mentioned, the ordering or the different DSL types might make a difference: set comes first, then before hooks, then require and then role after which comes the rest of the deploy script. Perhaps this can help someone in a similar situation.
Note, when I first tested this script I had before 'deploy', 'rvm:install_ruby' enabled, but I have since disabled it and the script continues to work...

Bundle install error when deploying via capistrano (& rvm)

Now I must admit that I'm stumbling around in the dark a little bit as far as this deployment lark is concerned. I'll try and explain the situation best I can; I have set up a test deployment server and am trying to deploy my app to it with capistrano, however, I am encountering some difficulties surrounding my gems and their dependencies, as the error below shoes.
[mike-test] executing command
[mike-test] rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell '1.9.3' -c 'cd /home/deploy/myapp/releases/20120910081544 && bundle install --gemfile /home/deploy/myapp/releases/20120910081544/Gemfile --path /home/depoy/myapp/shared/bundle --deployment --quiet --without development test'
** [out :: mike-test] Some gems seem to be missing from your vendor/cache directory.
** [out :: mike-test] Could not find log4r-1.1.10 in any of the sources
command finished in 9134ms
*** [deploy:update_code] rolling back
log4r isn't in my gemfile so I can only assume it's a (perhaps production only?) dependency of another gem. I don't know why bundler isn't downloading the gem and installing it if it can't find it? I put the gem in my gemfile, ran bundle install locally, and then committed and deployed again and got the same error but with a different gem this time (spreadsheet), so that appeared to solve the error in that case only, but doesn't identify the problem.
Something else to muddy the water, I'm trying to use RVM on the production server and despite reading a lot about it I'm still not 100% sure I get how it works, so that might be a factor.
My deploy.rb:
require "bundler/capistrano"
require "rvm/capistrano"
# SCM Settings
set :rvm_ruby_string, '1.9.3'
set :use_sudo, false
ssh_options[:forward_agent] = true
default_run_options[:pty] = true
set :branch, :mikedev
set :deploy_via, :remote_cache
set :copy_cache, true
set :git_enable_submodules, 0
set :repository, "our_git_repo.git"
set :scm, :git
set :user, :deploy
set :keep_releases, 1
set :application, "myapp"
set :deploy_to, "/home/deploy/myapp"
set :branch, "mikedev"
role :web, "mike-test"
role :app, "mike-test"
role :db, "mike-test", :primary => true
namespace :deploy do
desc "Restarting mod_rails with restart.txt"
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
end
Any guidance would be much appreciated.
bundle install --deployment ... will not download any gems if vendor/cache is present. It'll look for gems just there. There are two options:
remove vendor/cache directory (even if it's already empty) from your VCS
or run bundle package and add all new files under vendor/cache to your VCS
The latter seems to be better option. In this way you protect your deployment from outages of rubygems servers.

gems not found on target server after Capistrano deployment

I deployed my rails 3 app onto the production server using Capistrano. The deployment is correct, the application server is correctly restarted but when I log onto the production server and go into "my_path/current", I cannot run "rails c":
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
I checked the gem list and everything seems to be correctly installed though.
My config/deploy.rb file is:
require "bundler/capistrano"
# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user # Don't use system-wide RVM
set :application, "MyApp"
set :repository, "git#github.com:user/myapp.git"
set :scm, :git
set :branch, "master"
set :scm_verbose, true
set :scm_username, "user" # GITHUB user name
set :user, "user" # VPS user name
set :scm_passphrase, "pass" # VPS password
set :deploy_to, "/home/luc/be"
set :use_sudo, false
server "my_server", :web, :app, :db, :primary => true
# start / stop application server
namespace :deploy do
task :start do
invoke_command "cd /home/luc/be/current; bundle exec thin start -C config/thin.config"
end
task :stop do
invoke_command "cd /home/luc/be/current; bundle exec thin stop -C config/thin.config"
end
task :restart do
invoke_command "cd /home/luc/be/current; bundle exec thin restart -C config/thin.config"
end
end
Try bundle exec rails c. Also check what error, if any, is thrown when you visit the page in your browser

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