Rails 3 -- Bundler/Capistrano Errors - ruby-on-rails

I have a basic Rails 3 app working locally on my development box, but want to test out deploying early on to make sure everything works. I'm using Capistrano to deploy.
When I run cap deploy (after all the other necessary setup), it breaks on this command with this error:
[...]
* executing 'bundle:install'
* executing "bundle install --gemfile /var/www/trex/releases/20100917172521/Gemfile --path /var/www/trex/shared/bundle --deployment --quiet --without development test"
servers: ["www.[my domain].com"]
[www.[my domain].com] executing command
** [out :: www.[my domain].com] sh: bundle: command not found
command finished
[...]
So it looks like it can't find the bundle command on the server.
However, when I log in to the server...
$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
$ rails -v
Rails 3.0.0
$ bundle -v
Bundler version 1.0.0
...the bundle command works just fine.
What could be going wrong?
-
(Furthermore, for completeness:)
$ which ruby
~/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
$ which rails
~/.rvm/gems/ruby-1.9.2-p0/bin/rails
$ which bundle
~/.rvm/gems/ruby-1.9.2-p0/bin/bundle

UPDATE:
For RVM >= 1.11.3, you should now just use the rvm-capistrano gem. For older RVM >= 1.0.1, the answer below still applies.
ORIGINAL ANSWER:
Okay, though I still haven't gotten a full cap deploy to work, I did fix this problem. The problem was Capistrano trying to use a different path for Bundler (and other gems) than the RVM paths.
Check your Capistrano path by doing cap shell, then echo $PATH. You'll probably see your standard /usr/local/bin and /usr/bin, but that's not where RVM has Bundler, et al., stored.
Edit your Capistrano config/deploy.rb file, and add the following lines, per these instructions:
# 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.2'
set :rvm_type, :user # Don't use system-wide RVM
That finally got Capistrano to see Bundler and start loading gems appropriately.

Bundler isn't found because .bash_profile is not being loaded and thus your PATH is wrong. This is probably because you have the RVM script in .bash_profile.
The simple answer is to move the RVM script from .bash_profile to .bashrc and Capistrano should be able to find it (also verify that .bash_profile sources .bashrc).
Capistrano uses SSH to execute commands on the server via a non-interactive shell. This shell session will source .bashrc but not .bash_profile. I added an ECHO statement to both and ran an LS via SSH. You can see in the results below that only .bashrc is sourced:
$ ssh user#123.amazonaws.com ls
.bashrc loaded
git
file1
file2

I had an identical problem using rbenv. The solution was to take the rbenv specific lines from the bottom of my .bashrc file and put them at the top. The first line of my .bashrc file was returning aborting if the shell wasn't running in interactive mode.

That last line should actually be
set :rvm_type, :user
that is, user must be a symbol and not a variable, otherwise you'll get
undefined local variable or method `user'

No rvm/capistrano worked for me. The best solution I found was adding to deploy.rb file the following line (it's for non system-wide RVM):
set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

It was my understanding that the bundle command is not found because the PATH variable, defined in the user's ~/.bash_profile, isn't loaded by Capistrano.
To get around this I have created a task :bundle_gems.
task :bundle_gems do
run "cd #{deploy_to}/current && export PATH=/usr/local/pgsql/bin:/opt/ruby-enterprise-X.X.X/bin:$PATH && bundle install vendor/gems"
end
Note that I also include the path to PostgreSQL binaries - installation of the pg gem was failing because they could not be found, even when bundle could be found.
This seems like a messy approach, though. Presumably there is a more 'global' place to define paths to binaries that I don't know about.
Update 23/12
To add a directory to $PATH for all users: https://serverfault.com/questions/102932/adding-a-directory-to-path-in-centos
However this still won't be loaded because it is a non-interactive non-login shell.
One suggestion was to add the paths to /etc/bashrc: How do I set $PATH such that `ssh user#host command` works?
However this also didn't work for me. I believe its because SSH doesn't load /etc/bashrc either.
Another suggestion was to edit ~/.ssh/environment: http://www.ruby-forum.com/topic/79248. However this seems almost as messy as specifying the paths in deploy.rb.

This one worked for me:
set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

