RVM $PATH variables to homeless user - ruby

I have a staging server running Ubuntu 12.04 and I'm creating some post-receive scripts on git to automate deployment of some repositories we keep on that server.
I created a homeless user 'git' (for security reasons) to deal git's ssh connections.
I want to run compass compile (compass is installed as a ruby gem) when somebody pushes something on the repo.
So I used RVM to install and manage ruby on that server.
And I did all rvm installation process using the multi-user approach.
When I run ruby -v with the 'git' user, I get the right output, I mean, rvm multi-user instalation is almost ok.
The problem is when I try to run an executable gem.
I noticed my normal user's .bash_profile had this (added by rvm):
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Load RVM into a shell session *as a function*
This code add gems' bin directory to the $PATH.
[normaluser]$ echo $PATH
/usr/local/rvm/gems/ruby-1.9.2-p320/bin:/usr/local/rvm/gems/ruby-1.9.2-p320#global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p320/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/rvm/gems/ruby-1.9.2-p320/bin/:/usr/local/rvm/bin
As 'git' user is homeless it doesn't have a .bash_profile so I added that code to a script on /etc/profile.d/
But it didn't worked because every time I try to execute /usr/local/rvm/scripts/rvm as git, it exit's the git's bash session and return to my normal user session. It goes something like:
[normaluser]$ sudo -u git bash
[git]$ exec /usr/local/rvm/scripts/rvm
[normaluser]$
No erros are printed...
Why is rvm script exiting from bash session?
How can I set rvm $PATH on a homeless user?

three issues:
you should not use /bin/sh it is not supported by RVM, use /bin/bash instead (but you got it already
as explained by #Casper => exec replaces current shell with the command you run, there is no return
there are docs describing how to use RVM in init.d => https://rvm.io/integration/init-d
you should not try to source rvm for the user in shell initialization files as they are not used by init.d scripts, use wrappers instead, it's the most proven way to make it work.

Related

Jenkins won't recognize local commands

I'm attempting to integrate my Ruby/Cucumber/Appium test framework with our Jenkins environment.
On the local machine I have installed RVM to utilize ruby 2.4, the newest appium, cucumber, and I'm attempting to use the already-installed ruby gems for my jenkins jobs.
How do I get the Jenkins shell to utilize my local RVM instance? I don't want to have to install rvm, cucumber, and appium every time I run a job.
I have already attempted to add environment variables in the Jenkins settings but it still does not recognize my commands.
Note my admin has deleted the automatically generated jenkins user.
you probably want something like this in "Execute shell":
#!/bin/bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
rvm use 2.4
bundle install
bundle exec rake
you mention that your jenkins user is gone. the above assumes that there is an rvm setup in the $HOME directory of the user running your builds, so if that's not certain, or you don't know what user is executing your builds, you probably want to start there, echo $USER, and so on, and see if rvm works for that user outside of a jenkins build.

RVM + Zsh "RVM is not a function, selecting rubies with 'rvm use ...' will not work"

I'll start by saying I'm not an expert at command-line, so I really only barely know what I'm doing.
I cannot get RVM and oh-my-zsh to play nice together. I've tried several different solutions posted around, some on SO, but nothing I have found seems to fix the issue.
Initially, someone else setup RVM on my machine. I later setup oh-my-zsh myself, and remember having a lot of trouble doing so.
My .zhsrc file is completely default except for these 2 lines
export PATH=$HOME/bin:/usr/local/bin:/usr/local/rvm/bin:$PATH
#pretty sure this line does nothing, although I've seen this fix around
# several places. I noticed that I don't seem to have an .rvm file in my
# home directory which would be an issue, but I have no idea how to
# go about correcting this.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
I added the /usr/local/rvm to the Path directive as zsh returns zsh: rvm command not found without it there. Otherwise most rvm command work, with the exception of rvm use which throws the following error:
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.
I have ensured the profile preferences on my profile in iTerm2 are set to "Login Shell" as recommended by RVM
I have tried changing these preferences to the "Command" option and using the recommended /bin/bash/ --login as the command, however zsh no longer appears to work when this happens. Changing the command to /bin/zsh/ --login allows zsh to work again but does not solve the problem as I still get the "RVM is not a function" error when trying to run rvm use
The default ruby in RVM is set to 2.0.0. I can run ruby -v in my home folder and get the ruby version output which shows the default version.
Working on rails projects, I can run all rails commands IF the Gemfile specifies ruby 2.0.0, the default. However on a project that uses a different version, I get the following error:
Your Ruby version is 2.0.0, but your Gemfile specified 1.9.3
Then I cannot run rvm use 1.9.3 as it returns the above mentioned error.
How can I make zsh play nice with RVM?
Some info about my setup:
Mac OSX 10.9.1 (Mavericks)
iTerm2
Zsh 5.0.2 with oh-my-zsh
RVM 1.5.1
For me, I just had to add
source $HOME/.rvm/scripts/rvm
to my ~/.zshrc and it started working, after having the same error message as in this SO question.
Turns out that RVM was installed in the wrong place. When I had initially installed RVM I believe I did so with the sudo command, which ran the multi-user install (installed to /usr/local/rvm), and not the single user (should be in ~/.rvm/scrips/rvm), which is why all the posted solutions were not working for me.
I had to uninstall RVM with rvm implode. There were some permissions issues, so I had to manually go in and delete the files that could not be deleted. Ran the curl command to re-install RVM and now it's installed in the correct place.
Sourcing RVM in my .zshrc with [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" now works properly and I can switch between rubies.
#trustkr's answer worked for me, but would like to add...
I ran into this issue immediately after install of rvm. In order for the
source
to work in in the ~/.zshrc file to work, you will need either start a new terminal session and work there OR you can run
source ~/.rvm/scripts/rvm
in the same terminal session in which you installed.
[Mac OS 10.10.5 - iTerm - zsh]
I had the same issue added this one line at end of .zshrc file
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
and works now everything works fine on my ubuntu 16.04 operating system :)
You need to run the following:
$ source ~/.rvm/scripts/rvm
then run this
$ type rvm | head -n 1
and the result will be like
rvm is a shell function from /home/sun/.rvm/scripts/cli
Hope that helps!
Another solution :
1) In the terminal run :
which rvm
The output in my case is ---> /usr/share/rvm
2) Open .zshrc file
nano ~/.zshrc
add "source + the_path_to_your_rvm + /scripts/rvm", in my case it's :
source /usr/share/rvm/scripts/rvm
3) save the file (.zshrc)
4) close the terminal and open it again

