【发布时间】: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_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 执行命令但失败了。然后我按照this post 中的说明手动设置 Python 路径:
source /opt/python/run/venv/bin/activate
和
source /opt/python/current/env
然后我能够成功地创建我的用户。 官方的 AWS Django 部署指南没有提到任何关于此的内容。但我想你应该以某种方式在 .config 文件中设置你的 Python 路径。我不确定该怎么做,所以如果有人仍然想回答这个问题,我会对其进行测试并接受它作为答案,如果这样可以解决部署错误。
【问题讨论】:
-
根据用例,在例如的帮助下创建初始用户可能更方便。 fixtures.
标签: python django amazon-web-services amazon-ec2