#!/usr/bin/env bash
__rvm_pkgs=( sed grep tar gzip bzip2 bash curl git-core )
__mri_pkgs=( gcc-c++ patch zlib zlib-devel libffi-devel sqlite3-devel libxml2-devel libxslt-devel libreadline5 readline-devel libopenssl-devel )
__rvm_ruby_default="ruby-1.9.2-p180"
__cookbooks_path_default="/var/chef-solo"
log() { printf "===> $*\n" ; return $? ; }
fail() { log "\nERROR: $*\n" ; exit 1 ; }
usage() {
printf "
Usage
suse_chef_bootstrap.sh [options] [action]
Options
--rvm-version
- Install RVM version [head|latest|x.y.z]
--rvm-ruby - Install a specific RVM ruby
Actions
help - Display CLI help (this output)
"
}
install_rvm_prerequisites() {
log "Installing RVM pre-requisite packages..."
zypper --non-interactive install -l "${__rvm_pkgs[@]}"
}
install_mri_prerequisites() {
log "Installing RVM MRI ruby pre-requisite packages..."
zypper --non-interactive install -l "${__mri_pkgs[@]}"
}
install_rvm() {
local flags=""
if [[ -n "$__rvm_version" ]] ; then flags=" --version $__rvm_version" ; fi
if [[ -n "$__rvm_branch" ]] ; then flags=" --branch $__rvm_branch" ; fi
log "Installing RVM..."
bash <(curl -LB "http://rvm.beginrescueend.com/install/rvm") $flags
source /etc/profile.d/rvm.sh
}
install_rvm_ruby() {
if [[ -z "$__rvm_ruby" ]] ; then __rvm_ruby="$__rvm_ruby_default" ; fi
if rvm list strings | grep -q "^ruby-1.9.2-p180" >/dev/null ; then
log "RVM ruby $__rvm_ruby is already installed, so skipping."
else
log "Install RVM ruby ${__rvm_ruby}..."
rvm install $__rvm_ruby
fi
rvm $__rvm_ruby --default
}
compile_sqlite() {
local tar="sqlite-autoconf-3070500.tar.gz"
local dl_dir="/tmp/sqlite-$$"
log "Downloading sqlite3...."
mkdir -p $dl_dir
curl -L "http://www.sqlite.org/$tar" -o "$dl_dir/$tar"
log "Extracting sqlite3..."
(cd $dl_dir && tar -zxf "$dl_dir/$tar")
log "Configuring sqlite3..."
(cd $dl_dir/${tar%.tar.gz} && ./configure)
log "Compiling and installing sqlite3..."
(cd $dl_dir/${tar%.tar.gz} && make && make install)
}
ensure_current_sqlite() {
local current_version=$(sqlite3 --version)
local need_upgrade=1
if [[ $(echo $current_version | cut -d\. -f1) -gt 3 ]] ; then
need_upgrade=0
elif [[ $(echo $current_version | cut -d\. -f1) -eq 3 ]] ; then
if [[ $(echo $current_version | cut -d\. -f2) -gt 6 ]] ; then
need_upgrade=0
elif [[ $(echo $current_version | cut -d\. -f2) -eq 6 ]] ; then
if [[ $(echo $current_version | cut -d\. -f3) -ge 16 ]] ; then
need_upgrade=0
fi
fi
fi
if [[ $need_upgrade -eq 1 ]] ; then
log "Version of sqlite found was too old, so building one..."
compile_sqlite
else
log "Version of sqlite is good."
fi
}
install_chef_gem() {
log "Installing chef gem..."
rvm $__rvm_ruby gem install chef --no-ri --no-rdoc
}
clone_chef_repo() {
if [[ -z "$__cookbooks_path" ]] ; then
__cookbooks_path="$__cookbooks_path_default"
fi
log "Cloning chef repo into $__cookbooks_path..."
mkdir -p $(dirname $__cookbooks_path)
git clone git://github.com/fnichol/chef-repo.git $__cookbooks_path
}
installation_complete() {
printf "
Installation is complete.
"
}
# Parse CLI arguments
while [[ $# -gt 0 ]] ; do
token="$1" ; shift
case "$token" in
--rvm-version)
case "$1" in
*.*.*)
__rvm_version="$1" ; shift
;;
latest|stable)
__rvm_version="latest"
;;
head|master)
__rvm_version="head"
__rvm_branch="master"
;;
*)
fail "--version must be followed by a valid version number x.y.z"
;;
esac
;;
--rvm-ruby)
__rvm_ruby="$1"
;;
help|usage)
usage
exit 0
;;
*)
usage
exit 1
;;
esac
done
# Perform the actual installation
install_rvm_prerequisites
install_rvm
install_mri_prerequisites
ensure_current_sqlite
install_rvm_ruby
install_chef_gem
clone_chef_repo
installation_complete
exit 0