Fabric:执行makemigrations&迁移引发密钥错误

时间:2018-01-12 15:31:03

标签: python django virtualenv fabric virtualenvwrapper

我正在研究fabfile.py以停止重复并自动部署。

我正在使用Fabric提供的prefix("workon vpenv"):,但问题是:运行git时,一切正常,但是当我运行run("python manage.py makemigrations --settings=config.settings.production")时,我收到以下错误:

[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"

我的fabfile.py如下:

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")

什么可能导致错误?

当我在服务器上运行命令时,我没有错误。

1 个答案:

答案 0 :(得分:1)

您正在部署的目标计算机似乎对VP_SECRET_KEY环境变量一无所知。如果您愿意,可以在提供阶段设置它。

settings.base中,您从未设置的环境变量中获取SECRET_KEY配置参数。您应该在目标计算机中进行设置。例如:

export VP_SECRET_KEY="mysecretkey"