环境变量或 YAML 配置文件
背景:
步骤1->我们有一个盒子,通过在特定配置的测试模式下运行应用程序来运行应用程序的单元和功能测试。
步骤2->步骤 1 成功后,我们通过在另一个框中使用不同的配置集在测试模式下运行应用程序来运行应用程序的集成测试。
步骤3->步骤 2 成功后,我们通过在性能测试框中以生产模式运行应用程序来运行应用程序的性能测试。
步骤4->步骤 3 成功后,构建被认为是稳定的,并且使用该代码库更新 UAT 框,并且应用程序在生产模式下运行,以供客户审查和反馈。 步骤5->通过客户的 GO,生产框会使用代码库进行更新。
现在,从上面的步骤我们观察到,在步骤 1 和 2 中,当应用程序在测试模式下运行时,它具有不同的配置。步骤 3,4 和 5 的情况类似。
在这种情况下,建议的做法是什么?我们有 YAML 配置文件,但我个人认为维护大量配置文件没有意义。于是,我改变了
的做法 “每个环境的配置文件”
到
“每个rails模式的配置文件,将变量外部化到linux环境”。
我走在正确的轨道上吗?我的行动难道没有让事情变得简单吗?
这两种方法的优缺点是什么?
The background:
Step 1 -> We have a box that runs unit and functional tests of an application by running it in test mode with a specific configuration.
Step 2 -> Upon success of Step 1, we run integration tests of an application by running it in test mode with different configuration set, in another box.
Step 3 -> Upon success of step 2, we run the performance tests of an application by running it in production mode, in the performance test box.
Step 4 -> Upon success of step 3, the build is considered stable and the UAT box is updated with that code base and the application is run in production mode, for the customer review and feedback.
Step 5 -> With GO from the customer, the production box is updated with the code base.
Now, from above steps we observe that in steps 1 and 2, while the application runs in test mode it has different configuration. Similar is the case with steps 3,4 and 5.
In such situations, what is the recommended practice? We were having YAML configuration files, but personally I felt that maintaining numerous configuration files doesn't make sense. And so, I changed from the practise of
"Config file per environment"
to
"Config file per rails mode, externalizing the variables to linux environment".
Am I on right track? Doesn't my action, simplify things?
What are the pros and cons of these two approaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据我的经验,环境变量是最后的配置选项。它们绝对有自己的位置,但应用程序通常应该首先检查其他一些更可靠且明确有意的配置选项。我强烈建议从 YAML 文件加载配置,并仅使用环境变量作为后备。始终假设您的环境变量将应用于系统范围内的所有内容,并假设它会在某个时刻意外地取消设置或设置为错误的值。即,您的应用程序不应该提交 seppuku,因为某些配置目录被设置为
/
并且它没有权限(或者更糟糕的是,您擦除了驱动器,因为应用程序以root< /代码>)。或者更有可能的是,您的
RAILS_ENV
之类的东西在本应生产
时被设置为测试
,但没有人意识到,现在用户正在写入数据由于所有 500 的原因,到了错误的地方或/dev/null
。就我个人而言,我喜欢在任何时候回退到环境变量来获取配置值时删除 logger.warn 消息。
老实说,根据您的具体问题,我可能会在命令行上传递要启动的环境的配置值。
In my experience, environment variables are the configuration option of last-resort. They absolutely have their place, but applications should generally check some other more reliable and explicitly intentional configuration option first. I would highly recommend loading configuration from a YAML file and only use an environment variable as a fallback. Always assume that your environment variable is going to apply to everything system-wide and assume that it's going to accidentally get unset or set to the wrong value at some point. i.e., your app shouldn't commit seppuku because some configuration directory got set to
/
and it doesn't have permissions to it (or worse you wipe your drive because the app ran asroot
). Or more likely, something like yourRAILS_ENV
was set totest
when it should've beenproduction
and no one realized and now users are writing data to the wrong place or to/dev/null
on account of all the 500s.Personally, I like to drop
logger.warn
messages anytime I fallback to an environment variable for a configuration value.With your precise issue, honestly, I'd probably pass in the configuration value for which environment to start on the command line.
在我的公司,在某种程度上,我们实际上两者都有。
我们有一个 Rails 应用程序,可以指向另一个软件的许多不同安装之一,并使用该安装中的 API。要指定安装,需要设置大约 5 个变量。
我们将这些变量中的每一个都作为单独的环境变量,但是所有这些变量的设置都很快就过时了,我们不可避免地忘记了其中一个。
现在我们有了一个称为 ENV_TOKEN 的环境变量,并且我们有 yaml 文件,其中包含与有效 ENV_TOKEN 变量相对应的条目,以及 config/initializers 中设置 ENV[key]=value 的代码。
假设我有变量“FOO”和“BAR”,我想分别将它们设置为“一”和“二”。我可以创建一个 yaml 文件,其中包含:
然后我将环境变量 ENV_TOKEN 设置为 carolclarinet,并将 FOO 和 BAR 设置为 1 和 2。
我不知道这是否是最好的方法,但它对我们有用。
ETA:请注意,这仅用于开发和测试,我们软件的安装程序负责设置所有这些,因此我们的客户永远不会更改任何环境变量。
At my company, we actually have both, in a way.
We have a Rails app that can point at one of many different installations of another piece of software and use the API from that installation. To specify an installation, about 5 variables need to be set.
We had each of these variables as separate environment variables, but setting all of them got old really fast and we inevitably forgot one.
So now we have one environment variable we're calling ENV_TOKEN and we have yaml files that contain entries corresponding to the valid ENV_TOKEN variables, and code in config/initializers that sets ENV[key]=value.
So let's say I have variables "FOO" and "BAR" that I want to set to "one" and "two", respectively. I could create a yaml file that contains:
and then I would set my environment variable ENV_TOKEN to carolclarinet and FOO and BAR get set to one and two.
I have no idea if this is the best way of doing this, but it works for us.
ETA: Note that this is for developing and testing only, the installer of our software takes care of setting all this up so our customers never change any environment variables.
经过大量徒劳的谷歌搜索、与一些 Rails 人员的讨论和一些头脑风暴之后,我对代码进行了更改,这样我就有了“每个 Rails 模式的配置文件,将应用程序配置外部化为 YML 文件,该文件在我的案例仍然在 Rails 环境之外”
遵循不言自明的代码片段,了解我如何以简单的方式实现它。简单的解释是,environment.rb 文件中的代码片段从系统读取 YAML 文件,将所有键值对复制到 Rails 的 ENV 哈希中。该 ENV 哈希值在应用程序加载时/加载时/加载后随处可用。
有关详细信息,请参阅我的博客文章: https: //blog.codonomics.com/2011/02/externalizing-all-application-specific.html
After a lot of Googling in vain, discussions with some Rails folks and some brainstorming, I have made changes to the code such that I have "A config file per rails mode, externalizing the application configurations to a YML file, which in my case remains outside of the Rails environment"
Follow through the self-explanatory code snippets to get an understanding of how I achieve it in simple way. The quick explanation is that the code snippet in the environment.rb file reads the YAML file from system to copy all key value pairs to Rails' ENV hash. This ENV hash is available everywhere while/on/after application load.
More details of it can be found in my blog post: https://blog.codonomics.com/2011/02/externalizing-all-application-specific.html