使用 Elastic Beanstalk 设置 Flask 数据库迁移

Posted

技术标签:

【中文标题】使用 Elastic Beanstalk 设置 Flask 数据库迁移【英文标题】:setting up Flask db migrations with Elastic Beanstalk 【发布时间】:2015-04-24 05:06:16 【问题描述】:

我已经建立了一个基于 Flask sing eb(Elastic Beanstalk v3.1 的终端客户端)的站点。

在部署我的站点的新版本时,我收到“更新环境操作已完成,但出现错误。有关详细信息,请参阅故障排除文档。”消息。

我有.ebextensions/aws.config 文件

container_commands:
  01_migrate:
    command: "application.py db upgrade"
    leader_only: true

我尝试使用ignoreErrors: true 和不同的命令组合,但没有成功。有什么想法可以解决这个问题或摆脱迁移升级?

版本:

alembic==0.7.4
awsebcli==3.1
cement==2.4.0
Flask==0.10.1
Flask-Migrate==1.3.0
Flask-Script==2.0.5
Flask-SQLAlchemy==2.0
itsdangerous==0.24
Jinja2==2.7.3
jmespath==0.6.1
Mako==1.0.1
MarkupSafe==0.23
psycopg2==2.6
python-dateutil==2.4.0
PyYAML==3.11
six==1.9.0
SQLAlchemy==0.9.8
Werkzeug==0.10.1

在运行 Python 3.4(预配置 - Docker)的 64 位 Debian jessie v1.2.0 上使用 Python 3.4 部署的所有东西

编辑:拼写错误修复后 事件消息:

[Instance: i-a1627aab 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 01_migrate failed.

这些是来自 Elastic Beanstalk 的日志

[2015-02-22T23:37:16.010Z] INFO  [17720] - [CMD-AppDeploy/AppDeployStage0/EbExtensionPostBuild] : Activity execution failed, because: command failed with error code 1: Error occurred during build: Command 01_migrate failed (Executor::NonZeroExitStatus)
    at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor/exec.rb:81:in `sh'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor/exec.rb:24:in `sh'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/cfn-wrapper.rb:51:in `call_cfn_script'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/cfn-wrapper.rb:28:in `run_config_sets'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/infrahooks/infra-embeddedpostbuild.rb:20:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/executable.rb:56:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/executable.rb:56:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:213:in `block (3 levels) in exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `call'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:126:in `timeout_exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:110:in `block in create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:109:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:109:in `create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:209:in `block (2 levels) in exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:207:in `each'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:207:in `each_with_index'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:207:in `block in exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `call'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:126:in `timeout_exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:110:in `block in create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:109:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:109:in `create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:204:in `exec_stage'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:158:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command.rb:68:in `run'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command-processor.rb:64:in `block (2 levels) in execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `call'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:126:in `timeout_exec'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:110:in `block in create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:109:in `instance_eval'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:109:in `create'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command-processor.rb:62:in `block in execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command-processor.rb:106:in `execute_command'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/command-processor.rb:60:in `execute!'
    from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/bin/command-processor:46:in `<top (required)>'
    from /opt/elasticbeanstalk/lib/ruby/bin/command-processor:23:in `load'
    from /opt/elasticbeanstalk/lib/ruby/bin/command-processor:23:in `<main>'

【问题讨论】:

您可以尝试在此行命令上正确拼写升级:“application.py db updagre” 愚弄我,修正了拼写错误。但仍然有错误。 也许 ssh 进入 ec2 并手动运行命令以查看更多详细信息 好的,我通过 SSH 连接到 EC2,我发现的第一件事是超级错误,虚拟环境是用 python 2.7 而不是 3.4 创建的:# ***.com/questions/28665911/… 【参考方案1】:

@dnuske

请注意,您使用的解决方案堆栈是基于 Docker 的解决方案堆栈。因此,您的 eb-extensions 不会在 docker 上下文中运行,而是在主机中运行。

要执行您需要的操作,您需要更新 python dockerfile (https://github.com/aws/aws-eb-python-dockerfiles/blob/master/3.4.2-aws-eb-onbuild/Dockerfile) 以将升级步骤添加为该 docker 文件的一部分。完成此操作后,您可以将 docker 文件与应用程序捆绑在一起,您的步骤将根据需要进行。

如果这回答了您的问题,请告诉我。

谢谢, 丹维

【讨论】:

【参考方案2】:

确保您处于迁移所需的 Python 虚拟环境中。在我的实例中,我发现以下内容适用于 Flask-Migrate:

container_commands:
  01_migrate:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate; flask db upgrade"
    leader_only: true

【讨论】:

【参考方案3】:

将此文件添加到项目目录的根目录:

.ebextensions/migration.config

container_commands:
  01_migrate:
    command: "source /var/app/venv/staging-LQM1lest/bin/activate && python3 manage.py db upgrade"
    leader_only: true

在您的 EB 环境配置/软件/环境属性中检查PYTHONPATH。我的是/var/app/venv/staging-LQM1lest/bin

manage.py 是包含类似内容的文件

import os
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from application import application, db


application.config.from_object(os.environ['APP_SETTINGS'])

migrate = Migrate(application, db)
manager = Manager(application)

manager.add_command('db', MigrateCommand)


if __name__ == '__main__':
    manager.run()

【讨论】:

以上是关于使用 Elastic Beanstalk 设置 Flask 数据库迁移的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Elastic Beanstalk 设置实例类型?

使用 Elastic BeanStalk + Django 设置 ElastiCache Redis

使用 Postgres 在 Elastic Beanstalk 上设置 Django

使用 Elastic Beanstalk 设置 Flask 数据库迁移

如何使用 Elastic Beanstalk 为 AWS CDK 设置环境变量?

使用 HTTPS 请求在 Elastic Beanstalk 上设置 API 的问题