简体   繁体   English

如何通过 eb cli (django postgres) 迁移 AWS RDS 数据库?

[英]How can I migrate AWS RDS database through eb cli (django postgres)?

I have a django project deployed with elastic beanstalk CLI.我有一个使用弹性 beanstalk CLI 部署的 django 项目。 I added a postgres database (AWS RDS), the connection is runnning.我添加了一个 postgres 数据库(AWS RDS),连接正在运行。

My database is still empty.我的数据库仍然是空的。 How can I run migration commands or "python manage.py createsuperuser" inside of eb shell?如何在 eb shell 中运行迁移命令或“python manage.py createsuperuser”?

When I open eb ssh <env> and try ls , nothing is shown.当我打开eb ssh <env>并尝试ls时,没有显示任何内容。 Stepping one directory back with cd.. and again ls shows me ec2-user , healthd and webapp .使用cd..退回一个目录,然后ls再次显示ec2-userhealthdwebapp I have not the permissions to enter these folders.我没有进入这些文件夹的权限。 Isnt it possible, to run python commands inside of eb ssh?是否可以在 eb ssh 内部运行 python 命令?

I did also try container commands, .ebextensions/db-migrate.config:我也尝试过容器命令 .ebextensions/db-migrate.config:

container_commands:
  01_makemigrations:
    command: "python manage.py makemigrations"
    leader_only: true
  02_migrate:
    command: "python manage.py migrate --first main initial && python manage.py migrate"
    leader_only: true
option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: <app>.settings

After commiting and deploying again with eb deploy , I get this error:使用eb deploy再次提交和部署后,我收到此错误:

