Reputation: 31622
I'm trying to set an environment variable in my supervisord config by using the value of an existing environment variable. The existing variable is REDIS_PORT_6379_TCP_ADDR
(comes from a Docker linked container); the value is an IP address (e.g. 172.17.0.5
). This was my first naive attempt:
[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
Which doesn't work at all as supervisord can't parse it:
$ supervisord -c /etc/supervisor/supervisord.conf -n
Error: Unexpected end of key/value pairs
For help, use /usr/bin/supervisord -h
Then I tried quoting the environment section:
environment=REDIS_URL="redis://$REDIS_PORT_6379_TCP_ADDR:6379/0"
That didn't work as the variable didn't get interpolated before being passed to my program:
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?)
Then, per this answer, I tried using the %(ENV)
syntax:
environment=REDIS_URL="redis://%(ENV_REDIS_PORT_6379_TCP_ADDR):6379/0"
Once again it can't parse it:
$ 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
Same result if I remove the double quotes:
$ 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
I've tried the same things by putting export
into the command
section:
[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
Result:
$ 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'
Again:
[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
Result:
$ 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
What am I doing wrong?
edit:
$supervisord --version
3.0b2
Upvotes: 3
Views: 1967
Reputation: 31622
Turns out the problem was that I wasn't familiar enough with Python's string formatting syntax. I needed to add an s
at the end of the %()
to set the format type to string.
Final config:
[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
Upvotes: 4