I tried a number of the suggestions. Had problems with setting the paths in the deploy.rb file for the RVM environment. My final solution was to include the following:
In the config/deploy.rb file add:
require "bundler/capistrano"
Also in config/deploy.rb, or in my case config/production.rb as I was using the multistage option for Capistrano
after "deploy", "rvm:trust_rvmrc"
This step simply ensures that we stop getting the 'do you want to trust the .rvmrc file' and it calls a task in the deploy.rb file such as:
namespace :rvm do
task :trust_rvmrc do
run "rvm rvmrc trust #{release_path}"
end
end
After putting in these slight changes I was able to run cap production deploy which checked out the code; executed the asset pipeline deployment, linked up the release folder to current, executed bundle install and cleaned up.

Related

Capistrano rbenv - Ruby version not installed witn dry run

When I run Capistrano task with dry run it tells me that rbenv Ruby version can't be found. I assume with dry run it should use local environment. But when I run the commands locally I can easily find below mentioned directory and Ruby is installed.
> ./bin/bundle exec cap --dry-run development t
DEBUG [8171d925] Running [ ! -d ~/.rbenv/versions/2.4.3 ] as user#dev
DEBUG [8171d925] Command: [ ! -d ~/.rbenv/versions/2.4.3 ]
ERROR rbenv: 2.4.3 is not installed or not found in ~/.rbenv/versions/2.4.3
> ls ~/.rbenv/versions/2.4.3
bin include lib share
> rbenv global
2.4.3
> ruby -v
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-darwin16]
> bundle info capistrano
* capistrano (3.4.0)
My Capfile contains below lines.
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.4.3'
I'm using Mac OS and installed rbenv with homebrew.
Check your PATH and make sure it contains $HOME/.rbenv/shims and $HOME/.rbenv/bin
To view your path do:
$ echo $PATH
Also check that you have the following in your ~/.bash_profile
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
To check bash profile enter in terminal:
touch ~/.bash_profile; open ~/.bash_profile
Make sure it's the last setting in your ~/.bash_profile
There must have been some bug in capistrano/rbenv. I have changed my Gemfile as follows and problem is solved.
# gem 'capistrano-rbenv', '2.0.2'
gem 'capistrano-rbenv', '~> 2.1'
I assume with dry run it should use local environment.
This is not true.
A Capistrano dry-run simply prints out the remote commands that it would run in an actual deploy, but it does execute them at all (local or otherwise).
Since Capistrano is not executing any commands, any plugins that rely on results of those commands may not work. For example, the rbenv plugin is apparently expecting to run this command:
[ ! -d ~/.rbenv/versions/2.4.3 ]
In a dry-run scenario, this is not actually executed. Instead, Capistrano just prints the command and continues as if the command succeeded.
In this particular case, for [ ! -d ~/.rbenv/versions/2.4.3 ] to "succeed" means that the ~/.rbenv/versions/2.4.3 does not exist. The rbenv plugin thus prints an error an halts the deployment.
To summarize: in practice, the --dry-run option is not particularly useful.

'bundle is not installed' error

I am not good with devops. I had created a user called deploy. Previously I could ssh into my server then
su deploy
and run
RAILS_ENV=production bundle exec rails c
.
Some days ago, I completely forgot that I had a user called deploy. Being the root user, I installed ruby using rvm. RVM was already installed.
Now I remember that I have a user called deploy. I ran su deploy then ran
RAILS_ENV=production bundle exec rails c
I got the following error
The program 'bundle' is currently not installed. To run 'bundle'
please ask your administrator to install the package 'ruby-bundler'
Before I had installed ruby in super admin env I could run bundle in the deploy user env.
Is there a way to fix this?
PS: My capistrano deployment script is running without any problem even if it is being deployed by the "deploy" user
The problem is that when you ssh into the directory, at the directory, no .ruby-version file exists, so it use the default ruby version(run which ruby to see the exactly path), not the rvm ruby version, so it can't find the installed gems like bundler.
It's a environment problem.The capistrano deployment is working because you have assign ruby version in your deploy.rb like
set :rvm_ruby_version, 'ruby-2.2.2#zhitaotao'
so it can find correct ruby version, then find installed gems for the version.
The solution is add a file called .ruby-version, set the content to something like ruby-2.3.1.
If this not works, i suggest to reinstall ruby, bundler, and run bundle install for deploy user. We need to make sure that at the production directory, the ruby version is matched with the ruby version assigned in the deploy.rb.

How do I completely uninstall ruby, rails, rvm, gems?

I am very new to Ruby on Rails. I have installed ruby, rails, gems, and RVM (and possibly some more RoR-associated files) via Mac OS terminal.
When I first installed these softwares, they seemed to work fine, and I could execute command lines like:
rails new 'project' or rails server
But then I messed around with git, directory, and some sudo bundle/gem commands a little bit because "bundle install" command wouldn't work. Honestly, I don't know what I have done, but all of these command lines have stopped working now.
They output various error messages, such as:
1) There was an error parsing 'Gemfile': Undefined local variable or method for Gemfile. Bundler cannot continue.
2) bash: /usr/local/bin/rails: /usr/local/opt/ruby/bin/ruby: bad interpreter: No such file or directory
3) ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions
4) find spec_for exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException)
I think there were more errors, but these are all I can remember for now. Obviously, I am getting some intimidating error messages that I don't understand.
So I have come to a conclusion that I should uninstall all Ruby, Rails, Gems, RVM, and Homebrew files. But even this task looks very challenging to me.
I have tried numerous command lines in an attempt to delete them, but when I type in "rails" on spotlight, I still see lots of rails-associated files. Also, when I type "ruby -v" on terminal, it is still showing the ruby 2.0.0p648 version.
When I type in "which ruby" on terminal, it says "/usr/bin/ruby
When I type in "which rails" on terminal, it says "usr/local/bin/rails
In short, I just want to delete all of these RoR-related files, softwares, and every trace of them, and reinstall them clean. Please please help me. I do not want to give up coding. Is it too late to say that I'm sorry?
#This is my .bash_profile
# Enable tab completion
source ~/.profile
# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"
# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
alias subl="/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export RBENV_ROOT=/usr/local/var/rbenv
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
~
"~/.bash_profile" 21L, 720C
Ruby is installed on OS X by default. So you don't want to remove that, there are things that might require ruby that has nothing to do with rails or your rails projects, you should ignore it. (and you should probably not use spotlight for finding dev files, its just confusing as to what is safe and what is not to play with)
Secondly, you'll hardly EVER use sudo for any rails-related work on your mac, so if a tutorial wants you to run that command, don't.
And lastly, its possible it's not as bad as you think. So there's 2 steps. 1 - Fix your rvm environment and 2 - fix your gemfile
TO RESOLVE
--Start be reinstalling RVM
\curl -sSL https://get.rvm.io | bash
Now because it's changed your shell environment (added variables and aliases) and because you've changed some things that are unpredictable... Log out, and log back into your mac.
** Which ruby is in use now? **
which ruby
If you're system is still showing /usr/bin/ruby then you'll need to edit your shell profiles. Because I don't know what you might have done, or what shell env you're using I'll just be thorough. Any excess won't hurt.
You'll review (in the editor of your choice) 4 hidden files in your home directory
/Users/yourhome/.profile
/Users/yourhome/.bashrc
/Users/yourhome/.zshrc
/Users/yourhome/.bash_profile
If you're using bash, then make sure your bash_profile has in it
source ~/.profile
In the bashrc nad zshrc files make sure the rvm path exists
export PATH="$PATH:$HOME/.rvm/bin"
In the .profile file make sure this command exists
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
-- Once you've saved your changes, log out and back in (you shouldn't have have to do this but again, just being thorough).
Once that is done, you should be able to install a ruby -
rvm install ruby-2.2.3
Now if you say rvm use ruby-2.2.3 and then which ruby, you should see a pointer to your home directory where rvm lives.
* Now to fix the Gemfile *
You have an encoding problem, which is what caused you the errors that made you see spots in the first place. If you paste the contents of the file here
(in a terminal in the directory of your rails project)
cat Gemfile
Paste those contents here and it can be fixed.

How to un-extract and re-extract Capistrano to the right place

I'm getting this error when trying to do : bundle exec cap deploy
"RVM - Capistrano integration was extracted to a separate gem, \
install: `gem install rvm-capistrano` and remove the `$LOAD_PATH.unshift` line, \
note also the 'set :rvm_type, :user' is now the default (instead of :system)."
Unfortunately, none of those tips actually work.
The problem stems from when I had to revert my copy of RVM to an older copy to have it comply with POW. After I did this, this feature no longer worked. Which sort of makes sense.
So my question is simply, how do I re-engineer my Capistrano exactraction to not a seperate gem.
I tried uninstalling, everything, reinstalling everything, but that didn't seem to work.
Any ideas?
I had the same problem as you. In my deploy.rb file, I had to comment out this line:
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
i.e.
# $:.unshift(File.expand_path('./lib', ENV['rvm_path'])) # Add RVM's lib directory to the load path.
RVM works with POW, this is known issue: https://github.com/37signals/pow/issues/271
the easiest solution(in project dir):
rvm env . > .powenv
Just go and use latest RVM, POW, rvm-capistrano gem - it should be working just fine.

