Alternative deployment.
authorŁukasz Rekucki <lrekucki@gmail.com>
Tue, 11 May 2010 10:51:33 +0000 (12:51 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Tue, 11 May 2010 10:51:33 +0000 (12:51 +0200)
deployment.py [new file with mode: 0644]
wolnelektury.vhost.tmpl [new file with mode: 0644]
wolnelektury.wsgi.tmpl [new file with mode: 0644]

diff --git a/deployment.py b/deployment.py
new file mode 100644 (file)
index 0000000..b677fdf
--- /dev/null
@@ -0,0 +1,64 @@
+#!/srv/library/wolnelektury/pythonenv/bin/python
+from __future__ import with_statement
+
+import shutil
+import os
+import sys
+
+from string import Template
+
+def render_template(source, dest, context={}):
+    print "Rendering template:",
+    with open(source, 'rb') as source_file:
+        t = Template(source_file.read())
+    with open(dest, 'wb') as dest_file:
+        dest_file.write(t.safe_substitute(context))
+    print "done."
+
+def restart_wsgi():
+    print "Restarting wsgi application:",
+    os.system("touch %s" % WSGI_TARGET)
+    print "done."
+
+def update_application():
+    print "Updating repository.",
+    os.system("cd %s; git pull" % PROJECT_ROOT)
+    print "Installing requirements"
+    os.system("pip install -r %s" % os.path.join(PROJECT_ROOT, 'requirements.txt'))
+    print "Installing local requirements"
+    os.system("pip install -r %s" % os.path.join(ROOT, 'etc', 'requirements.txt'))
+    print "done."
+
+ROOT = os.path.dirname(os.path.abspath(__file__))
+
+PYTHON = os.path.join(ROOT, 'pythonenv', 'bin', 'python')
+PYTHON_SITE = os.path.join(ROOT, 'pythonenv', 'lib', 'python2.6', 'site-packages')
+
+PROJECT_NAME = 'redakcja'
+PROJECT_ROOT = os.path.join(ROOT, 'application')
+
+MEDIA_ROOT = os.path.join(ROOT, 'www', 'media')
+
+ADMIN_EMAIL = 'lrekucki@gmail.com'
+
+WSGI_TARGET = os.path.join(ROOT, 'www', 'wsgi', PROJECT_NAME + '.wsgi')
+WSGI_DIR = os.path.dirname(WSGI_TARGET)
+
+WSGI_USER = PROJECT_NAME
+WSGI_PROCESSES = 5
+WSGI_THREADS = 1
+
+DOMAIN = 'lektury.staging.nowoczesnapolska.org.pl'
+
+#
+# Load local configuration
+#
+sys.path = [ os.path.join(ROOT, 'etc') ] + sys.path
+
+from local_deployment import *
+
+if __name__ == '__main__':
+    update_application()
+    render_template(os.path.join(PROJECT_ROOT, PROJECT_NAME + '.wsgi.tmpl'), WSGI_TARGET, context=globals())
+    render_template(os.path.join(PROJECT_ROOT, PROJECT_NAME + '.vhost.tmpl'), os.path.join(ROOT, 'etc', PROJECT_NAME + '.vhost'), context=globals())
+    restart_wsgi()
diff --git a/wolnelektury.vhost.tmpl b/wolnelektury.vhost.tmpl
new file mode 100644 (file)
index 0000000..fbd21a9
--- /dev/null
@@ -0,0 +1,25 @@
+<VirtualHost *:80>
+    ServerName $DOMAIN
+    ServerAlias $DOMAIN_ALIASES
+    ServerAdmin $ADMIN_EMAIL
+
+    WSGIDaemonProcess $PROJECT_NAME user=$WSGI_USER group=$WSGI_USER processes=$WSGI_PROCESSES threads=$WSGI_THREADS display-name=%{GROUP}
+    WSGIProcessGroup $PROJECT_NAME
+
+    WSGIScriptAlias / $WSGI_TARGET
+    <Directory $WSGI_DIR>
+        Order allow,deny
+        allow from all
+    </Directory>
+
+    Alias /media $MEDIA_ROOT
+    <Directory $MEDIA_ROOT >
+        Options Indexes, FollowLinks
+        Order allow,deny
+        Allow from all
+    </Directory>
+
+    LogLevel warn
+    ErrorLog /var/log/apache2/$PROJECT_NAME/error.log
+    CustomLog /var/log/apache2/$PROJECT_NAME/access.log combined
+</VirtualHost>
diff --git a/wolnelektury.wsgi.tmpl b/wolnelektury.wsgi.tmpl
new file mode 100644 (file)
index 0000000..6b772f3
--- /dev/null
@@ -0,0 +1,24 @@
+#!$PYTHON
+import site
+site.addsitedir('$PYTHON_SITE')
+
+import os
+from os.path import abspath, dirname, join
+import sys
+
+# Redirect sys.stdout to sys.stderr for bad libraries like geopy that use
+# print statements for optional import exceptions.
+sys.stdout = sys.stderr
+
+# Add apps and lib directories to PYTHONPATH
+sys.path = [
+    '$PROJECT_ROOT',
+       '$PROJECT_ROOT/lib',
+       '$PROJECT_ROOT/apps',
+] + sys.path
+
+# Run Django
+os.environ['DJANGO_SETTINGS_MODULE'] = '$PROJECT_NAME.settings'
+
+from django.core.handlers.wsgi import WSGIHandler
+application = WSGIHandler()