Search code examples
pythondjangovirtualenvfabricvirtualenvwrapper

Fabric: executing makemigrations & migrate raises Secret Key Error


I'm working on a fabfile.py to stop repetition and deploy automatically.

I'm using the prefix("workon vpenv"): provided by fabric but here is the problem: when running git pull everything works well, but when I run run("python manage.py makemigrations --settings=config.settings.production") I get the following error:

[venuepark.com] run: python manage.py makemigrations --settings=config.settings.production
[venuepark.com] out: Traceback (most recent call last):
[venuepark.com] out:   File "manage.py", line 22, in <module>
[venuepark.com] out:     execute_from_command_line(sys.argv)
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
[venuepark.com] out:     utility.execute()
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 307, in execute
[venuepark.com] out:     settings.INSTALLED_APPS
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
[venuepark.com] out:     self._setup(name)
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
[venuepark.com] out:     self._wrapped = Settings(settings_module)
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/site-packages/django/conf/__init__.py", line 110, in __init__
[venuepark.com] out:     mod = importlib.import_module(self.SETTINGS_MODULE)
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/importlib/__init__.py", line 126, in import_module
[venuepark.com] out:     return _bootstrap._gcd_import(name[level:], package, level)
[venuepark.com] out:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[venuepark.com] out:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[venuepark.com] out:   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
[venuepark.com] out:   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
[venuepark.com] out:   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
[venuepark.com] out:   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[venuepark.com] out:   File "/home/tony/vp/vp/config/settings/production.py", line 1, in <module>
[venuepark.com] out:     from .base import *
[venuepark.com] out:   File "/home/tony/vp/vp/config/settings/base.py", line 24, in <module>
[venuepark.com] out:     SECRET_KEY = os.environ["VP_SECRET_KEY"]
[venuepark.com] out:   File "/home/tony/.virtualenvs/vpenv/lib/python3.6/os.py", line 669, in __getitem__
[venuepark.com] out:     raise KeyError(key) from None
[venuepark.com] out: KeyError: 'VP_SECRET_KEY'
[venuepark.com] out: 


Fatal error: run() received nonzero return code 1 while executing!

Requested: python manage.py makemigrations --settings=config.settings.production
Executed: /bin/bash -l -c "cd /home/tony/vp/vp >/dev/null && workon vpenv && python manage.py makemigrations --settings=config.settings.production"

my fabfile.py is as follows:

def deploy():
    code_directory = "/path/to/somewhere"
    with cd(code_directory):
        with prefix("workon vpenv"):
            run("git pull")
            run("python manage.py makemigrations --settings=config.settings.production")
            run("python manage.py migrate --settings=config.settings.production")
            run("python manage.py collectstatic --settings=config.settings.production")

        sudo("pkill gunicorn")
        sudo("systemctl daemon-reload")
        sudo("systemctl start gunicorn")
        sudo("systemctl enable gunicorn")
        sudo("systemctl restart nginx")

What could cause the error?

When I run the commands on the server itself I get no errors.


Solution

  • It seems that the target machine where you're deploying doesn't know anything about a VP_SECRET_KEY environment variable. You could set it during provision stage if you wish.

    In your settings.base you're taking the SECRET_KEY config parameter from an environment variable that is not set. You should set it in the target machine. Example:

    export VP_SECRET_KEY="mysecretkey"