本文介绍了Supervisord - 在 supervisord.conf 中使用一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

改为使用supervisod 作为过程控制系统.

Moved to using supervisod as a process control system.

我在我的 supervisord.conf 中有一个 LONG 和重复的 ENVIRONMENT 配置,为很多进程设置了很多环境变量.我需要在一个地方定义它并重用它,以保持配置干燥和可维护.主管可以这样做吗?如何?

I have a LONG and repeating ENVIRONMENT configuration in my supervisord.conf setting a lot of environment variables for a lot of processes. I need to define it one place and reuse it, to keep the configuration DRY and maintainable. is that possible with supervisor and how?

非干燥配置示例

[program:node-app1]
command=node /home/ubuntu/server/node-app1/app.js
directory=/home/ubuntu/server/node-app1
autostart=true
autorestart=true
stderr_logfile=/home/ubuntu/supervisor/node_app1/err.log
stdout_logfile=/home/ubuntu/supervisor/node_app1/out.log
user=ubuntu
priority=998
startretries=20
ENVIRONMENT=BROKER_URL="amqp://user:[email protected]:5672",
            NODE_ENV=envName,
            MONGO_URL="mongodb://path.to.mongo:27017",
            BASE_PUBLIC_API="http:path.to.api",
            REDIS_URL="redis://path.to.redis:6379",
            BACKEND_URL="https://path.to.backend",
            CHARTS_URL="https://path.to.charts"

[program:node-app2]
command=node /home/ubuntu/server/node-app2/app.js
directory=/home/ubuntu/server/node-app2
autostart=true
autorestart=true
stderr_logfile=/home/ubuntu/supervisor/node_app2/err.log
stdout_logfile=/home/ubuntu/supervisor/node_app2/out.log
user=ubuntu
priority=20
startretries=20
ENVIRONMENT=BROKER_URL="amqp://user:[email protected]:5672",
            NODE_ENV=envName,
            MONGO_URL="mongodb://path.to.mongo:27017",
            BASE_PUBLIC_API="http:path.to.api",
            REDIS_URL="redis://path.to.redis:6379",
            BACKEND_URL="https://path.to.backend",
            CHARTS_URL="https://path.to.charts"

可以共享的内容:ENVIRONMENT、日志的基本目录(每个应用程序的末尾都会改变)、startsecs 等常见变量.等

What could be shared: ENVIRONMENT, base directory for logs (only the end would change for each app), common variables like startsecs. etc

推荐答案

Options

通过从 supervisord 继承:

只要您使用的是版本 3.0a10 或更高版本,您可以在[supervisord] environment中设置环境变量,它们将在supervisord管理的所有进程的环境中.

Options

Via Inheritance from supervisord:

As long as you are using version 3.0a10 or above, you can set environment variables in [supervisord] environment and they will be in the environment of all processes under supervisord's management.

[supervisord]
...
environment=FAVORITE_VENTURE="RUSTY",FAVORITE_COLOR="RED"

使用从 Shell 继承的变量 supervisord

Supervisord 还有一个 %(ENV_VARNAME)s 扩展格式a> 用于解释允许在不同进程中移动变量名称的环境变量.但是配置的环境部分不会添加到 %(ENV_)s 机制可用的环境变量中,因此有必要使用 shell 已经设置的变量调用 supervisord.

Working with Variables supervisord inherited from a Shell

Supervisord also has a %(ENV_VARNAME)s expansion format for interpreting environment variables which would allow moving around variable names for different processes. But the configuration's environment section does not add to the environment variables available by the %(ENV_)s mechanism, so it would be necessary to call supervisord with variables already set by your shell.

例如,如果您使用 init 脚本来启动 supervisord 并且在基于 debian 的系统(即 Ubuntu)上,那么您可以从/etc/default/supervisor 中的以下内容开始:

As an example, if you use init scripts to start supervisord and are on a debian based system (i.e. Ubuntu) then you might start with the following in /etc/default/supervisor:

export SUPERVISOR_INCLUDES="main.conf test.conf"
export MAIN_RETRY_COUNT=2
export TEST_RETY_COUNT=1
MONGO_BASE="MONGO_URL=mongodb://path.to.mongo"
MAIN_MONGO_URL="${MONGO_BASE}:27017"
TEST_MONGO_URL="${MONGO_BASE}:27018"
export MAIN_ENV="${MAIN_MONGO_URL},OTHER_THING=\"Another thing with escaped quoting\""
export TEST_ENV=..

然后在配置中使用它们:

Then use them in configurations:

; supvervisord.conf
[includes]
files= %(here)s/subdir/other.conf %(ENV_SUPERVISOR_INCLUDES)s

; main.conf
[group:main]
...
[program:mainbackend]
startretries=%(ENV_MAIN_RETRY_COUNT)s
environment=%(ENV_MAIN_ENV)s

如果用户可以直接 sudo 和调用 supervisord,这种方法不会很好地工作,因为 sudo 既剥离了用户环境,也不运行传统的 shell init 脚本.但是您可以在 root 的 .bashrc 中 source/etc/default/supervisor 并使用 sudo bash -c "supervisord .." 或在调用 supervisord 之前获取它.

If users can sudo and call supervisord directly, this method doesn't work very well since sudo both strips the users environment and doesn't run traditional shell init scripts. But you can source /etc/default/supervisor in root's .bashrc and use sudo bash -c "supervisord .." or source it before calling supervisord.

这篇关于Supervisord - 在 supervisord.conf 中使用一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!