Skip to content

Instantly share code, notes, and snippets.

@jelder
Created May 25, 2012 02:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelder/2785410 to your computer and use it in GitHub Desktop.
Save jelder/2785410 to your computer and use it in GitHub Desktop.
Get the specificed version of Ruby onto a Linux host by any means necessary, and cache it in S3.
#!/bin/bash
# Get the specificed version of Ruby onto a Linux host by any means necessary. Invoke like this:
#
# curl https://raw.github.com/gist/2785410/install_ruby.sh | bash -s -- -r '1.9.3-p125' -b mybucket
#
# Jacob Elder <jacob@boundless.com>
usage() {
echo "Usage: $(basename $0) -r 1.9.3-p125 -b mybucket"
exit 1
}
pull_or_clone() {
if [ -d $2 ] ; then
( cd $2 ; git pull | grep -v 'Already up-to-date.' || true )
else
git clone $1 $2
fi
}
# The exit code for `s3cmd ls` is not meaningful.
s3_file_exists() {
s3cmd ls $1 | grep -q $1
}
setup() {
tempdir=$(mktemp -d /tmp/install_ruby.XXXXXXXX)
trap "rm -rf $tempdir" INT EXIT
cd $HOME
# We're going to either download it from S3, or build it locally and save it
# to S3. For this, we'll need a filename describing this environment.
os=$(uname -s)
machine=$(uname -m)
case $os in
"Darwin")
echo "This doesn't really support Darwin just yet."
#exit 1
release=$(sw_vers -productVersion)
;;
"Linux")
. /etc/lsb-release
os=${DISTRIB_ID}
release=${DISTRIB_RELEASE}
;;
*)
echo "Don't know what to do with $os."
exit 1
;;
esac
archive="ruby-${ruby_version}_${os}_${release}_${machine}.tar.bz2"
s3_path="s3://${bucket}/Ruby/${archive}"
# Check that s3cmd works
s3cmd ls s3://${bucket} 2>&1 > /dev/null
if [[ $? -ne 0 ]] ; then
echo "s3cmd isn't installed or configured!"
exit 1
fi
}
save_state() {
echo "Caching this Ruby environment at $s3_path"
tar --create --bzip2 --directory $HOME --file $tempdir/$archive .rbenv
s3cmd put $tempdir/$archive $s3_path
}
while getopts "hr:b:s" OPTION ; do
case $OPTION in
h)
usage
;;
r)
ruby_version=$OPTARG
;;
b)
bucket=$OPTARG
;;
s)
save_only=1
;;
esac
done
if [ -z $ruby_version ] || [ -z $bucket ] ; then
usage
fi
setup
if [ ! -z $save_only ] ; then
save_state
exit
fi
pull_or_clone git://github.com/sstephenson/rbenv.git ~/.rbenv
pull_or_clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
export PATH=$HOME/.rbenv/bin:$PATH
eval "$(~/.rbenv/bin/rbenv init -)"
if [ ! -d ".rbenv/versions/${ruby_version}" ] ; then
if s3_file_exists $s3_path ; then
# It exists, use it.
s3cmd get $s3_path $tempdir
echo "Extracting archived Ruby environment"
tar --extract --bzip2 --directory $HOME --file $tempdir/$archive
else
echo "gem: --no-ri --no-rdoc" > .gemrc
rbenv install $ruby_version
rbenv rehash
rbenv global $ruby_version
rbenv exec gem install bundler
rbenv rehash
save_state
fi
fi
rm -f ~/rbenv.sh
echo 'export PATH=$HOME/.rbenv/bin:$PATH' >> ~/rbenv.sh
echo 'eval "$(rbenv init -)"' >> ~/rbenv.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment