首页 > 解决方案 > Django migrate 不会在 Elastic Beanstalk 上创建任何迁移

问题描述

正如标题所说。这是我的.ebextestions/django.config(当然不包括在内.gitignore

container_commands:
    01_migrate:
        command: "source /var/app/venv/*/bin/activate python3 manage.py migrate --noinput"
        leader_only: true
    02_collectstatic:
        command: "source /var/app/venv/*/bin/activate python3 manage.py collectstatic --noinput"
    03_createsu:
        command: "source /var/app/venv/*/bin/activate python3 manage.py createsu"
        leader_only: true

option_settings:
    aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: "myApp.settings"
        PYTHONPATH: "/var/app/venv/staging-LQM1lest/bin:$PYTHONPATH"
    aws:elasticbeanstalk:container:python:
        WSGIPath: "myApp.wsgi:application"
    aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static: static

packages:
  yum:
    git: []
    python3-devel: []
    mariadb-devel: []

setting.py文件

# [...]

if 'RDS_HOSTNAME' in os.environ:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': os.environ['RDS_DB_NAME'],
            'USER': os.environ['RDS_USERNAME'],
            'PASSWORD': os.environ['RDS_PASSWORD'],
            'HOST': os.environ['RDS_HOSTNAME'],
            'PORT': os.environ['RDS_PORT'],
        }
    }

# [...]

cfn-init-cmd.log文件

2020-10-24 19:06:58,897 P9954 [INFO] ************************************************************
2020-10-24 19:06:58,898 P9954 [INFO] ConfigSet Infra-EmbeddedPreBuild
2020-10-24 19:06:58,901 P9954 [INFO] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2020-10-24 19:06:58,901 P9954 [INFO] Config prebuild_0_myApp
2020-10-24 19:07:03,679 P10119 [INFO] ************************************************************
2020-10-24 19:07:03,679 P10119 [INFO] ConfigSet Infra-EmbeddedPostBuild
2020-10-24 19:07:03,682 P10119 [INFO] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2020-10-24 19:07:03,682 P10119 [INFO] Config postbuild_0_myApp
2020-10-24 19:07:03,690 P10119 [INFO] ============================================================
2020-10-24 19:07:03,690 P10119 [INFO] Test for Command 01_migrate        <<== Test succeded
2020-10-24 19:07:03,694 P10119 [INFO] Completed successfully.
2020-10-24 19:07:03,694 P10119 [INFO] ============================================================
2020-10-24 19:07:03,694 P10119 [INFO] Command 01_migrate
2020-10-24 19:07:03,697 P10119 [INFO] Completed successfully.            <<== Migration completed
2020-10-24 19:07:03,704 P10119 [INFO] ============================================================
2020-10-24 19:07:03,704 P10119 [INFO] Command 02_collectstatic
2020-10-24 19:07:03,708 P10119 [INFO] Completed successfully.
2020-10-24 19:07:03,715 P10119 [INFO] ============================================================
2020-10-24 19:07:03,715 P10119 [INFO] Test for Command 03_createsu
2020-10-24 19:07:03,718 P10119 [INFO] Completed successfully.
2020-10-24 19:07:03,719 P10119 [INFO] ============================================================
2020-10-24 19:07:03,719 P10119 [INFO] Command 03_createsu
2020-10-24 19:07:03,722 P10119 [INFO] Completed successfully.

当我打开时,xyz.elasticbeanstalk.com/admin/我收到此消息:

Exception Value: (1146, "Table 'ebdb.django_site' doesn't exist")

我在本地连接到数据库,看到到目前为止还没有通过ython3 manage.py migrate user_auth --noinput命令创建表。(我已经makemigrations在本地运行并且migrations文件夹包含在 git 中)

我将所有 RDS 变量设置为弹性 beanstalk 配置。数据库不是与EB一起创建的,而是单独创建的,然后将其连接到EB。

我还 ssh'ed 到 EB 环境并使用source /var/app/venv/*/bin/activate. 然后我跑了python3 manage.py migrate --noinput,得到了这个错误:

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

此错误的原因是 IMO,'RDS_HOSTNAME' in os.environ返回False,因此未配置数据库。

所以,我跑了/opt/elasticbeanstalk/bin/get-config environment,我在列表中看到了所有带有 RDS 前缀的变量。

然后我python3在同一个环境中运行来检查它的值,os.environ并且只能看到'DJANGO_SETTINGS_MODULE'可用,但没有 RDS 前缀变量。

我从过去 2 天开始一​​直在研究它,阅读了数千个相关的博客、线程、文档,最后放弃了 :(。如果需要,我会提供任何其他相关信息。

标签: pythondjangoamazon-web-servicesamazon-elastic-beanstalkamazon-rds

解决方案


推荐阅读