How do I use 'rvm use' with puppet? - ruby

I'm trying to use Puppet to set up RVM on a variety of systems. Everything works fine until I try to specify which Ruby to use.
Running rvm use 1.9.3 with a Puppet exec yields an error, because 'rvm is not a function', since Puppet's exec forces all commands to be fully qualified.
How would I use Puppet to set the system Ruby through RVM? Is this even possible?

When you install rvm you need to source rvm.sh in order to get it working right away. The exact path to this file is usually disclosed in the installation messages.

You are getting good error message, it tells you that RVM can not be used interactively. This means even if RVM ignored the problem and set current ruby it would make no sense because running RVM as binary is a separate execution of shell which will not be able to set parent process (the shell / puppet) environment. To be able to set environment RVM has to be loaded as function in shell so it can change environment of current process.
So there are few ways to make it work:
subshell with multiple commands:
bash -c "source ~/.rvm/scripts/rvm ; rvm ..."
RVM set operation:
~/.rvm/bin/rvm {ruby-name} do {command}...
Some operations do not require above tricks (like setting default ruby):
~/.rvm/bin/rvm alias create default {ruby-name}
An extra explanation - default ruby is not a system ruby, it is a ruby that will be loaded when you source RVM, if you aim for availability of ruby in multiple places use alias and wrappers:
rvm alias create {my_app} {ruby-version}
rvm wrapp {ruby-version} --no-links --all
PATH=~/.rvm/environments/{my_app}:$PATH
This will create:
an alias - so it is easy to reference application ruby and no changes are needed in scripts to change ruby - just update alias
create wrappers for all gems installed in that ruby - includes wrappers for ruby and gem commands
add the PATH=... on top of any script that should work with ruby for your application.

Related

Issue with creating a gemset via Chef - .ruby-gemset and .ruby-version not getting created

I have included rvm --create --ruby-version ruby-2.3.3#storesview in my recipe to create a gemset. Running the kitchen converge does not result in the creation of .ruby-gemset and .ruby-version. But when I run rvm --create --ruby-version ruby-2.3.3#storesview directly on the VM terminal, the files are getting created. What could be the reason? Thanks.
Here's what my bash code looks like :
bash "somename" do
user 'root'
cwd "/var/www/html/APIStoresView/"
code <<-EOH
rvm --create --ruby-version ruby-2.3.3#storesview
EOH
end
In general use of rvm on servers should be discouraged, and Chef+rvm is dicey at the best of times. More specifically, appbundler (used by Chef to create marginally bullet proof binary stubs) sets a lot of environment variables that can confuse rvm.
If you need a specific version of Ruby for a project, I would highly recommend using poise-ruby+poise-ruby-build and Bundler rather than rvm and gemsets.

Run "rvm alias create default 2.0" command?

I have a Chef recipe which installs RVM and Ruby. in the recipe I am using the execute resource to run the following command:
rvm alias create default 2.0
but this does not set my default Ruby version to 2.0. If I run this command directly on the host it does work because when I type ruby -v the version is set to 2.0 but when I try to run it through the Chef recipe ruby -v still returns 1.8.
This is my resource declaration:
execute "set_ruby_to_version_#{version}" do
command "rvm alias create default #{version}"
action :run
end
How come it doesn't work through the Chef recipe? I know that there is a cookbook available to deal with RVM but I still want to know why this is not working.
since rvm 1.25 rvm does not create wrappers in $rvm_path/bin if you depend on this wrappers (it's already in your PATH) then run this after creating alias:
rvm wrapper default --no-prefix
if you need a wrapper for specific gem add it's name after the command, ex:
rvm wrapper default --no-prefix haml
it will create only this wrapper: $rvm_path/bin/haml
other approach would be adding the automatically generated wrappers to PATH:
PATH=$rvm_path/gems/ruby-2.1.0/wrappers:$PATH
rvm alias create default 2.1.2
rvm use 2.1.2
Works for me.

does rbenv set the PATH variable to point to the correct ruby?

I installed rbenv and it seems and copied 2 versions of ruby to ~/.rbenv/versions, it shows
them both correctly.
When I run
rbenv global 1.8.7-p72
It says ok and also points to it. However when I run--
ruby --version
I get ruby not found. Basically, the ruby executable is not found in the path. Can anyone help me with this problem?
My .bash_profile is
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv versions outputs --
1.8.7-p72
*1.9.3-p448 ( set by /home/user/.rbenv/version)
Also my ~/.rbenv/shims directory doesn't have a ruby executable. It has erb, gem,irb , rake, rdoc , ri and testrb. Could this be the issue?
I'm sorry I'm completely clueless
Did you execute all the step in the installation instructions https://github.com/sstephenson/rbenv? I.e. did you add the required snippets to your shell startup (.bash_profile) and so forth?
If you followed the instructions, could you provide the output of rbenv versions and which -a ruby for a start?
EDIT:
rbenv rehash seems to be what generates the shims. This must be executed every time you install a new ruby version. How did you install the rubies? With ruby-build, as recommended in the installation instructions?
When I encountered this error, all I had to do was to make sure that $PATH had shims prior to system version of ruby like so:
~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

how to add rvm to super user

hi i need to execute a ruby program from a init script i need to run the script as
sudo /etc/bin/differ.sh start
but the problem is rvm is not installed on root(super user) . so code in the differ script ruby "filename" wont execute because the gems and rvm is not installed in root.
i need solve how to add rvm and its gems to root (i have not installed by system wide installation).i have even tried the rvmsudo
rvmsudo ruby "filename"
but it's not working in script works good in terminal.
please free to ask any files or outputs i will post along with this question.
This page on integrating RVM with init.d may help: https://rvm.io/integration/init-d

How to use Bundler with path specified Ruby version

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

Resources