AWS Elastic Beanstalk容器命令失败

2024-05-16 06:23:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在努力将我的Django Web应用程序成功部署到AWS的Elastic Beanstalk上。我能够在本地计算机上从EB CLI部署我的应用程序,没有任何问题,直到我在.ebextensions文件夹中添加一个container_命令配置文件列表

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

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环境中,没有任何问题。现在唯一不同的是,这个新环境是在专有网络内启动的,并且使用最新推荐的平台

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

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

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

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


Tags: djangopyinfo应用程序only环境admin部署
3条回答

在深入浏览AWS文档和论坛之后,终于弄清了真相

本质上,随着Beanstalk从AmazonLinux迁移到AmazonLinux2,出现了很多变化。这些变化中有很多都被模糊地提到了

上面链接中提到的Python平台的一个主要区别是“在您的环境的Amazon EC2实例上,应用程序目录的路径是/var/app/current。在Amazon Linux AMI平台上,它是/opt/Python/current/app。”当您试图创建Django迁移脚本(我将在下面进一步详细解释)时,或者当您eb ssh进入Beanstalk实例并自己导航时,这一点至关重要

另一个主要区别是引入了平台挂钩,这在这篇精彩的文章here中提到。根据本文所述,“平台挂钩是应用程序包中的一组目录,您可以使用脚本填充这些目录。”实际上,这些脚本现在将处理以前的container_命令在.ebextensions配置文件中处理的内容。以下是这些平台挂钩的目录结构: Platform hooks directory structure

知道了这一点,在这个论坛here中,出色的社区成员在填补Amazon文档空白方面遇到了麻烦,我成功地部署了以下文件:

(请注意,“MDGOnline”是我的Django应用程序的名称)

.ebextensions\01\u 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\u 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。这个看起来很有前途

我真的希望AWS能够更好地记录这个迁移,但我希望这个答案可以为某人节省我为成功部署所花费的无数时间

请随时要求我澄清以上任何一点

编辑:我在上面的配置文件中添加了一个“container_命令”,因为我注意到另一个用户在部署平台挂钩时也遇到了“permission denied”错误。此“01_sh_executable”命令用于chmod应用程序hooks目录中的所有.sh脚本,以便Elastic Beanstalk在部署过程中具有执行这些脚本的适当权限。我在这个论坛上找到了这个容器命令解决方案here

这对我有用

container_commands:
  01_migrate:
    command: "source /var/app/venv/*/bin/activate && python /var/app/staging/manage.py migrate  noinput"
    leader_only: true

这可能有用 .ebextensions/django.config

   option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: mysite.wsgi:application
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static
packages: 
  yum:
    python3-devel: []
    mariadb-devel: []
container_commands:
  01_collectstatic:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py collectstatic  noinput"
  02_migrate:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py migrate  noinput"
    leader_only: true

相关问题 更多 >