Mar*_*ind 6 python django amazon-ec2 amazon-web-services
我有一个名为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)
Run Code Online (Sandbox Code Playgroud)
代码如下所示:
这是我在.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
Run Code Online (Sandbox Code Playgroud)
这是我的/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")
Run Code Online (Sandbox Code Playgroud)
而且我和文件夹__init__.py中都有文件./management//commands/
我从命令行本地尝试了这个命令,它工作正常,并创建用户没有错误.所以命令本身或者不应该有任何问题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 Path.我不确定如何做到这一点,所以如果有人仍想回答这个问题,我会测试它并接受它作为答案,如果这将解决部署错误.
仔细检查第二方法的链接。.ebextensions/02_python.config您可以在您创建的选项设置 ( ) 中设置 python 路径:
Run Code Online (Sandbox Code Playgroud)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 下更深一层,但现在工作正常。