RBENV resolve duplicate directories for ruby-build

I recently updated my ruby version for the first time since installing rbenv. On my development machine I discovered that I needed to reinstall bundler and all my gems again for each application using bundle install.
Now, when I login to my production server I notice that I have a ruby-build directory in my home folder at ~/ruby-build and in .rbenv/plugins. How can I find out which one my server is using and so which one to run git pull and install the new version of ruby? Can I just delete the directory at ~/ruby-build and deal with the other? Doing some exploring I find I also have a ruby-build in usr/local/bin on my server but this is not a directory.
I am completely confused by all of this. Rbenv lives in my home directory, rather than in any system directory on the server. Does this matter? How does it still work on a server even if I am not logged in?
Running rbenv versions from my home directory on the server reports "The program 'rbenv' is currently not installed" but lists the versions installed on my development laptop.
My .bashrc on the sever contains this entry at the top
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
My .bashrc on my development laptop contains this entry at the bottom.
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Since you include that in your .bashrc, I imagine that if you were to echo $RBENV_ROOT in the terminal you'll find out which rbenv directory is the one being used.
Although, you say that the rbenv command doesn't work so perhaps $RBENV_ROOT is not defined. In that case, you can simply do something like RBENV_ROOT=$HOME/.rbenv/ before that conditional -- or whatever you like -- to set the rbenv directory it should use.
When you do that, to define $RBENV_ROOT, re-login to make the changes take effect, or re-source the .bashrc file with source ~/.bashrc.

I'm getting "RVM is not a function" error on Mac OS X, and no posted solutions work

I'm on Mac OS x 10.8.2 ("Mountain Lion"), and I successfully installed RVM 1.17.8 and its dependencies. I'm able to use it to install Ruby versions using rvm install 1.9.2, but I can't execute rvm use <version> without getting this error:
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.
I have the following in ~/.bash_profile, which I have set to run, using source ~/.bash_profile, whenever a terminal window is launched:
export SVN_EDITOR=vim
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[ -r ~/.bashrc ] && source ~/.bashrc
I've tried running source ~/.rvm/scripts/rvm, which runs without returning an error, but I still get the "RVM is not a function" error when I try to use RVM to set the Ruby version within the terminal session.
I also tried uninstalling and reinstalling RVM using rvm implode. I've run out of ideas and I can't seem to find any more solutions online. Help?
Make sure your shell initialization files are set up properly:
rvm get head --auto-dotfiles
Then go to your terminal emulator preferences and enable login shell, sometimes it it required to use /bin/bash --login.
Also make sure to fully close terminal and open it fresh after changing the setting.
The solution ended up being on the RVM Help & Support page, in the ".bash_profile not being loaded on OSX" section. It seems that the issue was that I had my terminal set to open with the "default login shell" and when I changed it to instead open with the command "/usr/bash" in Preferences/Setup, "source ~/.rvm/scripts/rvm" started working, and rvm is now being recognized as a function.
look for .profile, if exists, remove it! loof for .zshrc, if exists, remove it! Now, use vi to edit your .bash_profile, add this line
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Now, close all console services and open again, make a test using
$ rvm gemset use global
Work fine for me!

How to load the RVM part of user's .bashrc to run Ruby CGI scripts under Apache?

I've configured a new server on Ubuntu 12.04 and I started to use RVM.
I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from
!#/usr/bin/ruby
to
!#/usr/bin/env ruby
However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment (last lines of .bashrc) was not loaded under Apache.
I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users and different paths.
The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:
sudo ln -s /home/apache_user/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby
But I want to know if there's a better solution, because I guess that rvm --default use is better than sudo ln -s.
I am thinking about:
loading the RVM Environment on startup (but I don't know how to achieve that);
loading the RVM Environment for each web request (which can degrade performance, and I don't know how to configure Apache to do that);
maybe the RVM Environment is loaded and all I must do is to guess the name of relevant variables to pass with PassEnv directive. But I doubt that. (Why Apache would run the .bashrc instead of another shell like csh or ksh?)
you can source the ruby environment, I'm not sure if it's enough to source it in $HOME for apache or if you need to modify /etc/init.d/apache2, but the line is:
source /path/to/rvm/environments/<name>
where for <name> you can either use full ruby name or an alias name
You can create aliases with:
rvm alias create veve 1.9.3-p125#my-project
which for RVM installed in /home/app/.rvm will allow you to use:
source /home/app/.rvm/environments/veve
in .bashrc or /etc/init.d/apache2 (just near top, bellow shebang).
you could always do
sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby
That will link the default version of the rvm-managed Ruby to /usr/bin/ruby and you will never have to do anything. set it and forget it.

Resources