Virtualenv未从supervisord AWS Elasticbeanstalk

时间:2017-04-13 17:31:02

标签: python django amazon-web-services elastic-beanstalk supervisord

我正在尝试将我的Django Channels应用程序部署到弹性beanstalk并在部署时启动daphne和worker我有一个在部署期间复制并重新启动的supervisord脚本。下面详细介绍了一些奇怪的行为。

这是我在部署期间运行的channels.config文件

container_commands:
  01_copy_supervisord_conf:
    command: "cp .ebextensions/supervisord/supervisord.conf /opt/python/etc/supervisord.conf"
  02_reload_supervisord:
    command: "supervisorctl -c /opt/python/etc/supervisord.conf reload"

当我在supervisord.conf中使用它时

[unix_http_server]
file=/opt/python/run/supervisor.sock   ; (the path to the socket file)
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner

[supervisord]
logfile=/opt/python/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=10MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/opt/python/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
directory=/opt/python/current/app    ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///opt/python/run/supervisor.sock

[program:httpd]
command=/opt/python/bin/httpdlaunch
numprocs=1
directory=/opt/python/current/app
autostart=true
autorestart=unexpected
startsecs=1                   ; number of secs prog must stay running (def. 1)
startretries=3                ; max # of serial start failures (default 3)
exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
killasgroup=false             ; SIGKILL the UNIX process group (def false)
redirect_stderr=false

EB部署得很好(尽管达芙妮和一名工人尚未开始运作)

如果我将它添加到我的supervisord.conf文件中:

[program:Daphne]
environment=PATH="/opt/python/run/venv/bin"
command=/opt/python/run/venv/bin/daphne -b 0.0.0.0 -p 5000 chat.asgi:channel_layer
directory=/opt/python/current/app
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/daphne.out.log

然后部署失败并显示以下错误:

error: , : file: /usr/lib64/python2.7/xmlrpclib.py line: 800
Traceback (most recent call last):
File "/opt/elasticbeanstalk/hooks/appdeploy/enact/01flip.py", line 42, in 
main()
File "/opt/elasticbeanstalk/hooks/appdeploy/enact/01flip.py", line 36, in main
config.restart_apache()
File "/opt/elasticbeanstalk/hooks/config.py", line 250, in restart_apache
apache_cmd('restart', should_be_running=True)
File "/opt/elasticbeanstalk/hooks/config.py", line 254, in apache_cmd
check_call('/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf %s httpd' % command, shell=True)
File "/usr/lib64/python2.7/subprocess.py", line 541, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf restart httpd' returned non-zero exit status 2.

但如果我进入实例而不是ps -aux |我可以看到达芙妮实际上正在跑步。

然后,如果我将它添加到supervisord.conf:

[program:Worker]
environment=PATH="/opt/python/run/venv/bin"
command=python manage.py runworker
directory=/opt/python/current/app
process_name=%(program_name)s_%(process_num)02d
numprocs=4
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/tmp/workers.out.log

它再次无法使用相同的错误进行部署,但是当我检查workers.out.log日志时,我看到了

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?

这意味着它不使用Worker程序的环境部分?

不确定这里哪里出错。我试着四处看看如何通过supervisord激活virtualenv,但我想出了我正在做的事情。

修改1 所以我能够通过在我的项目中包含一个shell脚本来修复部署无法部署的问题:

#!/bin/bash
VENV=$1
if [ -z $VENV ]; then
    echo "usage: runinenv [virtualenv_path] CMDS"
    exit 1
fi
. ${VENV}/bin/activate

这在我的supervisord.conf文件中:

[program:runvenv]
command=sh virtualenv-sh /opt/python/run/venv
directory=/opt/python/current/app
stdout_logfile=/tmp/venv.out.log

它成功部署,daphne仍然在运行,但是当我检查worker.out.log文件时,它仍然给我同样的错误,关于django没有被安装,并确保我已经激活了virtualenv。我刚刚做了什么?除非在一次通话后没有持续下去?

1 个答案:

答案 0 :(得分:1)

我有同样的问题,工人没有开始,但我能够通过替换:

来解决它
command=python manage.py runworker

command=/opt/python/run/venv/bin/python manage.py runworker

我想如果没有路径,它在启动工作时就不会使用virtualenv。