简体   繁体   English

在 AWS Elastic Beanstalk 上创建 Django 超级用户

[英]Create Django Superuser on AWS Elastic Beanstalk

I have a custom user class called MyUser.我有一个名为 MyUser 的自定义用户类。 It works fine locally with registrations, logins and so on.它可以在本地通过注册、登录等正常工作。 I'm trying to deploy my application to AWS Elastic Beanstalk and I'm running into some problems with creating my superuser.我正在尝试将我的应用程序部署到 AWS Elastic Beanstalk,但在创建超级用户时遇到了一些问题。

I tried making a script file and run it as the official AWS guide suggests.我尝试制作一个脚本文件并按照官方AWS 指南的建议运行它。 Didnt work well so I decided to try a secondary method suggested here and create a custom manage.py command to create my user.效果不佳,因此我决定尝试此处建议的辅助方法并创建自定义 manage.py 命令来创建我的用户。

When I deploy I get the following errors in the log.当我部署时,我在日志中收到以下错误。

[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)

The code looks like the following:代码如下所示:

This is my mysite.config file in .ebextensions/这是我在 .ebextensions/ 中的 mysite.config 文件

01_syncdb and 03_collectstatic works fine. 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

This is my /profiles/management/commands/createsu.py file:这是我的/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")

And I have __init__.py files in both /management/ and /commands/ folders.我在/management//commands/文件夹中都有__init__.py文件。

I tried this command locally from command line and it works fine and creates the user without errors.我从命令行在本地尝试了这个命令,它工作正常并且创建了没有错误的用户。 So there shouldnt be any issue with the command itself or the MyUser.objects.create_superuser() .所以命令本身或MyUser.objects.create_superuser()不应该有任何问题。

EDIT: I tried changing my def handle(): function to only set a variable to True and I still get the same errors.编辑:我尝试更改我的def handle():函数以仅将变量设置为True并且我仍然遇到相同的错误。 So it seems like the problem is not related to the create_superuser function or the handle, but more something with using manage.py.因此,问题似乎与 create_superuser 函数或句柄无关,而与使用 manage.py 相关。

Any ideas?有任何想法吗?

EDIT 2: I tried executing the command by SSH and failed.编辑 2:我尝试通过 SSH 执行命令但失败了。 I then followed the instructions in this post and set the Python Path's manually with:然后我按照这篇文章中的说明手动设置 Python 路径:

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

I was then able to successfully create my user.然后我能够成功创建我的用户。 The official AWS Django Deployment guide does not mention anything about this.官方的 AWS Django 部署指南没有提及这方面的任何内容。 But I guess you are suppose to set your Python Path's in the .config file somehow.但我想你应该以某种方式在 .config 文件中设置你的 Python 路径。 I'm not sure exactly how to do this so if someone still want to answer that, I will test it and accept it as answer if that will solve the deployment errors.我不确定如何做到这一点,所以如果有人仍然想回答这个问题,我会测试它并接受它作为答案,如果这能解决部署错误。

Double-check the link to your secondary method.仔细检查您的辅助方法的链接。 You can set the python path in the option settings ( .ebextensions/02_python.config ) that you've created:您可以在您创建的选项设置 ( .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"

However, I've done this and am still experiencing the issue you've described, so you'll have to see if it fixes it.但是,我已经这样做了并且仍然遇到您所描述的问题,因此您必须看看它是否可以解决它。

EDIT: It turns out my issue was a file structure issue.编辑:原来我的问题是文件结构问题。 I had the management directory in the project directory, when it should have been placed one level deeper in the directory of one of my apps.我在项目目录中有管理目录,而它应该放在我的一个应用程序目录中更深一层。

This placed it one level deeper beneath my manage.py and settings.py than is shown in the example, but it is working fine now.这将它置于我的 manage.py 和 settings.py 之下比示例中显示的更深一层,但它现在工作正常。

I know this could be late but I just wanted to share that I solved this issue by adding the file /profiles/management/commands/createsu.py into the app folder you are using.我知道这可能会晚,但我只想分享我通过将文件/profiles/management/commands/createsu.py添加到您正在使用的应用程序文件夹中解决了这个问题。

In my case was:在我的情况下是:

easy/easyapp/management/commands/createsu.py容易/easyapp/management/commands/createsu.py

where easy is my project and easyapp my app.其中是我的项目,我easyapp应用。

Another alternative that worked for me is to just go directly into the config.yml file and change the wsgi path there.另一种对我有用的替代方法是直接进入 config.yml 文件并在那里更改 wsgi 路径。 You can get access with the eb config command and just go down 50 lines or so, make your changes, escape and save.您可以使用eb config命令访问,只需向下 50 行左右,进行更改、转义并保存。 This is only an environment-specific solution though.不过,这只是特定于环境的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM