Skip to content

Commit

Permalink
Make it possible to install on OpenShift
Browse files Browse the repository at this point in the history
Requires Python 2.7 Cartrige with Postgres database
  • Loading branch information
vstoykov committed Sep 12, 2013
1 parent 1fce60c commit 588870d
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .openshift/action_hooks/build
@@ -0,0 +1,8 @@
#!/bin/bash
# This is a simple build script and will be executed on your CI system if
# available. Otherwise it will execute while your application is stopped
# before the deploy step. This script gets executed directly, so it
# could be python, php, ruby, etc.

# Activate VirtualEnv in order to use the correct libraries
echo "Execute 'build' script"
24 changes: 24 additions & 0 deletions .openshift/action_hooks/deploy
@@ -0,0 +1,24 @@
#!/bin/bash
# This deploy hook gets executed after dependencies are resolved and the
# build hook has been run but before the application has been started back
# up again. This script gets executed directly, so it could be python, php,
# ruby, etc.
echo "Execute 'deploy' script"

MANAGE="python ${OPENSHIFT_REPO_DIR}manage.py"
SYNC_DB="$MANAGE syncdb --migrate --noinput"
COLLECTSTATIC="$MANAGE collectstatic --noinput"

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rh/postgresql92/root/usr/lib64/

echo "Copying settings_local..."
cp "${OPENSHIFT_REPO_DIR}venelin/settings_openshift.py" "${OPENSHIFT_REPO_DIR}settings_local.py"

echo "Activate enviroment"
source $OPENSHIFT_HOMEDIR/python/virtenv/bin/activate

echo "Executing '$SYNC_DB'"
$SYNC_DB

echo "Executing '$COLLECTSTATIC'"
$COLLECTSTATIC
8 changes: 8 additions & 0 deletions .openshift/action_hooks/post_deploy
@@ -0,0 +1,8 @@
#!/bin/bash
# This is a simple post deploy hook executed after your application
# is deployed and started. This script gets executed directly, so
# it could be python, php, ruby, etc.
echo "Execute 'post_deploy' script"

echo "Link static folders"
ln -s ${OPENSHIFT_DATA_DIR}static ${OPENSHIFT_REPO_DIR}wsgi/static
6 changes: 6 additions & 0 deletions .openshift/action_hooks/pre_build
@@ -0,0 +1,6 @@
#!/bin/bash
# This is a simple script and will be executed on your CI system if
# available. Otherwise it will execute while your application is stopped
# before the build step. This script gets executed directly, so it
# could be python, php, ruby, etc.
echo "Execute 'pre_build' script"
4 changes: 4 additions & 0 deletions .openshift/action_hooks/pre_start_python
@@ -0,0 +1,4 @@
#!/bin/bash
echo "Execute pre_start_python"

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/rh/postgresql92/root/usr/lib64/
62 changes: 62 additions & 0 deletions app.py
@@ -0,0 +1,62 @@
#!/usr/bin/env python
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "venelin.settings")

from django.core.wsgi import get_wsgi_application
from django.conf import settings
from dj_static import Cling

IP = os.environ.get('OPENSHIFT_PYTHON_IP', '0.0.0.0')
PORT = int(os.environ.get('OPENSHIFT_PYTHON_PORT', 8080))


class MediaCling(Cling):
"""
This class is copied from https://github.com/kennethreitz/dj-static/
When this changes came to pypi then this class can be imported from there
"""
def __init__(self, application, base_dir=None):
super(MediaCling, self).__init__(application, base_dir=base_dir)
# override callable attribute with method
self.debug_cling = self._debug_cling

def _debug_cling(self, environ, start_response):
environ = self._transpose_environ(environ)
return self.cling(environ, start_response)

def get_base_dir(self):
return settings.MEDIA_ROOT

def get_base_url(self):
return settings.MEDIA_URL


def run_gevent_server(app, ip='0.0.0.0', port=8080):
from gevent.pywsgi import WSGIServer
http_address = '%s:%s' % (ip, port)
print("Start gevent wsgi server at %s" % http_address)
WSGIServer((ip, port), app).serve_forever()


def run_simple_wsgi_server(app, ip='0.0.0.0', port=8080):
from wsgiref.simple_server import make_server
http_address = '%s:%s' % (ip, port)
print("Start WSGI server at %s" % http_address)
make_server(ip, port, app).serve_forever()


def main():
application = MediaCling(Cling(get_wsgi_application()))
try:
run_gevent_server(application, IP, PORT)
except ImportError:
run_simple_wsgi_server(application, IP, PORT)


