[英]Create Django Superuser on AWS Elastic Beanstalk
我有一个名为 MyUser 的自定义用户类。 它可以在本地通过注册、登录等正常工作。 我正在尝试将我的应用程序部署到 AWS Elastic Beanstalk,但在创建超级用户时遇到了一些问题。
我尝试制作一个脚本文件并按照官方AWS 指南的建议运行它。 效果不佳,因此我决定尝试此处建议的辅助方法并创建自定义 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_syncdb
和03_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 执行命令但失败了。 然后我按照这篇文章中的说明手动设置 Python 路径:
source /opt/python/run/venv/bin/activate
和source /opt/python/current/env
然后我能够成功创建我的用户。 官方的 AWS Django 部署指南没有提及这方面的任何内容。 但我想你应该以某种方式在 .config 文件中设置你的 Python 路径。 我不确定如何做到这一点,所以如果有人仍然想回答这个问题,我会测试它并接受它作为答案,如果这能解决部署错误。
仔细检查您的辅助方法的链接。 您可以在您创建的选项设置 ( .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 之下比示例中显示的更深一层,但它现在工作正常。
我知道这可能会晚,但我只想分享我通过将文件/profiles/management/commands/createsu.py添加到您正在使用的应用程序文件夹中解决了这个问题。
在我的情况下是:
容易/easyapp/management/commands/createsu.py
其中易是我的项目,我easyapp应用。
另一种对我有用的替代方法是直接进入 config.yml 文件并在那里更改 wsgi 路径。 您可以使用eb config
命令访问,只需向下 50 行左右,进行更改、转义并保存。 不过,这只是特定于环境的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.