Django AWS Elastic Beanstalk迁移数据库

时间:2022-08-25 09:27:37

I'm deploying a Django project to AWS using Elastic Beanstalk and am stuck on migrating the database.

我正在使用Elastic Beanstalk将Django项目部署到AWS,但我仍然无法迁移数据库。

Where I'm at: I am able to successfully deploy my django project and load the page through mysubdomain.elasticbeanstalk.com. The page loads without error until I get to a page that needs to do a database call. I then get an error like relation "accounts_user" does not exist LINE 1: SELECT COUNT(*) FROM "accounts_user" because my database hasn't been migrated.

我在哪里:我能够成功部署我的django项目并通过mysubdomain.elasticbeanstalk.com加载页面。页面加载没有错误,直到我到达需要进行数据库调用的页面。然后我得到一个错误,如关系“accounts_user”不存在第1行:SELECT COUNT(*)FROM“accounts_user”因为我的数据库尚未迁移。

What I've tried: I've tried quite a few variations of things. Fortunately there are an abundance of * posts and a couple tutorials. Unfortunately, they all seem to be using a different version and what they suggest do not apply to my project.

我尝试了什么:我尝试了很多不同的东西。幸运的是,有大量的*帖子和几个教程。不幸的是,他们似乎都在使用不同的版本,他们的建议并不适用于我的项目。

It is pretty clear to me that I need to run the migration in a foobar.config file inside the .ebextensions/ folder. Here is the base of what I want to do:

我很清楚,我需要在.ebextensions /文件夹中的foobar.config文件中运行迁移。这是我想要做的基础:

container_commands:
  01_migrate:
    command: "python manage.py migrate --noinput"
    leader_only: true

In the logs, I see that the post deployment script tried to run but it failed. I don't receive any other info on the error, the only thing I see is something like "ERROR: 01_migrate post deployment script failed"

在日志中,我看到后部署脚本尝试运行但失败了。我没有收到有关错误的任何其他信息,我唯一看到的是“错误:01_migrate部署后脚本失败”

I find out that I need to activate the virtual environment for the command, which makes sense. From asdf I try this:

我发现我需要激活命令的虚拟环境,这是有道理的。从asdf我试试这个:

container_commands:
  01_migrate:
    command: "source /opt/python/run/venv/bin/activate && python rlg/manage.py migrate --noinput"
    leader_only: true

But it doesn't work. In fact, through SSH I find out I don't even have a /opt/python/ folder, only /opt/aws/ and /opt/elasticbeanstalk/. All tutorials and SO questions refer to this folder but I don't have it?

但它不起作用。事实上,通过SSH我发现我甚至没有/ opt / python /文件夹,只有/ opt / aws /和/ opt / elasticbeanstalk /。所有教程和SO问题都引用了这个文件夹,但我没有它?

VERSIONS: Python 3.4.1, Django 1.7.7, AWS CLI 3.2.1, Postgres 9.3

版本:Python 3.4.1,Django 1.7.7,AWS CLI 3.2.1,Postgres 9.3

3 个解决方案

#1


9  

The container_commands are NOT executed within the docker container. They are executed on the ec2 instance directly. Currently I'm using docker exec to do the migration. As the concerned docker container is afaik the last one started I use docker ps -a --no-trunc -q | head -n 1 to get the container id.

container_commands不在docker容器中执行。它们直接在ec2实例上执行。目前我正在使用docker exec进行迁移。由于有关的docker容器是afaik,最后一个开始我使用docker ps -a --no-trunc -q | head -n 1获取容器ID。

In the end my setup.config looks like that

最后我的setup.config看起来像那样

container_commands:
  01syncdb:
    command: "docker exec `docker ps -a --no-trunc -q | head -n 1` /var/app/bin/python /var/app/manage.py syncdb --noinput  &>> /tmp/deploy.log"
    leader_only: true
  02migrate:
    command: "docker exec `docker ps -a --no-trunc -q | head -n 1` /var/app/bin/python /var/app/manage.py migrate --noinput  &>> /tmp/deploy.log"
    leader_only: true

I hope that solves your problem as well.

我希望这也能解决你的问题。

#2


12  

I know this is an old post but I want to post my answer here as it took me quite a long time to figure out.

我知道这是一个老帖子,但我想在这里发布我的答案,因为我花了很长时间才弄明白。

Sebastian kind of pointed me in the right direction but the problem with this approach is it runs before the deployment (so you migrate the old code)

Sebastian指出了我正确的方向,但这种方法的问题是它在部署之前运行(所以你迁移旧代码)

You can also use the files command in ebextensions and write a file to /opt/elasticbeanstalk/hooks/appdeploy/post but this will run on ever instance

您还可以在ebextensions中使用files命令并将文件写入/ opt / elasticbeanstalk / hooks / appdeploy / post,但这将在任何实例上运行

You can combine these two things into:

您可以将这两件事合并为:

