create-dev-env.sh 6.82 KB
Newer Older
John Jarvis committed
1 2
#!/bin/bash
set -e
3
trap "ouch" ERR
John Jarvis committed
4

5 6 7 8 9 10 11 12 13 14 15 16 17 18
ouch() {
    printf '\E[31m'

    cat<<EOL
    
    !! ERROR !!

    The last command did not complete successfully, 
    see $LOG for more details or trying running the
    script again with the -v flag.  

EOL

}
John Jarvis committed
19
error() {
John Jarvis committed
20
      printf '\E[31m'; echo "$@"; printf '\E[0m' 
John Jarvis committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
}
output() {
      printf '\E[36m'; echo "$@"; printf '\E[0m' 
}
usage() {
    cat<<EO

    Usage: $PROG [-c] [-v] [-h]
    
            -c        compile scipy and numpy
            -v        set -x + spew
            -h        this

EO
    info
}

info() {

    cat<<EO
    MITx base dir : $BASE 
    Python dir : $PYTHON_DIR
    Ruby dir : $RUBY_DIR
    Ruby ver : $RUBY_VER

EO
John Jarvis committed
47
}
John Jarvis committed
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
clone_repos() {
    cd "$BASE"
    output "Cloning mitx"
    if [[ -d "$BASE/mitx" ]]; then
        mv "$BASE/mitx" "${BASE}/mitx.bak.$$"
    fi
    git clone git@github.com:MITx/mitx.git >>$LOG 
    output "Cloning askbot-devel"
    if [[ -d "$BASE/askbot-devel" ]]; then
        mv "$BASE/askbot-devel" "${BASE}/askbot-devel.bak.$$"
    fi
    git clone git@github.com:MITx/askbot-devel >>$LOG 
    output "Cloning data"
    if [[ -d "$BASE/data" ]]; then
        mv "$BASE/data" "${BASE}/data.bak.$$"
    fi
    hg clone ssh://hg-content@gp.mitx.mit.edu/data >>$LOG 
}
John Jarvis committed
67 68 69 70 71 72 73 74

PROG=${0##*/}
BASE="$HOME/mitx_all"
PYTHON_DIR="$BASE/python"
RUBY_DIR="$BASE/ruby"
RUBY_VER="1.9.3"
NUMPY_VER="1.6.2"
SCIPY_VER="0.10.1"
75
BREW_FILE="$BASE/mitx/brew-formulas.txt"
John Jarvis committed
76
LOG="/var/tmp/install.log"
77
APT_PKGS="curl git mercurial python-virtualenv build-essential python-dev gfortran liblapack-dev libfreetype6-dev libpng12-dev libxml2-dev libxslt-dev yui-compressor coffeescript"
John Jarvis committed
78

John Jarvis committed
79 80 81 82 83
if [[ $EUID -eq 0 ]]; then
    error "This script should not be run using sudo or as the root user"
    usage
    exit 1
fi
John Jarvis committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
ARGS=$(getopt "cvh" "$*")
if [[ $? != 0 ]]; then
    usage
    exit 1
fi
eval set -- "$ARGS"
while true; do 
    case $1 in 
        -c)
            compile=true
            shift
            ;;
        -v)
            set -x
            verbose=true
            shift
            ;;
        -h)
            usage
            exit 0
            ;;
        --)
            shift
            break
            ;;
    esac
done

cat<<EO

  This script will setup a local MITx environment, this
  includes

       * Django
       * A local copy of Python and library dependencies
       * A local copy of Ruby and library dependencies

  It will also attempt to install operating system dependencies
  with apt(debian) or brew(OSx).

  To compile scipy and numpy from source use the -c option

126 127 128 129
  STDOUT is redirected to /var/tmp/install.log, run
  $ tail -f /var/tmp/install.log
  to monitor progress

John Jarvis committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
EO
info
output "Press return to begin or control-C to abort"
read dummy

if [[ -f $HOME/.rvmrc ]]; then
    output "$HOME/.rvmrc alredy exists, not adding $RUBY_DIR"
else
    output "Creating $HOME/.rmrc so rvm uses $RUBY_DIR"
    echo "export rvm_path=$RUBY_DIR" > $HOME/.rvmrc
fi
mkdir -p $BASE
rm -f $LOG
case `uname -s` in
    [Ll]inux)
        command -v lsb_release &>/dev/null || {
            error "Please install lsb-release."
            exit 1
        }
        distro=`lsb_release -cs`
        case $distro in
            lisa|natty|oneiric|precise)
                output "Installing ubuntu requirements"
                sudo apt-get -y update
                sudo apt-get -y install $APT_PKGS 