eb deploy Creating application version archive "app-xxxx". eb deploy 创建应用程序版本存档“app-xxxx”。 Uploading: [##################################################] 100% Done...上传:[############################################## ###] 100% 完成...

2021-02-22 12:57:12 INFO Environment update is starting. 2021-02-22 12:57:12 INFO 环境更新开始。

2021-02-22 12:58:05 INFO Deploying new version to instance(s). 2021-02-22 12:58:05 INFO 将新版本部署到实例。

2021-02-22 12:58:21 INFO Instance deployment successfully generated a 'Procfile'. 2021-02-22 12:58:21 INFO 实例部署成功生成了“Procfile”。

2021-02-22 12:58:23 ERROR Instance deployment failed. 2021-02-22 12:58:23 错误实例部署失败。 For details, see 'eb-engine.log'.有关详细信息,请参阅“eb-engine.log”。

2021-02-22 12:58:24 ERROR [Instance: i-xxxx] Command failed on instance. 2021-02-22 12:58:24 错误 [实例:i-xxxx] 命令在实例上失败。 Return code: 1 Output: Engine execution has encountered an error..返回码:1 Output:引擎执行遇到错误..

2021-02-22 12:58:24 INFO Command execution completed on all instances. 2021-02-22 12:58:24 INFO 命令执行已在所有实例上完成。 Summary: [Successful: 0, Failed: 1].摘要:[成功:0,失败:1]。

2021-02-22 12:58:24 ERROR Unsuccessful command execution on instance id(s) 'i-xxxx'. 2021-02-22 12:58:24 错误在实例 ID“i-xxxx”上执行命令不成功。 Aborting the operation.中止操作。

2021-02-22 12:58:24 ERROR Failed to deploy application. 2021-02-22 12:58:24 错误无法部署应用程序。

ERROR: ServiceError - Failed to deploy application.错误:ServiceError - 无法部署应用程序。

Why are the commands failing?为什么命令失败? Any advice is appreciated.任何建议表示赞赏。 Btw, I have no problems with deploying with the local sql db.顺便说一句,我使用本地 sql 数据库部署没有问题。

UPDATE:更新:

eb-engine.log refers me to cfn-init.log. eb-engine.log 将我引向 cfn-init.log。 This is the output:这是 output:

2021-02-22 16:49:43,624 [ERROR] Command 01_makemigrations (python manage.py makemigrations) failed 2021-02-22 16:49:43,624 [错误] 命令 01_makemigrations (python manage.py makemigrations) 失败

2021-02-22 16:49:43,624 [ERROR] Error encountered during build of postbuild_0_django_test: Command 01_makemigrations failed 2021-02-22 16:49:43,624 [错误] 在构建 postbuild_0_django_test 期间遇到错误:命令 01_makemigrations 失败

Traceback (most recent call last): File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config CloudFormationCarpenter(config, self._auth_config).build(worklog)回溯(最后一次调用):文件“/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py”,第 542 行,在 run_config CloudFormationCarpenter(config, self._auth_config).build(worklog)

File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build changes['commands'] = CommandTool().apply(self._config.commands)文件“/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py”,第 260 行,在 build changes['commands'] = CommandTool().apply(self._config.commands)

File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply raise ToolError(u"Command %s failed" % name) ToolError: Command 01_makemigrations failed文件“/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py”,第 117 行,在 apply raise ToolError(u"Command %s failed" % name) ToolError: Command 01_makemigrations failed

In the Path I see python 2.7 (?) but actually Python 3.7 is running on 64bit Amazon Linux 2. I also tried executing the command with "python3".在路径中,我看到 python 2.7(?)但实际上 Python 3.7 正在 64 位 Amazon Linux 2 上运行。我也尝试使用“python3”执行命令。

The issue is that django is not loaded at that point.问题是此时未加载 django。

Solution:解决方案:

container_commands:
  01_migrate:
    command: |
      source $PYTHONPATH/activate
      pipenv run python ./manage.py migrate

Logs referring you to eb-engine.log and to cfn-init.log , but I found the final hint in cfn-init-cmd.log :将您引至eb-engine.logcfn-init.log ,但我在cfn-init-cmd.log中找到了最终提示:

2021-02-23 11:08:34,019 P5022 [INFO] ImportError: Couldn't import Django. 2021-02-23 11:08:34,019 P5022 [INFO] ImportError:无法导入 Django。 Are you sure it's installed and available on your PYTHONPATH environment variable?您确定它已安装并在您的 PYTHONPATH 环境变量中可用吗? Did you forget to activate a virtual environment?您是否忘记激活虚拟环境?

I did also try it for "createsuperuser".我也为“createsuperuser”尝试过。 No errors at all, but also no new user created.完全没有错误,也没有创建新用户。 There is no active command line to enter username/password.没有用于输入用户名/密码的活动命令行。 I try to update my answer if I found a solution for createsuperuser.如果我找到了 createsuperuser 的解决方案,我会尝试更新我的答案。

I can propose an alternate approach:我可以提出另一种方法:

Rather than running Django management commands from within the eb shell, you could adjust the DATABASE settings of your local Django project to the remote database, and execute management commands from your local terminal.与其从 eb shell 中运行 Django 管理命令,不如调整本地 Django 项目的DATABASE 设置到远程数据库,然后执行管理命令。

This would be useful for running commands ad-hoc, such as createsuperuser , but for routine maintenance commands, like migrate , you'd want to stick with the container_commands approach as part of your deployment process.这对于运行临时命令(例如createsuperuser )很有用,但对于例行维护命令(例如migrate ),您需要在部署过程中坚持使用container_commands方法。

Finally, if you're determined to run createsuperuser , specifically, within the eb shell, then you can use container_commands to do so non-interactively, with some additional settings configured.最后,如果您决定运行createsuperuser ,特别是在 eb shell 中,那么您可以使用container_commands以非交互方式执行此操作,并配置一些额外的设置。 From the Django Docs :来自Django 文档

When run interactively, this command will prompt for a password for the new superuser account.当以交互方式运行时,此命令将提示输入新超级用户帐户的密码。 When run non-interactively, you can provide a password by setting the DJANGO_SUPERUSER_PASSWORD environment variable.当以非交互方式运行时,您可以通过设置 DJANGO_SUPERUSER_PASSWORD 环境变量来提供密码。 Otherwise, no password will be set, and the superuser account will not be able to log in until a password has been manually set for it.否则,将不会设置密码,并且超级用户帐户将无法登录,直到手动为其设置密码。

In non-interactive mode, the USERNAME_FIELD and required fields (listed in REQUIRED_FIELDS) fall back to DJANGO_SUPERUSER_<uppercase_field_name> environment variables, unless they are overridden by a command line argument.在非交互模式下,USERNAME_FIELD 和必填字段(列在 REQUIRED_FIELDS 中)回退到 DJANGO_SUPERUSER_<uppercase_field_name> 环境变量,除非它们被命令行参数覆盖。 For example, to provide an email field, you can use DJANGO_SUPERUSER_EMAIL environment variable.例如,要提供 email 字段,您可以使用 DJANGO_SUPERUSER_EMAIL 环境变量。

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

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