Gemfile.lock, Bundler and Deploying with Capistrano - ruby-on-rails

I am trying to deploy and I do not want the Gemfile.lock in version control. But when it's removed I get the following error.
* executing "cd /var/www/vhosts/my-site/releases/20120628162217 && bundle install -- gemfile /var/www/vhosts/mysite/releases/20120628162217/Gemfile --path /var/www/vhosts/my-site/shared/bundle --deployment --quiet --without development test cucumber analytics"
servers: ["staging-beta.my-site.com"]
[staging-beta.my-sitehealth.com] executing command
** [out :: staging-beta.my-site.com] The --deployment flag requires a Gemfile.lock. Please make sure you have checked your Gemfile.lock into version control before deploying.
command finished in 332ms
Here is my deploy.rb file:
require 'thinking_sphinx/deploy/capistrano'
set :rvm_ruby_string, '1.9.3'
# Ignore the .git directory when creating a release
set :copy_exclude, ".git*"
set :stages, %w(production staging rails_ec2_west_staging vagrant analytics highgroove ec2_highgroove stage_analytics ec2_staging highgroove_staging ec2_chef ec2_sphinx_chef solr_staging ec2_sphinx_prod_chef)
set :default_stage, "staging"
set :bundle_without, [:development, :test, :cucumber, :analytics]
require 'bundler/capistrano'
set :custom_symlinks, {}
require 'capistrano/ext/multistage'
if ARGV.first == 'ec2_staging'
require 'rvm/capistrano'
# for dual deploys on ec2 staging
set :application, "my-site3"
else
set :application, "my-site"
end
set :use_sudo, false
set :user, "www-data"
set :group, "www-data"
ssh_options[:forward_agent] = true
set :branch, $1 if `git branch` =~ /\* (\S+)\s/m
set :scm, :git
set :repository, "git#github.com:my-company/my-site3.git"
set :deploy_to, "/var/www/vhosts/#{application}"
set :deploy_via, :remote_cache
set :deploy_env, 'production'
# If you are using Passenger mod_rails uncomment this:
# if you're still using the script/reapear helper you will need
# these http://github.com/rails/irs_process_scripts
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
task :seed, :roles => :db do
rake = fetch(:rake, "rake")
rails_env = fetch(:rails_env, "production")
run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} db:seed"
end
end
namespace :assets do
task :precompile do
run "cd #{current_path} && #{rake} RAILS_ENV=#{rails_env} my-site-web:assets:precompile"
end
end
task :uname do
run "uname -a"
end
after "deploy:update", "deploy:cleanup"
after "deploy:update", "assets:precompile"
require './config/boot'
#require 'airbrake/capistrano'

It's the best practice to include the gemfile.lock. From Bundlers rational page: http://gembundler.com/rationale.html
This is important: the Gemfile.lock makes your application a single package of both your own code and the third-party code it ran the last time you know for sure that everything worked. Specifying exact versions of the third-party code you depend on in your Gemfile would not provide the same guarantee, because gems usually declare a range of versions for their dependencies.

Related

capistrano deployment unable to bundle install gem with no internet connection

