python - AWS Elastic Beanstalk 容器命令失败

标签 python django amazon-web-services amazon-elastic-beanstalk

我一直在努力将我的 Django Web 应用程序成功部署到 AWS 的 Elastic Beanstalk。在我在 .ebextensions 文件夹中添加 container_commands 配置文件列表之前,我可以在我的本地机器上从 EB CLI 部署我的应用程序,没有任何问题。

以下是我的配置文件的内容:

container_commands:
  01_makeAppMigrations:
    command: "django-admin.py makemigrations"
    leader_only: true
  02_migrateApps:
    command: "django-admin.py migrate"
    leader_only: true
  03_create_superuser_for_django_admin:
    command: "django-admin.py createfirstsuperuser"
    leader_only: true
  04_collectstatic:
    command: "django-admin.py collectstatic --noinput"

我深入研究了日志,发现 cfn-init-cmd.log 中的这些消息最有帮助:
2020-06-18 04:01:49,965 P18083 [INFO] Config postbuild_0_DjangoApp_smt_prod
2020-06-18 04:01:49,991 P18083 [INFO] ============================================================
2020-06-18 04:01:49,991 P18083 [INFO] Test for Command 01_makeAppMigrations
2020-06-18 04:01:49,995 P18083 [INFO] Completed successfully.
2020-06-18 04:01:49,995 P18083 [INFO] ============================================================
2020-06-18 04:01:49,995 P18083 [INFO] Command 01_makeAppMigrations
2020-06-18 04:01:49,998 P18083 [INFO] -----------------------Command Output-----------------------
2020-06-18 04:01:49,998 P18083 [INFO]   /bin/sh: django-admin.py: command not found
2020-06-18 04:01:49,998 P18083 [INFO] ------------------------------------------------------------
2020-06-18 04:01:49,998 P18083 [ERROR] Exited with error code 127

我不确定为什么在这个最新环境中找不到该命令。
我已经使用相同的配置文件将相同的应用程序部署到之前的 beanstalk 环境中,完全没有问题。现在唯一的区别是这个新环境是在 VPC 中启动的,并且使用的是最新的推荐平台。

旧Beanstalk环境平台 :运行在 64 位 Amazon Linux/2.9.3 上的 Python 3.6

全新Beanstalk环境平台 :运行在 64 位 Amazon Linux 2/3.0.2 上的 Python 3.7

在此迁移过程中,我遇到了与此最新平台的语法更新相关的其他问题。我希望这个问题也只是一个简单的语法问题,但我已经深入挖掘,但没有运气......

如果有人能指出我在这里遗漏的一些明显的东西,我将不胜感激!
如果我能提供一些额外的信息,请告诉我!

最佳答案

在深入浏览 AWS 文档和论坛之后,终于搞清楚了这一切……
从本质上讲,随着 Beanstalk 从 Amazon Linux 迁移到 Amazon Linux 2,发生了很多变化。其中很多变化都含糊地提到 here .
上面链接中提到的 Python 平台的一个主要区别是“您环境的 Amazon EC2 实例上应用程序目录的路径是/var/app/current。它是 Amazon Linux 上的/opt/python/current/app AMI 平台。”这对于当您尝试创建 Django 迁移脚本时至关重要,我将在下面详细解释,或者当您 eb ssh 时进入 Beanstalk 实例并自己导航。
另一个主要区别是引入了Platform hooks,这篇精彩文章here中提到了这一点。 .根据这篇文章,“平台 Hook 是应用程序包内的一组目录,您可以用脚本填充它们。”基本上,这些脚本现在将处理之前在 .ebextensions 配置文件中处理的 container_commands。以下是这些平台 Hook 的目录结构:
Platform hooks directory structure
知道这一点,走过这个论坛 here ,在那里出色的社区成员经历了填补亚马逊文档空白的麻烦,我能够通过设置以下文件成功部署:
(请注意,“MDGOnline”是我的 Django 应用程序的名称)
.ebextensions\01_packages.config :

packages:
  yum:
    git: []
    postgresql-devel: []
    libjpeg-turbo-devel: []
.ebextensions\django.config :
container_commands:
  01_sh_executable:
    command: find .platform/hooks/ -type f -iname "*.sh" -exec chmod +x {} \;
option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: MDGOnline.settings
  aws:elasticbeanstalk:environment:proxy:staticfiles:    
    /static: static
    /static_files: static_files
  aws:elasticbeanstalk:container:python:
    WSGIPath: MDGOnline.wsgi:application
.platform\hooks\predeploy\01_migrations.sh :
#!/bin/bash

source /var/app/venv/*/bin/activate
cd /var/app/staging

python manage.py makemigrations
python manage.py migrate
python manage.py createfirstsuperuser
python manage.py collectstatic --noinput
请注意,'.sh' 脚本需要基于 linux。我遇到了一段时间的错误,部署将失败并在日志中提供以下消息:.platform\hooks\predeploy\01_migrations.sh failed with error fork/exec .platform\hooks\predeploy\01_migrations.sh: no such file or directory .
原来这是因为我在 Windows 开发环境中创建了这个脚本。我的解决方案是在 linux 环境中创建它,然后将其复制到我在 Windows 中的 dev 环境目录中。我敢肯定,有一些方法可以将 DOS 转换为 Unix。这个看起来很有前途 dos2unix !
我真的希望 AWS 能够更好地记录这次迁移,但我希望这个答案可以为某人节省我为成功部署而花费的无数时间。
请随时要求我澄清上述任何一项!
编辑:我在上面的配置文件中添加了一个“container_command”,因为我注意到另一个用户在部署时也遇到了平台 Hook 的“权限被拒绝”错误。这个“01_sh_executable”命令是对应用程序的hooks目录下的所有.sh脚本进行chmod,以便Elastic Beanstalk在部署过程中可以有适当的权限来执行它们。我在这个论坛中找到了这个容器命令解决方案 here :

关于python - AWS Elastic Beanstalk 容器命令失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62442212/

相关文章:

python - 如何从 Pandas 数据框中访问日期?

python - scikit-learn 中纵向/面板数据的交叉验证

ruby-on-rails - Rails4.1 : AWS errors missing region

python - Django Multi-Tenancy

amazon-web-services - 用于在 S3 事件上触发 Lambda 的 Cloudformation 模板

apache - 在 Amazon ubuntu 托管中安装替换 SSL 证书的最简单方法

python - nslookup 在 python 中使用服务器替代方案

c# - Win32com.client.dispatch Python 调用 C# 中的等价物

python - 如何在 Django 中向 URL 添加动态参数

python - Django Rest框架在POST请求中创建或更新