简体   繁体   English

如何为生产和开发交换配置文件?

[英]How to swap configuration files for Production and Development?

Some time ago I created a script using Python, the script will execute some actions in an instance based on a configuration file. 前一段时间,我使用Python创建了一个脚本,该脚本将在基于配置文件的实例中执行一些操作。

This is the issue, I created 2 configuration files. 这是问题,我创建了2个配置文件。

Config.py Config.py

instance= <Production url>
Value1= A
Value2= B
...

TestConfig.py TestConfig.py

instance= <Development url>
Value1= C
Value2= D
...

So when I want the script to execute the tasks in a development instance to do tests, I just import the TestConfig.py instead of the Config.py. 因此,当我希望脚本在开发实例中执行任务以进行测试时,只需导入TestConfig.py而不是Config.py。

Main.py Main.py

# from Config import *
from TestConfig import *

The problem comes when I update the script using git. 当我使用git更新脚本时,问题就来了。 If I want to run the script in development I have to modify the file manually, this means that I will have uncommited changes in the server. 如果要在开发中运行脚本,则必须手动修改文件,这意味着我将在服务器中进行未提交的更改。

Doing this change takes about 1 min of my time but I feel like I'm doing something wrong. 进行此更改大约需要花费我1分钟的时间,但是我感觉自己做错了什么。

Do you know if there's a standard or right way to accomplish this kind of tasks? 您是否知道完成这类任务的标准方法或正确方法?

Use that: 使用:

try:
    from TestConfig import *
except ImportError:
    from Config import *

On production, remove TestConfig.py 在生产中,删除TestConfig.py

在计算机上导出环境变量,然后根据该环境变量选择设置。

I think Django addresses this issue best with local_settings.py . 我认为Django最好使用local_settings.py解决此问题。 Based on this approach. 基于这种方法。 at the end of all your imports (after from config import * ), just add: 在所有导入的末尾( from config import * ),只需添加:

# Add this at the end of all imports
# This is safe to commit and even push to production so long as you don't have local_config in your production server
try:
    from local_config import *
except ImportError:
    pass

And create a local_config.py per machine. 并在每台计算机上创建一个local_config.py What this will do is import everything from config , and then again from local_config , overriding global configuration settings, should they have the same name as the settings inside config . 这样做是从config导入所有内容,然后再从local_config导入所有内容,并覆盖全局配置设置(如果它们的名称与config内部的设置相同)。

The other answers here offer perfectly fine solutions if you really want to differentiate between production and test environments in your script. 如果您确实想在脚本中的生产环境和测试环境之间进行区分,则此处的其他答案将提供非常好的解决方案。 I would advocate for a different approach, however: to properly test your code, you should create an entirely separate test environment and run your code there, without any changes (or changes to the config files). 但是,我主张采用另一种方法:要正确地测试代码,您应该创建一个完全独立的测试环境并在其中运行代码,而无需进行任何更改(或更改配置文件)。

I can't make any specific suggestions for how to go about this since I don't know what the script does. 由于我不知道脚本的作用,因此我无法针对该问题提出任何具体建议。 In general though, you should try to create a sandbox that spoofs your production environment and is completely isolated. 通常,您应该尝试创建一个欺骗您的生产环境并完全隔离的沙箱。 You can create a wrapper script that will run your code in the sandbox and modify the inputs and outputs as necessary to make your code interact with the test environment instead of the production environment. 您可以创建一个包装器脚本,该脚本将在沙箱中运行您的代码,并根据需要修改输入和输出,以使您的代码与测试环境(而非生产环境)进行交互。 This wrapper is where you should be choosing which environment the code runs in and which config files it uses. 您应该在此包装器中选择运行代码的环境以及使用的配置文件。

This approach abstracts the testing away from the code itself, making both easier to maintain. 这种方法使测试从代码本身中抽象出来,使两者都更易于维护。 Designing for test is a reasonable approach for hardware, where you are stuck with the hardware you have after fabrication, but it makes less sense for software, where wrappers and spoofed data are easier to manage. 对于硬件而言,测试设计是一种合理的方法,在这种方法中,制造商必须保留自己拥有的硬件,但是对于软件而言,意义不大,因为包装和欺骗性数据更易于管理。 You shouldn't have to modify your production code base just to handle testing. 您不必仅仅为了处理测试就修改生产代码库。

It also entirely elimiates the chance that you'll forget to change something when you want to switch between testing and deployment to production. 当您要在测试和部署到生产之间进行切换时,这也完全消除了您忘记更改某些东西的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM