未从主管 AWS Elasticbeanstalk 激活 Virtualenv

Posted

技术标签:

【中文标题】未从主管 AWS Elasticbeanstalk 激活 Virtualenv【英文标题】:Virtualenv is not being activated from supervisord AWS Elasticbeanstalk 【发布时间】:2017-09-09 22:30:27 【问题描述】:

我正在尝试将我的 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 部署良好(尽管 daphne 和一个 worker 尚未启动和运行)

如果我将它添加到我的 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.

但是如果我通过 ssh 进入实例而不是 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】:

我有同样的工人没有开始的问题,但我能够通过更换来解决它:

command=python manage.py runworker

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

我猜如果没有启动worker时它没有使用virtualenv的路径。

【讨论】:

以上是关于未从主管 AWS Elasticbeanstalk 激活 Virtualenv的主要内容,如果未能解决你的问题,请参考以下文章

请求正文未从 aws-serverless-express 模块进入 app.js 文件

AWS RDS MySQL 数据库未从 pymysql 获取 INSERT 语句

AWS::ElasticBeanstalk::ConfigurationTemplate 的 AWS CloudFormation 模板失败

AWS-ElasticBeanstalk:在哪里可以找到 Beanstalk 自动创建的域名指向啥?

强制 https aws elasticbeanstalk

从在 ElasticBeanstalk 中运行的 Flask 应用程序使用 AWS