create-dev-env.sh 7.9 KB
Newer Older
1
#!/usr/bin/env bash
John Jarvis committed
2
set -e
3 4 5 6 7 8 9

# posix compliant sanity check
if [ -z $BASH ] || [  $BASH = "/bin/sh" ]; then
    echo "Please use the bash interpreter to run this script"
    exit 1
fi

10
trap "ouch" ERR
John Jarvis committed
11

12 13 14 15
ouch() {
    printf '\E[31m'

    cat<<EOL
16

17 18
    !! ERROR !!

19
    The last command did not complete successfully,
20
    For more details or trying running the
21 22
    script again with the -v flag.

23
    Output of the script is recorded in $LOG
24 25

EOL
26
    printf '\E[0m'
27 28

}
John Jarvis committed
29
error() {
30
      printf '\E[31m'; echo "$@"; printf '\E[0m'
John Jarvis committed
31 32
}
output() {
33
      printf '\E[36m'; echo "$@"; printf '\E[0m'
John Jarvis committed
34 35 36 37 38
}
usage() {
    cat<<EO

    Usage: $PROG [-c] [-v] [-h]
39

John Jarvis committed
40
            -c        compile scipy and numpy
41
            -s        give access to global site-packages for virtualenv
John Jarvis committed
42 43 44 45 46 47 48 49 50 51
            -v        set -x + spew
            -h        this

EO
    info
}

info() {

    cat<<EO
52
    MITx base dir : $BASE
John Jarvis committed
53 54 55 56 57
    Python dir : $PYTHON_DIR
    Ruby dir : $RUBY_DIR
    Ruby ver : $RUBY_VER

EO
John Jarvis committed
58
}
John Jarvis committed
59

60 61
clone_repos() {
    cd "$BASE"
62

63
    if [[ -d "$BASE/mitx/.git" ]]; then
64 65
        output "Pulling mitx"
        cd "$BASE/mitx"
66
        git pull
67 68 69 70 71
    else
        output "Cloning mitx"
        if [[ -d "$BASE/mitx" ]]; then
            mv "$BASE/mitx" "${BASE}/mitx.bak.$$"
        fi
72
        git clone git@github.com:MITx/mitx.git
73
    fi
74

75
    # By default, dev environments start with a copy of 6.002x
76
    cd "$BASE"
77
    mkdir -p "$BASE/data"
78 79 80 81
    REPO="content-mit-6002x"
    if [[ -d "$BASE/data/$REPO/.git" ]]; then
        output "Pulling $REPO"
        cd "$BASE/data/$REPO"
82
        git pull
83
    else
84 85 86
        output "Cloning $REPO"
        if [[ -d "$BASE/data/$REPO" ]]; then
            mv "$BASE/data/$REPO" "${BASE}/data/$REPO.bak.$$"
87
        fi
88
	cd "$BASE/data"
89
        git clone git@github.com:MITx/$REPO
90
    fi
91
}
John Jarvis committed
92

93 94
### START

John Jarvis committed
95 96 97 98 99
PROG=${0##*/}
BASE="$HOME/mitx_all"
PYTHON_DIR="$BASE/python"
RUBY_DIR="$BASE/ruby"
RUBY_VER="1.9.3"
100
LOG="/var/tmp/install-$(date +%Y%m%d-%H%M%S).log"
John Jarvis committed
101

102 103 104

# Read arguments

John Jarvis committed
105 106 107 108 109
if [[ $EUID -eq 0 ]]; then
    error "This script should not be run using sudo or as the root user"
    usage
    exit 1
fi
110
ARGS=$(getopt "cvhs" "$*")
John Jarvis committed
111 112 113 114 115
if [[ $? != 0 ]]; then
    usage
    exit 1
fi
eval set -- "$ARGS"
116 117
while true; do
    case $1 in
John Jarvis committed
118 119 120 121
        -c)
            compile=true
            shift
            ;;
122 123 124 125
        -s)
            systempkgs=true
            shift
            ;;
John Jarvis committed
126 127 128 129 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 155
        -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

John Jarvis committed
156
  !!! Do not run this script from an existing virtualenv !!!
157

John Jarvis committed
158
  If you are in a ruby/python virtualenv please start a new
159
  shell.
John Jarvis committed
160

John Jarvis committed
161 162 163 164 165
EO
info
output "Press return to begin or control-C to abort"
read dummy

166 167 168

# Log all stdout and stderr

169 170 171
exec > >(tee $LOG)
exec 2>&1

172 173

# Install basic system requirements
John Jarvis committed
174

John Jarvis committed
175 176 177 178 179 180 181
mkdir -p $BASE
case `uname -s` in
    [Ll]inux)
        command -v lsb_release &>/dev/null || {
            error "Please install lsb-release."
            exit 1
        }
182

John Jarvis committed
183 184
        distro=`lsb_release -cs`
        case $distro in
185
            maya|lisa|natty|oneiric|precise|quantal)
186
                sudo apt-get install git
John Jarvis committed
187 188 189 190
                ;;
            *)
                error "Unsupported distribution - $distro"
                exit 1
191
               ;;
John Jarvis committed
192 193
        esac
        ;;
194

195
    Darwin)
