git-push and rvm issues - ruby-on-rails-3

I'm just starting to use git-deploy instead of capistrano, the problem is though that I'm using rvm on my server and the two are not mixing well.
Here is a link to the git-deploy which I'm using:
https://github.com/mislav/git-deploy
I'm using ruby 1.9.2-p180 on my server installed through rvm for the user. When I run my git push and git deploy runs my scripts in the deploy it installs the gems in vendor/.bundle instead of my gems directory: /home/vps/.rvm/gems
Here is my deploy/after_push script
#!/usr/bin/env bash
set -e
oldrev=$1
newrev=$2
run() {
[ -x $1 ] && $1 $oldrev $newrev
}
echo files changed: $(git diff $oldrev $newrev --diff-filter=ACDMR --name-only | wc -l)
umask 002
git submodule init && git submodule sync && git submodule update
export GEM_HOME=/home/vps/.rvm/gems/ruby-1.9.2-p180
export MY_RUBY_HOME=/home/vps/.rvm/rubies/ruby-1.9.2-p180
export GEM_PATH=/home/vps/.rvm/gems/ruby-1.9.2-p180:/home/vps/.rvm/gems/ruby-1.9.2-p180#global
export RUBY_VERSION=ruby-1.9.2-p180
export PATH=/home/vps/.rvm/gems/ruby-1.9.2-p180/bin:/home/vps/.rvm/gems/ruby-1.9.2-p180#global/bin:/home/vps/.rvm/rubies/ruby-1.9.2-p180/bin:/home/vps/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
export rvm_config_path=/home/vps/.rvm/config
export rvm_path=/home/vps/.rvm
export rvm_examples_path=/home/vps/.rvm/examples
export rvm_rubies_path=/home/vps/.rvm/rubies
export rvm_usr_path=/home/vps/.rvm/usr
export rvm_src_path=/home/vps/.rvm/src
export rvm_version=1.6.3
export rvm_gems_path=/home/vps/.rvm/gems
export rvm_ruby_string=ruby-1.9.2-p180
export rvm_tmp_path=/home/vps/.rvm/tmp
export rvm_lib_path=/home/vps/.rvm/lib
export rvm_repos_path=/home/vps/.rvm/repos
export rvm_log_path=/home/vps/.rvm/log
export rvm_help_path=/home/vps/.rvm/help
export rvm_environments_path=/home/vps/.rvm/environments
export rvm_archives_path=/home/vps/.rvm/archives
rvm use 1.9.2
run deploy/before_restart
run deploy/restart && run deploy/after_restart
Here is my deploy/before_restart
#!/home/vps/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
oldrev, newrev = ARGV
def run(cmd)
exit($?.exitstatus) unless system "umask 002 && #{cmd}"
end
RAILS_ENV = ENV['RAILS_ENV'] || 'production'
use_bundler = File.file? 'Gemfile'
rake_cmd = use_bundler ? 'bundle exec rake' : 'rake'
if use_bundler
bundler_args = ['--deployment']
BUNDLE_WITHOUT = ENV['BUNDLE_WITHOUT'] || 'development:test'
bundler_args << '--without' << BUNDLE_WITHOUT unless BUNDLE_WITHOUT.empty?
# update gem bundle
run "bundle install #{bundler_args.join(' ')}"
end
if File.file? 'Rakefile'
num_migrations = `git diff #{oldrev} #{newrev} --diff-filter=A --name-only`.split("\n").size
# run migrations if new ones have been added
run "#{rake_cmd} db:migrate RAILS_ENV=#{RAILS_ENV}" if num_migrations > 0
end
# clear cached assets (unversioned/ignored files)
run "git clean -x -f -- public/stylesheets public/javascripts"
# clean unversioned files from vendor/plugins (e.g. old submodules)
run "git clean -d -f -- vendor/plugins"
Not only does it install it in vendor/.bundle but it installs it for the system version of ruby which is 1.9.1 so I cannot use it with my rvm version which is what apache2 is running. My current work around for all this is to manually ssh in and run bundle install in that directory.
Is there a cleaner way of doing this?
Do I have to have all those exports in my script file?
Update:
Even when I manually go into the directory and run bundle install it puts the gems into vendor/bundle for some reason.
Update:
After entering the following in my before_restart
run "ruby -v"
run "type ruby"
I get this result:
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux]
ruby is /home/vps/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
I've taken out the bundler_args, but it still insists on installing my gems in vendor/bundle for ruby 1.9.1

Running bundle install --deloyment intentionally places gems in the vendor directory.
It's pretty unlikely that you actually have Ruby 1.9.1 installed. If you are using a Debian-derived distribution then it's because the package is misnamed as 1.9.1 when it actually installs 1.9.2.
Otherwise I'm not really sure why your rvm use 1.9.2 line would not be taking effect. Do run "ruby -v" perhaps in the before_restart script and check the version or run "type ruby" to check its path.

Related

New Ubuntu RVM package - I used to use the wrappers binary folder for executables, but which one now?

whenever I run my install scripts for deploying new code, I used to use the /home/<$USER>/.rvm/gems/ruby-2.6.5/wrappers/bundle folder for hard coding executable paths to bundler
Since I see that there is now a separate installation now specifically for ubuntu and that folder path no longer exists, what should I use in my install scripts?
doing things like the following in my deployment scripts does not seem to find bundle
sudo -u <my-user> bash << eof
echo 'remove the current cron tasks'
crontab -r
echo 'running bundle install'
bundle install
echo 'install cron tasks'
(crontab -l 2>/dev/null; echo "00 13 * * * /bin/bash /var/www/application/bin/daily.sh")| crontab -
crontab -l
eof

Deploy rails application after git push

I want to deploy my application on remote test server using capistrano gem.
Both git and rails should run on same server.
I have 2 users 'git' for git repositories and 'rails' with installed rvm. After git push i want to execute hook post-receive which runs su rails and then cap deploy.
When i tried push i got message:
remote: su: must be run from a terminal
How can i work around this message. Can i enable tty some way over git ssh connection?
I can give up from capistrano for this case but still i want rvm and rails to be used only by user rails (so su probably have to be used in each case).
edit
Now i walk around problem. Probably this is very bad solution but works ;). From bellow script's i removed original paths and echo's.
post-receive hook before walkaround
#!/bin/bash
while read oldrev newrev ref
do
su rails #here script fails
cd /path/to/rails/app/current/ && cap deploy
done
post-recive now
#!/bin/bash
while read oldrev newrev ref
do
ssh rails#localhost '/path/to/scripts/deploy.sh'
done
deploy.sh script
#!/bin/bash
CAP_DIR="/path/to/capistrano/dir"
RUBY="1.9.3-p194"
GEMSET="gemset_name"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
rvm use $RUBY
rvm gemset use $GEMSET
cd $CAP_DIR
cap deploy

No .rvm directory after installing rvm

I don't have a ~/.rvm file even after I upgrade my version of RVM using:
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
This command produces the following. How can I make sure a .rvm folder is created?
Upgrading the RVM installation in /opt/t/rvm/
chown: rvm: illegal group name
chown: rvm: illegal group name
RVM PATH line found in /Users/ros/.bashrc /Users/roseperrone/.zshrc.
RVM sourcing line found in /Users/ros/.bash_profile /Users/roseperrone/.zprofile.
Upgrade Notes:
* No new notes to display.
# RVM: Shell scripts enabling management of multiple ruby environments.
# RTFM: https://rvm.io/
# HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
# Cheatsheet: http://cheat.errtheblog.com/s/rvm
# Screencast: http://screencasts.org/episodes/how-to-use-rvm
# In case of any issues run 'rvm requirements' or read 'rvm notes'
Upgrade of RVM in /opt/t/rvm/ is complete.
You don't have a ~/.rvm, because you installed rvm to /opt/t/rvm/.
You may either just live with that and use /opt/t/rvm/ instead of ~/.rvm. Or remove rvm completely and make a new install (which should then default to ~/.rvm).

How to debug RVM setup from Chef and Vagrant?