container_commands:
  01migrate:
    command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post/ && echo -e '#!/bin/bash\ndocker exec `docker ps -a -q | head -n 1` python <path_to_code> migrate' > /opt/elasticbeanstalk/hooks/appdeploy/post/99_migrate.sh && chmod +x /opt/elasticbeanstalk/hooks/appdeploy/post/99_migrate.sh"
    leader_only: true

This will create a post deploy script in the correct dir and ONLY on the leader.

这将在正确的dir中创建一个post deploy脚本,并且仅在leader上创建。

This is working really well for me but be warned, the hooks directories are un-documented features

这对我来说非常有用,但要注意,钩子目录是未记录的功能

#3


-2  

Updating information regarding latest Elastic Beanstalk version.

更新有关最新Elastic Beanstalk版本的信息。

I'm using 64bit Amazon Linux 2016.09 v2.3.3 running Python 3.4.

我正在使用运行Python 3.4的64位Amazon Linux 2016.09 v2.3.3。

The following command works for me without having to activate the virtualenv or create a post app deploy hook.

以下命令适用于我,无需激活virtualenv或创建post app deploy钩子。

container_commands:
  01_migrate_db:
    command: "python manage.py db upgrade" <-- insert your migration command here
    leader_only: true

In order to prove that the container commands will execute on a post deploy phase, I made a change to my models, generated the updated migrations scripts, deployed the new version and checked to see if the database was successfully migrated: Success!

为了证明容器命令将在后期部署阶段执行,我对模型进行了更改,生成了更新的迁移脚本,部署了新版本并检查数据库是否已成功迁移:成功!

#1


9  

The container_commands are NOT executed within the docker container. They are executed on the ec2 instance directly. Currently I'm using docker exec to do the migration. As the concerned docker container is afaik the last one started I use docker ps -a --no-trunc -q | head -n 1 to get the container id.

container_commands不在docker容器中执行。它们直接在ec2实例上执行。目前我正在使用docker exec进行迁移。由于有关的docker容器是afaik,最后一个开始我使用docker ps -a --no-trunc -q | head -n 1获取容器ID。

In the end my setup.config looks like that

最后我的setup.config看起来像那样

container_commands:
  01syncdb:
    command: "docker exec `docker ps -a --no-trunc -q | head -n 1` /var/app/bin/python /var/app/manage.py syncdb --noinput  &>> /tmp/deploy.log"
    leader_only: true
  02migrate:
    command: "docker exec `docker ps -a --no-trunc -q | head -n 1` /var/app/bin/python /var/app/manage.py migrate --noinput  &>> /tmp/deploy.log"
    leader_only: true

I hope that solves your problem as well.

我希望这也能解决你的问题。

#2


12  

I know this is an old post but I want to post my answer here as it took me quite a long time to figure out.

我知道这是一个老帖子,但我想在这里发布我的答案,因为我花了很长时间才弄明白。

Sebastian kind of pointed me in the right direction but the problem with this approach is it runs before the deployment (so you migrate the old code)

Sebastian指出了我正确的方向,但这种方法的问题是它在部署之前运行(所以你迁移旧代码)

You can also use the files command in ebextensions and write a file to /opt/elasticbeanstalk/hooks/appdeploy/post but this will run on ever instance

您还可以在ebextensions中使用files命令并将文件写入/ opt / elasticbeanstalk / hooks / appdeploy / post,但这将在任何实例上运行

You can combine these two things into:

您可以将这两件事合并为:

container_commands:
  01migrate:
    command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post/ && echo -e '#!/bin/bash\ndocker exec `docker ps -a -q | head -n 1` python <path_to_code> migrate' > /opt/elasticbeanstalk/hooks/appdeploy/post/99_migrate.sh && chmod +x /opt/elasticbeanstalk/hooks/appdeploy/post/99_migrate.sh"
    leader_only: true

This will create a post deploy script in the correct dir and ONLY on the leader.

这将在正确的dir中创建一个post deploy脚本,并且仅在leader上创建。

This is working really well for me but be warned, the hooks directories are un-documented features

这对我来说非常有用,但要注意,钩子目录是未记录的功能

#3


-2  

Updating information regarding latest Elastic Beanstalk version.

更新有关最新Elastic Beanstalk版本的信息。

I'm using 64bit Amazon Linux 2016.09 v2.3.3 running Python 3.4.

我正在使用运行Python 3.4的64位Amazon Linux 2016.09 v2.3.3。

The following command works for me without having to activate the virtualenv or create a post app deploy hook.

以下命令适用于我,无需激活virtualenv或创建post app deploy钩子。

container_commands:
  01_migrate_db:
    command: "python manage.py db upgrade" <-- insert your migration command here
    leader_only: true

In order to prove that the container commands will execute on a post deploy phase, I made a change to my models, generated the updated migrations scripts, deployed the new version and checked to see if the database was successfully migrated: Success!

为了证明容器命令将在后期部署阶段执行,我对模型进行了更改,生成了更新的迁移脚本,部署了新版本并检查数据库是否已成功迁移:成功!