Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

amazon web services - Django AWS Elastic Beanstalk migrate database

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

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.

What I've tried: I've tried quite a few variations of things. Fortunately there are an abundance of stackoverflow 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:

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"

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

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

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

You can combine these two things into:

container_commands:
  01migrate:
    command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post/ && echo -e '#!/bin/bash
docker 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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...