【问题标题】:Create Django Superuser on AWS Elastic Beanstalk在 AWS Elastic Beanstalk 上创建 Django 超级用户
【发布时间】:2015-05-11 14:47:09
【问题描述】:

我有一个名为 MyUser 的自定义用户类。它在本地与注册、登录等工作正常。我正在尝试将我的应用程序部署到 AWS Elastic Beanstalk,但在创建超级用户时遇到了一些问题。

我尝试制作一个脚本文件并按照官方AWS guide 的建议运行它。效果不佳,所以我决定尝试第二种方法 suggested here 并创建自定义 manage.py 命令来创建我的用户。

部署时,我在日志中收到以下错误。

[Instance: i-8a0a6d6e Module: AWSEBAutoScalingGroup ConfigSet: null] Command failed on instance. Return code: 1 Output: [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 02_createsu failed.

[2015-03-10T08:05:20.464Z] INFO  [17937] : Command processor returning results: 
{"status":"FAILURE","api_version":"1.0","truncated":"false","results":[{"status":"FAILURE","msg":"[CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 02_createsu failed","returncode":1,"events":[]}]}


[2015-03-10T08:05:20.463Z] ERROR [17937] : Command execution failed: [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] command failed with error code 1: Error occurred during build: Command 02_createsu failed (ElasticBeanstalk::ActivityFatalError)
at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:189:in `rescue in exec'
...
caused by: command failed with error code 1: Error occurred during build: Command 02_createsu failed (Executor::NonZeroExitStatus)

代码如下所示:

这是我在 .ebextensions/ 中的 mysite.config 文件

01_syncdb03_collectstatic 工作正常。

container_commands:
  01_syncdb:    
    command: "django-admin.py migrate --noinput"
    leader_only: true
  02_createsu:
    command: "manage.py createsu"
    leader_only: true
  03_collectstatic:
    command: "django-admin.py collectstatic --noinput"
option_settings:
  - namespace: aws:elasticbeanstalk:container:python
    option_name: WSGIPath
    value: treerating/wsgi.py
  - option_name: DJANGO_SETTINGS_MODULE
    value: treerating.settings

这是我的/profiles/management/commands/createsu.py 文件:

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from profiles.models import MyUser

class Command(BaseCommand):
    def handle(self, *args, **options):
        if MyUser.objects.count() == 0:
            MyUser.objects.create_superuser("admin", "treerating", "password")

我在/management//commands/ 文件夹中都有__init__.py 文件。

我从命令行本地尝试了这个命令,它工作正常并且创建用户没有错误。所以命令本身或MyUser.objects.create_superuser() 应该没有任何问题。

编辑:我尝试将我的 def handle(): 函数更改为仅将变量设置为 True,但我仍然遇到相同的错误。因此,问题似乎与 create_superuser 函数或句柄无关,而与使用 manage.py 有关。

有什么想法吗?

编辑 2: 我尝试通过 SSH 执行命令但失败了。然后我按照this post 中的说明手动设置 Python 路径:

source /opt/python/run/venv/bin/activatesource /opt/python/current/env

然后我能够成功地创建我的用户。 官方的 AWS Django 部署指南没有提到任何关于此的内容。但我想你应该以某种方式在 .config 文件中设置你的 Python 路径。我不确定该怎么做,所以如果有人仍然想回答这个问题,我会对其进行测试并接受它作为答案,如果这样可以解决部署错误。

【问题讨论】:

  • 根据用例,在例如的帮助下创建初始用户可能更方便。 fixtures.

标签: python django amazon-web-services amazon-ec2


【解决方案1】:

仔细检查您的辅助方法的链接。您可以在已创建的选项设置 (.ebextensions/02_python.config) 中设置 python 路径:

option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "iotd.settings"
    "PYTHONPATH": "/opt/python/current/app/iotd:$PYTHONPATH"
    "ALLOWED_HOSTS": ".elasticbeanstalk.com"

但是,我已经这样做了,但仍然遇到您描述的问题,因此您必须查看它是否可以解决问题。

编辑:原来我的问题是文件结构问题。我在项目目录中有管理目录,而它应该被放置在我的一个应用程序的目录中更深一层。

这将它放置在我的 manage.py 和 settings.py 比示例中显示的更深一层,但它现在工作正常。

【讨论】:

    【解决方案2】:

    对我有用的另一种选择是直接进入 config.yml 文件并在那里更改 wsgi 路径。您可以使用eb config 命令获取访问权限,只需向下约 50 行,进行更改、转义并保存。不过,这只是一个特定于环境的解决方案。

    【讨论】:

      【解决方案3】:

      我知道这可能会迟到,但我只是想分享一下,我通过将文件 /profiles/management/commands/createsu.py 添加到您正在使用的应用程序文件夹中解决了这个问题。

      在我的情况下是:

      easy/easyapp/management/commands/createsu.py

      easy 是我的项目,easyapp 是我的应用程序。

      【讨论】:

        猜你喜欢
        • 2017-10-15
        • 2014-08-03
        • 2016-11-12
        • 2013-09-23
        • 2016-06-05
        • 2020-09-21
        • 2018-01-06
        • 2021-10-13
        • 2016-11-25
        相关资源
        最近更新 更多