196 197
        if [[ ! -w /usr/local ]]; then
            cat<<EO
198 199

        You need to be able to write to /usr/local for
200 201 202 203 204 205 206
        the installation of brew and brew packages.

        Either make sure the group you are in (most likely 'staff')
        can write to that directory or simply execute the following
        and re-run the script:

        $ sudo chown -R $USER /usr/local
John Jarvis committed
207
EO
208 209 210 211 212

            exit 1

        fi

213
        command -v brew &>/dev/null || {
John Jarvis committed
214
            output "Installing brew"
215
            /usr/bin/ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
216
        }
217 218
        command -v git &>/dev/null || {
            output "Installing git"
219
            brew install git
220 221
        }

John Jarvis committed
222 223 224 225 226 227
        ;;
    *)
        error "Unsupported platform"
        exit 1
        ;;
esac
228

229 230 231 232 233 234 235 236 237 238 239 240 241

# Clone MITx repositories

clone_repos


# Install system-level dependencies

bash $BASE/mitx/install-system-req.sh


# Install Ruby RVM

242
output "Installing rvm and ruby"
243 244 245 246 247 248 249 250 251 252

if ! grep -q "export rvm_path=$RUBY_DIR" ~/.rvmrc; then
    if [[ -f $HOME/.rvmrc ]]; then
        output "Copying existing .rvmrc to .rvmrc.bak"
        cp $HOME/.rvmrc $HOME/.rvmrc.bak
    fi
    output "Creating $HOME/.rvmrc so rvm uses $RUBY_DIR"
    echo "export rvm_path=$RUBY_DIR" > $HOME/.rvmrc
fi

253
curl -sL get.rvm.io | bash -s -- --version 1.15.7
254
source $RUBY_DIR/scripts/rvm
255
LESS="-E" rvm install $RUBY_VER --with-readline
256

257 258
output "Installing gem bundler"
gem install bundler
259

260
output "Installing ruby packages"
261 262
# hack :(
cd $BASE/mitx  || true
263 264
bundle install

265 266 267 268 269 270 271 272 273 274 275 276

# Install Python virtualenv

output "Installing python virtualenv"

case `uname -s` in
    Darwin)
        # Add brew's path
        PATH=/usr/local/share/python:/usr/local/bin:$PATH
        ;;
esac

277 278 279 280 281 282 283 284
if [[ $systempkgs ]]; then
    virtualenv --system-site-packages "$PYTHON_DIR"
else
    # default behavior for virtualenv>1.7 is
    # --no-site-packages
    virtualenv  "$PYTHON_DIR"
fi

285
# activate mitx python virtualenv
286
source $PYTHON_DIR/bin/activate
287

288 289 290 291 292
# compile numpy and scipy if requested

NUMPY_VER="1.6.2"
SCIPY_VER="0.10.1"

John Jarvis committed
293 294 295 296 297 298 299 300 301
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}"
302
    python setup.py install
John Jarvis committed
303 304
    output "Compiling scipy"
    cd "$BASE/scipy-${SCIPY_VER}"
305
    python setup.py install
John Jarvis committed
306 307 308 309
    cd "$BASE"
    rm -rf numpy-${NUMPY_VER} scipy-${SCIPY_VER}
fi

310 311 312 313 314 315 316 317 318 319 320 321 322
case `uname -s` in
    Darwin)
        # on mac os x get the latest distribute and pip
        curl http://python-distribute.org/distribute_setup.py | python
        pip install -U pip
        # need latest pytz before compiling numpy and scipy
        pip install -U pytz
        pip install numpy
        # fixes problem with scipy on 10.8
        pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev
        ;;
esac

323
output "Installing MITx pre-requirements"
324 325
pip install -r $BASE/mitx/pre-requirements.txt

326
output "Installing MITx requirements"
327 328
# Need to be in the mitx dir to get the paths to local modules right
cd $BASE/mitx
329
pip install -r requirements.txt
330

John Jarvis committed
331 332
mkdir "$BASE/log" || true
mkdir "$BASE/db" || true
John Jarvis committed
333

334 335 336

# Configure Git

337 338 339
output "Fixing your git default settings"
git config --global push.default current

340 341 342

### DONE

John Jarvis committed
343 344 345
cat<<END
   Success!!

346
   To start using Django you will need to activate the local Python
347
   and Ruby environment (at this time rvm only supports bash) :
John Jarvis committed
348 349 350

        $ source $RUBY_DIR/scripts/rvm
        $ source $PYTHON_DIR/bin/activate
351

352
   To initialize Django
353

John Jarvis committed
354
        $ cd $BASE/mitx
355 356 357 358 359 360
        $ rake django-admin[syncdb]
        $ rake django-admin[migrate]

   To start the Django on port 8000

        $ rake lms
361

362 363
   Or to start Django on a different <port#>

364
        $ rake django-admin[runserver,lms,dev,<port#>]
365

366
  If the  Django development server starts properly you
367 368 369 370 371
  should see:

      Development server is running at http://127.0.0.1:<port#>/
      Quit the server with CONTROL-C.

372
  Connect your browser to http://127.0.0.1:<port#> to
373 374 375
  view the Django site.


John Jarvis committed
376 377
END
exit 0