简体   繁体   中英

How do I use 'rvm use' with puppet?

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:

  1. subshell with multiple commands:

     bash -c "source ~/.rvm/scripts/rvm ; rvm ..." 
  2. RVM set operation:

     ~/.rvm/bin/rvm {ruby-name} do {command}... 
  3. 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:

  1. an alias - so it is easy to reference application ruby and no changes are needed in scripts to change ruby - just update alias
  2. create wrappers for all gems installed in that ruby - includes wrappers for ruby and gem commands
  3. add the PATH=... on top of any script that should work with ruby for your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM