#!/bin/sh # Shell library for service installation and general tools # Author: Christoph Helma, Andreas Maunz # Functions to install ruby and install services with bundler. # Ensures presence of ~/.opentox (CONFIG) and OT_PREFIX. # create a temporary directory in OT_PREFIX and a log directory in .opentox check_dest() { [ -d "$OT_PREFIX/tmp" ] || mkdir -p "$OT_PREFIX/tmp" if ! [ -d "$OT_PREFIX/tmp" ]; then echo "Could not create OT_PREFIX directory! Aborting..." exit 1 fi [ -d "$HOME/.opentox/log" ] || mkdir -p "$HOME/.opentox/log" if ! [ -d "$HOME/.opentox/log" ]; then echo "Could not create CONFIG (~/.opentox) directory! Aborting..." exit 1 fi } # export a LOG file name check_log() { local name="$1" if [ -z "$LOG" ]; then LOG="$OT_PREFIX/tmp/$name.log" echo printf "\033[1m%s\033[m\n" "$name ('$LOG'):" echo fi } # run a shell command # Signal exit status by formatted string # @param cmd command to execute # @param title textual description of command # @example { # run_cmd "ls" "list dir" # } run_cmd () { local cmd="$1"; local title="$2" printf "%50s" "$title" if ! eval "$cmd" >>$LOG 2>&1 ; then printf " [ \033[31m%s\033[m ]\n" "FAIL" echo "Last 10 lines of log:" tail -10 "$LOG" exit 1 fi printf " [ \033[32m%s\033[m ]\n" "OK" } # stop if required binaries not in path # @param list of binaries # @example { # check_utils "git curl" # } check_utils() { for u in $1; do UPATH=`which $u` if [ "$?" -eq 1 ]; then echo "'$u' missing. Install '$u' first." 1>&2 && exit 1 fi eval `echo $u | tr "[:lower:]" "[:upper:]" | tr "-" "_"`=$UPATH done } # install ruby using rbenv # configure the version in config.sh install_ruby() { printf "\n%50s\n" "RUBY (v. '$RUBY_NUM_VER')" local DIR=`pwd` check_utils "rbenv curl make tar" if ! $RBENV versions $RUBY_NUM_VER | grep $RUBY_NUM_VER>/dev/null 2>&1; then [ -d $OT_PREFIX/tmp ] || mkdir -p $OT_PREFIX/tmp && cd $OT_PREFIX/tmp ([ -d $OT_PREFIX/tmp/ruby-$RUBY_NUM_VER ] || $CURL $RUBY_DWL/ruby-$RUBY_NUM_VER.tar.gz 2>/dev/null | $TAR xz) && cd ruby-$RUBY_NUM_VER cmd="./configure --prefix=$HOME/.rbenv/versions/$RUBY_NUM_VER" && run_cmd "$cmd" "Configure" cmd="$MAKE -j2" && run_cmd "$cmd" "Make" cmd="$MAKE install" && run_cmd "$cmd" "Install" fi cd $DIR cmd="$RBENV rehash" && run_cmd "$cmd" "Rbenv rehash" if [ "$1" = "global" ]; then cmd="$RBENV global $RUBY_NUM_VER" && run_cmd "$cmd" "Rbenv set ruby global" else if ! $RBENV global | grep $RUBY_NUM_VER>/dev/null 2>&1; then cmd="$RBENV local $RUBY_NUM_VER" && run_cmd "$cmd" "Rbenv set ruby local" fi fi } # install a ruby gem using bundler install_with_bundler() { printf "\n%50s\n" "INSTALL" check_utils "gem rbenv" $GEM list | grep bundler >/dev/null 2>&1 || (cmd="$GEM install bundler" && run_cmd "$cmd" "Install bundler") cmd="$RBENV rehash" && run_cmd "$cmd" "Rbenv rehash" check_utils "gem rbenv bundle" cmd="$BUNDLE install" && run_cmd "$cmd" "Install using bundler" cmd="$RBENV rehash" && run_cmd "$cmd" "Rbenv rehash" } # download opentox git repo ot_git_download(){ printf "\n%50s\n" "GIT DOWNLOAD" check_utils "git" cd $OT_PREFIX cmd="$GIT clone git@github.com:opentox/$SERVICE.git" && run_cmd "$cmd" "Downloading $SERVICE git repository" } # emit notification if caller was the shell (the user), see http://goo.gl/grCOk notify() { echo echo "Installation finished (check above for errors)" echo if ps -o stat= -p $PPID | grep "s" >/dev/null 2>&1; then echo "IMPORTANT: How to configure your system if everything went fine:" echo "IMPORTANT: bash shell only: '. $OT_TOOLS_CONF; otconfig' (convenient ot-tools)" echo "IMPORTANT: other shells: '. $OT_UI_CONF' (no ot-tools available)" echo "IMPORTANT: More information: 'http://opentox.github.com/General/2012/08/09/install-opentox-development-environment'" echo "IMPORTANT: Thank you for your attention." fi } # Force loading configuration from local, if we are installing for the first time if [ -z "$OT_PREFIX" ]; then . ./config.sh else . $HOME/.opentox/config/install/config.sh fi check_dest touch "$OT_UI_CONF" . "$OT_UI_CONF" 2>/dev/null