Sudo Access to Rails gem installed from Github - ruby-on-rails

I am required to run the following Ubuntu bash script as "sudo" (FYI, this is because I am running it through Upstart):
cd /u/apps/MyRailsApp/current && /usr/bin/env RAILS_ENV=production script/delayed_job restart >> /var/log/upstart/delayed_job.log
However, when I run it as "sudo", I get the following error:
/var/lib/gems/1.9.1/gems/bundler-1.7.6/lib/bundler/source/git.rb:188:in rescue in load_spec_files': git#github.com:MyRemoteRepo/my_gem.git (at 1.0.1) is not yet checked out. Runbundle install` first. (Bundler::GitError)
It's not recommended to run "bundle install" as Sudo. When I run "bundle install" as a non-admin user, it is working and all the gems are recognized. How do I get the root user to recognize the gem?
The gem is in /home/ubuntu/.bundler/ruby/1.9.1/. My other gems are in /var/lib/gems/1.9.1/gems/
Thanks so much for your help.

Try adding "source /home/[username]/.bashrc" where [username] is non-admin user that can run the script successfully.

Related

Cronjobs for Rails stopped working after switching to asdf

I recently changed from rvm to asdf.
Since then, I can't get cronjobs to run.
crontab -l
* * * * * /bin/bash -l -c 'cd /var/www/jobs/code && RAILS_ENV=production bundle exec rake cron_test' >> /tmp/cron_test_output 2>&1 /tmp/cron_error
gives me /tmp/cron_error: bundle: command not found. There is no output to stderr.
gem install bundler has not worked for the jobs user.
Not sure where to install bundler to make this work.
The rake tasks work when run independently. The cronjobs that are not rake tasks run. Cron is working.
Edit:
While the cronjobs were working before, they obviously can't now find the location of bundler. By changing bundle exec to the full path /home/jobs/.asdf/shims/bundler exec, I have managed to get this to work.
I have been using the whenever gem to create cron jobs for rake tasks. Turns out the easiest way to get around this problem is to make sure that the environment path is included in the whenever generated crontab.
within config/schedule.rb for the whenever gem, I added the following:
env :PATH, ENV['PATH']
and now everything is working as the cron job can now find the bundler.
Based on this issue from the whenever gem.
And this one too.

bundle exec rake db:migrate says Run `bundle install`

bundle install
This works perfectly fine. But when I do
bundle exec rake db:migrate
It says
Could not find byebug-6.0.2 in any of the sources
Run `bundle install` to install missing gems.
From comments, I saw that you use 2 different types of commands, 1 with sudo and 1 without.
Problem is: sudo is another user (root user), so what you install by sudo may not be applied for other users and vice versa.
The solution I could think of is to use all sudo commands, since your current user seems not to have access to PG.
sudo bundle install
sudo bundle exec rake db:migrate
I just troubleshat this same problem. I finally tried removing byebug and was offered a choice of byebug_6.0.2 or byebug_8.1.0. Removing the earlier version didn’t do it so I removed the other as well. I then reinstalled using
gem install byebug
That gave me another failed migrate, this time blaming debug_inspector. I ran
gem install debug_inspector
After that I was able to run rake db:migrate as expected.

System rvm is not working on direct call

I am using CentOS. Here is my output:
# which rvm
/usr/local/rvm/bin/rvm
# rvm ruby-1.9.3-p392#gemset
# /usr/local/rvm/bin/rvm ruby-1.9.3-p392#gemset
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for a example.
As you can see if I am using just rvm everything is working. But capistrano using full path in commands like this
[46f28bd9] Command: cd /var/www/app/releases/20140718172057 && /usr/local/rvm/bin/rvm ruby-1.9.3-p392#gemset do bundle install --binstubs /var/www/app/shared/bin --path /var/www/app/shared/bundle --without development test --deployment --verbose
So it just fails and all gems being installed on default ruby instead of ruby-1.9.3-p392#gemset
Thanks for any suggestions.
My assumptions:
you are using Capistrano v3
have already included require 'capistrano/rvm' in your Capfile
when you cd into your project directory you have something like .ruby-version and .ruby-gemset setting up RVM for you.
You might want to try something like this
within fetch(:current_path) do
with rails_env: fetch(:rails_env) do
execute :bundle, "install"
end
end
The way Tasks work in v3 is different: https://github.com/capistrano/capistrano#tasks
tl;dr: execute(:bundle, :install) and execute('bundle install') don't behave identically!

cron job fails with "is not checked out. Please run `bundle install` (Bundler::GitError)"

I seem to have a similar problem as this post, where Bundler complains of a gem not being checked out. However, mine shows an error when running a rake task using a cron job. (Otherwise, the site seems to deploy fine in production using Phusion Passenger, and the rake task runs when called in the command line )
The error is:
/usr/local/lib/ruby/gems/1.9.1/gems/bundler-1.0.18/lib/bundler/source.rb:571:in load_spec_files': git://github.com/moneill/Google-Maps-for-Rails.git (at modified_markers) is not checked out. Please runbundle install` (Bundler::GitError)
I tried using bundle install --deployment ; bundle pack followed by bundle install --path vendor/cache.
One thing I haven't tried is to locally compile the git project and install the gem in the vendor/bundle folder. I am not using RVM for this particular server.
The cron job command is cd /home/[dir]/[rails_app_folder]/ && RAILS_ENV=production /usr/local/bin/bundle exec rake [task] --trace
Thank you!

Ruby On Rails Database

Whenever I run the command rvmsudo rake db:migrate (I need to use rvmsudo because I'm using rvm), the sqlite3 files generated are owned by root.
ls -l db/*.sqlite3
Because of this, I keep on getting a SQLite3 Read Only Error whenever I try to do anything to the database, and I have to manually enter the command:
sudo chown -R myusername db/*.sqlite3
rvmsudo rake db:migrate never did this before, and I am wondering why it is happening now.
Did you install RVM as root, and if so, is there a reason it wasn't installed as your normal (less-privileged) user? I use RVM daily, on a number of machines, and haven't had to install RVM as root.

Resources