我想将登录密码存储在 Django 配置中当前用户帐户的文件中。我正在使用推荐的便携方式来获取主文件夹,如:
os.path.expanduser("~")
这适用于所有环境,包括本地环境和在服务器上使用 gunicorn -D config.wsgi 启动时。
我的问题是,我引入了 supervisord 来控制 gunicorn 进程,现在这个函数不起作用,它只是返回 /。
这是supervisord.conf的相关部分
[program:kek_django]
command=.../venv/bin/gunicorn config.wsgi
directory=.../django
user=testuser
在此环境下,os.path.expanduser("~") 变为/。
你能告诉我如何通过修复环境或用于检测主目录的函数来解决这个问题吗?
注意:操作系统是 FreeBSD 10,如果相关的话
更新:os.environ 在运行进程下报告如下:
'SUPERVISOR_SERVER_URL': 'unix:///var/run/supervisor/supervisor.sock',
'RC_PID': '84177',
'SERVER_SOFTWARE': 'gunicorn/19.3.0',
'SUPERVISOR_ENABLED': '1',
'SUPERVISOR_PROCESS_NAME': 'test_django',
'PWD': '/',
'DJANGO_SETTINGS_MODULE': 'config.settings.production',
'SUPERVISOR_GROUP_NAME': 'test_django',
'PATH': '/sbin:/bin:/usr/sbin:/usr/bin',
'HOME': '/'
请您参考如下方法:
作为 supervisord 的 Subprocess Environment 文档说:
No shell is executed by
supervisordwhen it runs a subprocess, so environment variables such asUSER,PATH,HOME,SHELL,LOGNAME, etc. are not changed from their defaults or otherwise reassigned. This is particularly important to note when you are running a program from asupervisordrun as root with auser=stanza in the configuration. Unlikecron,supervisorddoes not attempt to divine and override “fundamental” environment variables likeUSER,PATH,HOME, andLOGNAMEwhen it performs asetuidto the user defined within theuser=program config option. If you need to set environment variables for a particular program that might otherwise be set by a shell invocation for a particular user, you must do it explicitly within theenvironment=program config option. An example of setting these enviroment variables is as below.
[program:apache2]
command=/home/chrism/bin/httpd -c "ErrorLog /dev/stdout" -DFOREGROUND
user=chrism
environment=HOME="/home/chrism",USER="chrism"
所以,这才是真正的解决办法。 (如果您动态构建 supervisord.conf 文件并且需要知道如何动态查找这些值,我可以解释一下,但这很容易,而且我认为您无论如何都不需要它。 )
[program:kek_django]
command=.../venv/bin/gunicorn config.wsgi
directory=.../django
user=testuser
environment=HOME="/home/testuser"
如果这对您没有意义,请考虑:
如果您以 root 身份运行 supervisord,则它没有 testuser 的 HOME 或其他任何内容。它所做的只是setuid(testuser),它只是改变了它的用户ID;它不会给 shell 或系统的任何其他部分任何机会来设置 testuser 的变量。大多数类似的工具都有伪造它的变通方法,遵循 cron 如何工作的老套路,但 supervisord 有意选择不这样做。
或者,作为 expanduser 的文档说:
On Unix, an initial
~is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial~useris looked up directly in the password directory.
然后快速浏览一下 the source表明它以最明显的方式做到了这一点。
因此,您的代码中存在三种明显的解决方法:
- 使用
~testuser而不是~(如果需要,您甚至可以通过用户名以编程方式生成)。 - 编写您自己的
expanduser函数,该函数仅执行pwd.getpwuid(os.getuid()).pw_dir而无需检查HOME。 - 如果是
/,则在启动时手动将HOME设置为pwd.getpwuid(os.getuid()).pw_dir。
