在OpenShift可扩展应用程序上运行Django 1.8

时间:2016-01-20 01:57:24

标签: django openshift

我在使用python 2.7在可扩展的OpenShift平台上运行Django(1.8.7)应用程序时遇到问题。由于某种原因,它无法获得环境变量OPENSHIFT_POSTGRESQL_DB_URL的信息。这是错误:

 mod_wsgi (pid=69306): Target WSGI script '/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/application' cannot be loaded as Python module.
 mod_wsgi (pid=69306): Exception occurred processing WSGI script '/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/application'.
 Traceback (most recent call last):
   File "/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/application", line 21, in <module>
     application = get_wsgi_application()
   File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/core/wsgi.py", line 14, in get_wsgi_application
     django.setup()
   File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/__init__.py", line 17, in setup
     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
   File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
     self._setup(name)
   File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/conf/__init__.py", line 44, in _setup
     self._wrapped = Settings(settings_module)
   File "/var/lib/openshift/5d5/python/virtenv/lib/python2.7/site-packages/Django-1.8.7-py2.7.egg/django/conf/__init__.py", line 92, in __init__
     mod = importlib.import_module(self.SETTINGS_MODULE)
   File "/opt/rh/python27/root/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
     __import__(name)
   File "/var/lib/openshift/5d5/app-root/runtime/repo/wsgi/bet/controller/settings.py", line 59, in <module>
     url = urlparse.urlparse(os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL'))
   File "/opt/rh/python27/root/usr/lib64/python2.7/urlparse.py", line 143, in urlparse
     tuple = urlsplit(url, scheme, allow_fragments)
   File "/opt/rh/python27/root/usr/lib64/python2.7/urlparse.py", line 182, in urlsplit
     i = url.find(':')
 AttributeError: 'NoneType' object has no attribute 'find'

这就是我在之前使用过的settings.py中使用该变量的方法:

import urlparse
DATABASES = {}
if ON_OPENSHIFT:
    url = urlparse.urlparse(os.environ.get('OPENSHIFT_POSTGRESQL_DB_URL'))

    DATABASES['default'] = {
        'ENGINE' : 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ['OPENSHIFT_APP_NAME'],
        'USER': url.username,
        'PASSWORD': url.password,
        'HOST': url.hostname,
        'PORT': url.port,
        }

这是我的application文件:

#!/usr/bin/env python

import os
import sys

sys.path.append(os.path.join(os.environ['OPENSHIFT_REPO_DIR'], 'wsgi', 'bet'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'controller.settings'
virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/'
virtualenv = os.path.join(virtenv, 'bin/activate_this.py')
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except:
    pass

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

如果我通过rhc ssh bet登录我的应用并尝试打印变量,则可以正常运行:

echo $OPENSHIFT_POSTGRESQL_DB_URL
postgresql://admin:password-mynick.rhcloud.com:port/

想一想我错过了什么。我看起来here没有运气。

注意:我的应用名为bet。但是,由于我将其转移到可扩展的应用程序,我在openshift中创建了abet应用程序。我在bet文件夹中将应用名称保留为wsgi。这可能导致一些问题吗?

2 个答案:

答案 0 :(得分:0)

好吧,看起来像bug中的某种OpenShift。我试图重新启动/重新加载应用程序,没有运气。

$ rhc app stop -a appname
$ rhc app start -a appname

停止并启动应用程序使其正常工作。 (重载环境变量)

答案 1 :(得分:0)

使用此链接

OpenShift Community

耐心等待,他们会联系并帮助你。

别忘了查看openshift日志文件夹

$ rhc ssh app-name

$ cd $OPENSHIFT_LOG_DIR

检查该文件夹中的日志文件,然后可以为您的问题提供一些有用的信息,您可以更好地描述它以支持团队。