I got RVM setup with Chef-solo on a Vagrant VM just fine, however I am confused on why bundler can not be found in the rails project.
So, after provisioning I see:
Last login: Thu Oct 4 15:23:58 2012 from 10.0.2.2
vagrant#vm:~$ ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux]
vagrant#vm:~$ gem list
*** LOCAL GEMS ***
bigdecimal (1.1.0)
bundler (1.3.2)
daemon_controller (1.1.1)
fastthread (1.0.7)
io-console (0.3)
json (1.5.4)
minitest (2.5.1)
passenger (3.0.18)
rack (1.5.2)
rake (10.0.3, 0.9.2.2)
rdoc (3.9.4)
rubygems-bundler (1.1.1)
rvm (1.11.3.6)
Going to the project directory I see:
vagrant#vm:~$ cd /www/vm/rails/current/
==============================================================================
= NOTICE =
==============================================================================
= RVM has encountered a new or modified .rvmrc file in the current directory =
= This is a shell script and therefore may contain any shell commands. =
= =
= Examine the contents of this file carefully to be sure the contents are =
= safe before trusting it! ( Choose v[iew] below to view the contents ) =
==============================================================================
Do you wish to trust this .rvmrc file? (/www/vm/rails/current/.rvmrc)
y[es], n[o], v[iew], c[ancel]> y
mkdir: cannot create directory `/usr/local/rvm/gems/ruby-1.9.3-p327#vm': Permission denied
gemset vm is not existing, creating.
mkdir: cannot create directory `/usr/local/rvm/gems/ruby-1.9.3-p327#vm': Permission denied
mkdir: cannot create directory `/usr/local/rvm/gems/ruby-1.9.3-p327#vm': Permission denied
But now, bundle can not be found anymore, and I also have problems to activate RVM... any ideas how to debug this?
$ rvm use ruby-1.9.3-p327
Please note that `rvm gem ...` was removed, try `gem ` or `rvm all do gem ` instead. ( see: 'rvm usage' )
PS my node json is basically this: https://github.com/mulderp/chef-rails-stack
For debugging I use https://github.com/mpapis/rvm-binary/blob/master/cookbooks/binary/recipes/default.rb#L11 :
class Chef::Resource::Script
def log_code command
if Chef::Config[:log_level] == :debug
code "{ #{command}; _ret=$?; echo \"Exit status was $_ret.\"; exit $_ret; } 2>&1 |
tee /var/log/#{#command.to_s.gsub(/ /,"_")}.log; exit ${PIPESTATUS[0]}"
else
code command
end
end
end
and then instead of code use log_code, it will save logs in /var/log/#{#command.to_s.gsub(/ /,"_")}.log
as for easy integration of RVM with Chef check https://gist.github.com/sevos/5076747 :
deploy_user = node[:deploy][:user]
deploy_user_home = File.join('/', 'home', deploy_user)
rvm_version = "head"
execute "install_rvm_for_deploy_user" do
user deploy_user
command "curl -L https://get.rvm.io | bash -s #{rvm_version}"
environment "HOME" => deploy_user_home
creates "#{deploy_user_home}/.rvm"
end
node['buildpack']['ruby_versions'].each do |ruby_version|
execute "install_rvm_ruby_#{ruby_version}" do
user deploy_user
environment "HOME" => deploy_user_home
command "#{deploy_user_home}/.rvm/bin/rvm install #{ruby_version} --autolibs=3"
end
end
file "#{deploy_user_home}/.rvmrc" do
content 'export rvm_trust_rvmrcs_flag=1'
owner deploy_user
mode 0644
end

Rails 3 - Whenever gem error: /usr/bin/env: ruby: No such file or directory

When using the 'whenever gem', I get an error in the log:
/usr/bin/env: ruby: No such file or directory
It works when I run the job manually. I've installed everything with RVM.
I've used the which command to find where my Ruby installation is, and I get:
kevin#lovely:/opt/personal$ which ruby
/home/kevin/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
and I've checked my $PATH variable, where it returns:
kevin#lovely:/opt/personal$ echo $PATH
/home/kevin/.rvm/gems/ruby-1.9.2-p290/bin:/home/kevin/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/kevin/.rvm/rubies/ruby-1.9.2-p290/bin:/home/kevin/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
I believe this should be set up right, but I'm probably wrong since it doesn't work. Can anyone point me in the right direction?
If you're interested, this is what my whenever crontab output is:
# Begin Whenever generated tasks for: rss
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /opt/personal && script/rails runner -e development '\''FeedEntry.update_from_feed("http://lovely/blog/feed/")'\'' >> /opt/personal/log/feedzirra.log 2>&1'
You're probably long past this issue but for future reference:
I had a similar problem only I was getting
/usr/bin/env: ruby: No such file or directory
It turned out the first line of the file script\rails was #!/usr/bin/env ruby1.9.1, which tells the system to invoke it with ruby1.9.1 as explained here. But it should have been #!/usr/bin/env ruby1.9.3 since that was the version I had installed.
Hope this helps someone in the future :)
My issue was that ruby is in /usr/local/bin which is not in the path of a headless bash. So I just made my rake task line in schedule.rb:
job_type :rake, "cd :path && PATH=/usr/local/bin:$PATH RAILS_ENV=:environment bundle exec rake :task :output"
I am successfully using whenever with RVM and bundler in production. Here are the relevant pieces of my capistrano setup that may help you:
# rvm and bundler integration
require 'rvm/capistrano'
require 'bundler/capistrano'
# RVM environment
set :rvm_ruby_string, "ruby-1.9.2#mygemset"
# crontab
set :whenever_roles, :cron
set :whenever_command, "bundle exec whenever"
set :whenever_environment, defer { stage }
require 'whenever/capistrano'
The :whenever_environment setting is because I am using a multi-stage deployment setup. You can ignore that or set it to a string that matches your setup if needed.
Most of this information can be found at the whenever github page under the "Capistrano integration" and "RVM Integration" section headers in the README.
I hope that helps.
I solved the problem about the same as Duke. Except I figure out that $PATH variable is not working for me.
sys_path = '/home/[user]/.rbenv/versions/[ruby_version]/bin'
job_type :runner, "cd :path && PATH=#{sys_path} bin/rails runner -e :environment ':task' :output"
job_type :rake, "cd :path && PATH=#{sys_path} :environment_variable=:environment bin/bundle exec rake :task --silent :output"
If none of these worked for you, try:
gem install rails
This did the job for me, hope it helps!