Я застрял при первом развертывании приложения колбы на Elastic Beanstalk.Не удается установить соединение с mongodb с ошибкой «mongoengine.connection.MongoEngineConnectionError: Вы не определили соединение по умолчанию».
Подробности
Приложение использует колбу, Connexion for API config, mongoengine в качестве клея mongodb.
connexion==1.1.15
Flask==1.0.1
mongoengine==0.15.0
pymongo==3.6.1
PyJWT==1.6.1
Показанный здесь пример был развернут на Python 3.4 (но приложение работает на Python 3.6 и локально, и на Elastic Beanstalk с тем же набором проблем)
То же приложение прекрасно работает влокальная среда или в EC2 (Amazon Linux AMI) при запуске в качестве приложения Python (с настройкой Apache ProxyPass).
Вот соответствующий код:
__init __. py
import logging.config
import connexion
import mongoengine as me
from api_server import encoder
app = connexion.App(__name__, specification_dir='./config/')
app.app.json_encoder = encoder.JSONEncoder
app.add_api('api_config.yaml', arguments={'title': 'My App'})
logging.config.fileConfig("./config/logging.conf")
# set the WSGI application callable to allow using uWSGI:
# uwsgi --http :8080 -w app
def main():
db = me.connect(alias="default", host="mongodb+srv://*[DB URL]*
if __name__ == '__main__':
main()
application.py
import logging
import sys
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, '/opt/python/current/app/')
from api_server import app as application
Структура каталогов
Base Dir(Elastic Benstalk Root dir)
-- application.py
-- api_server
-----__init__.py
-----config
--------app_config.ymal
wsgi.conf apache mod_wsgi configсозданный утилитой ElasticBeanstalk на сервере:
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
<VirtualHost *:80>
Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /opt/python/current/app/application.py
<Directory /opt/python/current/app/>
Require all granted
</Directory>
WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%{GROUP} \
python-home=/opt/python/run/venv/ \
python-path=/opt/python/current/app:/opt/python/run/venv/lib64/python3.4/site-packages:/opt/python/run/venv/lib/python3.4/site-packages user=wsgi group=wsgi \
home=/opt/python/current/app
WSGIProcessGroup wsgi
</VirtualHost>
LogFormat "%h (%{X-Forwarded-For}i) %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
Приложение инициализируется и обслуживает базовый ответ на запрос, но только когда ему требуется подключение к mongodb, оно выдает следующую ошибку:
[Sun May 27 09:37:30.839005 2018] [:error] [pid 16181] Traceback (most recent call last):
[Sun May 27 09:37:30.839064 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 2292, in wsgi_app
[Sun May 27 09:37:30.839125 2018] [:error] [pid 16181] response = self.full_dispatch_request()
[Sun May 27 09:37:30.839174 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1815, in full_dispatch_request
[Sun May 27 09:37:30.839227 2018] [:error] [pid 16181] rv = self.handle_user_exception(e)
[Sun May 27 09:37:30.839276 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1718, in handle_user_exception
[Sun May 27 09:37:30.839325 2018] [:error] [pid 16181] reraise(exc_type, exc_value, tb)
[Sun May 27 09:37:30.839378 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/_compat.py", line 35, in reraise
[Sun May 27 09:37:30.839426 2018] [:error] [pid 16181] raise value
[Sun May 27 09:37:30.839472 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1813, in full_dispatch_request
[Sun May 27 09:37:30.839513 2018] [:error] [pid 16181] rv = self.dispatch_request()
[Sun May 27 09:37:30.839544 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/flask/app.py", line 1799, in dispatch_request
[Sun May 27 09:37:30.839574 2018] [:error] [pid 16181] return self.view_functions[rule.endpoint](**req.view_args)
[Sun May 27 09:37:30.839604 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/decorator.py", line 66, in wrapper
[Sun May 27 09:37:30.839633 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839661 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/validation.py", line 122, in wrapper
[Sun May 27 09:37:30.839708 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839739 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/validation.py", line 293, in wrapper
[Sun May 27 09:37:30.839769 2018] [:error] [pid 16181] return function(request)
[Sun May 27 09:37:30.839798 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/decorator.py", line 42, in wrapper
[Sun May 27 09:37:30.839827 2018] [:error] [pid 16181] response = function(request)
[Sun May 27 09:37:30.839856 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/connexion/decorators/parameter.py", line 218, in wrapper
[Sun May 27 09:37:30.839885 2018] [:error] [pid 16181] return function(**kwargs)
[Sun May 27 09:37:30.839913 2018] [:error] [pid 16181] File "/opt/python/current/app/api_server/auth/auth_service.py", line 21, in wrapper
[Sun May 27 09:37:30.839942 2018] [:error] [pid 16181] queryObj= CampaignOwner.objects(campaignOwnerId=g.campaign_owner_id)
[Sun May 27 09:37:30.839971 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/queryset/manager.py", line 37, in __get__
[Sun May 27 09:37:30.840000 2018] [:error] [pid 16181] queryset = queryset_class(owner, owner._get_collection())
[Sun May 27 09:37:30.840029 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/document.py", line 191, in _get_collection
[Sun May 27 09:37:30.840058 2018] [:error] [pid 16181] db = cls._get_db()
[Sun May 27 09:37:30.840086 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/document.py", line 180, in _get_db
[Sun May 27 09:37:30.840115 2018] [:error] [pid 16181] return get_db(cls._meta.get('db_alias', DEFAULT_CONNECTION_NAME))
[Sun May 27 09:37:30.840144 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/connection.py", line 226, in get_db
[Sun May 27 09:37:30.840173 2018] [:error] [pid 16181] conn = get_connection(alias)
[Sun May 27 09:37:30.840201 2018] [:error] [pid 16181] File "/opt/python/run/venv/local/lib/python3.4/site-packages/mongoengine/connection.py", line 146, in get_connection
[Sun May 27 09:37:30.840230 2018] [:error] [pid 16181] raise MongoEngineConnectionError(msg)
[Sun May 27 09:37:30.840261 2018] [:error] [pid 16181] mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection
Needпомочь выяснить, что идет не так.1. Чего-то не хватает в конфигурации WSGI?2. Пробовал с помощью flask-mongoengine, app.config ["MONGODB_SETTING"] с тем же результатом, есть ли другой способ установить настройки БД?3. Как mongo engine / pymongo считывает настройки по умолчанию 4. Есть ли какие-либо другие проблемы в том, как я написал код запуска, который ведет к этому.
Пожалуйста, помогите с любой информацией, которую вы можете.Попытался найти всю возможную информацию, но не смог исправить проблему.