gpt4 book ai didi

django - 工具错误 : Command 01_migrate failed on Amazon LM2

转载 作者:行者123 更新时间:2023-12-04 17:16:20 25 4
gpt4 key购买 nike

我正在尝试将在 django 上制作的 web 应用程序部署到 AWS Elastic BeanStalk,但它显示以下错误:

cfnbootstrap.construction_errors.ToolError: Command 01_migrate failed


追溯:
2021-08-04 09:49:56,443 [ERROR] -----------------------BUILD FAILED!------------------------
2021-08-04 09:49:56,443 [ERROR] Unhandled exception during build: Command 01_migrate failed
Traceback (most recent call last):
File "/opt/aws/bin/cfn-init", line 176, in <module>
worklog.build(metadata, configSets)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 135, in build
Contractor(metadata).build(configSets, self)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 561, in build
self.run_config(config, worklog)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 573, in run_config
CloudFormationCarpenter(config, self._auth_config).build(worklog)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/construction.py", line 273, in build
self._config.commands)
File "/usr/lib/python3.7/site-packages/cfnbootstrap/command_tool.py", line 127, in apply
raise ToolError(u"Command %s failed" % name)
cfnbootstrap.construction_errors.ToolError: Command 01_migrate failed
db-migrate.config
在 LM1 上是:
container_commands:
01_migrate:
command: "django-admin.py migrate --noinput"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: packsapp.settings
对于 LM2 我试过这个:
container_commands:
01_migrate:
command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: packsapp.settings
也试过这个
container_commands:
01_migrate:
command: "source /var/app/venv/*/bin/activate && django-admin.py migrate --noinput"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: packsapp.settings
但它仍然失败。我需要改变什么?

最佳答案

正如前面的回答中提到的,Amazon Linux 2 (AL2) 与 Amazon Linux 1 (AL1) 有很大不同——更令人沮丧的是,AWS 文档没有提供准确的迁移方法。经过数小时的挖掘,这是我发现有效的方法:
Hook
AL2 介绍 hooks作为在部署的不同阶段运行 shell 命令的一种方法。我们正在寻找的钩子(Hook)是 postdeploy Hook 如下图:
首先,我们的目录结构应该是这样的:

my_project
|-- .platform/
| |-- hooks/
| |-- postdeploy/
| |-- 01_migrate.sh
|-- my_first_app
|-- my_second_app
接下来,我们添加 01_migrate.sh仅在领导者上执行的脚本:
#!/bin/bash

source "$PYTHONPATH/activate" && {

if [[ $EB_IS_COMMAND_LEADER == "true" ]];
then
# log which migrations have already been applied
python manage.py showmigrations;

# migrate
python manage.py migrate --noinput;
else
echo "this instance is NOT the leader";
fi

}
日志
如果配置正确,我们将看到在 /var/log/eb-hooks.log 下打印的迁移eb 日志的部分(即运行 eb logs):
----------------------------------------
/var/log/eb-hooks.log
----------------------------------------
my_first_app
[X] 0001_my_first_app_migration_1
[X] 0002_my_first_app_migration_2
my_second_app
[X] 0001_my_second_app_migration_1
[X] 0002_my_second_app_migration_2
我们现在可以删除进行迁移的容器命令。
笔记
  • 您需要将上面的shell文件添加到postdeploy在我看来,钩子(Hook)完全不直观
  • 您可以添加其他 django 命令,例如 collectstatic (通常我只是将它包含在上面的文件中)
  • 与 AL1 上的命令一样,shell 脚本“钩子(Hook)”按字母顺序执行(因此是 01 前缀)

  • 祝你好运!

    关于django - 工具错误 : Command 01_migrate failed on Amazon LM2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68649290/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com