我正在尝试使用现有环境变量的值在我的supervisord配置中设置环境变量。现有变量是REDIS_PORT_6379_TCP_ADDR
(来自Docker链接容器);该值是IP地址(例如172.17.0.5
)。这是我第一次天真的尝试:
[program:sidekiq]
user=web
directory=/var/www
environment=REDIS_URL=redis://$REDIS_PORT_6379_TCP_ADDR:6379/0
command=bundle exec sidekiq -c 50
redirect_stderr=true
autorestart=true
这根本不起作用,因为supervisord无法解析它:
$ supervisord -c /etc/supervisor/supervisord.conf -n
Error: Unexpected end of key/value pairs
For help, use /usr/bin/supervisord -h
然后我尝试引用环境部分:
environment=REDIS_URL="redis://$REDIS_PORT_6379_TCP_ADDR:6379/0"
这不起作用,因为变量在传递给我的程序之前没有被插值:
2014-06-16T03:08:35Z 240 TID-oqy09ga9c WARN: the scheme redis does not accept registry part: $REDIS_PORT_6379_TCP_ADDR:6379 (or bad hostname?)
然后,根据this answer,我尝试使用%(ENV)
语法:
environment=REDIS_URL="redis://%(ENV_REDIS_PORT_6379_TCP_ADDR):6379/0"
再次无法解析它:
$ supervisord -c /etc/supervisor/supervisord.conf -n
Error: Format string 'REDIS_URL="redis://%(ENV_REDIS_PORT_6379_TCP_ADDR):6379/0"' for 'environment' is badly formatted
For help, use /usr/bin/supervisord -h
如果我删除双引号,结果相同:
$ supervisord -c /etc/supervisor/supervisord.conf -n
Error: Format string 'REDIS_URL=redis://%(ENV_REDIS_PORT_6379_TCP_ADDR):6379/0' for 'environment' is badly formatted
For help, use /usr/bin/supervisord -h
我通过将export
放入command
部分尝试了同样的事情:
[program:sidekiq]
user=web
directory=/var/www
command="export REDIS_URL=redis://$REDIS_PORT_6379_TCP_ADDR:6379/0; bundle exec sidekiq -c 50"
redirect_stderr=true
autorestart=true
结果:
$ supervisord -c /etc/supervisor/supervisord.conf -n
2014-06-16 03:35:00,170 WARN Included extra file "/etc/supervisor/conf.d/sidekiq.conf" during parsing
2014-06-16 03:35:00,193 INFO RPC interface 'supervisor' initialized
2014-06-16 03:35:00,194 INFO supervisord started with pid 346
2014-06-16 03:35:01,197 INFO spawnerr: can't find command 'export REDIS_URL=redis://$REDIS_PORT_6379_TCP_ADDR:6379/0; bundle exec sidekiq -c 50'
再次:
[program:sidekiq]
user=web
directory=/var/www
command="export REDIS_URL=redis://%(ENV_REDIS_PORT_6379_TCP_ADDR):6379/0; bundle exec sidekiq -c 50"
redirect_stderr=true
autorestart=true
结果:
$ supervisord -c /etc/supervisor/supervisord.conf -n
Error: Format string '"export REDIS_URL=redis://%(ENV_REDIS_PORT_6379_TCP_ADDR):6379/0; bundle exec sidekiq -c 50"' for 'command' is badly formatted
For help, use /usr/bin/supervisord -h
我做错了什么?
编辑:
$supervisord --version
3.0b2
答案 0 :(得分:4)
原来问题是我对Python的字符串格式化语法不够熟悉。我需要在s
的末尾添加%()
,以将格式类型设置为字符串。
最终配置:
[program:sidekiq]
user=web
directory=/var/www
environment=REDIS_URL=redis://%(ENV_REDIS_PORT_6379_TCP_ADDR)s:6379/0
command=bundle exec sidekiq -c 50
redirect_stderr=true
autorestart=true