Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

devops: make project Heroku deployable with gunicorn/gevent

  • Loading branch information...
commit 219b1dfe878a4839250b24e342eab4d9bae4dd86 1 parent 30d76c4
Yaniv Aknin yaniv-aknin authored
14 .profile.d/activate_vendor_tree.sh
View
@@ -0,0 +1,14 @@
+#!sh
+
+export PATH="/app/vendor/bin:$PATH"
+export CFLAGS="-I/app/vendor/include"
+export LDFLAGS="-L/app/vendor/lib -R/app/vendor/lib"
+export LD_LIBRARY_PATH="/app/vendor/lib:$LD_LIBRARY_PATH"
+
+python_path_segment="/app/vendor/lib/python2.7/dist-packages:/app/vendor/lib/python2.7/dist-packages"
+if [ -z "$PYTHONPATH" ]; then
+ export PYTHONPATH="$python_path_segment"
+else
+ export PYTHONPATH="$python_path_segment:$PYTHONPATH"
+fi
+unset python_path_segment
2  .profile.d/shell_preferences.sh
View
@@ -0,0 +1,2 @@
+export EDITOR=tee # makes pasting text in IPython easier
+export PAGER=more # no less on Heroku :(
1  Procfile
View
@@ -0,0 +1 @@
+web: gunicorn --bind 0.0.0.0:$PORT --config config/gunicorn application:application
5 application.py
View
@@ -0,0 +1,5 @@
+from config import settings
+from config.log import setup_logging
+from backend.app import initialize_app
+setup_logging()
+application = initialize_app(settings)
16 bin/post_compile
View
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+[ "`uname`" = "Darwin" ] && SED_FLAG='-l' || SED_FLAG='-u'
+indent() {
+ RE="s/^/ /"
+ sed $SED_FLAG "$RE"
+}
+
+# argparse is part of Python 2.7, but some of our dependencies install it anyway; this causes ugly warnings
+pip --quiet uninstall --yes argparse
+
+# we need vendored binaries to run our code, and our code must run to do things like building assets
+. .profile.d/activate_vendor_tree.sh
+
+echo "---->> Building webassets"
+./manage.py assets build 2>&1 | indent
36 bin/pre_compile
View
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+if [ -z "$DEBUG" ]; then
+ echo ' ERROR: unable to find DEBUG environment variable!'
+ echo ' (did you run "heroku labs:enable user-env-compile" for this app?)'
+ exit 1
+fi
+
+vendor() {
+ printf " "
+ echo -n "$1 "
+ curl --location --fail --silent $1 | tar -zx -C vendor && echo OK
+}
+
+echo " (lines by the app will be prefixed with ---->>)"
+echo "---->> Fetching vendored binaries"
+mkdir -p vendor
+vendor "http://fusic.s3.amazonaws.com/executables/heroku/coffee-script-1.4.0.tar.gz"
+vendor "http://fusic.s3.amazonaws.com/executables/heroku/libpcre3-dev-7.8-3build1.tar.gz"
+vendor "http://fusic.s3.amazonaws.com/executables/heroku/node-0.8.17.tar.gz"
+
+if ! grep -q psycopg2 requirements.txt; then
+ echo "---->> Injecting psycopg2 requirement to requirements.txt"
+ echo psycopg2==2.4.6 >> requirements.txt
+fi
+
+echo "---->> Injecting shell environment"
+# Even though Heroku's Python buildback has a hook mechanism, hooks can't change the buildpack's process environment
+# (they are spawned, not sourced). This makes it possible to write hooks in any language, but makes vendoring stuff
+# that should be linked against libraries installed during the rest of the build process harder. The kludge below
+# hijacks $BIN_DIR/steps/pylibmc, which is sourced after this code and before pip is ran. Puked a little in my mouth.
+# See also: https://twitter.com/aknin/status/290832546260979712
+cat > $BIN_DIR/steps/pylibmc << EOF
+echo "---->> Injected environment hook executing"
+source .profile.d/activate_vendor_tree.sh
+EOF
9 config/gunicorn
View
@@ -0,0 +1,9 @@
+#!python
+from config.cdn import get_cache
+
+preload=True
+worker_class="gevent"
+workers=8
+worker_connections=8
+def on_starting(server):
+ get_cache(force_rebuild=True)
3  requirements.txt
View
@@ -10,6 +10,9 @@ URLObject==2.3.1
Werkzeug==0.8.3
boto==2.8.0
clint==0.3.1
+gevent==0.13.8
+greenlet==0.4.0
+gunicorn==0.17.2
ipython==0.13.1
pyScss==1.1.4
pycrypto==2.6
23 runcommands.sh
View
@@ -16,4 +16,27 @@ export SECRET_KEY="development"
# local runcommands
[ -f "${root}"/runcommands.local.sh ] && source runcommands.local.sh
+# functions
+function cloud_setup() {
+ [ -z "$1" ] && { echo "usage: $0 <setup-name>"; return 1; }
+ echo "Switching this environment to use $1 resources"
+ set -a
+ source <(heroku config -s -a $1 < /dev/null)
+ set +a
+ if [ "$SECRET_KEY" = "development" ]; then
+ warning "Failed getting setup configuration or setup has invalid SECRET_KEY variable"
+ return 1
+ fi
+ source <(python << EOF
+import urlobject
+url = urlobject.URLObject("$DATABASE_URL")
+def out(k, v): print("export %s=%s" % (k,v))
+out("PGHOST", url.hostname)
+out("PGUSER", url.username)
+out("PGPASSWORD", url.password)
+out("PGDATABASE", url.path[1:])
+EOF
+)
+}
+
unset root
1  runtime.txt
View
@@ -0,0 +1 @@
+python-2.7.3
Please sign in to comment.
Something went wrong with that request. Please try again.