155
                clone_repos
John Jarvis committed
156 157 158 159
                ;;
            *)
                error "Unsupported distribution - $distro"
                exit 1
160
               ;;
John Jarvis committed
161 162 163 164 165
        esac
        ;;
    Darwin)
        command -v brew &>/dev/null || { 
            output "Installing brew"
166
            /usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)" 
John Jarvis committed
167
        } 
168 169 170 171 172 173 174 175 176 177 178
        command -v git &>/dev/null || {
            output "Installing git"
            brew install git >> $LOG
        }
        command -v hg &>/dev/null || {
            output "Installaing mercurial"
            brew install mercurial >> $LOG
        }

        clone_repos

John Jarvis committed
179
        output "Installing OSX requirements"
180 181 182 183
        if [[ ! -r $BREW_FILE ]]; then
            error "$BREW_FILE does not exist, needed to install brew deps"
            exit 1
        fi
John Jarvis committed
184
        # brew errors if the package is already installed
185
        for pkg in $(cat $BREW_FILE); do
John Jarvis committed
186 187 188 189 190 191 192 193 194 195 196 197 198
            grep $pkg <(brew list) &>/dev/null || {
                output "Installing $pkg"
                brew install $pkg  >>$LOG 
            }
        done
        command -v pip &>/dev/null || {
            output "Installing pip"
            sudo easy_install pip  >>$LOG 
        }
        command -v virtualenv &>/dev/null || {
            output "Installing virtualenv"
            sudo pip install virtualenv virtualenvwrapper >> $LOG
        }
John Jarvis committed
199 200 201 202 203
        command -v coffee &>/dev/null || {
            output "Installing coffee script"
            curl http://npmjs.org/install.sh | sh
            npm install -g coffee-script
        }
John Jarvis committed
204 205 206 207 208 209
        ;;
    *)
        error "Unsupported platform"
        exit 1
        ;;
esac
210 211 212 213 214 215 216 217 218 219

output "Installing rvm and ruby"
curl -sL get.rvm.io | bash -s stable
source $RUBY_DIR/scripts/rvm
rvm install $RUBY_VER
virtualenv "$PYTHON_DIR"
source $PYTHON_DIR/bin/activate
output "Installing gem bundler"
gem install bundler
output "Installing ruby packages"
220 221
# hack :(
cd $BASE/mitx  || true
222 223 224 225
bundle install

cd $BASE

John Jarvis committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
if [[ -n $compile ]]; then
    output "Downloading numpy and scipy"
    curl -sL -o numpy.tar.gz http://downloads.sourceforge.net/project/numpy/NumPy/${NUMPY_VER}/numpy-${NUMPY_VER}.tar.gz
    curl -sL -o scipy.tar.gz http://downloads.sourceforge.net/project/scipy/scipy/${SCIPY_VER}/scipy-${SCIPY_VER}.tar.gz
    tar xf numpy.tar.gz
    tar xf scipy.tar.gz
    rm -f numpy.tar.gz scipy.tar.gz
    output "Compiling numpy"
    cd "$BASE/numpy-${NUMPY_VER}"
    python setup.py install >>$LOG  2>&1
    output "Compiling scipy"
    cd "$BASE/scipy-${SCIPY_VER}"
    python setup.py install >>$LOG  2>&1
    cd "$BASE"
    rm -rf numpy-${NUMPY_VER} scipy-${SCIPY_VER}
fi

output "Installing askbot requirements"
pip install -r askbot-devel/askbot_requirements.txt >>$LOG 
pip install -r askbot-devel/askbot_requirements_dev.txt >>$LOG 
output "Installing MITx requirements"
pip install -r mitx/pre-requirements.txt >> $LOG
pip install -r mitx/requirements.txt >>$LOG 

John Jarvis committed
250 251
mkdir "$BASE/log" || true
mkdir "$BASE/db" || true
John Jarvis committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

cat<<END
   
   Success!!

   To start using Django you will need
   to activate the local Python and Ruby
   environment:

        $ source $RUBY_DIR/scripts/rvm
        $ source $PYTHON_DIR/bin/activate
  
   To initialize and start a local instance of Django:
        
        $ cd $BASE/mitx
        $ django-admin.py syncdb --settings=envs.dev --pythonpath=.
        $ django-admin.py migrate --settings=envs.dev --pythonpath=.
        $ django-admin.py runserver --settings=envs.dev --pythonpath=.   
       
END
exit 0