Skip to content

Instantly share code, notes, and snippets.

@jeffmccune
Created April 19, 2012 04:30
Show Gist options
  • Save jeffmccune/2418574 to your computer and use it in GitHub Desktop.
Save jeffmccune/2418574 to your computer and use it in GitHub Desktop.
Ruby Virtual Environment

Assumptions

  • The "system ruby" is installed from the distribution packages
  • Puppet is not anywhere in $LOAD_PATH except for the portion specified by the RUBYLIB environment variable, pointing at a VCS working copy.
  • The same for Facter, MCollective, any software produced by Puppet Labs.
  • Ruby Gems is not installed in the system paths. (We'll install it in a user path later on so we need the system to be nice and clean)
  • A Bourne style shell is used by the account that will start jobs. (Bash or ZSH)

Setup

Make sure ruby-rdoc is installed. yum install ruby-rdoc.

Clone this repository somewhere, or unpack the zipball from Github if you don't have Git installed.

Gemset Alpha

Activate some gemset:

root@pe-centos5:~# GEMSET=alpha source /vagrant/src/set_runtime/bin/activate
Created /root/gemsets/alpha
(alpha)root@pe-centos5:~#

Ruby Gems 1.3.1 is what ships in EPEL, so that's what we're going to install.

(alpha)$ wget http://production.cf.rubygems.org/rubygems/rubygems-1.3.1.tgz
(alpha)$ tar xvzf rubygems-1.3.1.tgz
(alpha)$ ruby setup.rb --prefix=$VIRTUAL_ENV

Now install some gem.

(alpha)root@pe-centos5:~# gem install rspec-core --version=2.8.0 --no-ri --no-rdoc
Successfully installed rspec-core-2.8.0
1 gem installed
(alpha)root@pe-centos5:~# gem list
*** LOCAL GEMS ***
rspec-core (2.8.0)
(alpha)root@pe-centos5:~# which ruby
/usr/bin/ruby
(alpha)root@pe-centos5:~# ruby --version
ruby 1.8.5 (2006-08-25) [i386-linux]
(alpha)root@pe-centos5:~# which gem
/root/gemsets/alpha/bin/gem
(alpha)root@pe-centos5:~# which rspec
/root/gemsets/alpha/gems/bin/rspec
(alpha)root@pe-centos5:~# rspec --version
2.8.0

Gemset Beta

Now switch to another shell and repeat the process, but with rspec 2.9.0 instead of 2.8.0 and GEMSET=beta instead of GEMSET=alpha

$ ssh root@pe-centos5
Last login: Wed Apr 18 21:21:54 2012 from 172.16.214.1
root@pe-centos5:~# GEMSET=beta source /vagrant/src/set_runtime/bin/activate
Created /root/gemsets/beta
(beta)root@pe-centos5:~# cd rubygems-1.3.1
(beta)root@pe-centos5:~/rubygems-1.3.1# ruby setup.rb --prefix=$VIRTUAL_ENV
...
RubyGems installed the following executables:
  /root/gemsets/beta/bin/gem
(beta)root@pe-centos5:~# gem install rspec-core --version=2.9.0 --no-ri --no-rdoc
Successfully installed rspec-core-2.9.0
1 gem installed
(beta)root@pe-centos5:~# gem list
*** LOCAL GEMS ***
rspec-core (2.9.0)
rspec-mocks (2.9.0)
(beta)root@pe-centos5:~# which ruby
/usr/bin/ruby
(beta)root@pe-centos5:~# ruby --version
ruby 1.8.5 (2006-08-25) [i386-linux]
(beta)root@pe-centos5:~# which gem
/root/gemsets/beta/bin/gem
(beta)root@pe-centos5:~# which rspec
/root/gemsets/beta/gems/bin/rspec
(beta)root@pe-centos5:~# rspec --version
2.9.0

EOF

# This file copied from the Virtual Env project. (MIT Licensed)
# The default gemset
: ${GEMSET:=default_gemset}
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
# reset old environment variables
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "$_OLD_VIRTUAL_RUBYLIB" ] ; then
RUBYLIB="$_OLD_VIRTUAL_RUBYLIB"
export RUBYLIB
unset _OLD_VIRTUAL_RUBYLIB
fi
if [ -n "$_OLD_VIRTUAL_GEM_HOME" ] ; then
GEM_HOME="$_OLD_VIRTUAL_GEM_HOME"
export GEM_HOME
unset _OLD_VIRTUAL_GEM_HOME
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
hash -r
fi
if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi
unset VIRTUAL_ENV
if [ ! "$1" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}
# unset irrelavent variables
deactivate nondestructive
VIRTUAL_ENV="${HOME}/gemsets/${GEMSET:-global}"
export VIRTUAL_ENV
# Create the directory if it doesn't exist.
if ! [[ -d "${VIRTUAL_ENV}" ]]; then
mkdir -p "${VIRTUAL_ENV}"
echo "Created ${VIRTUAL_ENV}"
fi
_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/gems/bin:$VIRTUAL_ENV/bin:$PATH"
export PATH
_OLD_VIRTUAL_RUBYLIB="$RUBYLIB"
RUBYLIB="$VIRTUAL_ENV/lib"
export RUBYLIB
_OLD_VIRTUAL_GEM_HOME="$GEM_HOME"
GEM_HOME="$VIRTUAL_ENV/gems"
export GEM_HOME
if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
fi
fi
export PS1
fi
# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
hash -r
fi
# vim:ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment