Add local binstubs to rbenv (getting command not found) - ruby-on-rails

If you have simple shell command binstubs in the local bin directory of a Rails project (e.g. not generated by or running a gem), rbenv seems to have trouble executing them. For example, #tpope's heroku binstubs generate a binstub named production in the local bin directory:
#!/bin/sh
HEROKU_APP=myapp-production HKAPP=myapp-production exec "${HEROKU_COMMAND:-heroku}" "$#"
After an rbenv rehash, the production command shows up in the ~/.rbenv/shims directory looking something like this:
#!/usr/bin/env bash
set -e
[ -n "$RBENV_DEBUG" ] && set -x
program="${0##*/}"
if [ "$program" = "ruby" ]; then
for arg; do
case "$arg" in
-e* | -- ) break ;;
*/* )
if [ -f "$arg" ]; then
export RBENV_DIR="${arg%/*}"
break
fi
;;
esac
done
fi
export RBENV_ROOT="/Users/Username/.rbenv"
exec "/usr/local/Cellar/rbenv/0.4.0/libexec/rbenv" exec "$program" "$#"
So executing which production gives you:
/Users/Username/.rbenv/shims/production
But executing rbenv which production (or trying to run the command) gives you:
rbenv: production: command not found
I'm new to rbenv so maybe I missed a config step?

Apparently project-specific binstubs in Rails projects should be kept outside of the local bin/ directory since those are primarily for application scripts. So one approach (i.e. that used by the rbenv-binstubs plugin) is to keep local binstubs separate and override rbenv shims with that local binstub directory (e.g. .bundle/bin). So by using the plugin you might see this result from which production:
/Users/Username/.rbenv/shims/production
But if you move the production binstub to .bundle/bin, then rbenv which production should yield:
{PROJECT_ROOT}/.bundle/bin/production

Related

Bundler Trying To Run Under Wrong Version of Ruby

I have a rails site, which I deploy via ssh using a git post-receive hook. When I ssh into the server and run bundle install it runs correctly under the specified ruby version of 2.2.2. However, when I push to the server from my local machine and it hits the 'bundle install command', I get the following:
hooks/post-receive: /usr/local/bin/bundle: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
I can't find for the life of me why it is pointing to ruby1.9.1. This directory does not exist. I do see a directory for ruby2.3 in that directory, but not ruby2.2.2 which is the correct directory. Something is quite fouled up, but I can't figure how to fix it. Anyone seen anything like this?
UPDATE: Here is my post-receive hook, as per the request below...
#!/bin/bash
GIT_DIR=/home/deploy/www_production
WORK_TREE=/home/deploy/www
export MGOTS_DATABASE_USER='user'
export MGOTS_DATABASE_PASSWORD='pass'
export RAILS_ENV="production"
. ~/.bash_profile
while read oldrev newrev ref
do
if [[ $ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
mkdir -p $WORK_TREE
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log
# start deploy tasks
cd $WORK_TREE
bundle install
rake db:create
rake db:migrate
rake assets:precompile
rake requests:cleanup
sudo restart puma-manager
sudo service nginx restart
# end deploy tasks
echo "Git hooks deploy complete"
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
UPDATE: For the sake of clarity, as the answer points to the correct place to find the answer, but doesn't state it exactly, I am posting my updated hook file here. You can see the difference between this one and the one above, and that is what solved the problem. Please note that the path to the rvm directory can be found by typing the command: which rvm - that's the one you want to point to.
#!/bin/bash
GIT_DIR=/home/deploy/www_production
WORK_TREE=/home/deploy/www
export MGOTS_DATABASE_USER='user'
export MGOTS_DATABASE_PASSWORD='pass'
export RAILS_ENV="production"
export RUBYGEMS_GEMDEPS="/home/deploy/.rvm/ruby-2.2.2#www/gems"
. ~/.bash_profile
[[ -s "/usr/share/rvm/bin/rvm" ]] && source "/usr/share/rvm/bin/rvm"
while read oldrev newrev ref
do
if [[ $ref = refs/heads/master ]];
then
echo "Master ref received. Deploying master branch to production..."
mkdir -p $WORK_TREE
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log
# start deploy tasks
cd $WORK_TREE
bundle install
rake db:create
rake db:migrate
rake assets:precompile
rake requests:cleanup
sudo restart puma-manager
sudo service nginx restart
# end deploy tasks
echo "Git hooks deploy complete"
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
You need to load RVM functions to the shell script. link
Or just switch to Rbenv :)
First, set your default ruby to use the version 2.2.2
Are you using RVM? For RVM its: rvm use --default 2.2.2

When executing "bundle exec rails runner", how can I specify the application's directory?

When I execute this command:
/usr/local/bin/bundle exec rails runner -e production "load 'job_alerta_validacao.rb'"
It returns:
Could not locate Gemfile
How can I set where the application is directly in the command without needing to create a script with the "cd" command?
The most common way to use bundler is to cd to the directory containing the Gemfile. This is also the most robust way, since some other gems will probably assume that the working directory is the root of the Rails project.
If you just don't want to change your current shell's working directory, cd in a subshell:
(cd /some/directory; /usr/local/bin/bundle exec rails runner -e production "load 'job_alerta_validacao.rb'")
If you really want to execute something through bundler with a working directory other than the directory with the Gemfile, tell bundler where to find the Gemfile with the BUNDLE_GEMFILE environment variable:
BUNDLE_GEMFILE=/some/directory/Gemfile /usr/local/bin/bundle exec rails runner -e production "load 'job_alerta_validacao.rb'"

-su: bundle: command not found when starting unicorn

I'm following a tutorial at DigitalOcean (fyi, this is the tutorials, link1 , link2 ), to install a production ready rails app using unicorn, and nginx, and when I get to the part on installing unicorn. When I try typing this into the console:
sudo service unicorn_appname start
I get this error:
Starting appname
-su: bundle: command not found
All users can bundle. This message makes no sense to me. Any ideas?
I also followed the same guide and had the same issue.
The startup script in init.d/unicorn_appname is evaluated to:
su - deploy -c cd /home/deploy/appname && bundle exec unicorn -c config/unicorn.rb -E production -D
root user on startup will first su - into the rails user (in this case 'deploy') then excutes bundle to start up unicorn. rbenv is single user, only 'deploy' has bundle installed. The path to bundle is likely stored in your .bashrc file if you followed the guide. However .bashrc file which is not invoked by login in through su - and that caused the bundle not installed error.
The solution would be to include the paths related to rbenv in .profile. This way when root su - into 'deploy' the paths are loaded.
You can fix this by adding the rbenv shims path to your .profile
cd ~
vi .profile
look for the line that starts with PATH="
just after the quote add the following:
$HOME/.rbenv/shims:
save the file ( ESC :wq )
start the service as you normally would.
Looks like you starting service with "system wide Ruby /usr/bin/ruby" which doesn't have gem "bundler" installed yet, not with version of Ruby which you expecting to be in runtime (installed through the RVM or RBENV somewhere in user namespace). Make sure desired Ruby version infrastructure is in the path prior to call "bundle exec unicorn...".
Part of my unicorn_service script:
USER="ubuntu"
APP_NAME="my_app_name"
APP_ROOT="/home/$USER/$APP_NAME"
ENV="production"
# environment settings
PATH="/home/$USER/.rbenv/shims:/home/$USER/.rbenv/bin:$PATH"
When I ran sudo service unicorn start I got the same error i.e bundle command not found.
Initially, path was present in my .bashrc file.
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Adding the path in .bash_profile did the work for me. I guess it is taking $PATH from .bash_profile not from .bashrc.
After copying this path to .bash_profile and then doing source .bash_profile worked for me.

bundle not found when running services using upstart/foreman

I'm setting up a staging environment for a Rails 4 app on Ubuntu 12.04.
I use:
- rbenv for managing rubies
- capistrano for deployemnt
- foreman for managing services
In particular, I want to run que as a service. My Procfile looks like
que: bundle exec rake que:work
I have exported the Procfile for upstart successfully. My sapp-que-1.conf looks like
start on starting ft-id-que
stop on stopping ft-id-que
respawn
exec su - deployer -c 'cd /home/deployer/apps/sapp/releases/20140307080502; export PORT=5000; bundle exec rake que:work >> /home/deployer/apps/sapp/current/log/que-1.log 2>&1'
When I try to start it (sudo start sapp) I get the following error:
-su: bundle: command not found
However if I cd into /home/deployer/apps/sapp/releases/20140307080502 and I manually run bundle exec rake que:work the rake is executed.
Am I missing anything here?
Thanks.
Could you have .bash_profile ?
The su does not read .bashrc.
[SOLVED] Special user, .bashrc not being executed upon login
I create .bash_profile with
# Load the default .profile
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile"
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

Error while installing ruby on rails on server

After installing rvm when i run this
source $HOME/rvm/scripts/rvm
i get error
-bash: /root/rvm/scripts/rvm: No such file or directory
reference: Problem deploying Ruby+RVM and daemontools
I found the answer but looking at the rvmsudo script installed with
rvm, here is a working run script:
#!/bin/sh
# redirect stderr to stdout
exec 2>&1
cd /app
# load rvm
. /usr/local/rvm/scripts/rvm
# select ruby version for this application
rvm use 1.9.1
# # depending on your configuration you may need to provide the absolute path to rvm, like that:
# /usr/local/bin/rvm use 1.9.1
# build the exec command line preserving the rvm environment
command="exec sudo -u app_user /usr/bin/env PATH='$PATH'"
[[ -n "${GEM_HOME:-}" ]] && command="${command} GEM_HOME='$GEM_HOME' "
[[ -n "${GEM_PATH:-}" ]] && command="${command} GEM_PATH='$GEM_PATH' "
# this is where your real command line goes
command="${command} ruby main.rb"
# run the application
eval "${command}"
Try source ~/.rvm/scripts/rvm. Exit out of your current shell and then try again it should work.

Resources