if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print("\nServer stopped")
30 changes: 30 additions & 0 deletions openshiftlibs.py
@@ -0,0 +1,30 @@
#!/usr/bin/env python
import hashlib, os


def get_openshift_secret_token():
"""
Gets the secret token provided by OpenShift
or generates one (this is slightly less secure, but good enough for now)
"""
token = os.getenv('OPENSHIFT_SECRET_TOKEN')
name = os.getenv('OPENSHIFT_APP_NAME')
uuid = os.getenv('OPENSHIFT_APP_UUID')
if token is not None:
return token
elif (name is not None and uuid is not None):
return hashlib.sha256(name + '-' + uuid).hexdigest()
return None


def openshift_secure(value):
"""
Pass a value and try to secure it with openshift secret token if available
"""
secret_token = get_openshift_secret_token()

if secret_token:
return hashlib.sha256(secret_token + '-' + value).hexdigest()
return value
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,7 +1,7 @@
Django>=1.5.2
Pillow>=2.1.0
South>=0.8.2
django-extensions>=1.1.1
django-extensions>=1.2.0
django-tinymce>=1.5.1
django-disqus>=0.4.1
django-imagekit>=3.0.3
Expand Down
26 changes: 26 additions & 0 deletions setup.py
@@ -0,0 +1,26 @@
#!/usr/bin/env python

from setuptools import setup

setup(
name='venelins_site',
version='1.0',
description='venelin.sytes.net',
author='Venelin Stoykov',
author_email='vkstoykov@gmail.com',
url='http://venelin.sytes.net/',
install_requires=[
'Django>=1.5.2',
'Pillow>=2.1.0',
'South>=0.8.2',
'django-extensions>=1.2.0',
'django-tinymce>=1.5.1',
'django-disqus>=0.4.1',
'django-imagekit>=3.0.3',
'django-admin-tools',
'pygments',
'gevent',
'dj-static',
'psycopg2',
],
)
36 changes: 36 additions & 0 deletions venelin/settings_openshift.py
@@ -0,0 +1,36 @@
import os
from openshiftlibs import openshift_secure

DATA_DIR = os.environ['OPENSHIFT_DATA_DIR']

STATIC_ROOT = os.path.join(DATA_DIR, 'static/')
MEDIA_ROOT = os.path.join(DATA_DIR, 'media/')

DEBUG = False
TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = [
'*',
]

DATABASES = {
'sqlite': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(DATA_DIR, 'site.db'),
},
'postgres': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['OPENSHIFT_APP_NAME'],
'USER': os.environ['OPENSHIFT_POSTGRESQL_DB_USERNAME'],
'PASSWORD': os.environ['OPENSHIFT_POSTGRESQL_DB_PASSWORD'],
'HOST': os.environ['OPENSHIFT_POSTGRESQL_DB_HOST'],
'PORT': os.environ['OPENSHIFT_POSTGRESQL_DB_PORT'],
}
}
DATABASES['default'] = DATABASES['postgres']

DISQUS_WEBSITE_SHORTNAME = 'venelin'

SERVER_EMAIL = DEFAULT_FROM_EMAIL = 'webmaster@venelin.sytes.net'

SECRET_KEY = openshift_secure('qi!k%l+n@hs8l8%)t@j2bl6_jj_x2q-g^em=i!6m17(7x1^$9r')
2 changes: 1 addition & 1 deletion robots.txt → venelin/templates/robots.txt
@@ -1,4 +1,4 @@
Sitemap: http://venelin.sytes.net/sitemap.xml
Sitemap: http://{{ request.get_host }}/sitemap.xml

User-agent: *
Disallow: /static/
Expand Down
3 changes: 2 additions & 1 deletion venelin/urls.py
Expand Up @@ -19,6 +19,8 @@
(r'^highlighter/', include('syntaxhighlighter.urls')),

(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),
(r'^robots\.txt$', 'django.shortcuts.render', {'template_name': 'robots.txt', 'content_type': 'text/plain; charset=utf-8'}),
(r'^(?P<path>favicon\.ico)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

(r'^', include('pages.urls')),
)
Expand All @@ -33,5 +35,4 @@
urlpatterns = patterns('django.views.static',
url(r'^%s/(?P<path>.*)$' % settings.MEDIA_URL.strip('/'), 'serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'^(?P<path>favicon\.ico)$', 'serve', {'document_root': settings.STATIC_ROOT}),
) + staticfiles_urlpatterns() + urlpatterns

0 comments on commit 588870d

Please sign in to comment.