Capistrano 'Bundle Not Found' Error During Deployment

When I run cap deploy:update I get the error below, indicating that bundle is not found. When I run echo $PATH from cap shell the /var/lib/gems/1.9.1/bin path which contains bundle is missing, however, this path is in both /etc/profile and ~/.bashrc. Anyone know how to solve this problem?
[192.168.10.100] executing command
*** [err :: 192.168.10.100] sh:
*** [err :: 192.168.10.100] bundle: not found
*** [err :: 192.168.10.100]
command finished in 25ms
failed: "sh -c 'bundle install --gemfile /data/www/apps/my_app/releases/201104
04163717/Gemfile --path /data/www/apps/my_apps/shared/bundle --deployment --qui
et --without development test'" on 192.168.10.100
To avoide such problem you should have most recent versions of RVM (currently it is 1.13.5) installed in both places: locally and on remote server.
Next, check if your deploy.rb has
require "rvm/capistrano"
require "bundler/capistrano"
This line is not needed anymore:
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
Hope this will help
Ok, I've recently had some experience with this. Looks like there are a couple of ways that this problem can be solved. First, you can determine if in fact the remote execution (via Capistrano) is what's messed up vs. the host itself. Looks like you've done this with the Capistrano remote shell:
$ cap shell
> echo $PATH
Good. I'll bet when you login to the machine and 'echo $PATH' there, the right stuff comes out... same here.
I've found two ways to fix this: One is to enable the environment execution in the remote host's ssh daemon. In theory this would work, but I didn't want to ask the sysadmin if it was ok to open this up. You basically edit the ssh configuration files to set the 'PermitUserEnvironment' to 'yes' and add the required environment settings to the deploy user's ~/.ssh/environment file -- your system-specific man pages are probably better than my trying to generalize.
I opted for what seems rather hackish, and has the drawback that it is global for all hosts you deploy the app to (so if your ruby / gems locations are different on different hosts, this won't work) -- but: I added the default_environment settings to the config/deploy.rb script:
set :default_environment, {
'PATH' => "/usr/local/bin:/bin:/usr/bin:/bin:/<ruby-dir>/bin",
'GEM_HOME' => '<ruby-dir>/lib/ruby/gems/1.8',
'GEM_PATH' => '<ruby-dir>lib/ruby/gems/1.8',
'BUNDLE_PATH' => '<ruby-dir>/lib/ruby/gems/1.8/gems'
}
AMMENDED: It isn't so 'hackish' if you consider the following:
- The environment-specific deploy scripts (deploy/foo.rb) can
override the default in deploy.rb
- PermitUserEnvironment hides the configuration deep in the
.ssh directory of the deploy user; :default_environment at
least exposes it in the checked-in sources.
This also solves the problem of not being able to do remote rake tasks, etc., via Capistrano. Be aware that the Capistrano gem, at least the version I have and with my deploy set up in the "standard" way, will install the gems into the /shared/bundle
directory, which gets picked up by the app. The method I described requires a minimal subset of gems in the system directories referenced by the default environment so that the remote Capistrano commands can execute bundle, rake, etc.
You didn't say if you were using RVM (my solution doesn't); however, this solution is very close to one of the recommended RVM solutions. Alternately, you could just use the 'rvm/capistrano' solution; look for RVM Capistrano integration on the RVM website for more details.
Have you manually installed the bundler gem on the remote box? You can't use the bundle command or install any bundles until you do.
Are you using RVM ?
DaneS some possible solutions:
place
require "bundler/capistrano"
in your script as bundler now has support for capistrano
https://github.com/carlhuda/bundler/blob/1-0-stable/lib/bundler/capistrano.rb
And maybe
before "deploy:cold",
"deploy:install_bundler"
task :install_bundler, :roles => :app do
run "type -P bundle &>/dev/null || { gem install bundler --no-rdoc --no-ri; }"
end
The install_bundler task will only be installed if not found.

Resources