【问题标题】:How do set an environment variable while referencing an existing environment variable in supervisord config?如何在 supervisord 配置中引用现有环境变量时设置环境变量?
【发布时间】:2014-08-05 20:03:58
【问题描述】:

我正在尝试使用现有环境变量的值在我的 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

【问题讨论】:

    标签: python supervisord


    【解决方案1】:

    原来问题在于我对 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
    

    【讨论】:

    • 很奇怪,对我来说它不起作用。 gist.github.com/k-bx/6fe5238ffbcb64224889您适用于哪个版本?
    • @KonstantineRybnikov 抱歉,我不再将 supervisord 用于我的用例,因此没有任何可用的资源可供检查。抱歉,我无法提供更多帮助!
    • 没问题!我最终只是将数据写入 /var/log/MY_VAR ,非常适合我们:)
    猜你喜欢
    • 1970-01-01
    • 2012-06-08
    • 2017-11-13
    • 2012-07-15
    • 2015-04-25
    • 2014-09-29
    • 1970-01-01
    • 2014-12-17
    • 2018-07-13
    相关资源
    最近更新 更多