I am deploying my application to a new server which has everything installed.
I am using the following capistrano deploy.rb:
require "capistrano/ext/multistage"
require "bundler/capistrano"
set :default_environment, {
'ORACLE_HOME' => "/opt/oraclient/64/11.2.0.2/",
'LD_LIBRARY_PATH' => "$ORACLE_HOME/lib:/usr/local/lib",
'PATH' => "/opt/ruby/bin:$PATH:$ORACLE_HOME/bin"
}
SECURE_FILES = ['database.yml', 'ldap.yml', 'initializers/secret_token.rb']
set :application, "myapp"
set :use_sudo, false
set :scm, :git
set :repository, "ssh://git#hostname:7999/web/myapp.git"
set :user, "webuser"
set :deploy_via, :remote_cache
after "deploy:update_code", "custom:create_symlinks", "custom:assets_precompile", "custom:miscellaneous"
after "deploy", "deploy:migrate"
after "deploy", "deploy:cleanup"
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
namespace :web do
desc "Enable maintenance mode for apache"
task :enable_maintenance, :role => :web do
run "mkdir -p #{shared_path}/system"
on_rollback { run "rm -f #{shared_path}/system/maintenance.html" }
page = File.read('public/maintenance.html')
put page, "#{shared_path}/system/maintenance.html", :mode => 0644
end
desc "Disable maintenance mode for apache"
task :disable_maintenance, :role => :web do
run "rm -f #{shared_path}/system/maintenance.html"
end
end
end
namespace :custom do
desc "Creating config, bundler-GEMS symlinks"
task :create_symlinks, :roles => :app do
#Secure Configuration File Symlinks
SECURE_FILES.each do |link|
fobj = "#{release_path}/config/#{link}"
run <<-CMD
if [ -e #{fobj} ]; then rm -f #{fobj}; fi;
rm -f #{previous_release}/config/#{fobj};
ln -s #{vormetric_path}/#{application}/#{link} #{fobj};
CMD
end
#Bundler GEM Installation Symlink
shared_bundler_dir = File.join(shared_path, 'bundle')
release_bundler_dir = File.join(current_release, 'vendor/bundle')
run "ln -s #{shared_bundler_dir} #{release_bundler_dir}"
end
desc "Assets Pre-Compilation"
task :assets_precompile, :roles => :app do
run "cd #{current_release} && RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
end
desc "Miscellaneous Tasks"
task :miscellaneous, :roles => :app do
run "chmod -f +w #{current_release}/db/schema.rb"
end
end
This is box-specific deploy script myhostname.rb:
server "myhostname", :app, :web, :db, :primary => true
set :deploy_to, "/opt/web/var/myapp"
set :rails_env, "customertest"
set :branch, "staging"
Now the remote box does not have access to internet, but I have all my gems stored under vendor/cache. So it should pick up from there.(vendor/cache has nokigiri under /myapp/current/vendor/cache on remote server)
When i run
cap deploy servername
, i get the following error:
** [out :: myhost] An error occurred while installing nokogiri (1.5.9), and Bundler cannot
** [out :: myhost] continue.
** [out :: myhost] Make sure that `gem install nokogiri -v '1.5.9'` succeeds before bundling.
My remote box where the code is supposed to be deployed has the following folders set up:
/opt/web/var/myapp
/opt/web/var/myapp/current(where all the code is cloned currently)
/opt/web/var/myapp/releases
/opt/web/var/myapp/shared
I am not sure how it i supposed to pick up and install the gem.
This quote from bundler docs might be relevant (emphasis mine):
http://bundler.io/v1.9/bundle_package.html
By default, if you simply run bundle install after running bundle package, Bundler will still connect to rubygems.org to check whether a platform-specific gem exists for any of the gems in vendor/cache.
This behavior can be avoided by instead running bundle install --local. Note that this requires you to have the correctly platformed version for all of your gems already cached. The easiest way to achieve this is to run bundle package on an identical machine and then check in those vendored gems.
So, in short, you need to pass the --local flag when running bundler in the production server to avoid connecting to rubygems altogether.
The solution for use with capistrano is to set this variable in you deploy.rb file:
# deployment and quiet are used by default, we add the local flag
set :bundle_flags, "--deployment --quiet --local"

block in materialize Could not find multi_json-1.8.0 in any of the sources rails

Here is my deploy.rb
require "bundler/capistrano"
set :rvm_ruby_string, "ruby-2.0.0-p247"
set :rvm_type, :user
set :application, "myapp"
set :repository, "git#bitbucket.org:user/myapp.git"
set :user, "my-server-username-ssh"
set :branch, "master"
set :deploy_to, "/var/rails_apps/myapp" # I have current, release and shared directory here
set :deploy_via, :copy
set :use_sudo, true
set :rvm_install_with_sudo, true
default_run_options[:pty] = true
set :port, 1234
set :scm, :git
role :web, "myapp.com"
role :app, "myapp.com"
role :db, "myapp.com", :primary => true
role :db, "myapp.com"
after "deploy:update_code","deploy:config_symlink"
set :rvm_type, :system
# If you are using Passenger mod_rails uncomment this:
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
task :config_symlink do
#run "cp #{release_path}/config/database.yml.example #{release_path}/config/database.yml"
end
desc 'Re-establish database.yml'
task :set_database_symlink do
run "rm -fr #{current_path}/config/database.yml && cd #{current_path}/config &&
ln -nfs #{shared_path}/database.yml database.yml"
end
end
before 'deploy', 'rvm:install_ruby'
before 'deploy', 'rvm:create_gemset'
require "rvm/capistrano"
if i run cap deploy:migrate
I am getting error no database found error.
And when i manually run rake db:create in my server I am facing this error
/usr/local/rvm/gems/ruby-2.0.0-p247#global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:92:in `block in materialize': Could not find multi_json-1.8.0 in any of the sources (Bundler::GemNotFound)
How can i create and migrate my database.
Edit-1
Even i have updated my certificate using
rvm osx-ssl-certs update all
In my gem list i can fine
multi_json (1.8.2)
I already had this gem installed but I need to
bundle update multi_json
Seems like you might have requirements for multiple versions of the multi_json gem. You can try specifying the gem version like:
gem 'multi_json', '1.8.0'
Rerun the bundle command locally and checkin your Gemfile.lock before you deploy.

Capistrano Deploy failing error for rails - bundle: not found

This is the first time I am deploying and getting errors.
here is my deploy.rb file
require 'bundler/capistrano'
set :application, "app"
set :scm, :git
set :repository, "git#github.com:myname/#{application}.git"
set :branch, "master"
server "198.12.78.92", :web, :app, :db, primary: true
set :user, "myname"
set :deploy_to, "/home/#{user}/public_html/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
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
Here is the error I am getting.
*** [deploy:update_code] rolling back
* executing "rm -rf /home/myname/public_html/app/releases/20130313073408; true"
servers: ["198.12.78.92"]
[198.12.78.92] executing command
command finished in 891ms
failed: "sh -c 'cp -RPp /home/myname/public_html/app/shared/cached-copy /home/myname/public_html/app/releases/20130313073408 && (echo dd92017bc8bb7f951df52d6a14c933e3033fd24b > /home/myname/public_html/app/releases/20130313073408/REVISION)'" on 198.12.78.92
EDIT - I have commented "set :deploy_via, :remote_cache" and now getting bundle: not found error though
OK It seems the answer is (see my comment to your question):
Have a recent rvm installed on both your workstation and the server (I have 1.17.1).
add:
gem 'rvm-capistrano'
to your Gemfile (inside group :development as the capistrano gem)
add:
require "rvm/capistrano"
require "bundler/capistrano"
to your config/deploy.rb
That should do it
If using rbenv put this in .bashrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
before the following line to capistrano load the environment even if connect with non-interactive shell
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
probably will also work also with rvm initialization
I've had the same problems. Below is solution for RBenv and RVM.
RBENV
Install correctly RBenv. Install bundler gem. Pefrorm 'rbenv rehash'.
Add to deploy.rb or deploy/.rb
set :default_environment, { 'PATH' => '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' }
RVM
Install correctly RVM. Install bundler gem.
gem 'rvm-capistrano'
deloy.rb or deploy/.rb
require 'rvm/capistrano'
set :rvm_ruby_string, 'ruby-2.0.0-p247' # Change to your ruby version
set :rvm_type, :system # :user if RVM installed in $HOME
For further configuration info read: https://github.com/wayneeseguin/rvm-capistrano
Good luck.

Deploying first app to VPS, struggling with capistrano

I'm trying to deploy my first app to a VPS ubuntu 12.10, using capsitrano, and following the RailsCasts "Deploying to a VPS" videocast.
One more question, i haven't added an nginx config, nor a unicorn config file. Can i go without them? ( I will want to config nginx trough console.)
My deploy.rb file
require "bundler/capistrano"
set :application, "picurwebaruhaz"
set :scm, "git"
set :repository, "git://github.com/gwuix2/picurwebaruhaz.git"
set :branch, "master"
set :user, "gwuix2"
set :deploy_to, "/home/#{user}/#{application}"
default_run_options[:pty] = true
# 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, "198.211.117.84" # Your HTTP server, Apache/etc
role :app, "198.211.117.84" # This may be the same as your `Web` server
role :db, "198.211.117.84", :primary => true # This is where Rails migrations will run
role :db, "198.211.117.84"
# 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
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
I get the following error:
* executing "cd /home/gwuix2/picurwebaruhaz/releases/20130322113243 && bundle install --gemfile /home/gwuix2/picurwebaruhaz/releases/20130322113243/Gemfile --path /home/gwuix2/picurwebaruhaz/shared/bundle --deployment --quiet --without development test"
servers: ["198.211.117.84"]
[198.211.117.84] executing command
** [out :: 198.211.117.84] sh: 1: bundle: not found
command finished in 357ms
*** [deploy:update_code] rolling back
* executing "rm -rf /home/gwuix2/picurwebaruhaz/releases/20130322113243; true"
servers: ["198.211.117.84"]
[198.211.117.84] executing command
command finished in 384ms
failed: "sh -c 'cd /home/gwuix2/picurwebaruhaz/releases/20130322113243 && bundle install --gemfile /home/gwuix2/picurwebaruhaz/releases/20130322113243/Gemfile --path /home/gwuix2/picurwebaruhaz/shared/bundle --deployment --quiet --without development test'" on 198.211.117.84
Edit:
Here is my app:
APP ON GITHUB https://github.com/gwuix2/picurwebaruhaz
Edit_2:
If I SSH into the server and run $ gem install bundler, it installs, but when I run:
gwuix2#picurbolt:~$ sudo gem install bundler sudo: gem: command not
found
any suggestions?
Can't figure it out myself.
deploy.rb needs:
require "rvm/capistrano"
require "bundler/capistrano"
Try running bundler command without sudo:
gwuix2#picurbolt:~$ gem install bundler sudo: gem: command not found

ror deployment with capistrano -> cap:deploy

i've a problem with the deploying.
i have a ubuntu 11.10 server, i have installed ruby 1.9.2 and rails 3.2.6 with RVM.
i have yet made the pull of my git, it's located in /home/**/idepro.git
i runned the "capify ." command and i modify the deploy.rb file.
i haven't any problem when i run the commands "cap deploy:setup" and "cap deploy:check", but when i run "cap deploy" i have a problem with the enviorment path. The error is:
** [out :: 176.58.****] /usr/bin/env: ruby
** [out :: 176.58.****] : No such file or directory
these are the ror's path:
******#******:~$ which ruby
/home/******/.rvm/rubies/ruby-1.9.2-p320/bin/ruby
and this is the value of $PATH:
******#******:~$ echo $PATH
/home/******/.rvm/gems/ruby-1.9.2-p320/bin:/home/******/.rvm/gems/ruby-1.9.2-p320#global/bin:/home/******/.rvm/rubies/ruby-1.9.2-p320/bin:/home/******/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/******/.rvm/gems/ruby-1.9.2-p320/bin
This is my deploy.rb file:
# RVM bootstrap
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
#require 'rvm/capistrano'
set :rvm_ruby_string, 'ruby1.9.2-p290'
# bundler bootstrap
require 'bundler/capistrano'
# main details
set :application, "176.58.******"
role :web, "176.58.******"
role :app, "176.58.******"
role :db, "176.58.******", :primary => true
ssh_options[:port] = ******
set :user, "******"
set :password, "******"
# server details
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :deploy_to, "/var/www/idealarm2"
set :deploy_via, :remote_cache
set :user, "passenger"
set :use_sudo, false
# repo details
set :scm, :git
set :scm_username, "passenger"
set :repository, "/home/******/idepro.git"
set :branch, "master"
set :git_enable_submodules, 1
# tasks
namespace :deploy do
task :start, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
task :stop, :roles => :app do
# Do nothing.
end
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
end
#default_environment['PATH']='/home/******/.rvm/gems/ruby-1.9.2-p320/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin/:/usr/bin:/bin'
#default_environment['GEM_PATH']='/home/******/.rvm/gems/ruby-1.9.2-p320:/home/******/.rvm/gems/ruby-1.9.2-p320#global:/usr/lib/ruby/gems/1.$
Could anyone help me with the PATH configuration?
I'm using capistrano with RVM with no issues, having followed the 'Integration via the rvm-capistrano gem' section instructions at https://rvm.io//integration/capistrano/.

Resources