Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of #1188 "Serve staticfiles in the webapp if whitenoise is installed" to 0.9.x #1295

Merged
merged 2 commits into from
Sep 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions check-dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@
print "Note that txamqp requires python 2.5 or greater."
warning += 1

# Test for whitenoise
try:
import whitenoise
except ImportError:
print "[INFO]"
print "Unable to import the 'whitenoise' module."
print "This is useful for serving static files."


if fatal:
print "%d necessary dependencies not met. Graphite will not function until these dependencies are fulfilled." % fatal
Expand Down
58 changes: 39 additions & 19 deletions conf/graphite.wsgi.example
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
import os, sys
sys.path.append('/opt/graphite/webapp')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings')
import os
import sys

import django
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module

if django.VERSION < (1, 4):
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'graphite.settings') # noqa

from django.conf import settings
from django.core.wsgi import get_wsgi_application
from graphite.logger import log

application = get_wsgi_application()

try:
import whitenoise
except ImportError:
whitenoise = False
else:
# From 1.4 wsgi support was improved and since 1.7 old style WSGI script
# causes AppRegistryNotReady exception
# https://docs.djangoproject.com/en/dev/releases/1.7/#wsgi-scripts
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
# WhiteNoise < 2.0.1 does not support Python 2.6
if sys.version_info[:2] < (2, 7):
whitenoise_version = tuple(map(
int, getattr(whitenoise, '__version__', '0').split('.')))
if whitenoise_version < (2, 0, 1):
whitenoise = False

if whitenoise:
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
prefix = "/".join((settings.URL_PREFIX.strip('/'), 'static'))
for directory in settings.STATICFILES_DIRS:
application.add_files(directory, prefix=prefix)
for app_path in settings.INSTALLED_APPS:
module = import_module(app_path)
directory = os.path.join(os.path.dirname(module.__file__), 'static')
if os.path.isdir(directory):
application.add_files(directory, prefix=prefix)

# READ THIS
# Initializing the search index can be very expensive, please include
# the WSGIImportScript directive pointing to this script in your vhost
# config to ensure the index is preloaded before any requests are handed
# to the process.
from graphite.logger import log
# Initializing the search index can be very expensive. The import below
# ensures the index is preloaded before any requests are handed to the
# process.
log.info("graphite.wsgi - pid %d - reloading search index" % os.getpid())
import graphite.metrics.search
import graphite.metrics.search # noqa
3 changes: 3 additions & 0 deletions docs/config-local-settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ STATIC_ROOT
alias /opt/graphite/static/;
}

Alternatively, static files can be served directly by the Graphite webapp if
you install the ``whitenoise`` Python package.

DASHBOARD_CONF
`Default: CONF_DIR/dashboard.conf`
The location of the Graphite-web Dashboard configuration
Expand Down
9 changes: 8 additions & 1 deletion examples/example-graphite-vhost.conf
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ WSGISocketPrefix run/wsgi
# file in this directory that you can safely use, just copy it to graphite.wgsi
WSGIScriptAlias / /opt/graphite/conf/graphite.wsgi

# XXX To serve static files, either:
# django-admin.py collectstatic --noinput --settings=graphite.settings
# * Install the whitenoise Python package (pip install whitenoise)
# or
# * Collect static files in a directory by running:
# django-admin.py collectstatic --noinput --settings=graphite.settings
# And set an alias to serve static files with Apache:
Alias /content/ /opt/graphite/webapp/content/
<Location "/content/">
SetHandler None
Expand All @@ -57,4 +64,4 @@ WSGISocketPrefix run/wsgi
Allow from all
</Directory>

</VirtualHost>
</VirtualHost>
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ sphinx
sphinx_rtd_theme
cairocffi
git+git://github.com/graphite-project/[email protected]#egg=whisper
whitenoise