使用 Nginx、Gunicorn 和 systemd 进行部署时,如何使用 getpass 向 Django 应用程序提供输入?

问题描述 投票:0回答:1

我有一个 Django 应用程序,磁盘上有使用 AES Cypher 加密的密钥,并且必须在内存中未加密才能在进程中使用。

我在

settings.py
中有这行:

AES_CIPHER_KEY = getpass("Enter AES cipher key: (leave blank if you don't need decryption)")

这是我的

gunicorn.socket
:

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

这是我的

gunicorn.service
:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/samir/src/samirpy
ExecStart=/root/samir/venv/bin/gunicorn --access-logfile --workers 3 --bind unix:/var/log/gunicorn/samir.sock samir.wsgi:application

[Install]
WantedBy=multi-user.target

这是我的

nginx.conf
:

server {
    listen 80;
    server_name example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /var/www;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

这是

wsgi.py
:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'samir.settings')

application = get_wsgi_application()

我希望发生的是当我更新代码并使用以下命令重新启动服务时:

sudo systemctl restart gunicorn
它会在启动主进程之前提示输入密钥。

django nginx gunicorn systemd
1个回答
0
投票

通常,服务是交互式的,并且不会从您的终端接收输入;他们的标准输入总是连接到/dev/null。 (此外,您已将服务设置为通过 multi-user.target 在启动时启动;您是否要在每次服务器启动时通过 IPMI/KVM 输入密码?)

对于密码,systemd 有带外方法;参见

man systemd-ask-password
。如果您的服务在启动期间使用此功能(即在通知 systemd 已完成启动之前),systemctl 将为您显示密码提示 – 并且在启动期间(当不涉及 systemctl 时)您仍然能够回答密码提示无需 KVM。


此外,除此之外,“settings.py”听起来应该是程序中“最少”的交互部分——用户输入提示应该“真的”放在其他地方,而不是常量定义的一部分。 (这就像在 C 中做 #include "/dev/stdin"...)


© www.soinside.com 2019 - 2024. All rights reserved.