【问题标题】:Running Django migrations when deploying to Elastic Beanstalk在部署到 Elastic Beanstalk 时运行 Django 迁移
【发布时间】:2015-09-06 04:36:47
【问题描述】:

我在 Elastic Beanstalk 上设置了我的 Django 应用程序,最近对我希望现在应用到实时数据库的数据库进行了更改。我知道我需要将其设置为容器命令,在检查数据库后,我可以看到迁移已运行,但我不知道如何对迁移进行更多控制。例如,我只希望在必要时运行迁移,但据我了解,假设该命令仍列在配置文件中,容器将在每次部署时运行迁移。此外,在某些情况下,我会在迁移期间获得一些选项,例如:

Any objects realted to these content types by a foreign key will also be deleted.
Are you sure you want to delete these content types?
If you're unsure, answer 'no'

如何设置容器命令以在部署阶段使用 yes 响应此问题?

这是我当前的配置文件

container_commands:
  01_migrate:
    command: 'source /opt/python/run/venv/bin/actiate && python app/manage.py makemigrations'
    command: 'source /opt/python/run/venv/bin/activate && python app/manage.py migrate'

有没有办法将这 2 个命令设置为仅在必要时运行并响应我在迁移期间收到的是/否选项?

【问题讨论】:

  • 我认为这不是正确的方法!您应该在本地 makemigrations 然后将迁移推送到生产环境。在部署期间应用迁移。您可以在本地进行所有决策。如果错误地编写了错误的模型,最终可能会丢失数据。

标签: python django amazon-web-services


【解决方案1】:

诀窍在于 container_commands 的完整输出在 /var/log/cfn-init-cmd.log 中(Amazon Linux 2 Elastic Beanstalk 于 2020 年 11 月发布)。

要查看此内容,您将运行:

eb ssh [environment-name]
sudo tail -n 50 -f /var/log/cfn-init-cmd.log

这似乎没有任何明显的记录,eb logs 也没有显示它;我是在 /var/log 中四处寻找的。​​p>

Django example 管理命令 django-admin.py migrate 对我不起作用。相反,我不得不使用类似的东西:

01_migrate:
  command: "$PYTHONPATH/python manage.py migrate"
  leader_only: true
02_collectstatic:
  command: "$PYTHONPATH/python manage.py collectstatic --noinput --verbosity=0 --clear"

要在部署时查看环境变量的值,您可以创建如下调试命令:

03_debug:
  command: "env"

您可以使用eb ssh; sudo cat /opt/elasticbeanstalk/deployment/env 查看大部分环境变量,但在部署时似乎存在一些细微差别,因此请使用上面的env 来确定。

在这里您会看到$PYTHONPATH 以非典型方式指向 virtualenv 的bin 目录,而不是site-packages 目录。

【讨论】:

    【解决方案2】:

    参考 Oscar Chen 的回答,您可以使用 eb cli with 设置环境变量

    eb setenv key1=value1 key2=valu2 ...etc
    

    【讨论】:

      【解决方案3】:

      确保在迁移和运行时使用相同的设置! 因此,我建议您在 django.config

      中更改这种代码
      container_commands:
        01_migrate:
          command: "source /opt/python/run/venv/bin/activate && python manage.py migrate"
          leader_only: true
      

      到:

      container_commands:
        01_migrate:
          command: "django-admin migrate"
          leader_only: true
      option_settings:
        aws:elasticbeanstalk:application:environment:
          DJANGO_SETTINGS_MODULE: fund.productionSettings
      

      推荐here。这将帮助您避免使用错误设置的问题。

      Morema​​nage.pydjango-admin.py.

      【讨论】:

      • 谢谢,但请缩进最后一行 (DJANGO_SETTINGS_MODULE: fund.productionSettings)。
      【解决方案4】:

      除了可以添加到部署脚本的自动迁移(每次更新环境时都会运行,如果您长时间运行迁移或其他 Django 管理命令可能不理想),您可以通过 ssh 进入 EB 实例手动运行迁移。

      以下是在使用 Amazon Linux 2(Python 3.7、3.8)时如何手动运行迁移(以及任何其他 Django 管理命令) ) 由 Elastic Beanstalk 创建:

      首先,从您的 EB cli:eb ssh 连接实例。

      虚拟环境可以通过 source /var/app/venv/*/bin/activate

      manage.py 可以运行 python3 /var/app/current/manage.py

      现在唯一棘手的一点是获取 Elastic Beanstalk 的环境变量。您可以通过/opt/elasticbeanstalk/bin/get-config 访问它们,我对 bash 脚本不是很熟悉,但是这里有一个我用来获取和设置环境变量的小脚本,也许有人可以改进它以减少硬编码:

      #! /bin/bash
      export DJANGO_SECRET_KEY=$(/opt/elasticbeanstalk/bin/get-config environment -k DJANGO_SECRET_KEY)
      ...
      

      有关 Amazon Linux 2 splatform 脚本工具的更多信息:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platforms-scripts.html

      【讨论】:

        【解决方案5】:

        django-admin 方法无法正常工作,因为它没有正确配置。您也可以在中使用python manage.py migrate .ebextentions/django.config

        container_commands:
          01_migrate:
            command: "python manage.py migrate"
            leader_only: true
        

        【讨论】:

          【解决方案6】:

          This answer 如果您只想向一些提示发送“是”,它似乎对您有用。

          您还可以考虑使用 --noinput 标志,以便您的配置如下所示:

          container_commands:
            01_migrate:
              command: 'source /opt/python/run/venv/bin/actiate && python app/manage.py makemigrations'
              command: 'source /opt/python/run/venv/bin/activate && python app/manage.py migrate --noinput
          

          这采用默认设置,即“否”。

          看来还有an open issue/fix可以更好的解决这个问题。

          【讨论】:

            【解决方案7】:

            我不确定是否有特定的方式来回答是或否。但是您可以将--noinput 附加到您的容器命令中。使用--noinput 选项禁止所有用户提示,例如“您确定吗?”确认消息。

            try
                command: 'source /opt/python/run/venv/bin/activate && python app/manage.py migrate --noinput'
            

            或者.. 您可以通过 ssh 进入您的 elasticbean 实例并手动运行您的命令。 这样您就可以更好地控制迁移。

            1. 使用pip install awsebcli 安装awsebcli
            2. 输入eb ssh Your EnvironmentName
            3. 使用以下命令导航到您的 eb 实例应用目录:

            • sudo -s
            • 来源/opt/python/run/venv/bin/activate
            • 来源/opt/python/current/env
            • cd /opt/python/current/app

            • 然后运行你的命令。

              ./manage.py 迁移

            希望对你有帮助

            【讨论】:

            • 源 /opt/python/run/venv/bin/activate
            • 此修复适用于在 amazon linux 上运行的 beanstalk。对于 amazon linux 2,请参阅文档。
            猜你喜欢
            • 2020-10-08
            • 2020-09-21
            • 2020-08-17
            • 2020-03-24
            • 2019-03-05
            • 2021-02-03
            • 1970-01-01
            • 2017-02-24
            • 2017-04-07
            相关资源
            最近更新 更多